blob: 0cafdb493fcf65d055fa99623d2390129ba975cc [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020063#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020067#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020069#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010070#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020072#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020073#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau522d6c02009-12-06 18:49:18 +010075const char HTTP_100[] =
76 "HTTP/1.1 100 Continue\r\n\r\n";
77
78const struct chunk http_100_chunk = {
79 .str = (char *)&HTTP_100,
80 .len = sizeof(HTTP_100)-1
81};
82
Willy Tarreaua9679ac2010-01-03 17:32:57 +010083/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400102
103/* same as 302 except that the browser MUST retry with the same method */
104const char *HTTP_307 =
105 "HTTP/1.1 307 Temporary Redirect\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
110/* same as 301 except that the browser MUST retry with the same method */
111const char *HTTP_308 =
112 "HTTP/1.1 308 Permanent Redirect\r\n"
113 "Content-length: 0\r\n"
114 "Location: "; /* not terminated since it will be concatenated with the URL */
115
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
117const char *HTTP_401_fmt =
118 "HTTP/1.0 401 Unauthorized\r\n"
119 "Cache-Control: no-cache\r\n"
120 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200121 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
123 "\r\n"
124 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
125
Willy Tarreau844a7e72010-01-31 21:46:18 +0100126const char *HTTP_407_fmt =
127 "HTTP/1.0 407 Unauthorized\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
132 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800133 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135
136const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200137 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_400] = 400,
139 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400140 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400142 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100143 [HTTP_ERR_500] = 500,
144 [HTTP_ERR_502] = 502,
145 [HTTP_ERR_503] = 503,
146 [HTTP_ERR_504] = 504,
147};
148
Willy Tarreau80587432006-12-24 17:47:20 +0100149static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200150 [HTTP_ERR_200] =
151 "HTTP/1.0 200 OK\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
157
Willy Tarreau0f772532006-12-23 20:51:41 +0100158 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100159 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
165
166 [HTTP_ERR_403] =
167 "HTTP/1.0 403 Forbidden\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
173
CJ Ess108b1dd2015-04-07 12:03:37 -0400174 [HTTP_ERR_405] =
175 "HTTP/1.0 405 Method Not Allowed\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
181
Willy Tarreau0f772532006-12-23 20:51:41 +0100182 [HTTP_ERR_408] =
183 "HTTP/1.0 408 Request Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
189
CJ Ess108b1dd2015-04-07 12:03:37 -0400190 [HTTP_ERR_429] =
191 "HTTP/1.0 429 Too Many Requests\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
197
Willy Tarreau0f772532006-12-23 20:51:41 +0100198 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200199 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200204 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100205
206 [HTTP_ERR_502] =
207 "HTTP/1.0 502 Bad Gateway\r\n"
208 "Cache-Control: no-cache\r\n"
209 "Connection: close\r\n"
210 "Content-Type: text/html\r\n"
211 "\r\n"
212 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
213
214 [HTTP_ERR_503] =
215 "HTTP/1.0 503 Service Unavailable\r\n"
216 "Cache-Control: no-cache\r\n"
217 "Connection: close\r\n"
218 "Content-Type: text/html\r\n"
219 "\r\n"
220 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
221
222 [HTTP_ERR_504] =
223 "HTTP/1.0 504 Gateway Time-out\r\n"
224 "Cache-Control: no-cache\r\n"
225 "Connection: close\r\n"
226 "Content-Type: text/html\r\n"
227 "\r\n"
228 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
229
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232/* status codes available for the stats admin page (strictly 4 chars length) */
233const char *stat_status_codes[STAT_STATUS_SIZE] = {
234 [STAT_STATUS_DENY] = "DENY",
235 [STAT_STATUS_DONE] = "DONE",
236 [STAT_STATUS_ERRP] = "ERRP",
237 [STAT_STATUS_EXCD] = "EXCD",
238 [STAT_STATUS_NONE] = "NONE",
239 [STAT_STATUS_PART] = "PART",
240 [STAT_STATUS_UNKN] = "UNKN",
241};
242
243
William Lallemand73025dd2014-04-24 14:38:37 +0200244/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_req_keywords.list)
247};
248
249/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200250struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200251 .list = LIST_HEAD_INIT(http_res_keywords.list)
252};
253
Willy Tarreau80587432006-12-24 17:47:20 +0100254/* We must put the messages here since GCC cannot initialize consts depending
255 * on strlen().
256 */
257struct chunk http_err_chunks[HTTP_ERR_SIZE];
258
Willy Tarreaua890d072013-04-02 12:01:06 +0200259/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
260static struct hdr_ctx static_hdr_ctx;
261
Willy Tarreau42250582007-04-01 01:30:43 +0200262#define FD_SETS_ARE_BITFIELDS
263#ifdef FD_SETS_ARE_BITFIELDS
264/*
265 * This map is used with all the FD_* macros to check whether a particular bit
266 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
267 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
268 * byte should be encoded. Be careful to always pass bytes from 0 to 255
269 * exclusively to the macros.
270 */
271fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
272fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100273fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200274
275#else
276#error "Check if your OS uses bitfields for fd_sets"
277#endif
278
Willy Tarreau87b09662015-04-03 00:22:06 +0200279static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200280
David Carlier7365f7d2016-04-04 11:54:42 +0100281static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
282static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100283
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200284/* This function returns a reason associated with the HTTP status.
285 * This function never fails, a message is always returned.
286 */
287const char *get_reason(unsigned int status)
288{
289 switch (status) {
290 case 100: return "Continue";
291 case 101: return "Switching Protocols";
292 case 102: return "Processing";
293 case 200: return "OK";
294 case 201: return "Created";
295 case 202: return "Accepted";
296 case 203: return "Non-Authoritative Information";
297 case 204: return "No Content";
298 case 205: return "Reset Content";
299 case 206: return "Partial Content";
300 case 207: return "Multi-Status";
301 case 210: return "Content Different";
302 case 226: return "IM Used";
303 case 300: return "Multiple Choices";
304 case 301: return "Moved Permanently";
305 case 302: return "Moved Temporarily";
306 case 303: return "See Other";
307 case 304: return "Not Modified";
308 case 305: return "Use Proxy";
309 case 307: return "Temporary Redirect";
310 case 308: return "Permanent Redirect";
311 case 310: return "Too many Redirects";
312 case 400: return "Bad Request";
313 case 401: return "Unauthorized";
314 case 402: return "Payment Required";
315 case 403: return "Forbidden";
316 case 404: return "Not Found";
317 case 405: return "Method Not Allowed";
318 case 406: return "Not Acceptable";
319 case 407: return "Proxy Authentication Required";
320 case 408: return "Request Time-out";
321 case 409: return "Conflict";
322 case 410: return "Gone";
323 case 411: return "Length Required";
324 case 412: return "Precondition Failed";
325 case 413: return "Request Entity Too Large";
326 case 414: return "Request-URI Too Long";
327 case 415: return "Unsupported Media Type";
328 case 416: return "Requested range unsatisfiable";
329 case 417: return "Expectation failed";
330 case 418: return "I'm a teapot";
331 case 422: return "Unprocessable entity";
332 case 423: return "Locked";
333 case 424: return "Method failure";
334 case 425: return "Unordered Collection";
335 case 426: return "Upgrade Required";
336 case 428: return "Precondition Required";
337 case 429: return "Too Many Requests";
338 case 431: return "Request Header Fields Too Large";
339 case 449: return "Retry With";
340 case 450: return "Blocked by Windows Parental Controls";
341 case 451: return "Unavailable For Legal Reasons";
342 case 456: return "Unrecoverable Error";
343 case 499: return "client has closed connection";
344 case 500: return "Internal Server Error";
345 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200346 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 case 503: return "Service Unavailable";
348 case 504: return "Gateway Time-out";
349 case 505: return "HTTP Version not supported";
350 case 506: return "Variant also negociate";
351 case 507: return "Insufficient storage";
352 case 508: return "Loop detected";
353 case 509: return "Bandwidth Limit Exceeded";
354 case 510: return "Not extended";
355 case 511: return "Network authentication required";
356 case 520: return "Web server is returning an unknown error";
357 default:
358 switch (status) {
359 case 100 ... 199: return "Informational";
360 case 200 ... 299: return "Success";
361 case 300 ... 399: return "Redirection";
362 case 400 ... 499: return "Client Error";
363 case 500 ... 599: return "Server Error";
364 default: return "Other";
365 }
366 }
367}
368
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200369/* This function returns HTTP_ERR_<num> (enum) matching http status code.
370 * Returned value should match codes from http_err_codes.
371 */
372static const int http_get_status_idx(unsigned int status)
373{
374 switch (status) {
375 case 200: return HTTP_ERR_200;
376 case 400: return HTTP_ERR_400;
377 case 403: return HTTP_ERR_403;
378 case 405: return HTTP_ERR_405;
379 case 408: return HTTP_ERR_408;
380 case 429: return HTTP_ERR_429;
381 case 500: return HTTP_ERR_500;
382 case 502: return HTTP_ERR_502;
383 case 503: return HTTP_ERR_503;
384 case 504: return HTTP_ERR_504;
385 default: return HTTP_ERR_500;
386 }
387}
388
Willy Tarreau80587432006-12-24 17:47:20 +0100389void init_proto_http()
390{
Willy Tarreau42250582007-04-01 01:30:43 +0200391 int i;
392 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100393 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200394
Willy Tarreau80587432006-12-24 17:47:20 +0100395 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
396 if (!http_err_msgs[msg]) {
397 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
398 abort();
399 }
400
401 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
402 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
403 }
Willy Tarreau42250582007-04-01 01:30:43 +0200404
405 /* initialize the log header encoding map : '{|}"#' should be encoded with
406 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
407 * URL encoding only requires '"', '#' to be encoded as well as non-
408 * printable characters above.
409 */
410 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
411 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100412 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200413 for (i = 0; i < 32; i++) {
414 FD_SET(i, hdr_encode_map);
415 FD_SET(i, url_encode_map);
416 }
417 for (i = 127; i < 256; i++) {
418 FD_SET(i, hdr_encode_map);
419 FD_SET(i, url_encode_map);
420 }
421
422 tmp = "\"#{|}";
423 while (*tmp) {
424 FD_SET(*tmp, hdr_encode_map);
425 tmp++;
426 }
427
428 tmp = "\"#";
429 while (*tmp) {
430 FD_SET(*tmp, url_encode_map);
431 tmp++;
432 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200433
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100434 /* initialize the http header encoding map. The draft httpbis define the
435 * header content as:
436 *
437 * HTTP-message = start-line
438 * *( header-field CRLF )
439 * CRLF
440 * [ message-body ]
441 * header-field = field-name ":" OWS field-value OWS
442 * field-value = *( field-content / obs-fold )
443 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
444 * obs-fold = CRLF 1*( SP / HTAB )
445 * field-vchar = VCHAR / obs-text
446 * VCHAR = %x21-7E
447 * obs-text = %x80-FF
448 *
449 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
450 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
451 * "obs-fold" is volontary forgotten because haproxy remove this.
452 */
453 memset(http_encode_map, 0, sizeof(http_encode_map));
454 for (i = 0x00; i <= 0x08; i++)
455 FD_SET(i, http_encode_map);
456 for (i = 0x0a; i <= 0x1f; i++)
457 FD_SET(i, http_encode_map);
458 FD_SET(0x7f, http_encode_map);
459
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200460 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200461 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200462 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100463 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
Willy Tarreau53b6c742006-12-17 13:37:46 +0100466/*
467 * We have 26 list of methods (1 per first letter), each of which can have
468 * up to 3 entries (2 valid, 1 null).
469 */
470struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100471 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 int len;
473 const char text[8];
474};
475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100476const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100477 ['C' - 'A'] = {
478 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
479 },
480 ['D' - 'A'] = {
481 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
482 },
483 ['G' - 'A'] = {
484 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
485 },
486 ['H' - 'A'] = {
487 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
488 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200489 ['O' - 'A'] = {
490 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
491 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100492 ['P' - 'A'] = {
493 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
494 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
495 },
496 ['T' - 'A'] = {
497 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
498 },
499 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200500 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100501 */
502};
503
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100505 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
506 [HTTP_METH_GET] = { "GET", 3 },
507 [HTTP_METH_HEAD] = { "HEAD", 4 },
508 [HTTP_METH_POST] = { "POST", 4 },
509 [HTTP_METH_PUT] = { "PUT", 3 },
510 [HTTP_METH_DELETE] = { "DELETE", 6 },
511 [HTTP_METH_TRACE] = { "TRACE", 5 },
512 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
513};
514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100515/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200516 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100517 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
518 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
519 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
520 * digit. Note: please do not overwrite values in assignment since gcc-2.95
521 * will not handle them correctly. It's worth noting that chars 128..255 are
522 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100523 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100524const unsigned char http_char_classes[256] = {
525 [ 0] = HTTP_FLG_CTL,
526 [ 1] = HTTP_FLG_CTL,
527 [ 2] = HTTP_FLG_CTL,
528 [ 3] = HTTP_FLG_CTL,
529 [ 4] = HTTP_FLG_CTL,
530 [ 5] = HTTP_FLG_CTL,
531 [ 6] = HTTP_FLG_CTL,
532 [ 7] = HTTP_FLG_CTL,
533 [ 8] = HTTP_FLG_CTL,
534 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
535 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
536 [ 11] = HTTP_FLG_CTL,
537 [ 12] = HTTP_FLG_CTL,
538 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
539 [ 14] = HTTP_FLG_CTL,
540 [ 15] = HTTP_FLG_CTL,
541 [ 16] = HTTP_FLG_CTL,
542 [ 17] = HTTP_FLG_CTL,
543 [ 18] = HTTP_FLG_CTL,
544 [ 19] = HTTP_FLG_CTL,
545 [ 20] = HTTP_FLG_CTL,
546 [ 21] = HTTP_FLG_CTL,
547 [ 22] = HTTP_FLG_CTL,
548 [ 23] = HTTP_FLG_CTL,
549 [ 24] = HTTP_FLG_CTL,
550 [ 25] = HTTP_FLG_CTL,
551 [ 26] = HTTP_FLG_CTL,
552 [ 27] = HTTP_FLG_CTL,
553 [ 28] = HTTP_FLG_CTL,
554 [ 29] = HTTP_FLG_CTL,
555 [ 30] = HTTP_FLG_CTL,
556 [ 31] = HTTP_FLG_CTL,
557 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
558 ['!'] = HTTP_FLG_TOK,
559 ['"'] = HTTP_FLG_SEP,
560 ['#'] = HTTP_FLG_TOK,
561 ['$'] = HTTP_FLG_TOK,
562 ['%'] = HTTP_FLG_TOK,
563 ['&'] = HTTP_FLG_TOK,
564 [ 39] = HTTP_FLG_TOK,
565 ['('] = HTTP_FLG_SEP,
566 [')'] = HTTP_FLG_SEP,
567 ['*'] = HTTP_FLG_TOK,
568 ['+'] = HTTP_FLG_TOK,
569 [','] = HTTP_FLG_SEP,
570 ['-'] = HTTP_FLG_TOK,
571 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
572 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
573 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
574 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
576 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
577 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
579 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
580 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
581 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
582 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
583 [':'] = HTTP_FLG_SEP,
584 [';'] = HTTP_FLG_SEP,
585 ['<'] = HTTP_FLG_SEP,
586 ['='] = HTTP_FLG_SEP,
587 ['>'] = HTTP_FLG_SEP,
588 ['?'] = HTTP_FLG_SEP,
589 ['@'] = HTTP_FLG_SEP,
590 ['A'] = HTTP_FLG_TOK,
591 ['B'] = HTTP_FLG_TOK,
592 ['C'] = HTTP_FLG_TOK,
593 ['D'] = HTTP_FLG_TOK,
594 ['E'] = HTTP_FLG_TOK,
595 ['F'] = HTTP_FLG_TOK,
596 ['G'] = HTTP_FLG_TOK,
597 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
598 ['I'] = HTTP_FLG_TOK,
599 ['J'] = HTTP_FLG_TOK,
600 ['K'] = HTTP_FLG_TOK,
601 ['L'] = HTTP_FLG_TOK,
602 ['M'] = HTTP_FLG_TOK,
603 ['N'] = HTTP_FLG_TOK,
604 ['O'] = HTTP_FLG_TOK,
605 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
606 ['Q'] = HTTP_FLG_TOK,
607 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
608 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
609 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
610 ['U'] = HTTP_FLG_TOK,
611 ['V'] = HTTP_FLG_TOK,
612 ['W'] = HTTP_FLG_TOK,
613 ['X'] = HTTP_FLG_TOK,
614 ['Y'] = HTTP_FLG_TOK,
615 ['Z'] = HTTP_FLG_TOK,
616 ['['] = HTTP_FLG_SEP,
617 [ 92] = HTTP_FLG_SEP,
618 [']'] = HTTP_FLG_SEP,
619 ['^'] = HTTP_FLG_TOK,
620 ['_'] = HTTP_FLG_TOK,
621 ['`'] = HTTP_FLG_TOK,
622 ['a'] = HTTP_FLG_TOK,
623 ['b'] = HTTP_FLG_TOK,
624 ['c'] = HTTP_FLG_TOK,
625 ['d'] = HTTP_FLG_TOK,
626 ['e'] = HTTP_FLG_TOK,
627 ['f'] = HTTP_FLG_TOK,
628 ['g'] = HTTP_FLG_TOK,
629 ['h'] = HTTP_FLG_TOK,
630 ['i'] = HTTP_FLG_TOK,
631 ['j'] = HTTP_FLG_TOK,
632 ['k'] = HTTP_FLG_TOK,
633 ['l'] = HTTP_FLG_TOK,
634 ['m'] = HTTP_FLG_TOK,
635 ['n'] = HTTP_FLG_TOK,
636 ['o'] = HTTP_FLG_TOK,
637 ['p'] = HTTP_FLG_TOK,
638 ['q'] = HTTP_FLG_TOK,
639 ['r'] = HTTP_FLG_TOK,
640 ['s'] = HTTP_FLG_TOK,
641 ['t'] = HTTP_FLG_TOK,
642 ['u'] = HTTP_FLG_TOK,
643 ['v'] = HTTP_FLG_TOK,
644 ['w'] = HTTP_FLG_TOK,
645 ['x'] = HTTP_FLG_TOK,
646 ['y'] = HTTP_FLG_TOK,
647 ['z'] = HTTP_FLG_TOK,
648 ['{'] = HTTP_FLG_SEP,
649 ['|'] = HTTP_FLG_TOK,
650 ['}'] = HTTP_FLG_SEP,
651 ['~'] = HTTP_FLG_TOK,
652 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100653};
654
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100655/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100656 * Adds a header and its CRLF at the tail of the message's buffer, just before
657 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100658 * 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_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100663{
664 int bytes, len;
665
666 len = strlen(text);
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}
673
674/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100675 * Adds a header and its CRLF at the tail of the message's buffer, just before
676 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100677 * the buffer is only opened and the space reserved, but nothing is copied.
678 * The header is also automatically added to the index <hdr_idx>, and the end
679 * of headers is automatically adjusted. The number of bytes added is returned
680 * on success, otherwise <0 is returned indicating an error.
681 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100682int http_header_add_tail2(struct http_msg *msg,
683 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100684{
685 int bytes;
686
Willy Tarreau9b28e032012-10-12 23:49:43 +0200687 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100688 if (!bytes)
689 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100690 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100691 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
692}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693
694/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100695 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
696 * If so, returns the position of the first non-space character relative to
697 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
698 * to return a pointer to the place after the first space. Returns 0 if the
699 * header name does not match. Checks are case-insensitive.
700 */
701int http_header_match2(const char *hdr, const char *end,
702 const char *name, int len)
703{
704 const char *val;
705
706 if (hdr + len >= end)
707 return 0;
708 if (hdr[len] != ':')
709 return 0;
710 if (strncasecmp(hdr, name, len) != 0)
711 return 0;
712 val = hdr + len + 1;
713 while (val < end && HTTP_IS_SPHT(*val))
714 val++;
715 if ((val >= end) && (len + 2 <= end - hdr))
716 return len + 2; /* we may replace starting from second space */
717 return val - hdr;
718}
719
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200720/* Find the first or next occurrence of header <name> in message buffer <sol>
721 * using headers index <idx>, and return it in the <ctx> structure. This
722 * structure holds everything necessary to use the header and find next
723 * occurrence. If its <idx> member is 0, the header is searched from the
724 * beginning. Otherwise, the next occurrence is returned. The function returns
725 * 1 when it finds a value, and 0 when there is no more. It is very similar to
726 * http_find_header2() except that it is designed to work with full-line headers
727 * whose comma is not a delimiter but is part of the syntax. As a special case,
728 * if ctx->val is NULL when searching for a new values of a header, the current
729 * header is rescanned. This allows rescanning after a header deletion.
730 */
731int http_find_full_header2(const char *name, int len,
732 char *sol, struct hdr_idx *idx,
733 struct hdr_ctx *ctx)
734{
735 char *eol, *sov;
736 int cur_idx, old_idx;
737
738 cur_idx = ctx->idx;
739 if (cur_idx) {
740 /* We have previously returned a header, let's search another one */
741 sol = ctx->line;
742 eol = sol + idx->v[cur_idx].len;
743 goto next_hdr;
744 }
745
746 /* first request for this header */
747 sol += hdr_idx_first_pos(idx);
748 old_idx = 0;
749 cur_idx = hdr_idx_first_idx(idx);
750 while (cur_idx) {
751 eol = sol + idx->v[cur_idx].len;
752
753 if (len == 0) {
754 /* No argument was passed, we want any header.
755 * To achieve this, we simply build a fake request. */
756 while (sol + len < eol && sol[len] != ':')
757 len++;
758 name = sol;
759 }
760
761 if ((len < eol - sol) &&
762 (sol[len] == ':') &&
763 (strncasecmp(sol, name, len) == 0)) {
764 ctx->del = len;
765 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100766 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200767 sov++;
768
769 ctx->line = sol;
770 ctx->prev = old_idx;
771 ctx->idx = cur_idx;
772 ctx->val = sov - sol;
773 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100774 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200775 eol--;
776 ctx->tws++;
777 }
778 ctx->vlen = eol - sov;
779 return 1;
780 }
781 next_hdr:
782 sol = eol + idx->v[cur_idx].cr + 1;
783 old_idx = cur_idx;
784 cur_idx = idx->v[cur_idx].next;
785 }
786 return 0;
787}
788
Willy Tarreauc90dc232015-02-20 13:51:36 +0100789/* Find the first or next header field in message buffer <sol> using headers
790 * index <idx>, and return it in the <ctx> structure. This structure holds
791 * everything necessary to use the header and find next occurrence. If its
792 * <idx> member is 0, the first header is retrieved. Otherwise, the next
793 * occurrence is returned. The function returns 1 when it finds a value, and
794 * 0 when there is no more. It is equivalent to http_find_full_header2() with
795 * no header name.
796 */
797int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
798{
799 char *eol, *sov;
800 int cur_idx, old_idx;
801 int len;
802
803 cur_idx = ctx->idx;
804 if (cur_idx) {
805 /* We have previously returned a header, let's search another one */
806 sol = ctx->line;
807 eol = sol + idx->v[cur_idx].len;
808 goto next_hdr;
809 }
810
811 /* first request for this header */
812 sol += hdr_idx_first_pos(idx);
813 old_idx = 0;
814 cur_idx = hdr_idx_first_idx(idx);
815 while (cur_idx) {
816 eol = sol + idx->v[cur_idx].len;
817
818 len = 0;
819 while (1) {
820 if (len >= eol - sol)
821 goto next_hdr;
822 if (sol[len] == ':')
823 break;
824 len++;
825 }
826
827 ctx->del = len;
828 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100829 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100830 sov++;
831
832 ctx->line = sol;
833 ctx->prev = old_idx;
834 ctx->idx = cur_idx;
835 ctx->val = sov - sol;
836 ctx->tws = 0;
837
Willy Tarreau2235b262016-11-05 15:50:20 +0100838 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100839 eol--;
840 ctx->tws++;
841 }
842 ctx->vlen = eol - sov;
843 return 1;
844
845 next_hdr:
846 sol = eol + idx->v[cur_idx].cr + 1;
847 old_idx = cur_idx;
848 cur_idx = idx->v[cur_idx].next;
849 }
850 return 0;
851}
852
Willy Tarreau68085d82010-01-18 14:54:04 +0100853/* Find the end of the header value contained between <s> and <e>. See RFC2616,
854 * par 2.2 for more information. Note that it requires a valid header to return
855 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200856 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100857char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858{
859 int quoted, qdpair;
860
861 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100862
863#if defined(__x86_64__) || \
864 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
865 defined(__ARM_ARCH_7A__)
866 /* speedup: skip everything not a comma nor a double quote */
867 for (; s <= e - sizeof(int); s += sizeof(int)) {
868 unsigned int c = *(int *)s; // comma
869 unsigned int q = c; // quote
870
871 c ^= 0x2c2c2c2c; // contains one zero on a comma
872 q ^= 0x22222222; // contains one zero on a quote
873
874 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
875 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
876
877 if ((c | q) & 0x80808080)
878 break; // found a comma or a quote
879 }
880#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200881 for (; s < e; s++) {
882 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200883 else if (quoted) {
884 if (*s == '\\') qdpair = 1;
885 else if (*s == '"') quoted = 0;
886 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200887 else if (*s == '"') quoted = 1;
888 else if (*s == ',') return s;
889 }
890 return s;
891}
892
893/* Find the first or next occurrence of header <name> in message buffer <sol>
894 * using headers index <idx>, and return it in the <ctx> structure. This
895 * structure holds everything necessary to use the header and find next
896 * occurrence. If its <idx> member is 0, the header is searched from the
897 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 * 1 when it finds a value, and 0 when there is no more. It is designed to work
899 * with headers defined as comma-separated lists. As a special case, if ctx->val
900 * is NULL when searching for a new values of a header, the current header is
901 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200902 */
903int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200905 struct hdr_ctx *ctx)
906{
Willy Tarreau68085d82010-01-18 14:54:04 +0100907 char *eol, *sov;
908 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200909
Willy Tarreau68085d82010-01-18 14:54:04 +0100910 cur_idx = ctx->idx;
911 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200912 /* We have previously returned a value, let's search
913 * another one on the same line.
914 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200915 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100917 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 eol = sol + idx->v[cur_idx].len;
919
920 if (sov >= eol)
921 /* no more values in this header */
922 goto next_hdr;
923
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 /* values remaining for this header, skip the comma but save it
925 * for later use (eg: for header deletion).
926 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200927 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100928 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200929 sov++;
930
931 goto return_hdr;
932 }
933
934 /* first request for this header */
935 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100936 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200938 while (cur_idx) {
939 eol = sol + idx->v[cur_idx].len;
940
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200941 if (len == 0) {
942 /* No argument was passed, we want any header.
943 * To achieve this, we simply build a fake request. */
944 while (sol + len < eol && sol[len] != ':')
945 len++;
946 name = sol;
947 }
948
Willy Tarreau33a7e692007-06-10 19:45:56 +0200949 if ((len < eol - sol) &&
950 (sol[len] == ':') &&
951 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100952 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200953 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100954 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200955 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100956
Willy Tarreau33a7e692007-06-10 19:45:56 +0200957 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100958 ctx->prev = old_idx;
959 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200960 ctx->idx = cur_idx;
961 ctx->val = sov - sol;
962
963 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200964 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100965 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200966 eol--;
967 ctx->tws++;
968 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200969 ctx->vlen = eol - sov;
970 return 1;
971 }
972 next_hdr:
973 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100974 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200975 cur_idx = idx->v[cur_idx].next;
976 }
977 return 0;
978}
979
980int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100981 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200982 struct hdr_ctx *ctx)
983{
984 return http_find_header2(name, strlen(name), sol, idx, ctx);
985}
986
Willy Tarreau68085d82010-01-18 14:54:04 +0100987/* Remove one value of a header. This only works on a <ctx> returned by one of
988 * the http_find_header functions. The value is removed, as well as surrounding
989 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100990 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100991 * message <msg>. The new index is returned. If it is zero, it means there is
992 * no more header, so any processing may stop. The ctx is always left in a form
993 * that can be handled by http_find_header2() to find next occurrence.
994 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100995int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100996{
997 int cur_idx = ctx->idx;
998 char *sol = ctx->line;
999 struct hdr_idx_elem *hdr;
1000 int delta, skip_comma;
1001
1002 if (!cur_idx)
1003 return 0;
1004
1005 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001006 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +01001007 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001008 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +01001009 http_msg_move_end(msg, delta);
1010 idx->used--;
1011 hdr->len = 0; /* unused entry */
1012 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +01001013 if (idx->tail == ctx->idx)
1014 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +01001015 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +01001016 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +01001017 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001018 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001019 return ctx->idx;
1020 }
1021
1022 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001023 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1024 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001025 */
1026
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001027 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001028 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001029 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001030 NULL, 0);
1031 hdr->len += delta;
1032 http_msg_move_end(msg, delta);
1033 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001034 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001035 return ctx->idx;
1036}
1037
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001038/* This function handles a server error at the stream interface level. The
1039 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +01001040 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001041 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001042 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001044static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +01001045 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046{
Willy Tarreau2019f952017-03-14 11:07:31 +01001047 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001048 channel_auto_read(si_oc(si));
1049 channel_abort(si_oc(si));
1050 channel_auto_close(si_oc(si));
1051 channel_erase(si_oc(si));
1052 channel_auto_close(si_ic(si));
1053 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001054 if (msg)
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001055 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001056 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001057 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001059 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060}
1061
Willy Tarreau87b09662015-04-03 00:22:06 +02001062/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001063 * and message.
1064 */
1065
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001066struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +01001067{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001068 const int msgnum = http_get_status_idx(s->txn->status);
1069
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001070 if (s->be->errmsg[msgnum].str)
1071 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001072 else if (strm_fe(s)->errmsg[msgnum].str)
1073 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001074 else
1075 return &http_err_chunks[msgnum];
1076}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001078void
1079http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1080{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001081 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001082 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001083 stream_int_retnclose(&s->si[0], msg);
1084}
1085
Willy Tarreau53b6c742006-12-17 13:37:46 +01001086/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001087 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1088 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001089 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001090enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001091{
1092 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001094
1095 m = ((unsigned)*str - 'A');
1096
1097 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 for (h = http_methods[m]; h->len > 0; h++) {
1099 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001100 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001102 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001103 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001104 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001105 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001106}
1107
Willy Tarreau21d2af32008-02-14 20:25:24 +01001108/* Parse the URI from the given transaction (which is assumed to be in request
1109 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1110 * It is returned otherwise.
1111 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001112char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001113{
1114 char *ptr, *end;
1115
Willy Tarreau9b28e032012-10-12 23:49:43 +02001116 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001117 end = ptr + txn->req.sl.rq.u_l;
1118
1119 if (ptr >= end)
1120 return NULL;
1121
1122 /* RFC2616, par. 5.1.2 :
1123 * Request-URI = "*" | absuri | abspath | authority
1124 */
1125
1126 if (*ptr == '*')
1127 return NULL;
1128
1129 if (isalpha((unsigned char)*ptr)) {
1130 /* this is a scheme as described by RFC3986, par. 3.1 */
1131 ptr++;
1132 while (ptr < end &&
1133 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1134 ptr++;
1135 /* skip '://' */
1136 if (ptr == end || *ptr++ != ':')
1137 return NULL;
1138 if (ptr == end || *ptr++ != '/')
1139 return NULL;
1140 if (ptr == end || *ptr++ != '/')
1141 return NULL;
1142 }
1143 /* skip [user[:passwd]@]host[:[port]] */
1144
1145 while (ptr < end && *ptr != '/')
1146 ptr++;
1147
1148 if (ptr == end)
1149 return NULL;
1150
1151 /* OK, we got the '/' ! */
1152 return ptr;
1153}
1154
William Lallemand65ad6e12014-01-31 15:08:02 +01001155/* Parse the URI from the given string and look for the "/" beginning the PATH.
1156 * If not found, return NULL. It is returned otherwise.
1157 */
1158static char *
1159http_get_path_from_string(char *str)
1160{
1161 char *ptr = str;
1162
1163 /* RFC2616, par. 5.1.2 :
1164 * Request-URI = "*" | absuri | abspath | authority
1165 */
1166
1167 if (*ptr == '*')
1168 return NULL;
1169
1170 if (isalpha((unsigned char)*ptr)) {
1171 /* this is a scheme as described by RFC3986, par. 3.1 */
1172 ptr++;
1173 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1174 ptr++;
1175 /* skip '://' */
1176 if (*ptr == '\0' || *ptr++ != ':')
1177 return NULL;
1178 if (*ptr == '\0' || *ptr++ != '/')
1179 return NULL;
1180 if (*ptr == '\0' || *ptr++ != '/')
1181 return NULL;
1182 }
1183 /* skip [user[:passwd]@]host[:[port]] */
1184
1185 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1186 ptr++;
1187
1188 if (*ptr == '\0' || *ptr == ' ')
1189 return NULL;
1190
1191 /* OK, we got the '/' ! */
1192 return ptr;
1193}
1194
Willy Tarreau71241ab2012-12-27 11:30:54 +01001195/* Returns a 302 for a redirectable request that reaches a server working in
1196 * in redirect mode. This may only be called just after the stream interface
1197 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1198 * follow normal proxy processing. NOTE: this function is designed to support
1199 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001200 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001201void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001202{
1203 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001204 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001205 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001206 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001207
1208 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001209 trash.len = strlen(HTTP_302);
1210 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001211
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001212 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001213
Willy Tarreauefb453c2008-10-26 20:49:47 +01001214 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001215 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001216 return;
1217
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001218 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001219 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001220 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1221 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001222 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001223
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001224 /* 3: add the request URI. Since it was already forwarded, we need
1225 * to temporarily rewind the buffer.
1226 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001227 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001228 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001229
Willy Tarreauefb453c2008-10-26 20:49:47 +01001230 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001231 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 +02001232
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001233 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001234
Willy Tarreauefb453c2008-10-26 20:49:47 +01001235 if (!path)
1236 return;
1237
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001238 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001239 return;
1240
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001241 memcpy(trash.str + trash.len, path, len);
1242 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001243
1244 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001245 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1246 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001247 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001248 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1249 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001250 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001251
1252 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001253 si_shutr(si);
1254 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001255 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001256 si->state = SI_ST_CLO;
1257
1258 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001259 txn->status = 302;
1260 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001261
1262 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001263 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001264 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001265}
1266
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001267/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001268 * that the server side is closed. Note that err_type is actually a
1269 * bitmask, where almost only aborts may be cumulated with other
1270 * values. We consider that aborted operations are more important
1271 * than timeouts or errors due to the fact that nobody else in the
1272 * logs might explain incomplete retries. All others should avoid
1273 * being cumulated. It should normally not be possible to have multiple
1274 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001275 * Note that connection errors appearing on the second request of a keep-alive
1276 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001277 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001278void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001279{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001280 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001281
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001282 /* set s->txn->status for http_error_message(s) */
1283 s->txn->status = 503;
1284
Willy Tarreauefb453c2008-10-26 20:49:47 +01001285 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001286 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001287 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001288 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001289 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001290 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001291 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001292 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001293 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001294 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001295 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001296 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001297 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001298 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001299 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001300 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001301 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001302 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001303 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001304 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001305 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001306 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001307 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001308 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001309 http_error_message(s));
1310 else { /* SI_ET_CONN_OTHER and others */
1311 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001312 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001313 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001314 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001315}
1316
Willy Tarreau42250582007-04-01 01:30:43 +02001317extern const char sess_term_cond[8];
1318extern const char sess_fin_state[8];
1319extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001320struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001321struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001322struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001323struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324
Willy Tarreau117f59e2007-03-04 18:17:17 +01001325/*
1326 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001327 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001328 */
1329void capture_headers(char *som, struct hdr_idx *idx,
1330 char **cap, struct cap_hdr *cap_hdr)
1331{
1332 char *eol, *sol, *col, *sov;
1333 int cur_idx;
1334 struct cap_hdr *h;
1335 int len;
1336
1337 sol = som + hdr_idx_first_pos(idx);
1338 cur_idx = hdr_idx_first_idx(idx);
1339
1340 while (cur_idx) {
1341 eol = sol + idx->v[cur_idx].len;
1342
1343 col = sol;
1344 while (col < eol && *col != ':')
1345 col++;
1346
1347 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001348 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001349 sov++;
1350
1351 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001352 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001353 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1354 if (cap[h->index] == NULL)
1355 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001356 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001357
1358 if (cap[h->index] == NULL) {
1359 Alert("HTTP capture : out of memory.\n");
1360 continue;
1361 }
1362
1363 len = eol - sov;
1364 if (len > h->len)
1365 len = h->len;
1366
1367 memcpy(cap[h->index], sov, len);
1368 cap[h->index][len]=0;
1369 }
1370 }
1371 sol = eol + idx->v[cur_idx].cr + 1;
1372 cur_idx = idx->v[cur_idx].next;
1373 }
1374}
1375
1376
Willy Tarreau42250582007-04-01 01:30:43 +02001377/* either we find an LF at <ptr> or we jump to <bad>.
1378 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001379#define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0)
Willy Tarreau42250582007-04-01 01:30:43 +02001380
1381/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1382 * otherwise to <http_msg_ood> with <state> set to <st>.
1383 */
1384#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1385 ptr++; \
1386 if (likely(ptr < end)) \
1387 goto good; \
1388 else { \
1389 state = (st); \
1390 goto http_msg_ood; \
1391 } \
1392 } while (0)
1393
1394
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001396 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1398 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1399 * will give undefined results.
1400 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1401 * and that msg->sol points to the beginning of the response.
1402 * If a complete line is found (which implies that at least one CR or LF is
1403 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1404 * returned indicating an incomplete line (which does not mean that parts have
1405 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1406 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1407 * upon next call.
1408 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001409 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1411 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001412 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001414const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001415 enum ht_state state, const char *ptr, const char *end,
1416 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001417{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001418 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001419
Willy Tarreau8973c702007-01-21 23:58:29 +01001420 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001423 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1425
1426 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001427 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1429 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001430 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001431 state = HTTP_MSG_ERROR;
1432 break;
1433
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001435 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001437 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 goto http_msg_rpcode;
1439 }
1440 if (likely(HTTP_IS_SPHT(*ptr)))
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1442 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001443 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001444 state = HTTP_MSG_ERROR;
1445 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001446
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001448 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 if (likely(!HTTP_IS_LWS(*ptr)))
1450 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1451
1452 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001453 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001454 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1455 }
1456
1457 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001458 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001459 http_msg_rsp_reason:
1460 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001461 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001462 msg->sl.st.r_l = 0;
1463 goto http_msg_rpline_eol;
1464
Willy Tarreau8973c702007-01-21 23:58:29 +01001465 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001468 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 goto http_msg_rpreason;
1470 }
1471 if (likely(HTTP_IS_SPHT(*ptr)))
1472 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1473 /* so it's a CR/LF, so there is no reason phrase */
1474 goto http_msg_rsp_reason;
1475
Willy Tarreau8973c702007-01-21 23:58:29 +01001476 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001477 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001478 if (likely(!HTTP_IS_CRLF(*ptr)))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001480 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 http_msg_rpline_eol:
1482 /* We have seen the end of line. Note that we do not
1483 * necessarily have the \n yet, but at least we know that we
1484 * have EITHER \r OR \n, otherwise the response would not be
1485 * complete. We can then record the response length and return
1486 * to the caller which will be able to register it.
1487 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001488 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001489 return ptr;
1490
Willy Tarreau8973c702007-01-21 23:58:29 +01001491 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001492#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001493 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1494 exit(1);
1495#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001496 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 }
1498
1499 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001500 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 if (ret_state)
1502 *ret_state = state;
1503 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001504 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001506}
1507
Willy Tarreau8973c702007-01-21 23:58:29 +01001508/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 * This function parses a request line between <ptr> and <end>, starting with
1510 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1511 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1512 * will give undefined results.
1513 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1514 * and that msg->sol points to the beginning of the request.
1515 * If a complete line is found (which implies that at least one CR or LF is
1516 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1517 * returned indicating an incomplete line (which does not mean that parts have
1518 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1519 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1520 * upon next call.
1521 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001522 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1524 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001525 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001526 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001527const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001528 enum ht_state state, const char *ptr, const char *end,
1529 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001530{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001531 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001535 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 if (likely(HTTP_IS_TOKEN(*ptr)))
1537 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001540 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1542 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001543
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 if (likely(HTTP_IS_CRLF(*ptr))) {
1545 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001546 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001548 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001550 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001552 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 msg->sl.rq.v_l = 0;
1554 goto http_msg_rqline_eol;
1555 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001556 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001557 state = HTTP_MSG_ERROR;
1558 break;
1559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001561 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001563 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 goto http_msg_rquri;
1565 }
1566 if (likely(HTTP_IS_SPHT(*ptr)))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1568 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1569 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001573#if defined(__x86_64__) || \
1574 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1575 defined(__ARM_ARCH_7A__)
1576 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1577 while (ptr <= end - sizeof(int)) {
1578 int x = *(int *)ptr - 0x21212121;
1579 if (x & 0x80808080)
1580 break;
1581
1582 x -= 0x5e5e5e5e;
1583 if (!(x & 0x80808080))
1584 break;
1585
1586 ptr += sizeof(int);
1587 }
1588#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001589 if (ptr >= end) {
1590 state = HTTP_MSG_RQURI;
1591 goto http_msg_ood;
1592 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001593 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001594 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001595 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001598 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1600 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001602 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001603 /* non-ASCII chars are forbidden unless option
1604 * accept-invalid-http-request is enabled in the frontend.
1605 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001606 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001607 if (msg->err_pos < -1)
1608 goto invalid_char;
1609 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001610 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001611 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1612 }
1613
1614 if (likely(HTTP_IS_CRLF(*ptr))) {
1615 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1616 goto http_msg_req09_uri_e;
1617 }
1618
1619 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001620 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001621 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001622 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001623 state = HTTP_MSG_ERROR;
1624 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001627 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001629 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 goto http_msg_rqver;
1631 }
1632 if (likely(HTTP_IS_SPHT(*ptr)))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1634 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1635 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001639 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001641
1642 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001643 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001644 http_msg_rqline_eol:
1645 /* We have seen the end of line. Note that we do not
1646 * necessarily have the \n yet, but at least we know that we
1647 * have EITHER \r OR \n, otherwise the request would not be
1648 * complete. We can then record the request length and return
1649 * to the caller which will be able to register it.
1650 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001651 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001652 return ptr;
1653 }
1654
1655 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001656 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001657 state = HTTP_MSG_ERROR;
1658 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001661#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1663 exit(1);
1664#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001665 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001669 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 if (ret_state)
1671 *ret_state = state;
1672 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001673 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001677/*
1678 * Returns the data from Authorization header. Function may be called more
1679 * than once so data is stored in txn->auth_data. When no header is found
1680 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001681 * searching again for something we are unable to find anyway. However, if
1682 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001684 */
1685
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001686/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1687 * set according to global.tune.bufsize.
1688 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001689char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001690
1691int
Willy Tarreau87b09662015-04-03 00:22:06 +02001692get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001693{
1694
Willy Tarreaueee5b512015-04-03 23:46:31 +02001695 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001696 struct chunk auth_method;
1697 struct hdr_ctx ctx;
1698 char *h, *p;
1699 int len;
1700
1701#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001702 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001703#endif
1704
1705 if (txn->auth.method == HTTP_AUTH_WRONG)
1706 return 0;
1707
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001708 txn->auth.method = HTTP_AUTH_WRONG;
1709
1710 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001711
1712 if (txn->flags & TX_USE_PX_CONN) {
1713 h = "Proxy-Authorization";
1714 len = strlen(h);
1715 } else {
1716 h = "Authorization";
1717 len = strlen(h);
1718 }
1719
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001720 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001721 return 0;
1722
1723 h = ctx.line + ctx.val;
1724
1725 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001726 len = p - h;
1727 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001728 return 0;
1729
David Carlier7365f7d2016-04-04 11:54:42 +01001730 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1731 return 0;
1732
Willy Tarreau5c557d12016-03-13 08:17:02 +01001733 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001734
1735 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1736
1737 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001738 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001739
1740 if (len < 0)
1741 return 0;
1742
1743
1744 get_http_auth_buff[len] = '\0';
1745
1746 p = strchr(get_http_auth_buff, ':');
1747
1748 if (!p)
1749 return 0;
1750
1751 txn->auth.user = get_http_auth_buff;
1752 *p = '\0';
1753 txn->auth.pass = p+1;
1754
1755 txn->auth.method = HTTP_AUTH_BASIC;
1756 return 1;
1757 }
1758
1759 return 0;
1760}
1761
Willy Tarreau58f10d72006-12-04 02:26:12 +01001762
Willy Tarreau8973c702007-01-21 23:58:29 +01001763/*
1764 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001765 * depending on the initial msg->msg_state. The caller is responsible for
1766 * ensuring that the message does not wrap. The function can be preempted
1767 * everywhere when data are missing and recalled at the exact same location
1768 * with no information loss. The message may even be realigned between two
1769 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001770 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001771 * fields. Note that msg->sol will be initialized after completing the first
1772 * state, so that none of the msg pointers has to be initialized prior to the
1773 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001774 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001775void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776{
Willy Tarreau3770f232013-12-07 00:01:53 +01001777 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001779 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001780
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001781 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001782 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001783 ptr = buf->p + msg->next;
1784 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 if (unlikely(ptr >= end))
1787 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001790 /*
1791 * First, states that are specific to the response only.
1792 * We check them first so that request and headers are
1793 * closer to each other (accessed more often).
1794 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001795 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001796 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001797 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001798 /* we have a start of message, but we have to check
1799 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001800 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001801 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001802 if (unlikely(ptr != buf->p)) {
1803 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001804 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001805 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001806 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001807 }
Willy Tarreau26927362012-05-18 23:22:52 +02001808 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001809 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001810 hdr_idx_init(idx);
1811 state = HTTP_MSG_RPVER;
1812 goto http_msg_rpver;
1813 }
1814
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001815 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1816 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001817 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001818 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001819
1820 if (unlikely(*ptr == '\n'))
1821 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1822 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1823 /* stop here */
1824
Willy Tarreau8973c702007-01-21 23:58:29 +01001825 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001826 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001827 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001828 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1829 /* stop here */
1830
Willy Tarreau8973c702007-01-21 23:58:29 +01001831 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001832 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001833 case HTTP_MSG_RPVER_SP:
1834 case HTTP_MSG_RPCODE:
1835 case HTTP_MSG_RPCODE_SP:
1836 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001837 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001838 state, ptr, end,
1839 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001840 if (unlikely(!ptr))
1841 return;
1842
1843 /* we have a full response and we know that we have either a CR
1844 * or an LF at <ptr>.
1845 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001846 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1847
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001849 if (likely(*ptr == '\r'))
1850 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1851 goto http_msg_rpline_end;
1852
Willy Tarreau8973c702007-01-21 23:58:29 +01001853 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001855 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001856 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001857 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1858 /* stop here */
1859
1860 /*
1861 * Second, states that are specific to the request only
1862 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001864 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001866 /* we have a start of message, but we have to check
1867 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001868 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001869 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001870 if (likely(ptr != buf->p)) {
1871 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001872 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001873 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001874 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 }
Willy Tarreau26927362012-05-18 23:22:52 +02001876 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001877 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001878 state = HTTP_MSG_RQMETH;
1879 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001881
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001882 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1883 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001885 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 if (unlikely(*ptr == '\n'))
1888 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1889 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001890 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001893 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001894 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001895 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001896 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001897
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001899 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 case HTTP_MSG_RQMETH_SP:
1901 case HTTP_MSG_RQURI:
1902 case HTTP_MSG_RQURI_SP:
1903 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001904 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001905 state, ptr, end,
1906 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 if (unlikely(!ptr))
1908 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 /* we have a full request and we know that we have either a CR
1911 * or an LF at <ptr>.
1912 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001913 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001914
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 if (likely(*ptr == '\r'))
1917 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001921 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 /* check for HTTP/0.9 request : no version information available.
1923 * msg->sol must point to the first of CR or LF.
1924 */
1925 if (unlikely(msg->sl.rq.v_l == 0))
1926 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001927
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001928 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001929 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001930 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001931
Willy Tarreau8973c702007-01-21 23:58:29 +01001932 /*
1933 * Common states below
1934 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001936 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001938 if (likely(!HTTP_IS_CRLF(*ptr))) {
1939 goto http_msg_hdr_name;
1940 }
1941
1942 if (likely(*ptr == '\r'))
1943 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1944 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001947 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001948 /* assumes msg->sol points to the first char */
1949 if (likely(HTTP_IS_TOKEN(*ptr)))
1950 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001951
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001952 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001954
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001955 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1956 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001957 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001958 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001959
1960 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001962
1963 /* and we still accept this non-token character */
1964 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001967 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001968 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969 if (likely(HTTP_IS_SPHT(*ptr)))
1970 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001973 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001974
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975 if (likely(!HTTP_IS_CRLF(*ptr))) {
1976 goto http_msg_hdr_val;
1977 }
1978
1979 if (likely(*ptr == '\r'))
1980 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1981 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001984 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001985 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001986 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001987
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001989 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 if (likely(HTTP_IS_SPHT(*ptr))) {
1991 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 for (; buf->p + msg->sov < ptr; msg->sov++)
1993 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001994 goto http_msg_hdr_l1_sp;
1995 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001996 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001997 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001998 goto http_msg_complete_header;
1999
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002000 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002001 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002002 /* assumes msg->sol points to the first char, and msg->sov
2003 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002005
2006 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
2007 * and lower. In fact since most of the time is spent in the loop, we
2008 * also remove the sign bit test so that bytes 0x8e..0x0d break the
2009 * loop, but we don't care since they're very rare in header values.
2010 */
2011#if defined(__x86_64__)
2012 while (ptr <= end - sizeof(long)) {
2013 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
2014 goto http_msg_hdr_val2;
2015 ptr += sizeof(long);
2016 }
2017#endif
2018#if defined(__x86_64__) || \
2019 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
2020 defined(__ARM_ARCH_7A__)
2021 while (ptr <= end - sizeof(int)) {
2022 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
2023 goto http_msg_hdr_val2;
2024 ptr += sizeof(int);
2025 }
2026#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002027 if (ptr >= end) {
2028 state = HTTP_MSG_HDR_VAL;
2029 goto http_msg_ood;
2030 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002031 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002032 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002033 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002035 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002036 /* Note: we could also copy eol into ->eoh so that we have the
2037 * real header end in case it ends with lots of LWS, but is this
2038 * really needed ?
2039 */
2040 if (likely(*ptr == '\r'))
2041 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2042 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002045 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002046 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002047 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002049 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002050 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002051 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2052 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 for (; buf->p + msg->eol < ptr; msg->eol++)
2054 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002055 goto http_msg_hdr_val;
2056 }
2057 http_msg_complete_header:
2058 /*
2059 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002060 * Assumes msg->sol points to the first char, msg->sov points
2061 * to the first character of the value and msg->eol to the
2062 * first CR or LF so we know how the line ends. We insert last
2063 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002064 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002065 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002066 idx, idx->tail) < 0)) {
2067 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002068 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002069 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002070
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002072 if (likely(!HTTP_IS_CRLF(*ptr))) {
2073 goto http_msg_hdr_name;
2074 }
2075
2076 if (likely(*ptr == '\r'))
2077 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2078 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002079
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002080 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002081 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002082 /* Assumes msg->sol points to the first of either CR or LF.
2083 * Sets ->sov and ->next to the total header length, ->eoh to
2084 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2085 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002086 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002087 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002089 msg->eoh = msg->sol;
2090 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002091 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002092 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002093 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002094
2095 case HTTP_MSG_ERROR:
2096 /* this may only happen if we call http_msg_analyser() twice with an error */
2097 break;
2098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002099 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002100#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2102 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002103#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002104 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002105 }
2106 http_msg_ood:
2107 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002108 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002109 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002110 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002112 http_msg_invalid:
2113 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002114 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002115 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002117 return;
2118}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002119
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002120/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2121 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2122 * nothing is done and 1 is returned.
2123 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002124static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002125{
2126 int delta;
2127 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002128 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002129
2130 if (msg->sl.rq.v_l != 0)
2131 return 1;
2132
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002133 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2134 if (txn->meth != HTTP_METH_GET)
2135 return 0;
2136
Willy Tarreau9b28e032012-10-12 23:49:43 +02002137 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002138
2139 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002140 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2141 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002142 }
2143 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002144 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002145 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002146 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002147 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002148 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002149 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002150 NULL, NULL);
2151 if (unlikely(!cur_end))
2152 return 0;
2153
2154 /* we have a full HTTP/1.0 request now and we know that
2155 * we have either a CR or an LF at <ptr>.
2156 */
2157 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2158 return 1;
2159}
2160
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002161/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002162 * and "keep-alive" values. If we already know that some headers may safely
2163 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002164 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2165 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002166 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002167 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2168 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2169 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002170 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002171 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002172void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002173{
Willy Tarreau5b154472009-12-21 20:11:07 +01002174 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002175 const char *hdr_val = "Connection";
2176 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002177
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002178 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002179 return;
2180
Willy Tarreau88d349d2010-01-25 12:15:43 +01002181 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2182 hdr_val = "Proxy-Connection";
2183 hdr_len = 16;
2184 }
2185
Willy Tarreau5b154472009-12-21 20:11:07 +01002186 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002187 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002188 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002189 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2190 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002191 if (to_del & 2)
2192 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002193 else
2194 txn->flags |= TX_CON_KAL_SET;
2195 }
2196 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2197 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002198 if (to_del & 1)
2199 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002200 else
2201 txn->flags |= TX_CON_CLO_SET;
2202 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002203 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2204 txn->flags |= TX_HDR_CONN_UPG;
2205 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002206 }
2207
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002208 txn->flags |= TX_HDR_CONN_PRS;
2209 return;
2210}
Willy Tarreau5b154472009-12-21 20:11:07 +01002211
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002212/* Apply desired changes on the Connection: header. Values may be removed and/or
2213 * added depending on the <wanted> flags, which are exclusively composed of
2214 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2215 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2216 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002217void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002218{
2219 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002220 const char *hdr_val = "Connection";
2221 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002222
2223 ctx.idx = 0;
2224
Willy Tarreau88d349d2010-01-25 12:15:43 +01002225
2226 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2227 hdr_val = "Proxy-Connection";
2228 hdr_len = 16;
2229 }
2230
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002231 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002232 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002233 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2234 if (wanted & TX_CON_KAL_SET)
2235 txn->flags |= TX_CON_KAL_SET;
2236 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002237 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002238 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002239 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2240 if (wanted & TX_CON_CLO_SET)
2241 txn->flags |= TX_CON_CLO_SET;
2242 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002243 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002244 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002245 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002246
2247 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2248 return;
2249
2250 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2251 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002252 hdr_val = "Connection: close";
2253 hdr_len = 17;
2254 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2255 hdr_val = "Proxy-Connection: close";
2256 hdr_len = 23;
2257 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002258 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002259 }
2260
2261 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2262 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002263 hdr_val = "Connection: keep-alive";
2264 hdr_len = 22;
2265 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2266 hdr_val = "Proxy-Connection: keep-alive";
2267 hdr_len = 28;
2268 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002269 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002270 }
2271 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002272}
2273
Christopher Faulet113f7de2015-12-14 14:52:13 +01002274/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2275 * point to the first byte of data after the chunk size, so that we know we can
2276 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2277 * forming the chunk size. That way it is always possible to differentiate
2278 * between the start of the body and the start of the data. Return the number
2279 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2280 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002281 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002282static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002283{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002284 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002285 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002286 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002287 const char *end = buf->data + buf->size;
2288 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002289 unsigned int chunk = 0;
2290
2291 /* The chunk size is in the following form, though we are only
2292 * interested in the size and CRLF :
2293 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2294 */
2295 while (1) {
2296 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002297 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002298 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002299 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002300 if (c < 0) /* not a hex digit anymore */
2301 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002302 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002303 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002304 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002305 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002306 chunk = (chunk << 4) + c;
2307 }
2308
Willy Tarreaud98cf932009-12-27 22:54:55 +01002309 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002310 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002311 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002312
Willy Tarreau2235b262016-11-05 15:50:20 +01002313 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002314 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002315 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002316 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002317 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002318 }
2319
Willy Tarreaud98cf932009-12-27 22:54:55 +01002320 /* Up to there, we know that at least one byte is present at *ptr. Check
2321 * for the end of chunk size.
2322 */
2323 while (1) {
2324 if (likely(HTTP_IS_CRLF(*ptr))) {
2325 /* we now have a CR or an LF at ptr */
2326 if (likely(*ptr == '\r')) {
2327 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002328 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002329 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002330 return 0;
2331 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002332
Willy Tarreaud98cf932009-12-27 22:54:55 +01002333 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002334 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002335 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002336 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002337 /* done */
2338 break;
2339 }
2340 else if (*ptr == ';') {
2341 /* chunk extension, ends at next CRLF */
2342 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002343 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002344 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002345 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002346
2347 while (!HTTP_IS_CRLF(*ptr)) {
2348 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002349 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002350 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002351 return 0;
2352 }
2353 /* we have a CRLF now, loop above */
2354 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002355 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002356 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002357 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002358 }
2359
Christopher Faulet113f7de2015-12-14 14:52:13 +01002360 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2361 * or may not be present. We save the number of bytes parsed into
2362 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002363 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002364 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002365 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002366 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002367 msg->chunk_len = chunk;
2368 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002369 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002370 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002371 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002372 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002373}
2374
Christopher Faulet113f7de2015-12-14 14:52:13 +01002375/* This function skips trailers in the buffer associated with HTTP message
2376 * <msg>. The first visited position is msg->next. If the end of the trailers is
2377 * found, the function returns >0. So, the caller can automatically schedul it
2378 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2379 * data are available, the function does not change anything except maybe
2380 * msg->sol if it could parse some lines, and returns zero. If a parse error
2381 * is encountered, the function returns < 0 and does not change anything except
2382 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2383 * state before calling this function, which implies that all non-trailers data
2384 * have already been scheduled for forwarding, and that msg->next exactly
2385 * matches the length of trailers already parsed and not forwarded. It is also
2386 * important to note that this function is designed to be able to parse wrapped
2387 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002388 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002389static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002390{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002391 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002392
Christopher Faulet113f7de2015-12-14 14:52:13 +01002393 /* we have msg->next which points to next line. Look for CRLF. But
2394 * first, we reset msg->sol */
2395 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002396 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002397 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002398 const char *start = b_ptr(buf, msg->next + msg->sol);
2399 const char *stop = bi_end(buf);
2400 const char *ptr = start;
2401 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002402
2403 /* scan current line and stop at LF or CRLF */
2404 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002405 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002406 return 0;
2407
2408 if (*ptr == '\n') {
2409 if (!p1)
2410 p1 = ptr;
2411 p2 = ptr;
2412 break;
2413 }
2414
2415 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002416 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002417 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002418 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002419 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002420 p1 = ptr;
2421 }
2422
2423 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002424 if (ptr >= buf->data + buf->size)
2425 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002426 }
2427
2428 /* after LF; point to beginning of next line */
2429 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002430 if (p2 >= buf->data + buf->size)
2431 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002432
Christopher Faulet2fb28802015-12-01 10:40:57 +01002433 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002434 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002435 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002436 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002437
Christopher Fauletd7c91962015-04-30 11:48:27 +02002438 /* LF/CRLF at beginning of line => end of trailers at p2.
2439 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002440 * from this message. */
2441 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002442 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002443
Willy Tarreaud98cf932009-12-27 22:54:55 +01002444 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002445 }
2446}
2447
Christopher Faulet113f7de2015-12-14 14:52:13 +01002448/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2449 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2450 * in order to include this part into the next forwarding phase. Note that the
2451 * caller must ensure that ->p points to the first byte to parse. It returns
2452 * the number of bytes parsed on success, so the caller can set msg_state to
2453 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2454 * change anything and returns zero. If a parse error is encountered, the
2455 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2456 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002457 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002458static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002459{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002460 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002461 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002462 int bytes;
2463
2464 /* NB: we'll check data availabilty at the end. It's not a
2465 * problem because whatever we match first will be checked
2466 * against the correct length.
2467 */
2468 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002469 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002470 if (*ptr == '\r') {
2471 bytes++;
2472 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002473 if (ptr >= buf->data + buf->size)
2474 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002475 }
2476
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002477 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002478 return 0;
2479
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002480 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002481 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002482 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002483 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002484 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002485}
Willy Tarreau5b154472009-12-21 20:11:07 +01002486
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002487/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2488 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2489 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2490 * Unparsable qvalues return 1000 as "q=1.000".
2491 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002492int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002493{
2494 int q = 1000;
2495
Willy Tarreau506c69a2014-07-08 00:59:48 +02002496 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002497 goto out;
2498 q = (*qvalue++ - '0') * 1000;
2499
2500 if (*qvalue++ != '.')
2501 goto out;
2502
Willy Tarreau506c69a2014-07-08 00:59:48 +02002503 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002504 goto out;
2505 q += (*qvalue++ - '0') * 100;
2506
Willy Tarreau506c69a2014-07-08 00:59:48 +02002507 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002508 goto out;
2509 q += (*qvalue++ - '0') * 10;
2510
Willy Tarreau506c69a2014-07-08 00:59:48 +02002511 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002512 goto out;
2513 q += (*qvalue++ - '0') * 1;
2514 out:
2515 if (q > 1000)
2516 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002517 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002518 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002519 return q;
2520}
William Lallemand82fe75c2012-10-23 10:25:10 +02002521
Willy Tarreau87b09662015-04-03 00:22:06 +02002522void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002523{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002524 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002525 int tmp = TX_CON_WANT_KAL;
2526
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002527 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2528 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002529 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2530 tmp = TX_CON_WANT_TUN;
2531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2534 tmp = TX_CON_WANT_TUN;
2535 }
2536
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002537 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002538 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2539 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002540 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002541 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2542 tmp = TX_CON_WANT_CLO;
2543 else
2544 tmp = TX_CON_WANT_SCL;
2545 }
2546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002547 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002548 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2549 tmp = TX_CON_WANT_CLO;
2550
2551 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2552 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2553
2554 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2555 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2556 /* parse the Connection header and possibly clean it */
2557 int to_del = 0;
2558 if ((msg->flags & HTTP_MSGF_VER_11) ||
2559 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002560 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002561 to_del |= 2; /* remove "keep-alive" */
2562 if (!(msg->flags & HTTP_MSGF_VER_11))
2563 to_del |= 1; /* remove "close" */
2564 http_parse_connection_header(txn, msg, to_del);
2565 }
2566
2567 /* check if client or config asks for explicit close in KAL/SCL */
2568 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2569 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2570 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2571 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2572 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002573 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002574 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2575}
William Lallemand82fe75c2012-10-23 10:25:10 +02002576
Willy Tarreaud787e662009-07-07 10:14:51 +02002577/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2578 * processing can continue on next analysers, or zero if it either needs more
2579 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002580 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002581 * when it has nothing left to do, and may remove any analyser when it wants to
2582 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002583 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002584int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002585{
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /*
2587 * We will parse the partial (or complete) lines.
2588 * We will check the request syntax, and also join multi-line
2589 * headers. An index of all the lines will be elaborated while
2590 * parsing.
2591 *
2592 * For the parsing, we use a 28 states FSM.
2593 *
2594 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002595 * req->buf->p = beginning of request
2596 * req->buf->p + msg->eoh = end of processed headers / start of current one
2597 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002598 * msg->eol = end of current header or line (LF or CRLF)
2599 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002600 *
2601 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002602 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002603 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2604 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002606
Willy Tarreau59234e92008-11-30 23:51:27 +01002607 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002608 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002609 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002611 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002612
Willy Tarreau87b09662015-04-03 00:22:06 +02002613 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 +01002614 now_ms, __FUNCTION__,
2615 s,
2616 req,
2617 req->rex, req->wex,
2618 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002619 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002620 req->analysers);
2621
Willy Tarreau52a0c602009-08-16 22:45:38 +02002622 /* we're speaking HTTP here, so let's speak HTTP to the client */
2623 s->srv_error = http_return_srv_error;
2624
Willy Tarreau83e3af02009-12-28 17:39:57 +01002625 /* There's a protected area at the end of the buffer for rewriting
2626 * purposes. We don't want to start to parse the request if the
2627 * protected area is affected, because we may have to move processed
2628 * data later, which is much more complicated.
2629 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002630 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002631
2632 /* This point is executed when some data is avalaible for analysis,
2633 * so we log the end of the idle time. */
2634 if (s->logs.t_idle == -1)
2635 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2636
Willy Tarreau379357a2013-06-08 12:55:46 +02002637 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002638 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002639 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002640 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002641 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002642 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002643 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002644 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002645 return 0;
2646 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002647 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2648 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2649 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002650 }
2651
Willy Tarreau065e8332010-01-08 00:30:20 +01002652 /* Note that we have the same problem with the response ; we
2653 * may want to send a redirect, error or anything which requires
2654 * some spare space. So we'll ensure that we have at least
2655 * maxrewrite bytes available in the response buffer before
2656 * processing that one. This will only affect pipelined
2657 * keep-alive requests.
2658 */
2659 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002660 unlikely(!channel_is_rewritable(&s->res) ||
2661 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2662 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2663 if (s->res.buf->o) {
2664 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002665 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002666 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002667 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002668 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2669 s->res.flags |= CF_WAKE_WRITE;
2670 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002671 return 0;
2672 }
2673 }
2674
Willy Tarreau9b28e032012-10-12 23:49:43 +02002675 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002676 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002677 }
2678
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 /* 1: we might have to print this header in debug mode */
2680 if (unlikely((global.mode & MODE_DEBUG) &&
2681 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002682 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002684
Willy Tarreau9b28e032012-10-12 23:49:43 +02002685 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002686 /* this is a bit complex : in case of error on the request line,
2687 * we know that rq.l is still zero, so we display only the part
2688 * up to the end of the line (truncated by debug_hdr).
2689 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002690 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002692
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 sol += hdr_idx_first_pos(&txn->hdr_idx);
2694 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002695
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 while (cur_idx) {
2697 eol = sol + txn->hdr_idx.v[cur_idx].len;
2698 debug_hdr("clihdr", s, sol, eol);
2699 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2700 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002701 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 }
2703
Willy Tarreau58f10d72006-12-04 02:26:12 +01002704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 /*
2706 * Now we quickly check if we have found a full valid request.
2707 * If not so, we check the FD and buffer states before leaving.
2708 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002709 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002710 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002711 * on a keep-alive stream, if we encounter and error, close, t/o,
2712 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002713 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002714 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002715 * Last, we may increase some tracked counters' http request errors on
2716 * the cases that are deliberately the client's fault. For instance,
2717 * a timeout or connection reset is not counted as an error. However
2718 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002720
Willy Tarreau655dce92009-11-08 13:10:58 +01002721 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002722 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002724 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002725 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002726 stream_inc_http_req_ctr(s);
2727 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002728 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002731
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 /* 1: Since we are in header mode, if there's no space
2733 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002734 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002735 * must terminate it now.
2736 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002737 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002738 /* FIXME: check if URI is set and return Status
2739 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002740 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002741 stream_inc_http_req_ctr(s);
2742 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002743 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002744 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002745 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002746 goto return_bad_req;
2747 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002750 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002751 if (!(s->flags & SF_ERR_MASK))
2752 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002753
Willy Tarreaufcffa692010-01-10 14:21:19 +01002754 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002755 goto failed_keep_alive;
2756
Willy Tarreau0f228a02015-05-01 15:37:53 +02002757 if (sess->fe->options & PR_O_IGNORE_PRB)
2758 goto failed_keep_alive;
2759
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002761 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002762 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002763 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002764 }
2765
Willy Tarreaudc979f22012-12-04 10:39:01 +01002766 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002767 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002768 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002769 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002770 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002771 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002772 proxy_inc_fe_req_ctr(sess->fe);
2773 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002774 if (sess->listener->counters)
2775 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002776
Willy Tarreaue7dff022015-04-03 01:14:29 +02002777 if (!(s->flags & SF_FINST_MASK))
2778 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002779 return 0;
2780 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002781
Willy Tarreau59234e92008-11-30 23:51:27 +01002782 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002783 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002784 if (!(s->flags & SF_ERR_MASK))
2785 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002786
Willy Tarreaufcffa692010-01-10 14:21:19 +01002787 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002788 goto failed_keep_alive;
2789
Willy Tarreau0f228a02015-05-01 15:37:53 +02002790 if (sess->fe->options & PR_O_IGNORE_PRB)
2791 goto failed_keep_alive;
2792
Willy Tarreau59234e92008-11-30 23:51:27 +01002793 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002794 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002795 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002796 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002797 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002799 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002801 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002802 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002803
Willy Tarreau87b09662015-04-03 00:22:06 +02002804 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002805 proxy_inc_fe_req_ctr(sess->fe);
2806 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002807 if (sess->listener->counters)
2808 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002809
Willy Tarreaue7dff022015-04-03 01:14:29 +02002810 if (!(s->flags & SF_FINST_MASK))
2811 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 return 0;
2813 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002814
Willy Tarreau59234e92008-11-30 23:51:27 +01002815 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002816 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002817 if (!(s->flags & SF_ERR_MASK))
2818 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002819
Willy Tarreaufcffa692010-01-10 14:21:19 +01002820 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002821 goto failed_keep_alive;
2822
Willy Tarreau0f228a02015-05-01 15:37:53 +02002823 if (sess->fe->options & PR_O_IGNORE_PRB)
2824 goto failed_keep_alive;
2825
Willy Tarreau4076a152009-04-02 15:18:36 +02002826 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002827 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002829 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002831 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002832 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002833 stream_inc_http_err_ctr(s);
2834 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002835 proxy_inc_fe_req_ctr(sess->fe);
2836 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002837 if (sess->listener->counters)
2838 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002839
Willy Tarreaue7dff022015-04-03 01:14:29 +02002840 if (!(s->flags & SF_FINST_MASK))
2841 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002842 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002843 }
2844
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002845 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002846 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002847 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002848#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002849 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2850 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002851 /* We need more data, we have to re-enable quick-ack in case we
2852 * previously disabled it, otherwise we might cause the client
2853 * to delay next data.
2854 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002855 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002856 }
2857#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002858
Willy Tarreaufcffa692010-01-10 14:21:19 +01002859 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2860 /* If the client starts to talk, let's fall back to
2861 * request timeout processing.
2862 */
2863 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002864 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002865 }
2866
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002868 if (!tick_isset(req->analyse_exp)) {
2869 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2870 (txn->flags & TX_WAIT_NEXT_RQ) &&
2871 tick_isset(s->be->timeout.httpka))
2872 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2873 else
2874 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2875 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 /* we're not ready yet */
2878 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002879
2880 failed_keep_alive:
2881 /* Here we process low-level errors for keep-alive requests. In
2882 * short, if the request is not the first one and it experiences
2883 * a timeout, read error or shutdown, we just silently close so
2884 * that the client can try again.
2885 */
2886 txn->status = 0;
2887 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002888 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002889 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002890 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002891 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002892 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002893 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002894 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002895
Willy Tarreaud787e662009-07-07 10:14:51 +02002896 /* OK now we have a complete HTTP request with indexed headers. Let's
2897 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002898 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002899 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002900 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002901 * byte after the last LF. msg->sov points to the first byte of data.
2902 * msg->eol cannot be trusted because it may have been left uninitialized
2903 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002904 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002905
Willy Tarreau87b09662015-04-03 00:22:06 +02002906 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002907 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002908
Willy Tarreaub16a5742010-01-10 14:46:16 +01002909 if (txn->flags & TX_WAIT_NEXT_RQ) {
2910 /* kill the pending keep-alive timeout */
2911 txn->flags &= ~TX_WAIT_NEXT_RQ;
2912 req->analyse_exp = TICK_ETERNITY;
2913 }
2914
2915
Willy Tarreaud787e662009-07-07 10:14:51 +02002916 /* Maybe we found in invalid header name while we were configured not
2917 * to block on that, so we have to capture it now.
2918 */
2919 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002920 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002921
Willy Tarreau59234e92008-11-30 23:51:27 +01002922 /*
2923 * 1: identify the method
2924 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002925 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002926
2927 /* we can make use of server redirect on GET and HEAD */
2928 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002929 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002930
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 /*
2932 * 2: check if the URI matches the monitor_uri.
2933 * We have to do this for every request which gets in, because
2934 * the monitor-uri is defined by the frontend.
2935 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002936 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2937 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002938 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002939 sess->fe->monitor_uri,
2940 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002941 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002942 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002943 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002945
Willy Tarreaue7dff022015-04-03 01:14:29 +02002946 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002947 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002948
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002950 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002951 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002952
Willy Tarreau59234e92008-11-30 23:51:27 +01002953 ret = acl_pass(ret);
2954 if (cond->pol == ACL_COND_UNLESS)
2955 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002956
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 if (ret) {
2958 /* we fail this request, let's return 503 service unavail */
2959 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002960 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002961 if (!(s->flags & SF_ERR_MASK))
2962 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002963 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002964 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002965 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /* nothing to fail, let's reply normaly */
2968 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002969 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002970 if (!(s->flags & SF_ERR_MASK))
2971 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 goto return_prx_cond;
2973 }
2974
2975 /*
2976 * 3: Maybe we have to copy the original REQURI for the logs ?
2977 * Note: we cannot log anymore if the request has been
2978 * classified as invalid.
2979 */
2980 if (unlikely(s->logs.logwait & LW_REQ)) {
2981 /* we have a complete HTTP request that we must log */
2982 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2983 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002984
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 if (urilen >= REQURI_LEN)
2986 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002987 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002989
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002990 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002991 s->do_log(s);
2992 } else {
2993 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002994 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002995 }
Willy Tarreau06619262006-12-17 08:37:22 +01002996
Willy Tarreau91852eb2015-05-01 13:26:00 +02002997 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2998 * exactly one digit "." one digit. This check may be disabled using
2999 * option accept-invalid-http-request.
3000 */
3001 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3002 if (msg->sl.rq.v_l != 8) {
3003 msg->err_pos = msg->sl.rq.v;
3004 goto return_bad_req;
3005 }
3006
3007 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3008 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3009 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3010 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3011 msg->err_pos = msg->sl.rq.v + 4;
3012 goto return_bad_req;
3013 }
3014 }
Willy Tarreau13317662015-05-01 13:47:08 +02003015 else {
3016 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3017 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3018 goto return_bad_req;
3019 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003020
Willy Tarreau5b154472009-12-21 20:11:07 +01003021 /* ... and check if the request is HTTP/1.1 or above */
3022 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003023 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3024 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3025 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003026 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003027
3028 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003029 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 +01003030
Willy Tarreau88d349d2010-01-25 12:15:43 +01003031 /* if the frontend has "option http-use-proxy-header", we'll check if
3032 * we have what looks like a proxied connection instead of a connection,
3033 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3034 * Note that this is *not* RFC-compliant, however browsers and proxies
3035 * happen to do that despite being non-standard :-(
3036 * We consider that a request not beginning with either '/' or '*' is
3037 * a proxied connection, which covers both "scheme://location" and
3038 * CONNECT ip:port.
3039 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003040 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003041 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003042 txn->flags |= TX_USE_PX_CONN;
3043
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003044 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003045 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003046
Willy Tarreau59234e92008-11-30 23:51:27 +01003047 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003048 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003049 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003050 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003051
Willy Tarreau557f1992015-05-01 10:05:17 +02003052 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3053 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003054 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003055 * The length of a message body is determined by one of the following
3056 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003057 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003058 * 1. Any response to a HEAD request and any response with a 1xx
3059 * (Informational), 204 (No Content), or 304 (Not Modified) status
3060 * code is always terminated by the first empty line after the
3061 * header fields, regardless of the header fields present in the
3062 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003063 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003064 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3065 * the connection will become a tunnel immediately after the empty
3066 * line that concludes the header fields. A client MUST ignore any
3067 * Content-Length or Transfer-Encoding header fields received in
3068 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003069 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003070 * 3. If a Transfer-Encoding header field is present and the chunked
3071 * transfer coding (Section 4.1) is the final encoding, the message
3072 * body length is determined by reading and decoding the chunked
3073 * data until the transfer coding indicates the data is complete.
3074 *
3075 * If a Transfer-Encoding header field is present in a response and
3076 * the chunked transfer coding is not the final encoding, the
3077 * message body length is determined by reading the connection until
3078 * it is closed by the server. If a Transfer-Encoding header field
3079 * is present in a request and the chunked transfer coding is not
3080 * the final encoding, the message body length cannot be determined
3081 * reliably; the server MUST respond with the 400 (Bad Request)
3082 * status code and then close the connection.
3083 *
3084 * If a message is received with both a Transfer-Encoding and a
3085 * Content-Length header field, the Transfer-Encoding overrides the
3086 * Content-Length. Such a message might indicate an attempt to
3087 * perform request smuggling (Section 9.5) or response splitting
3088 * (Section 9.4) and ought to be handled as an error. A sender MUST
3089 * remove the received Content-Length field prior to forwarding such
3090 * a message downstream.
3091 *
3092 * 4. If a message is received without Transfer-Encoding and with
3093 * either multiple Content-Length header fields having differing
3094 * field-values or a single Content-Length header field having an
3095 * invalid value, then the message framing is invalid and the
3096 * recipient MUST treat it as an unrecoverable error. If this is a
3097 * request message, the server MUST respond with a 400 (Bad Request)
3098 * status code and then close the connection. If this is a response
3099 * message received by a proxy, the proxy MUST close the connection
3100 * to the server, discard the received response, and send a 502 (Bad
3101 * Gateway) response to the client. If this is a response message
3102 * received by a user agent, the user agent MUST close the
3103 * connection to the server and discard the received response.
3104 *
3105 * 5. If a valid Content-Length header field is present without
3106 * Transfer-Encoding, its decimal value defines the expected message
3107 * body length in octets. If the sender closes the connection or
3108 * the recipient times out before the indicated number of octets are
3109 * received, the recipient MUST consider the message to be
3110 * incomplete and close the connection.
3111 *
3112 * 6. If this is a request message and none of the above are true, then
3113 * the message body length is zero (no message body is present).
3114 *
3115 * 7. Otherwise, this is a response message without a declared message
3116 * body length, so the message body length is determined by the
3117 * number of octets received prior to the server closing the
3118 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003119 */
3120
Willy Tarreau32b47f42009-10-18 20:55:02 +02003121 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003122 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003123 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003124 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003125 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3126 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003127 /* chunked not last, return badreq */
3128 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003129 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003130 }
3131
Willy Tarreau1c913912015-04-30 10:57:51 +02003132 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003133 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003134 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3135 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3136 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3137 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003138 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003139 signed long long cl;
3140
Willy Tarreauad14f752011-09-02 20:33:27 +02003141 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003142 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003143 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003144 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003145
Willy Tarreauad14f752011-09-02 20:33:27 +02003146 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003147 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003148 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003149 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003150
Willy Tarreauad14f752011-09-02 20:33:27 +02003151 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003152 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003153 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003154 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003155
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003156 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003157 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003158 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003159 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003160
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003161 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003162 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003163 }
3164
Willy Tarreau34dfc602015-05-01 10:09:49 +02003165 /* even bodyless requests have a known length */
3166 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003167
Willy Tarreau179085c2014-04-28 16:48:56 +02003168 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3169 * only change if both the request and the config reference something else.
3170 * Option httpclose by itself sets tunnel mode where headers are mangled.
3171 * However, if another mode is set, it will affect it (eg: server-close/
3172 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3173 * if FE and BE have the same settings (common). The method consists in
3174 * checking if options changed between the two calls (implying that either
3175 * one is non-null, or one of them is non-null and we are there for the first
3176 * time.
3177 */
3178 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003179 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003180 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003181
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003182 /* we may have to wait for the request's body */
3183 if ((s->be->options & PR_O_WREQ_BODY) &&
3184 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3185 req->analysers |= AN_REQ_HTTP_BODY;
3186
Willy Tarreaud787e662009-07-07 10:14:51 +02003187 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003188 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003189 req->analyse_exp = TICK_ETERNITY;
3190 return 1;
3191
3192 return_bad_req:
3193 /* We centralize bad requests processing here */
3194 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3195 /* we detected a parsing error. We want to archive this request
3196 * in the dedicated proxy area for later troubleshooting.
3197 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003198 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003199 }
3200
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003201 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003202 txn->req.msg_state = HTTP_MSG_ERROR;
3203 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003204 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003205
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003206 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003207 if (sess->listener->counters)
3208 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003209
3210 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003211 if (!(s->flags & SF_ERR_MASK))
3212 s->flags |= SF_ERR_PRXCOND;
3213 if (!(s->flags & SF_FINST_MASK))
3214 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003215
Christopher Faulet0184ea72017-01-05 14:06:34 +01003216 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003217 req->analyse_exp = TICK_ETERNITY;
3218 return 0;
3219}
3220
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003221
Willy Tarreau347a35d2013-11-22 17:51:09 +01003222/* This function prepares an applet to handle the stats. It can deal with the
3223 * "100-continue" expectation, check that admin rules are met for POST requests,
3224 * and program a response message if something was unexpected. It cannot fail
3225 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003226 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003227 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003228 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003229 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003230int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003231{
3232 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003233 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003234 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003235 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003236 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003237 struct uri_auth *uri_auth = s->be->uri_auth;
3238 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003239 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003240
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003241 appctx = si_appctx(si);
3242 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3243 appctx->st1 = appctx->st2 = 0;
3244 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3245 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003246 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003247 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003248
3249 uri = msg->chn->buf->p + msg->sl.rq.u;
3250 lookup = uri + uri_auth->uri_len;
3251
3252 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3253 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003254 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003255 break;
3256 }
3257 }
3258
3259 if (uri_auth->refresh) {
3260 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3261 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003262 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003263 break;
3264 }
3265 }
3266 }
3267
3268 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3269 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003270 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003271 break;
3272 }
3273 }
3274
Willy Tarreau1e62df92016-01-11 18:57:53 +01003275 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3276 if (memcmp(h, ";typed", 6) == 0) {
3277 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3278 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3279 break;
3280 }
3281 }
3282
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003283 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3284 if (memcmp(h, ";st=", 4) == 0) {
3285 int i;
3286 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003287 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003288 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3289 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003290 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003291 break;
3292 }
3293 }
3294 break;
3295 }
3296 }
3297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 appctx->ctx.stats.scope_str = 0;
3299 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003300 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3301 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3302 int itx = 0;
3303 const char *h2;
3304 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3305 const char *err;
3306
3307 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3308 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003309 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003310 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3311 itx++;
3312 h++;
3313 }
3314
3315 if (itx > STAT_SCOPE_TXT_MAXLEN)
3316 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003317 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003318
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003319 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003320 memcpy(scope_txt, h2, itx);
3321 scope_txt[itx] = '\0';
3322 err = invalid_char(scope_txt);
3323 if (err) {
3324 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003325 appctx->ctx.stats.scope_str = 0;
3326 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003327 }
3328 break;
3329 }
3330 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003331
3332 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003333 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003334 int ret = 1;
3335
3336 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003337 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 +01003338 ret = acl_pass(ret);
3339 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3340 ret = !ret;
3341 }
3342
3343 if (ret) {
3344 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003346 break;
3347 }
3348 }
3349
3350 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003351 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003352 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003353 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003354 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3355 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003356 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003357 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003358 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003359 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3360 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003361 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003362 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003363 else {
3364 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003365 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003366 }
3367
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003368 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003369 return 1;
3370}
3371
Lukas Tribus67db8df2013-06-23 17:37:13 +02003372/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3373 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3374 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003375void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003376{
3377#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003378 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003379 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3380#endif
3381#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003382 if (from->ss_family == AF_INET6) {
3383 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003384 /* v4-mapped addresses need IP_TOS */
3385 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3386 else
3387 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3388 }
3389#endif
3390}
3391
Willy Tarreau87b09662015-04-03 00:22:06 +02003392int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003393 const char* name, unsigned int name_len,
3394 const char *str, struct my_regex *re,
3395 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003396{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003397 struct hdr_ctx ctx;
3398 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003399 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003400 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3401 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003402 struct chunk *output = get_trash_chunk();
3403
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003404 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003405
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003406 /* Choose the header browsing function. */
3407 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003408 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003409 http_find_hdr_func = http_find_header2;
3410 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003411 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003412 http_find_hdr_func = http_find_full_header2;
3413 break;
3414 default: /* impossible */
3415 return -1;
3416 }
3417
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003418 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3419 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003420 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003421 char *val = ctx.line + ctx.val;
3422 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003423
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003424 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3425 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003426
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003427 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003428 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003429 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003430
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003431 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003432
3433 hdr->len += delta;
3434 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003435
3436 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003437 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003438 }
3439
3440 return 0;
3441}
3442
Willy Tarreau87b09662015-04-03 00:22:06 +02003443static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003444 const char* name, unsigned int name_len,
3445 struct list *fmt, struct my_regex *re,
3446 int action)
3447{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003448 struct chunk *replace;
3449 int ret = -1;
3450
3451 replace = alloc_trash_chunk();
3452 if (!replace)
3453 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003454
3455 replace->len = build_logline(s, replace->str, replace->size, fmt);
3456 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003457 goto leave;
3458
3459 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003460
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003461 leave:
3462 free_trash_chunk(replace);
3463 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003464}
3465
Willy Tarreau87b09662015-04-03 00:22:06 +02003466/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003467 * transaction <txn>. Returns the verdict of the first rule that prevents
3468 * further processing of the request (auth, deny, ...), and defaults to
3469 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3470 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003471 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3472 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3473 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003474 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003475enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003476http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003477{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003478 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003479 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003480 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003481 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003482 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003483 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003484 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003485 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003486
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003487 /* If "the current_rule_list" match the executed rule list, we are in
3488 * resume condition. If a resume is needed it is always in the action
3489 * and never in the ACL or converters. In this case, we initialise the
3490 * current rule, and go to the action execution point.
3491 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003492 if (s->current_rule) {
3493 rule = s->current_rule;
3494 s->current_rule = NULL;
3495 if (s->current_rule_list == rules)
3496 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003497 }
3498 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003499
Willy Tarreauff011f22011-01-06 17:51:27 +01003500 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003501
Willy Tarreau96257ec2012-12-27 10:46:37 +01003502 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003503 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003504 int ret;
3505
Willy Tarreau192252e2015-04-04 01:47:55 +02003506 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003507 ret = acl_pass(ret);
3508
Willy Tarreauff011f22011-01-06 17:51:27 +01003509 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003510 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003511
3512 if (!ret) /* condition not matched */
3513 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003514 }
3515
Willy Tarreauacc98002015-09-27 23:34:39 +02003516 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003517resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003518 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003519 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003520 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003522 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003523 if (deny_status)
3524 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003525 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003527 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003528 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003529 if (deny_status)
3530 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003531 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003533 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003534 /* Auth might be performed on regular http-req rules as well as on stats */
3535 auth_realm = rule->arg.auth.realm;
3536 if (!auth_realm) {
3537 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3538 auth_realm = STATS_DEFAULT_REALM;
3539 else
3540 auth_realm = px->id;
3541 }
3542 /* send 401/407 depending on whether we use a proxy or not. We still
3543 * count one error, because normal browsing won't significantly
3544 * increase the counter but brute force attempts will.
3545 */
3546 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3547 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003548 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003549 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003550 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003551
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003552 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003553 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3554 return HTTP_RULE_RES_BADREQ;
3555 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003556
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003557 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003558 s->task->nice = rule->arg.nice;
3559 break;
3560
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003561 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003562 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003563 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003564 break;
3565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003566 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003567#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003568 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003569 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003570#endif
3571 break;
3572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003573 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003574 s->logs.level = rule->arg.loglevel;
3575 break;
3576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003577 case ACT_HTTP_REPLACE_HDR:
3578 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003579 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3580 rule->arg.hdr_add.name_len,
3581 &rule->arg.hdr_add.fmt,
3582 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003583 return HTTP_RULE_RES_BADREQ;
3584 break;
3585
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003586 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003587 ctx.idx = 0;
3588 /* remove all occurrences of the header */
3589 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3590 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3591 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003592 }
Willy Tarreau85603282015-01-21 20:39:27 +01003593 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003595 case ACT_HTTP_SET_HDR:
3596 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003597 /* The scope of the trash buffer must be limited to this function. The
3598 * build_logline() function can execute a lot of other function which
3599 * can use the trash buffer. So for limiting the scope of this global
3600 * buffer, we build first the header value using build_logline, and
3601 * after we store the header name.
3602 */
3603 len = rule->arg.hdr_add.name_len + 2,
3604 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003605 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003606 trash.str[rule->arg.hdr_add.name_len] = ':';
3607 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3608 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003610 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003611 /* remove all occurrences of the header */
3612 ctx.idx = 0;
3613 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3614 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3615 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3616 }
3617 }
3618
Willy Tarreau96257ec2012-12-27 10:46:37 +01003619 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3620 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003621
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003622 case ACT_HTTP_DEL_ACL:
3623 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003624 struct pat_ref *ref;
3625 char *key;
3626 int len;
3627
3628 /* collect reference */
3629 ref = pat_ref_lookup(rule->arg.map.ref);
3630 if (!ref)
3631 continue;
3632
3633 /* collect key */
3634 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3635 key = trash.str;
3636 key[len] = '\0';
3637
3638 /* perform update */
3639 /* returned code: 1=ok, 0=ko */
3640 pat_ref_delete(ref, key);
3641
3642 break;
3643 }
3644
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003645 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003646 struct pat_ref *ref;
3647 char *key;
3648 struct chunk *trash_key;
3649 int len;
3650
3651 trash_key = get_trash_chunk();
3652
3653 /* collect reference */
3654 ref = pat_ref_lookup(rule->arg.map.ref);
3655 if (!ref)
3656 continue;
3657
3658 /* collect key */
3659 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3660 key = trash_key->str;
3661 key[len] = '\0';
3662
3663 /* perform update */
3664 /* add entry only if it does not already exist */
3665 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003666 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003667
3668 break;
3669 }
3670
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003671 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003672 struct pat_ref *ref;
3673 char *key, *value;
3674 struct chunk *trash_key, *trash_value;
3675 int len;
3676
3677 trash_key = get_trash_chunk();
3678 trash_value = get_trash_chunk();
3679
3680 /* collect reference */
3681 ref = pat_ref_lookup(rule->arg.map.ref);
3682 if (!ref)
3683 continue;
3684
3685 /* collect key */
3686 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3687 key = trash_key->str;
3688 key[len] = '\0';
3689
3690 /* collect value */
3691 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3692 value = trash_value->str;
3693 value[len] = '\0';
3694
3695 /* perform update */
3696 if (pat_ref_find_elt(ref, key) != NULL)
3697 /* update entry if it exists */
3698 pat_ref_set(ref, key, value, NULL);
3699 else
3700 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003701 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003702
3703 break;
3704 }
William Lallemand73025dd2014-04-24 14:38:37 +02003705
Thierry FOURNIER42148732015-09-02 17:17:33 +02003706 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003707 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3708 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003709
Willy Tarreauacc98002015-09-27 23:34:39 +02003710 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003711 case ACT_RET_ERR:
3712 case ACT_RET_CONT:
3713 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003714 case ACT_RET_STOP:
3715 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003716 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003717 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003718 return HTTP_RULE_RES_YIELD;
3719 }
William Lallemand73025dd2014-04-24 14:38:37 +02003720 break;
3721
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003722 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003723 /* Note: only the first valid tracking parameter of each
3724 * applies.
3725 */
3726
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003727 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003728 struct stktable *t;
3729 struct stksess *ts;
3730 struct stktable_key *key;
3731 void *ptr;
3732
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003733 t = rule->arg.trk_ctr.table.t;
3734 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 +02003735
3736 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003737 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003738
3739 /* let's count a new HTTP request as it's the first time we do it */
3740 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3741 if (ptr)
3742 stktable_data_cast(ptr, http_req_cnt)++;
3743
3744 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3745 if (ptr)
3746 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3747 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3748
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003749 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003750 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003751 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003752 }
3753 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003754 break;
3755
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003756 /* other flags exists, but normaly, they never be matched. */
3757 default:
3758 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003759 }
3760 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003761
3762 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003763 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003764}
3765
Willy Tarreau71241ab2012-12-27 11:30:54 +01003766
Willy Tarreau51d861a2015-05-22 17:30:48 +02003767/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3768 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3769 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3770 * is returned, the process can continue the evaluation of next rule list. If
3771 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3772 * is returned, it means the operation could not be processed and a server error
3773 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3774 * deny rule. If *YIELD is returned, the caller must call again the function
3775 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003776 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003777static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003778http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003779{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003780 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003781 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003782 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003783 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003784 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003785 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003786
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003787 /* If "the current_rule_list" match the executed rule list, we are in
3788 * resume condition. If a resume is needed it is always in the action
3789 * and never in the ACL or converters. In this case, we initialise the
3790 * current rule, and go to the action execution point.
3791 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003792 if (s->current_rule) {
3793 rule = s->current_rule;
3794 s->current_rule = NULL;
3795 if (s->current_rule_list == rules)
3796 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003797 }
3798 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003799
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003800 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801
3802 /* check optional condition */
3803 if (rule->cond) {
3804 int ret;
3805
Willy Tarreau192252e2015-04-04 01:47:55 +02003806 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003807 ret = acl_pass(ret);
3808
3809 if (rule->cond->pol == ACL_COND_UNLESS)
3810 ret = !ret;
3811
3812 if (!ret) /* condition not matched */
3813 continue;
3814 }
3815
Willy Tarreauacc98002015-09-27 23:34:39 +02003816 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003817resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003818 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003819 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003820 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003821
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003822 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003823 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003824 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003825
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003826 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003827 s->task->nice = rule->arg.nice;
3828 break;
3829
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003830 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003831 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003832 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003833 break;
3834
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003835 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003836#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003837 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003838 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003839#endif
3840 break;
3841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003842 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003843 s->logs.level = rule->arg.loglevel;
3844 break;
3845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_HTTP_REPLACE_HDR:
3847 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003848 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3849 rule->arg.hdr_add.name_len,
3850 &rule->arg.hdr_add.fmt,
3851 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003852 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003853 break;
3854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003855 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003856 ctx.idx = 0;
3857 /* remove all occurrences of the header */
3858 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3859 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3860 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3861 }
Willy Tarreau85603282015-01-21 20:39:27 +01003862 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003863
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003864 case ACT_HTTP_SET_HDR:
3865 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003866 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3867 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3868 trash.len = rule->arg.hdr_add.name_len;
3869 trash.str[trash.len++] = ':';
3870 trash.str[trash.len++] = ' ';
3871 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 +01003872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003873 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003874 /* remove all occurrences of the header */
3875 ctx.idx = 0;
3876 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3877 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3878 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3879 }
3880 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003881 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3882 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003883
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003884 case ACT_HTTP_DEL_ACL:
3885 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003886 struct pat_ref *ref;
3887 char *key;
3888 int len;
3889
3890 /* collect reference */
3891 ref = pat_ref_lookup(rule->arg.map.ref);
3892 if (!ref)
3893 continue;
3894
3895 /* collect key */
3896 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3897 key = trash.str;
3898 key[len] = '\0';
3899
3900 /* perform update */
3901 /* returned code: 1=ok, 0=ko */
3902 pat_ref_delete(ref, key);
3903
3904 break;
3905 }
3906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003907 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003908 struct pat_ref *ref;
3909 char *key;
3910 struct chunk *trash_key;
3911 int len;
3912
3913 trash_key = get_trash_chunk();
3914
3915 /* collect reference */
3916 ref = pat_ref_lookup(rule->arg.map.ref);
3917 if (!ref)
3918 continue;
3919
3920 /* collect key */
3921 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3922 key = trash_key->str;
3923 key[len] = '\0';
3924
3925 /* perform update */
3926 /* check if the entry already exists */
3927 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003928 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003929
3930 break;
3931 }
3932
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003933 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003934 struct pat_ref *ref;
3935 char *key, *value;
3936 struct chunk *trash_key, *trash_value;
3937 int len;
3938
3939 trash_key = get_trash_chunk();
3940 trash_value = get_trash_chunk();
3941
3942 /* collect reference */
3943 ref = pat_ref_lookup(rule->arg.map.ref);
3944 if (!ref)
3945 continue;
3946
3947 /* collect key */
3948 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3949 key = trash_key->str;
3950 key[len] = '\0';
3951
3952 /* collect value */
3953 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3954 value = trash_value->str;
3955 value[len] = '\0';
3956
3957 /* perform update */
3958 if (pat_ref_find_elt(ref, key) != NULL)
3959 /* update entry if it exists */
3960 pat_ref_set(ref, key, value, NULL);
3961 else
3962 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003963 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003964
3965 break;
3966 }
William Lallemand73025dd2014-04-24 14:38:37 +02003967
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003968 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003969 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3970 return HTTP_RULE_RES_BADREQ;
3971 return HTTP_RULE_RES_DONE;
3972
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003973 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3974 /* Note: only the first valid tracking parameter of each
3975 * applies.
3976 */
3977
3978 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3979 struct stktable *t;
3980 struct stksess *ts;
3981 struct stktable_key *key;
3982 void *ptr;
3983
3984 t = rule->arg.trk_ctr.table.t;
3985 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3986
3987 if (key && (ts = stktable_get_entry(t, key))) {
3988 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3989
3990 /* let's count a new HTTP request as it's the first time we do it */
3991 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3992 if (ptr)
3993 stktable_data_cast(ptr, http_req_cnt)++;
3994
3995 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3996 if (ptr)
3997 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3998 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3999
4000 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
4001 if (sess->fe != s->be)
4002 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
4003
4004 /* When the client triggers a 4xx from the server, it's most often due
4005 * to a missing object or permission. These events should be tracked
4006 * because if they happen often, it may indicate a brute force or a
4007 * vulnerability scan. Normally this is done when receiving the response
4008 * but here we're tracking after this ought to have been done so we have
4009 * to do it on purpose.
4010 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02004011 if ((unsigned)(txn->status - 400) < 100) {
4012 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
4013 if (ptr)
4014 stktable_data_cast(ptr, http_err_cnt)++;
4015
4016 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
4017 if (ptr)
4018 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
4019 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
4020 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08004021 }
4022 }
4023 break;
4024
Thierry FOURNIER42148732015-09-02 17:17:33 +02004025 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004026 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4027 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004028
Willy Tarreauacc98002015-09-27 23:34:39 +02004029 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004030 case ACT_RET_ERR:
4031 case ACT_RET_CONT:
4032 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004033 case ACT_RET_STOP:
4034 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004035 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004036 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004037 return HTTP_RULE_RES_YIELD;
4038 }
William Lallemand73025dd2014-04-24 14:38:37 +02004039 break;
4040
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004041 /* other flags exists, but normaly, they never be matched. */
4042 default:
4043 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004044 }
4045 }
4046
4047 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004048 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004049}
4050
4051
Willy Tarreau71241ab2012-12-27 11:30:54 +01004052/* Perform an HTTP redirect based on the information in <rule>. The function
4053 * returns non-zero on success, or zero in case of a, irrecoverable error such
4054 * as too large a request to build a valid response.
4055 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004056static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004057{
Willy Tarreaub329a312015-05-22 16:27:37 +02004058 struct http_msg *req = &txn->req;
4059 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004060 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004061 struct chunk *chunk;
4062 int ret = 0;
4063
4064 chunk = alloc_trash_chunk();
4065 if (!chunk)
4066 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004067
4068 /* build redirect message */
4069 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004070 case 308:
4071 msg_fmt = HTTP_308;
4072 break;
4073 case 307:
4074 msg_fmt = HTTP_307;
4075 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004076 case 303:
4077 msg_fmt = HTTP_303;
4078 break;
4079 case 301:
4080 msg_fmt = HTTP_301;
4081 break;
4082 case 302:
4083 default:
4084 msg_fmt = HTTP_302;
4085 break;
4086 }
4087
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004088 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4089 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004090
4091 switch(rule->type) {
4092 case REDIRECT_TYPE_SCHEME: {
4093 const char *path;
4094 const char *host;
4095 struct hdr_ctx ctx;
4096 int pathlen;
4097 int hostlen;
4098
4099 host = "";
4100 hostlen = 0;
4101 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004102 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004103 host = ctx.line + ctx.val;
4104 hostlen = ctx.vlen;
4105 }
4106
4107 path = http_get_path(txn);
4108 /* build message using path */
4109 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004110 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4112 int qs = 0;
4113 while (qs < pathlen) {
4114 if (path[qs] == '?') {
4115 pathlen = qs;
4116 break;
4117 }
4118 qs++;
4119 }
4120 }
4121 } else {
4122 path = "/";
4123 pathlen = 1;
4124 }
4125
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004126 if (rule->rdr_str) { /* this is an old "redirect" rule */
4127 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004128 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4129 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004130
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004131 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004132 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4133 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004134 }
4135 else {
4136 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004137 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004138
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004139 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004140 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4141 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004142 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004143 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004144 memcpy(chunk->str + chunk->len, "://", 3);
4145 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004146
4147 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004148 memcpy(chunk->str + chunk->len, host, hostlen);
4149 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004150
4151 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004152 memcpy(chunk->str + chunk->len, path, pathlen);
4153 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004154
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004155 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004156 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004157 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004158 if (chunk->len > chunk->size - 5)
4159 goto leave;
4160 chunk->str[chunk->len] = '/';
4161 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004162 }
4163
4164 break;
4165 }
4166 case REDIRECT_TYPE_PREFIX: {
4167 const char *path;
4168 int pathlen;
4169
4170 path = http_get_path(txn);
4171 /* build message using path */
4172 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004173 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004174 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4175 int qs = 0;
4176 while (qs < pathlen) {
4177 if (path[qs] == '?') {
4178 pathlen = qs;
4179 break;
4180 }
4181 qs++;
4182 }
4183 }
4184 } else {
4185 path = "/";
4186 pathlen = 1;
4187 }
4188
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004189 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004190 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4191 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004192
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004193 /* add prefix. Note that if prefix == "/", we don't want to
4194 * add anything, otherwise it makes it hard for the user to
4195 * configure a self-redirection.
4196 */
4197 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004198 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4199 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004200 }
4201 }
4202 else {
4203 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004204 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004205
4206 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004207 if (chunk->len + pathlen > chunk->size - 4)
4208 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004209 }
4210
4211 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004212 memcpy(chunk->str + chunk->len, path, pathlen);
4213 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004214
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004215 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004216 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004217 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004218 if (chunk->len > chunk->size - 5)
4219 goto leave;
4220 chunk->str[chunk->len] = '/';
4221 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004222 }
4223
4224 break;
4225 }
4226 case REDIRECT_TYPE_LOCATION:
4227 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004228 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004229 if (chunk->len + rule->rdr_len > chunk->size - 4)
4230 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004231
4232 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004233 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4234 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004235 }
4236 else {
4237 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004238 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004239
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004240 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004241 if (chunk->len > chunk->size - 4)
4242 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004243 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004244 break;
4245 }
4246
4247 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004248 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4249 chunk->len += 14;
4250 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4251 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004252 }
4253
Willy Tarreau19b14122017-02-28 09:48:11 +01004254 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004255 txn->status = rule->code;
4256 /* let's log the request time */
4257 s->logs.tv_request = now;
4258
Willy Tarreau19b14122017-02-28 09:48:11 +01004259 if ((req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004260 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004261 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4262 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4263 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004264 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004265 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004266 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4267 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004268 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004269 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4270 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004271 }
4272 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004273 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4274 chunk->len += 4;
4275 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4276 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004277 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004278 bi_fast_delete(req->chn->buf, req->sov);
4279 req->next -= req->sov;
4280 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004281 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01004282 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004283 req->msg_state = HTTP_MSG_CLOSED;
4284 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004285 /* Trim any possible response */
4286 res->chn->buf->i = 0;
4287 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004288 } else {
4289 /* keep-alive not possible */
4290 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004291 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4292 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004293 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004294 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4295 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004296 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004297 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004298 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004299 }
4300
Willy Tarreaue7dff022015-04-03 01:14:29 +02004301 if (!(s->flags & SF_ERR_MASK))
4302 s->flags |= SF_ERR_LOCAL;
4303 if (!(s->flags & SF_FINST_MASK))
4304 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004305
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004306 ret = 1;
4307 leave:
4308 free_trash_chunk(chunk);
4309 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004310}
4311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004312/* This stream analyser runs all HTTP request processing which is common to
4313 * frontends and backends, which means blocking ACLs, filters, connection-close,
4314 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004315 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004316 * either needs more data or wants to immediately abort the request (eg: deny,
4317 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004318 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004319int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004320{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004321 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004322 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004323 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004324 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004325 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004326 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004327 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004328
Willy Tarreau655dce92009-11-08 13:10:58 +01004329 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004330 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004331 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004332 }
4333
Willy Tarreau87b09662015-04-03 00:22:06 +02004334 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 +02004335 now_ms, __FUNCTION__,
4336 s,
4337 req,
4338 req->rex, req->wex,
4339 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004340 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004341 req->analysers);
4342
Willy Tarreau65410832014-04-28 21:25:43 +02004343 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004344 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004345
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004346 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004347 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004348 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004349
Willy Tarreau0b748332014-04-29 00:13:29 +02004350 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004351 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4352 goto return_prx_yield;
4353
Willy Tarreau0b748332014-04-29 00:13:29 +02004354 case HTTP_RULE_RES_CONT:
4355 case HTTP_RULE_RES_STOP: /* nothing to do */
4356 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004357
Willy Tarreau0b748332014-04-29 00:13:29 +02004358 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4359 if (txn->flags & TX_CLTARPIT)
4360 goto tarpit;
4361 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004362
Willy Tarreau0b748332014-04-29 00:13:29 +02004363 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4364 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004365
Willy Tarreau0b748332014-04-29 00:13:29 +02004366 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004367 goto done;
4368
Willy Tarreau0b748332014-04-29 00:13:29 +02004369 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4370 goto return_bad_req;
4371 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004372 }
4373
Willy Tarreau52542592014-04-28 18:33:26 +02004374 /* OK at this stage, we know that the request was accepted according to
4375 * the http-request rules, we can check for the stats. Note that the
4376 * URI is detected *before* the req* rules in order not to be affected
4377 * by a possible reqrep, while they are processed *after* so that a
4378 * reqdeny can still block them. This clearly needs to change in 1.6!
4379 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004380 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004381 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004382 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004383 txn->status = 500;
4384 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004385 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004386
Willy Tarreaue7dff022015-04-03 01:14:29 +02004387 if (!(s->flags & SF_ERR_MASK))
4388 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004389 goto return_prx_cond;
4390 }
4391
4392 /* parse the whole stats request and extract the relevant information */
4393 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004394 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004395 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004396
Willy Tarreau0b748332014-04-29 00:13:29 +02004397 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4398 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004399
Willy Tarreau0b748332014-04-29 00:13:29 +02004400 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4401 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004402 }
4403
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004404 /* evaluate the req* rules except reqadd */
4405 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004406 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004407 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004408
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004409 if (txn->flags & TX_CLDENY)
4410 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004411
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004412 if (txn->flags & TX_CLTARPIT) {
4413 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004414 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004415 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004416 }
Willy Tarreau06619262006-12-17 08:37:22 +01004417
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004418 /* add request headers from the rule sets in the same order */
4419 list_for_each_entry(wl, &px->req_add, list) {
4420 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004421 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004422 ret = acl_pass(ret);
4423 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4424 ret = !ret;
4425 if (!ret)
4426 continue;
4427 }
4428
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004429 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004430 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004431 }
4432
Willy Tarreau52542592014-04-28 18:33:26 +02004433
4434 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004435 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004436 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004437 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4438 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004439
Willy Tarreaue7dff022015-04-03 01:14:29 +02004440 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4441 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4442 if (!(s->flags & SF_FINST_MASK))
4443 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004444
Willy Tarreau70730dd2014-04-24 18:06:27 +02004445 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004446 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4447 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004448 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004449 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004450 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004451
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004452 /* check whether we have some ACLs set to redirect this request */
4453 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004454 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004455 int ret;
4456
Willy Tarreau192252e2015-04-04 01:47:55 +02004457 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004458 ret = acl_pass(ret);
4459 if (rule->cond->pol == ACL_COND_UNLESS)
4460 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004461 if (!ret)
4462 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004463 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004464 if (!http_apply_redirect_rule(rule, s, txn))
4465 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004466 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004467 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004468
Willy Tarreau2be39392010-01-03 17:24:51 +01004469 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4470 * If this happens, then the data will not come immediately, so we must
4471 * send all what we have without waiting. Note that due to the small gain
4472 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004473 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004474 * itself once used.
4475 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004476 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004477
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004478 done: /* done with this analyser, continue with next ones that the calling
4479 * points will have set, if any.
4480 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004481 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004482 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4483 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004484 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004485
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004486 tarpit:
4487 /* When a connection is tarpitted, we use the tarpit timeout,
4488 * which may be the same as the connect timeout if unspecified.
4489 * If unset, then set it to zero because we really want it to
4490 * eventually expire. We build the tarpit as an analyser.
4491 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004492 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004493
4494 /* wipe the request out so that we can drop the connection early
4495 * if the client closes first.
4496 */
4497 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004498
4499 /* Allow cookie logging
4500 */
4501 if (s->be->cookie_name || sess->fe->capture_name)
4502 manage_client_side_cookies(s, req);
4503
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004504 txn->status = http_err_codes[deny_status];
4505
Christopher Faulet0184ea72017-01-05 14:06:34 +01004506 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004507 req->analysers |= AN_REQ_HTTP_TARPIT;
4508 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4509 if (!req->analyse_exp)
4510 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004511 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004512 sess->fe->fe_counters.denied_req++;
4513 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004514 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004515 if (sess->listener->counters)
4516 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004517 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004518
4519 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004520
4521 /* Allow cookie logging
4522 */
4523 if (s->be->cookie_name || sess->fe->capture_name)
4524 manage_client_side_cookies(s, req);
4525
Willy Tarreau0b748332014-04-29 00:13:29 +02004526 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004527 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004528 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004529 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02004530 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004531 sess->fe->fe_counters.denied_req++;
4532 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004533 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004534 if (sess->listener->counters)
4535 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004536 goto return_prx_cond;
4537
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004538 return_bad_req:
4539 /* We centralize bad requests processing here */
4540 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4541 /* we detected a parsing error. We want to archive this request
4542 * in the dedicated proxy area for later troubleshooting.
4543 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004544 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004546
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004547 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004548 txn->req.msg_state = HTTP_MSG_ERROR;
4549 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004550 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004551
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004552 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004553 if (sess->listener->counters)
4554 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004555
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004556 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004557 if (!(s->flags & SF_ERR_MASK))
4558 s->flags |= SF_ERR_PRXCOND;
4559 if (!(s->flags & SF_FINST_MASK))
4560 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004561
Christopher Faulet0184ea72017-01-05 14:06:34 +01004562 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004563 req->analyse_exp = TICK_ETERNITY;
4564 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004565
4566 return_prx_yield:
4567 channel_dont_connect(req);
4568 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004569}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004570
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004571/* This function performs all the processing enabled for the current request.
4572 * It returns 1 if the processing can continue on next analysers, or zero if it
4573 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004574 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004575 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004576int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004577{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004578 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004579 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004580 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004581 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004582
Willy Tarreau655dce92009-11-08 13:10:58 +01004583 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004584 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004585 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004586 return 0;
4587 }
4588
Willy Tarreau87b09662015-04-03 00:22:06 +02004589 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 +02004590 now_ms, __FUNCTION__,
4591 s,
4592 req,
4593 req->rex, req->wex,
4594 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004595 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004596 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004597
Willy Tarreau59234e92008-11-30 23:51:27 +01004598 /*
4599 * Right now, we know that we have processed the entire headers
4600 * and that unwanted requests have been filtered out. We can do
4601 * whatever we want with the remaining request. Also, now we
4602 * may have separate values for ->fe, ->be.
4603 */
Willy Tarreau06619262006-12-17 08:37:22 +01004604
Willy Tarreau59234e92008-11-30 23:51:27 +01004605 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004606 * If HTTP PROXY is set we simply get remote server address parsing
4607 * incoming request. Note that this requires that a connection is
4608 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004609 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004610 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004611 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004612 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004613
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004614 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004615 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004616 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004617 txn->req.msg_state = HTTP_MSG_ERROR;
4618 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004619 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004620 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004621
Willy Tarreaue7dff022015-04-03 01:14:29 +02004622 if (!(s->flags & SF_ERR_MASK))
4623 s->flags |= SF_ERR_RESOURCE;
4624 if (!(s->flags & SF_FINST_MASK))
4625 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004626
4627 return 0;
4628 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004629
4630 path = http_get_path(txn);
4631 url2sa(req->buf->p + msg->sl.rq.u,
4632 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004633 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004634 /* if the path was found, we have to remove everything between
4635 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4636 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4637 * u_l characters by a single "/".
4638 */
4639 if (path) {
4640 char *cur_ptr = req->buf->p;
4641 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4642 int delta;
4643
4644 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4645 http_msg_move_end(&txn->req, delta);
4646 cur_end += delta;
4647 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4648 goto return_bad_req;
4649 }
4650 else {
4651 char *cur_ptr = req->buf->p;
4652 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4653 int delta;
4654
4655 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4656 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4657 http_msg_move_end(&txn->req, delta);
4658 cur_end += delta;
4659 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4660 goto return_bad_req;
4661 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004662 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004663
Willy Tarreau59234e92008-11-30 23:51:27 +01004664 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004665 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004666 * Note that doing so might move headers in the request, but
4667 * the fields will stay coherent and the URI will not move.
4668 * This should only be performed in the backend.
4669 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004670 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004671 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004672
William Lallemanda73203e2012-03-12 12:48:57 +01004673 /* add unique-id if "header-unique-id" is specified */
4674
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004675 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004676 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4677 goto return_bad_req;
4678 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004680 }
William Lallemanda73203e2012-03-12 12:48:57 +01004681
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004682 if (sess->fe->header_unique_id && s->unique_id) {
4683 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004685 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004686 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004687 goto return_bad_req;
4688 }
4689
Cyril Bontéb21570a2009-11-29 20:04:48 +01004690 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004691 * 9: add X-Forwarded-For if either the frontend or the backend
4692 * asks for it.
4693 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004694 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004695 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004696 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4697 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4698 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004699 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004700 /* The header is set to be added only if none is present
4701 * and we found it, so don't do anything.
4702 */
4703 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004704 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004705 /* Add an X-Forwarded-For header unless the source IP is
4706 * in the 'except' network range.
4707 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004708 if ((!sess->fe->except_mask.s_addr ||
4709 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4710 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004712 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004714 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004716 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004717
4718 /* Note: we rely on the backend to get the header name to be used for
4719 * x-forwarded-for, because the header is really meant for the backends.
4720 * However, if the backend did not specify any option, we have to rely
4721 * on the frontend's header name.
4722 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004723 if (s->be->fwdfor_hdr_len) {
4724 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004726 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004727 len = sess->fe->fwdfor_hdr_len;
4728 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004729 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004730 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 +01004731
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004732 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004733 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004734 }
4735 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004736 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004737 /* FIXME: for the sake of completeness, we should also support
4738 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004739 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004740 int len;
4741 char pn[INET6_ADDRSTRLEN];
4742 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004743 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004744 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004745
Willy Tarreau59234e92008-11-30 23:51:27 +01004746 /* Note: we rely on the backend to get the header name to be used for
4747 * x-forwarded-for, because the header is really meant for the backends.
4748 * However, if the backend did not specify any option, we have to rely
4749 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004750 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004751 if (s->be->fwdfor_hdr_len) {
4752 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004753 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004754 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004755 len = sess->fe->fwdfor_hdr_len;
4756 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004757 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004758 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004759
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004760 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004761 goto return_bad_req;
4762 }
4763 }
4764
4765 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004766 * 10: add X-Original-To if either the frontend or the backend
4767 * asks for it.
4768 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004769 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004770
4771 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004772 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004773 /* Add an X-Original-To header unless the destination IP is
4774 * in the 'except' network range.
4775 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004776 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004777
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004778 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004779 ((!sess->fe->except_mask_to.s_addr ||
4780 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4781 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004782 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004783 (((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 +02004784 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004785 int len;
4786 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004787 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004788
4789 /* Note: we rely on the backend to get the header name to be used for
4790 * x-original-to, because the header is really meant for the backends.
4791 * However, if the backend did not specify any option, we have to rely
4792 * on the frontend's header name.
4793 */
4794 if (s->be->orgto_hdr_len) {
4795 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004796 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004797 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004798 len = sess->fe->orgto_hdr_len;
4799 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004800 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004801 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 +02004802
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004803 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004804 goto return_bad_req;
4805 }
4806 }
4807 }
4808
Willy Tarreau50fc7772012-11-11 22:19:57 +01004809 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4810 * If an "Upgrade" token is found, the header is left untouched in order not to have
4811 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4812 * "Upgrade" is present in the Connection header.
4813 */
4814 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4815 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004816 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004817 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004818 unsigned int want_flags = 0;
4819
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004820 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004821 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004822 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004823 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004824 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004825 want_flags |= TX_CON_CLO_SET;
4826 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004827 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004828 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004829 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004830 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004831 want_flags |= TX_CON_KAL_SET;
4832 }
4833
4834 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004835 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004836 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004837
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004838
Willy Tarreau522d6c02009-12-06 18:49:18 +01004839 /* If we have no server assigned yet and we're balancing on url_param
4840 * with a POST request, we may be interested in checking the body for
4841 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004842 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004843 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004844 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004845 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004846 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004847 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004848 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004849
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004850 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004851 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004852 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004853#ifdef TCP_QUICKACK
4854 /* We expect some data from the client. Unless we know for sure
4855 * we already have a full request, we have to re-enable quick-ack
4856 * in case we previously disabled it, otherwise we might cause
4857 * the client to delay further data.
4858 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004859 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004860 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004861 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004862 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004863 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004864#endif
4865 }
Willy Tarreau03945942009-12-22 16:50:27 +01004866
Willy Tarreau59234e92008-11-30 23:51:27 +01004867 /*************************************************************
4868 * OK, that's finished for the headers. We have done what we *
4869 * could. Let's switch to the DATA state. *
4870 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004871 req->analyse_exp = TICK_ETERNITY;
4872 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004873
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004874 /* if the server closes the connection, we want to immediately react
4875 * and close the socket to save packets and syscalls.
4876 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004877 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004878 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004879
Willy Tarreau59234e92008-11-30 23:51:27 +01004880 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004881 /* OK let's go on with the BODY now */
4882 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004883
Willy Tarreau59234e92008-11-30 23:51:27 +01004884 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004885 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004886 /* we detected a parsing error. We want to archive this request
4887 * in the dedicated proxy area for later troubleshooting.
4888 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004889 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004890 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004891
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004892 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004893 txn->req.msg_state = HTTP_MSG_ERROR;
4894 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004895 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004896 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004897
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004898 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004899 if (sess->listener->counters)
4900 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004901
Willy Tarreaue7dff022015-04-03 01:14:29 +02004902 if (!(s->flags & SF_ERR_MASK))
4903 s->flags |= SF_ERR_PRXCOND;
4904 if (!(s->flags & SF_FINST_MASK))
4905 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004906 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004907}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004908
Willy Tarreau60b85b02008-11-30 23:28:40 +01004909/* This function is an analyser which processes the HTTP tarpit. It always
4910 * returns zero, at the beginning because it prevents any other processing
4911 * from occurring, and at the end because it terminates the request.
4912 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004913int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004914{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004915 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004916
4917 /* This connection is being tarpitted. The CLIENT side has
4918 * already set the connect expiration date to the right
4919 * timeout. We just have to check that the client is still
4920 * there and that the timeout has not expired.
4921 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004922 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004923 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004924 !tick_is_expired(req->analyse_exp, now_ms))
4925 return 0;
4926
4927 /* We will set the queue timer to the time spent, just for
4928 * logging purposes. We fake a 500 server error, so that the
4929 * attacker will not suspect his connection has been tarpitted.
4930 * It will not cause trouble to the logs because we can exclude
4931 * the tarpitted connections by filtering on the 'PT' status flags.
4932 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004933 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4934
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004935 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004936 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004937
Christopher Faulet0184ea72017-01-05 14:06:34 +01004938 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004939 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004940
Willy Tarreaue7dff022015-04-03 01:14:29 +02004941 if (!(s->flags & SF_ERR_MASK))
4942 s->flags |= SF_ERR_PRXCOND;
4943 if (!(s->flags & SF_FINST_MASK))
4944 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004945 return 0;
4946}
4947
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004948/* This function is an analyser which waits for the HTTP request body. It waits
4949 * for either the buffer to be full, or the full advertised contents to have
4950 * reached the buffer. It must only be called after the standard HTTP request
4951 * processing has occurred, because it expects the request to be parsed and will
4952 * look for the Expect header. It may send a 100-Continue interim response. It
4953 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4954 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4955 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004956 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004957int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004958{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004959 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004960 struct http_txn *txn = s->txn;
4961 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004962
4963 /* We have to parse the HTTP request body to find any required data.
4964 * "balance url_param check_post" should have been the only way to get
4965 * into this. We were brought here after HTTP header analysis, so all
4966 * related structures are ready.
4967 */
4968
Willy Tarreau890988f2014-04-10 11:59:33 +02004969 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4970 /* This is the first call */
4971 if (msg->msg_state < HTTP_MSG_BODY)
4972 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004973
Willy Tarreau890988f2014-04-10 11:59:33 +02004974 if (msg->msg_state < HTTP_MSG_100_SENT) {
4975 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4976 * send an HTTP/1.1 100 Continue intermediate response.
4977 */
4978 if (msg->flags & HTTP_MSGF_VER_11) {
4979 struct hdr_ctx ctx;
4980 ctx.idx = 0;
4981 /* Expect is allowed in 1.1, look for it */
4982 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4983 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004984 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004985 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004986 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004987 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004988 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004990
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004991 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004992 * req->buf->p still points to the beginning of the message. We
4993 * must save the body in msg->next because it survives buffer
4994 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004995 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004996 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004997
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004998 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004999 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5000 else
5001 msg->msg_state = HTTP_MSG_DATA;
5002 }
5003
Willy Tarreau890988f2014-04-10 11:59:33 +02005004 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
5005 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02005006 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02005007 goto missing_data;
5008
5009 /* OK we have everything we need now */
5010 goto http_end;
5011 }
5012
5013 /* OK here we're parsing a chunked-encoded message */
5014
Willy Tarreau522d6c02009-12-06 18:49:18 +01005015 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005016 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005017 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005018 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01005019 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005020 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01005021
Willy Tarreau115acb92009-12-26 13:56:06 +01005022 if (!ret)
5023 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005024 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005025 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01005026 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005027 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005028 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005029 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01005030 }
5031
Willy Tarreaud98cf932009-12-27 22:54:55 +01005032 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005033 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5034 * for at least a whole chunk or the whole content length bytes after
5035 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005036 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005037 if (msg->msg_state == HTTP_MSG_TRAILERS)
5038 goto http_end;
5039
Willy Tarreaue115b492015-05-01 23:05:14 +02005040 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005041 goto http_end;
5042
5043 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005044 /* we get here if we need to wait for more data. If the buffer is full,
5045 * we have the maximum we can expect.
5046 */
5047 if (buffer_full(req->buf, global.tune.maxrewrite))
5048 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005049
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005050 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005051 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005052 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005053
Willy Tarreaue7dff022015-04-03 01:14:29 +02005054 if (!(s->flags & SF_ERR_MASK))
5055 s->flags |= SF_ERR_CLITO;
5056 if (!(s->flags & SF_FINST_MASK))
5057 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005058 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005059 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005060
5061 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005062 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005063 /* Not enough data. We'll re-use the http-request
5064 * timeout here. Ideally, we should set the timeout
5065 * relative to the accept() date. We just set the
5066 * request timeout once at the beginning of the
5067 * request.
5068 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005069 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005070 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005071 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005072 return 0;
5073 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005074
5075 http_end:
5076 /* The situation will not evolve, so let's give up on the analysis. */
5077 s->logs.tv_request = now; /* update the request timer to reflect full request */
5078 req->analysers &= ~an_bit;
5079 req->analyse_exp = TICK_ETERNITY;
5080 return 1;
5081
5082 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005083 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005084 txn->req.msg_state = HTTP_MSG_ERROR;
5085 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005086 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005087
Willy Tarreaue7dff022015-04-03 01:14:29 +02005088 if (!(s->flags & SF_ERR_MASK))
5089 s->flags |= SF_ERR_PRXCOND;
5090 if (!(s->flags & SF_FINST_MASK))
5091 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005092
Willy Tarreau522d6c02009-12-06 18:49:18 +01005093 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005094 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005095 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005096 if (sess->listener->counters)
5097 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005098 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005099}
5100
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005101/* send a server's name with an outgoing request over an established connection.
5102 * Note: this function is designed to be called once the request has been scheduled
5103 * for being forwarded. This is the reason why it rewinds the buffer before
5104 * proceeding.
5105 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005106int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005107
5108 struct hdr_ctx ctx;
5109
Mark Lamourinec2247f02012-01-04 13:02:01 -05005110 char *hdr_name = be->server_id_hdr_name;
5111 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005112 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005113 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005114 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005115
William Lallemandd9e90662012-01-30 17:27:17 +01005116 ctx.idx = 0;
5117
Willy Tarreau211cdec2014-04-17 20:18:08 +02005118 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005119 if (old_o) {
5120 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005121 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005122 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005123 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005124 }
5125
Willy Tarreau9b28e032012-10-12 23:49:43 +02005126 old_i = chn->buf->i;
5127 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 -05005128 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005129 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005130 }
5131
5132 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005133 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005134 memcpy(hdr_val, hdr_name, hdr_name_len);
5135 hdr_val += hdr_name_len;
5136 *hdr_val++ = ':';
5137 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005138 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5139 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005140
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005141 if (old_o) {
5142 /* If this was a forwarded request, we must readjust the amount of
5143 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005144 * variations. Note that the current state is >= HTTP_MSG_BODY,
5145 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005146 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005147 old_o += chn->buf->i - old_i;
5148 b_adv(chn->buf, old_o);
5149 txn->req.next -= old_o;
5150 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005151 }
5152
Mark Lamourinec2247f02012-01-04 13:02:01 -05005153 return 0;
5154}
5155
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156/* Terminate current transaction and prepare a new one. This is very tricky
5157 * right now but it works.
5158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005159void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005161 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005162 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005163 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005164 struct connection *srv_conn;
5165 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005166 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005167
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168 /* FIXME: We need a more portable way of releasing a backend's and a
5169 * server's connections. We need a safer way to reinitialize buffer
5170 * flags. We also need a more accurate method for computing per-request
5171 * data.
5172 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005173 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174
Willy Tarreau4213a112013-12-15 10:25:42 +01005175 /* unless we're doing keep-alive, we want to quickly close the connection
5176 * to the server.
5177 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005178 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005179 !si_conn_ready(&s->si[1])) {
5180 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5181 si_shutr(&s->si[1]);
5182 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005183 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184
Willy Tarreaue7dff022015-04-03 01:14:29 +02005185 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005186 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005187 if (unlikely(s->srv_conn))
5188 sess_change_server(s, NULL);
5189 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005190
5191 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005192 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193
Willy Tarreaueee5b512015-04-03 23:46:31 +02005194 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195 int n;
5196
Willy Tarreaueee5b512015-04-03 23:46:31 +02005197 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 if (n < 1 || n > 5)
5199 n = 0;
5200
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005201 if (fe->mode == PR_MODE_HTTP) {
5202 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005203 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005204 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005205 (be->mode == PR_MODE_HTTP)) {
5206 be->be_counters.p.http.rsp[n]++;
5207 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005208 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209 }
5210
5211 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005212 s->logs.bytes_in -= s->req.buf->i;
5213 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005214
5215 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005216 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005217 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005218 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 s->do_log(s);
5220 }
5221
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005222 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005223 stream_stop_content_counters(s);
5224 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005225
Willy Tarreau610ecce2010-01-04 21:15:02 +01005226 s->logs.accept_date = date; /* user-visible date for logging */
5227 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005228 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5229 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005230 tv_zero(&s->logs.tv_request);
5231 s->logs.t_queue = -1;
5232 s->logs.t_connect = -1;
5233 s->logs.t_data = -1;
5234 s->logs.t_close = 0;
5235 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5236 s->logs.srv_queue_size = 0; /* we will get this number soon */
5237
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005238 s->logs.bytes_in = s->req.total = s->req.buf->i;
5239 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240
5241 if (s->pend_pos)
5242 pendconn_free(s->pend_pos);
5243
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005244 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005245 if (s->flags & SF_CURR_SESS) {
5246 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005247 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005249 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005250 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 }
5252
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005253 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254
Willy Tarreau4213a112013-12-15 10:25:42 +01005255 /* only release our endpoint if we don't intend to reuse the
5256 * connection.
5257 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005258 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005259 !si_conn_ready(&s->si[1])) {
5260 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005261 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005262 }
5263
Willy Tarreau350f4872014-11-28 14:42:25 +01005264 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5265 s->si[1].err_type = SI_ET_NONE;
5266 s->si[1].conn_retries = 0; /* used for logging too */
5267 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005268 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 +01005269 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);
5270 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 +02005271 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5272 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5273 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005274
Willy Tarreaueee5b512015-04-03 23:46:31 +02005275 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005277 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005278
5279 if (prev_status == 401 || prev_status == 407) {
5280 /* In HTTP keep-alive mode, if we receive a 401, we still have
5281 * a chance of being able to send the visitor again to the same
5282 * server over the same connection. This is required by some
5283 * broken protocols such as NTLM, and anyway whenever there is
5284 * an opportunity for sending the challenge to the proper place,
5285 * it's better to do it (at least it helps with debugging).
5286 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005287 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005288 if (srv_conn)
5289 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005290 }
5291
Willy Tarreau53f96852016-02-02 18:50:47 +01005292 /* Never ever allow to reuse a connection from a non-reuse backend */
5293 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5294 srv_conn->flags |= CO_FL_PRIVATE;
5295
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005296 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005297 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005299 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005300 s->req.flags |= CF_NEVER_WAIT;
5301 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005302 }
5303
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 /* if the request buffer is not empty, it means we're
5305 * about to process another request, so send pending
5306 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005307 * Just don't do this if the buffer is close to be full,
5308 * because the request will wait for it to flush a little
5309 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005311 if (s->req.buf->i) {
5312 if (s->res.buf->o &&
5313 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5314 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5315 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005316 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005317
Willy Tarreau714ea782015-11-25 20:11:11 +01005318 /* we're removing the analysers, we MUST re-enable events detection.
5319 * We don't enable close on the response channel since it's either
5320 * already closed, or in keep-alive with an idle connection handler.
5321 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005322 channel_auto_read(&s->req);
5323 channel_auto_close(&s->req);
5324 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005325
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005326 /* we're in keep-alive with an idle connection, monitor it if not already done */
5327 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005328 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005329 if (!srv)
5330 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005331 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005332 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005333 else if (prev_flags & TX_NOT_FIRST)
5334 /* note: we check the request, not the connection, but
5335 * this is valid for strategies SAFE and AGGR, and in
5336 * case of ALWS, we don't care anyway.
5337 */
5338 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005339 else
5340 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005341 }
Christopher Faulete6006242017-03-10 11:52:44 +01005342 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5343 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344}
5345
5346
5347/* This function updates the request state machine according to the response
5348 * state machine and buffer flags. It returns 1 if it changes anything (flag
5349 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5350 * it is only used to find when a request/response couple is complete. Both
5351 * this function and its equivalent should loop until both return zero. It
5352 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5353 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005354int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005356 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005357 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005358 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 unsigned int old_state = txn->req.msg_state;
5360
Willy Tarreau610ecce2010-01-04 21:15:02 +01005361 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5362 return 0;
5363
5364 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005365 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005366 * We can shut the read side unless we want to abort_on_close,
5367 * or we have a POST request. The issue with POST requests is
5368 * that some browsers still send a CRLF after the request, and
5369 * this CRLF must be read so that it does not remain in the kernel
5370 * buffers, otherwise a close could cause an RST on some systems
5371 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005372 * Note that if we're using keep-alive on the client side, we'd
5373 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005374 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005375 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005376 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005377 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5378 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5379 !(s->be->options & PR_O_ABRT_CLOSE) &&
5380 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005381 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382
Willy Tarreau40f151a2012-12-20 12:10:09 +01005383 /* if the server closes the connection, we want to immediately react
5384 * and close the socket to save packets and syscalls.
5385 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005386 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005387
Willy Tarreau7f876a12015-11-18 11:59:55 +01005388 /* In any case we've finished parsing the request so we must
5389 * disable Nagle when sending data because 1) we're not going
5390 * to shut this side, and 2) the server is waiting for us to
5391 * send pending data.
5392 */
5393 chn->flags |= CF_NEVER_WAIT;
5394
Willy Tarreau610ecce2010-01-04 21:15:02 +01005395 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5396 goto wait_other_side;
5397
5398 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5399 /* The server has not finished to respond, so we
5400 * don't want to move in order not to upset it.
5401 */
5402 goto wait_other_side;
5403 }
5404
5405 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5406 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005407 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005408 txn->req.msg_state = HTTP_MSG_TUNNEL;
5409 goto wait_other_side;
5410 }
5411
5412 /* When we get here, it means that both the request and the
5413 * response have finished receiving. Depending on the connection
5414 * mode, we'll have to wait for the last bytes to leave in either
5415 * direction, and sometimes for a close to be effective.
5416 */
5417
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005418 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5419 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005420 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5421 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005422 }
5423 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5424 /* Option forceclose is set, or either side wants to close,
5425 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005426 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005427 * once both states are CLOSED.
5428 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005429 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5430 channel_shutr_now(chn);
5431 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005433 }
5434 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005435 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5436 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005437 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005438 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5439 channel_auto_read(chn);
5440 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005441 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 }
5443
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005444 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005445 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005446 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005447
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005448 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449 txn->req.msg_state = HTTP_MSG_CLOSING;
5450 goto http_msg_closing;
5451 }
5452 else {
5453 txn->req.msg_state = HTTP_MSG_CLOSED;
5454 goto http_msg_closed;
5455 }
5456 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005457 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005458 }
5459
5460 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5461 http_msg_closing:
5462 /* nothing else to forward, just waiting for the output buffer
5463 * to be empty and for the shutw_now to take effect.
5464 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005465 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466 txn->req.msg_state = HTTP_MSG_CLOSED;
5467 goto http_msg_closed;
5468 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005469 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005470 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 txn->req.msg_state = HTTP_MSG_ERROR;
5472 goto wait_other_side;
5473 }
5474 }
5475
5476 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5477 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005478 /* see above in MSG_DONE why we only do this in these states */
5479 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5480 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5481 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005482 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005483 goto wait_other_side;
5484 }
5485
5486 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005487 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488}
5489
5490
5491/* This function updates the response state machine according to the request
5492 * state machine and buffer flags. It returns 1 if it changes anything (flag
5493 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5494 * it is only used to find when a request/response couple is complete. Both
5495 * this function and its equivalent should loop until both return zero. It
5496 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5497 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005498int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005500 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005501 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005502 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005503 unsigned int old_state = txn->rsp.msg_state;
5504
Willy Tarreau610ecce2010-01-04 21:15:02 +01005505 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5506 return 0;
5507
5508 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5509 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005510 * still monitor the server connection for a possible close
5511 * while the request is being uploaded, so we don't disable
5512 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005514 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515
5516 if (txn->req.msg_state == HTTP_MSG_ERROR)
5517 goto wait_other_side;
5518
5519 if (txn->req.msg_state < HTTP_MSG_DONE) {
5520 /* The client seems to still be sending data, probably
5521 * because we got an error response during an upload.
5522 * We have the choice of either breaking the connection
5523 * or letting it pass through. Let's do the later.
5524 */
5525 goto wait_other_side;
5526 }
5527
5528 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5529 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005530 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005532 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005533 goto wait_other_side;
5534 }
5535
5536 /* When we get here, it means that both the request and the
5537 * response have finished receiving. Depending on the connection
5538 * mode, we'll have to wait for the last bytes to leave in either
5539 * direction, and sometimes for a close to be effective.
5540 */
5541
5542 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5543 /* Server-close mode : shut read and wait for the request
5544 * side to close its output buffer. The caller will detect
5545 * when we're in DONE and the other is in CLOSED and will
5546 * catch that for the final cleanup.
5547 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005548 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5549 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005551 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5552 /* Option forceclose is set, or either side wants to close,
5553 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005554 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005555 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005557 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5558 channel_shutr_now(chn);
5559 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005560 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561 }
5562 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005563 /* The last possible modes are keep-alive and tunnel. Tunnel will
5564 * need to forward remaining data. Keep-alive will need to monitor
5565 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005567 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005568 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005569 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5570 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 }
5572
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005573 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005574 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005575 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5577 goto http_msg_closing;
5578 }
5579 else {
5580 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5581 goto http_msg_closed;
5582 }
5583 }
5584 goto wait_other_side;
5585 }
5586
5587 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5588 http_msg_closing:
5589 /* nothing else to forward, just waiting for the output buffer
5590 * to be empty and for the shutw_now to take effect.
5591 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005592 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005593 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5594 goto http_msg_closed;
5595 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005596 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005597 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005599 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005600 if (objt_server(s->target))
5601 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005602 goto wait_other_side;
5603 }
5604 }
5605
5606 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5607 http_msg_closed:
5608 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005609 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005610 channel_auto_close(chn);
5611 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005612 goto wait_other_side;
5613 }
5614
5615 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005616 /* We force the response to leave immediately if we're waiting for the
5617 * other side, since there is no pending shutdown to push it out.
5618 */
5619 if (!channel_is_empty(chn))
5620 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005621 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005622}
5623
5624
5625/* Resync the request and response state machines. Return 1 if either state
5626 * changes.
5627 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005628int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005629{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005630 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005631 int old_req_state = txn->req.msg_state;
5632 int old_res_state = txn->rsp.msg_state;
5633
Willy Tarreau610ecce2010-01-04 21:15:02 +01005634 http_sync_req_state(s);
5635 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005636 if (!http_sync_res_state(s))
5637 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005638 if (!http_sync_req_state(s))
5639 break;
5640 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005641
Willy Tarreau610ecce2010-01-04 21:15:02 +01005642 /* OK, both state machines agree on a compatible state.
5643 * There are a few cases we're interested in :
5644 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5645 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5646 * directions, so let's simply disable both analysers.
5647 * - HTTP_MSG_CLOSED on the response only means we must abort the
5648 * request.
5649 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5650 * with server-close mode means we've completed one request and we
5651 * must re-initialize the server connection.
5652 */
5653
5654 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5655 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5656 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5657 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005658 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005659 channel_auto_close(&s->req);
5660 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005661 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005662 channel_auto_close(&s->res);
5663 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005664 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005665 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005666 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005667 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005668 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005669 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005670 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005671 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005672 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005673 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005674 channel_auto_close(&s->res);
5675 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005676 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005677 channel_abort(&s->req);
5678 channel_auto_close(&s->req);
5679 channel_auto_read(&s->req);
5680 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005681 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005682 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5683 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005684 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005685 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5686 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5687 /* server-close/keep-alive: terminate this transaction,
5688 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005689 * a fresh-new transaction, but only once we're sure there's
5690 * enough room in the request and response buffer to process
5691 * another request. The request buffer must not hold any
5692 * pending output data and the request buffer must not have
5693 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005694 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005695 if (s->req.buf->o)
5696 s->req.flags |= CF_WAKE_WRITE;
5697 else if (channel_congested(&s->res))
5698 s->res.flags |= CF_WAKE_WRITE;
Christopher Faulete6006242017-03-10 11:52:44 +01005699 else {
5700 s->req.analysers = AN_REQ_FLT_END;
5701 s->res.analysers = AN_RES_FLT_END;
5702 txn->flags |= TX_WAIT_CLEANUP;
5703 return 1;
5704 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005705 }
5706
Willy Tarreau610ecce2010-01-04 21:15:02 +01005707 return txn->req.msg_state != old_req_state ||
5708 txn->rsp.msg_state != old_res_state;
5709}
5710
Willy Tarreaud98cf932009-12-27 22:54:55 +01005711/* This function is an analyser which forwards request body (including chunk
5712 * sizes if any). It is called as soon as we must forward, even if we forward
5713 * zero byte. The only situation where it must not be called is when we're in
5714 * tunnel mode and we want to forward till the close. It's used both to forward
5715 * remaining data and to resync after end of body. It expects the msg_state to
5716 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005717 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005718 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005719 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005720 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005721int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005722{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005723 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005724 struct http_txn *txn = s->txn;
5725 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005726 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005727
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005728 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5729 return 0;
5730
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005731 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005732 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005733 /* Output closed while we were sending data. We must abort and
5734 * wake the other side up.
5735 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005736 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005737 msg->msg_state = HTTP_MSG_ERROR;
5738 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005739 return 1;
5740 }
5741
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 /* Note that we don't have to send 100-continue back because we don't
5743 * need the data to complete our job, and it's up to the server to
5744 * decide whether to return 100, 417 or anything else in return of
5745 * an "Expect: 100-continue" header.
5746 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005747 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005748 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5749 ? HTTP_MSG_CHUNK_SIZE
5750 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005751
5752 /* TODO/filters: when http-buffer-request option is set or if a
5753 * rule on url_param exists, the first chunk size could be
5754 * already parsed. In that case, msg->next is after the chunk
5755 * size (including the CRLF after the size). So this case should
5756 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005757 }
5758
Willy Tarreau7ba23542014-04-17 21:50:00 +02005759 /* Some post-connect processing might want us to refrain from starting to
5760 * forward data. Currently, the only reason for this is "balance url_param"
5761 * whichs need to parse/process the request after we've enabled forwarding.
5762 */
5763 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005764 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005765 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005766 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005767 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005768 }
5769 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5770 }
5771
Willy Tarreau80a92c02014-03-12 10:41:13 +01005772 /* in most states, we should abort in case of early close */
5773 channel_auto_close(req);
5774
Willy Tarreauefdf0942014-04-24 20:08:57 +02005775 if (req->to_forward) {
5776 /* We can't process the buffer's contents yet */
5777 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005779 }
5780
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005781 if (msg->msg_state < HTTP_MSG_DONE) {
5782 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5783 ? http_msg_forward_chunked_body(s, msg)
5784 : http_msg_forward_body(s, msg));
5785 if (!ret)
5786 goto missing_data_or_waiting;
5787 if (ret < 0)
5788 goto return_bad_req;
5789 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005790
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005791 /* other states, DONE...TUNNEL */
5792 /* we don't want to forward closes on DONE except in tunnel mode. */
5793 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5794 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005795
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005796 if (http_resync_states(s)) {
5797 /* some state changes occurred, maybe the analyser
5798 * was disabled too. */
5799 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5800 if (req->flags & CF_SHUTW) {
5801 /* request errors are most likely due to the
5802 * server aborting the transfer. */
5803 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005804 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005805 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005806 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005807 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005808 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005809 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005810 }
5811
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005812 /* If "option abortonclose" is set on the backend, we want to monitor
5813 * the client's connection and forward any shutdown notification to the
5814 * server, which will decide whether to close or to go on processing the
5815 * request. We only do that in tunnel mode, and not in other modes since
5816 * it can be abused to exhaust source ports. */
5817 if (s->be->options & PR_O_ABRT_CLOSE) {
5818 channel_auto_read(req);
5819 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5820 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5821 s->si[1].flags |= SI_FL_NOLINGER;
5822 channel_auto_close(req);
5823 }
5824 else if (s->txn->meth == HTTP_METH_POST) {
5825 /* POST requests may require to read extra CRLF sent by broken
5826 * browsers and which could cause an RST to be sent upon close
5827 * on some systems (eg: Linux). */
5828 channel_auto_read(req);
5829 }
5830 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005831
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005832 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005833 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005834 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005835 if (!(s->flags & SF_ERR_MASK))
5836 s->flags |= SF_ERR_CLICL;
5837 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005838 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005839 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005840 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005841 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005842 }
5843
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005844 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005845 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005846 if (objt_server(s->target))
5847 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005848
5849 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005850 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005851
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005852 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005853 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005854 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005855
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005856 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005857 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005858 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005859 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005860 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005861
Willy Tarreau5c620922011-05-11 19:56:11 +02005862 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005863 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005864 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005865 * modes are already handled by the stream sock layer. We must not do
5866 * this in content-length mode because it could present the MSG_MORE
5867 * flag with the last block of forwarded data, which would cause an
5868 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005869 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005870 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005871 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005872
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005873 return 0;
5874
Willy Tarreaud98cf932009-12-27 22:54:55 +01005875 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005876 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005877 if (sess->listener->counters)
5878 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005879
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005880 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005881 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005882 txn->req.msg_state = HTTP_MSG_ERROR;
5883 if (txn->status) {
5884 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005885 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005886 } else {
5887 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005888 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005889 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005890 req->analysers &= AN_REQ_FLT_END;
5891 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005892
Willy Tarreaue7dff022015-04-03 01:14:29 +02005893 if (!(s->flags & SF_ERR_MASK))
5894 s->flags |= SF_ERR_PRXCOND;
5895 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005896 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005897 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005898 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005899 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005900 }
5901 return 0;
5902
5903 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005904 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005905 txn->req.msg_state = HTTP_MSG_ERROR;
5906 if (txn->status) {
5907 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005908 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005909 } else {
5910 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005911 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005912 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005913 req->analysers &= AN_REQ_FLT_END;
5914 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005915
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005916 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005917 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005918 if (objt_server(s->target))
5919 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005920
Willy Tarreaue7dff022015-04-03 01:14:29 +02005921 if (!(s->flags & SF_ERR_MASK))
5922 s->flags |= SF_ERR_SRVCL;
5923 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005924 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005925 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005926 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005927 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005928 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005929 return 0;
5930}
5931
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005932/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5933 * processing can continue on next analysers, or zero if it either needs more
5934 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005935 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 * when it has nothing left to do, and may remove any analyser when it wants to
5937 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005938 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005939int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005940{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005941 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005942 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005943 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005944 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005945 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005946 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005947 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005948
Willy Tarreau87b09662015-04-03 00:22:06 +02005949 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 +02005950 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005951 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005952 rep,
5953 rep->rex, rep->wex,
5954 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005955 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005956 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005957
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 /*
5959 * Now parse the partial (or complete) lines.
5960 * We will check the response syntax, and also join multi-line
5961 * headers. An index of all the lines will be elaborated while
5962 * parsing.
5963 *
5964 * For the parsing, we use a 28 states FSM.
5965 *
5966 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005967 * rep->buf->p = beginning of response
5968 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5969 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005970 * msg->eol = end of current header or line (LF or CRLF)
5971 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 */
5973
Willy Tarreau628c40c2014-04-24 19:11:26 +02005974 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005975 /* There's a protected area at the end of the buffer for rewriting
5976 * purposes. We don't want to start to parse the request if the
5977 * protected area is affected, because we may have to move processed
5978 * data later, which is much more complicated.
5979 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005980 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005981 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005982 /* some data has still not left the buffer, wake us once that's done */
5983 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5984 goto abort_response;
5985 channel_dont_close(rep);
5986 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005987 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005988 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005989 }
5990
Willy Tarreau379357a2013-06-08 12:55:46 +02005991 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5992 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5993 buffer_slow_realign(rep->buf);
5994
Willy Tarreau9b28e032012-10-12 23:49:43 +02005995 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005996 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005997 }
5998
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005999 /* 1: we might have to print this header in debug mode */
6000 if (unlikely((global.mode & MODE_DEBUG) &&
6001 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006002 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006003 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006004
Willy Tarreau9b28e032012-10-12 23:49:43 +02006005 sol = rep->buf->p;
6006 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006008
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006009 sol += hdr_idx_first_pos(&txn->hdr_idx);
6010 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006011
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012 while (cur_idx) {
6013 eol = sol + txn->hdr_idx.v[cur_idx].len;
6014 debug_hdr("srvhdr", s, sol, eol);
6015 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6016 cur_idx = txn->hdr_idx.v[cur_idx].next;
6017 }
6018 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006019
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 /*
6021 * Now we quickly check if we have found a full valid response.
6022 * If not so, we check the FD and buffer states before leaving.
6023 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006024 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 * responses are checked first.
6026 *
6027 * Depending on whether the client is still there or not, we
6028 * may send an error response back or not. Note that normally
6029 * we should only check for HTTP status there, and check I/O
6030 * errors somewhere else.
6031 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006032
Willy Tarreau655dce92009-11-08 13:10:58 +01006033 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034 /* Invalid response */
6035 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6036 /* we detected a parsing error. We want to archive this response
6037 * in the dedicated proxy area for later troubleshooting.
6038 */
6039 hdr_response_bad:
6040 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006041 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006043 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006044 if (objt_server(s->target)) {
6045 objt_server(s->target)->counters.failed_resp++;
6046 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006047 }
Willy Tarreau64648412010-03-05 10:41:54 +01006048 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006049 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006050 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006052 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006053 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006054 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055
Willy Tarreaue7dff022015-04-03 01:14:29 +02006056 if (!(s->flags & SF_ERR_MASK))
6057 s->flags |= SF_ERR_PRXCOND;
6058 if (!(s->flags & SF_FINST_MASK))
6059 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060
6061 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006062 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006063
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006064 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006065 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006066 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006067 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068 goto hdr_response_bad;
6069 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006070
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006071 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006072 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006074 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006075 else if (txn->flags & TX_NOT_FIRST)
6076 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006077
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006078 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006079 if (objt_server(s->target)) {
6080 objt_server(s->target)->counters.failed_resp++;
6081 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006082 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006083
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006084 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006085 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006086 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006087 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006088 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006089 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02006090
Willy Tarreaue7dff022015-04-03 01:14:29 +02006091 if (!(s->flags & SF_ERR_MASK))
6092 s->flags |= SF_ERR_SRVCL;
6093 if (!(s->flags & SF_FINST_MASK))
6094 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006095 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006096 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006097
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006098 /* read timeout : return a 504 to the client. */
6099 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006100 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006101 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006102
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006103 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006104 if (objt_server(s->target)) {
6105 objt_server(s->target)->counters.failed_resp++;
6106 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006107 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006108
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006109 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006110 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006112 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006113 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006114 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02006115
Willy Tarreaue7dff022015-04-03 01:14:29 +02006116 if (!(s->flags & SF_ERR_MASK))
6117 s->flags |= SF_ERR_SRVTO;
6118 if (!(s->flags & SF_FINST_MASK))
6119 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006120 return 0;
6121 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006122
Willy Tarreauf003d372012-11-26 13:35:37 +01006123 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006124 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006125 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006126 s->be->be_counters.cli_aborts++;
6127 if (objt_server(s->target))
6128 objt_server(s->target)->counters.cli_aborts++;
6129
Christopher Faulet0184ea72017-01-05 14:06:34 +01006130 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006131 channel_auto_close(rep);
6132
6133 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006134 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006135 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01006136
Willy Tarreaue7dff022015-04-03 01:14:29 +02006137 if (!(s->flags & SF_ERR_MASK))
6138 s->flags |= SF_ERR_CLICL;
6139 if (!(s->flags & SF_FINST_MASK))
6140 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006141
Willy Tarreau87b09662015-04-03 00:22:06 +02006142 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006143 return 0;
6144 }
6145
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006146 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006147 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006148 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006149 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006150 else if (txn->flags & TX_NOT_FIRST)
6151 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006153 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006154 if (objt_server(s->target)) {
6155 objt_server(s->target)->counters.failed_resp++;
6156 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006157 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006158
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006159 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006160 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006161 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006162 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006163 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006164 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006165
Willy Tarreaue7dff022015-04-03 01:14:29 +02006166 if (!(s->flags & SF_ERR_MASK))
6167 s->flags |= SF_ERR_SRVCL;
6168 if (!(s->flags & SF_FINST_MASK))
6169 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006170 return 0;
6171 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006172
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006173 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006174 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006175 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006176 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006177 else if (txn->flags & TX_NOT_FIRST)
6178 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006179
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006180 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006181 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006182 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006183
Willy Tarreaue7dff022015-04-03 01:14:29 +02006184 if (!(s->flags & SF_ERR_MASK))
6185 s->flags |= SF_ERR_CLICL;
6186 if (!(s->flags & SF_FINST_MASK))
6187 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188
Willy Tarreau87b09662015-04-03 00:22:06 +02006189 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006190 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006191 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006192
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006193 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006194 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006195 return 0;
6196 }
6197
6198 /* More interesting part now : we know that we have a complete
6199 * response which at least looks like HTTP. We have an indicator
6200 * of each header's length, so we can parse them quickly.
6201 */
6202
6203 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006204 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006205
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006206 /*
6207 * 1: get the status code
6208 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006209 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006210 if (n < 1 || n > 5)
6211 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006212 /* when the client triggers a 4xx from the server, it's most often due
6213 * to a missing object or permission. These events should be tracked
6214 * because if they happen often, it may indicate a brute force or a
6215 * vulnerability scan.
6216 */
6217 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006218 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006219
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006220 if (objt_server(s->target))
6221 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006222
Willy Tarreau91852eb2015-05-01 13:26:00 +02006223 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6224 * exactly one digit "." one digit. This check may be disabled using
6225 * option accept-invalid-http-response.
6226 */
6227 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6228 if (msg->sl.st.v_l != 8) {
6229 msg->err_pos = 0;
6230 goto hdr_response_bad;
6231 }
6232
6233 if (rep->buf->p[4] != '/' ||
6234 !isdigit((unsigned char)rep->buf->p[5]) ||
6235 rep->buf->p[6] != '.' ||
6236 !isdigit((unsigned char)rep->buf->p[7])) {
6237 msg->err_pos = 4;
6238 goto hdr_response_bad;
6239 }
6240 }
6241
Willy Tarreau5b154472009-12-21 20:11:07 +01006242 /* check if the response is HTTP/1.1 or above */
6243 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006244 ((rep->buf->p[5] > '1') ||
6245 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006246 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006247
6248 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006249 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 +01006250
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006251 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006252 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006253
Willy Tarreau9b28e032012-10-12 23:49:43 +02006254 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006255
Willy Tarreau39650402010-03-15 19:44:39 +01006256 /* Adjust server's health based on status code. Note: status codes 501
6257 * and 505 are triggered on demand by client request, so we must not
6258 * count them as server failures.
6259 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006260 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006261 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006262 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006263 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006264 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006265 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006266
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006267 /*
6268 * 2: check for cacheability.
6269 */
6270
6271 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006272 case 100:
6273 /*
6274 * We may be facing a 100-continue response, in which case this
6275 * is not the right response, and we're waiting for the next one.
6276 * Let's allow this response to go to the client and wait for the
6277 * next one.
6278 */
6279 hdr_idx_init(&txn->hdr_idx);
6280 msg->next -= channel_forward(rep, msg->next);
6281 msg->msg_state = HTTP_MSG_RPBEFORE;
6282 txn->status = 0;
6283 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006284 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006285 goto next_one;
6286
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006287 case 200:
6288 case 203:
6289 case 206:
6290 case 300:
6291 case 301:
6292 case 410:
6293 /* RFC2616 @13.4:
6294 * "A response received with a status code of
6295 * 200, 203, 206, 300, 301 or 410 MAY be stored
6296 * by a cache (...) unless a cache-control
6297 * directive prohibits caching."
6298 *
6299 * RFC2616 @9.5: POST method :
6300 * "Responses to this method are not cacheable,
6301 * unless the response includes appropriate
6302 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006303 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006304 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006305 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006306 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6307 break;
6308 default:
6309 break;
6310 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006311
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006312 /*
6313 * 3: we may need to capture headers
6314 */
6315 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006316 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006317 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006318 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006319
Willy Tarreau557f1992015-05-01 10:05:17 +02006320 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6321 * by RFC7230#3.3.3 :
6322 *
6323 * The length of a message body is determined by one of the following
6324 * (in order of precedence):
6325 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006326 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6327 * the connection will become a tunnel immediately after the empty
6328 * line that concludes the header fields. A client MUST ignore
6329 * any Content-Length or Transfer-Encoding header fields received
6330 * in such a message. Any 101 response (Switching Protocols) is
6331 * managed in the same manner.
6332 *
6333 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006334 * (Informational), 204 (No Content), or 304 (Not Modified) status
6335 * code is always terminated by the first empty line after the
6336 * header fields, regardless of the header fields present in the
6337 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006338 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006339 * 3. If a Transfer-Encoding header field is present and the chunked
6340 * transfer coding (Section 4.1) is the final encoding, the message
6341 * body length is determined by reading and decoding the chunked
6342 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006343 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006344 * If a Transfer-Encoding header field is present in a response and
6345 * the chunked transfer coding is not the final encoding, the
6346 * message body length is determined by reading the connection until
6347 * it is closed by the server. If a Transfer-Encoding header field
6348 * is present in a request and the chunked transfer coding is not
6349 * the final encoding, the message body length cannot be determined
6350 * reliably; the server MUST respond with the 400 (Bad Request)
6351 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006352 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006353 * If a message is received with both a Transfer-Encoding and a
6354 * Content-Length header field, the Transfer-Encoding overrides the
6355 * Content-Length. Such a message might indicate an attempt to
6356 * perform request smuggling (Section 9.5) or response splitting
6357 * (Section 9.4) and ought to be handled as an error. A sender MUST
6358 * remove the received Content-Length field prior to forwarding such
6359 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006360 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006361 * 4. If a message is received without Transfer-Encoding and with
6362 * either multiple Content-Length header fields having differing
6363 * field-values or a single Content-Length header field having an
6364 * invalid value, then the message framing is invalid and the
6365 * recipient MUST treat it as an unrecoverable error. If this is a
6366 * request message, the server MUST respond with a 400 (Bad Request)
6367 * status code and then close the connection. If this is a response
6368 * message received by a proxy, the proxy MUST close the connection
6369 * to the server, discard the received response, and send a 502 (Bad
6370 * Gateway) response to the client. If this is a response message
6371 * received by a user agent, the user agent MUST close the
6372 * connection to the server and discard the received response.
6373 *
6374 * 5. If a valid Content-Length header field is present without
6375 * Transfer-Encoding, its decimal value defines the expected message
6376 * body length in octets. If the sender closes the connection or
6377 * the recipient times out before the indicated number of octets are
6378 * received, the recipient MUST consider the message to be
6379 * incomplete and close the connection.
6380 *
6381 * 6. If this is a request message and none of the above are true, then
6382 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006383 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006384 * 7. Otherwise, this is a response message without a declared message
6385 * body length, so the message body length is determined by the
6386 * number of octets received prior to the server closing the
6387 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006388 */
6389
6390 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006391 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006392 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006393 * FIXME: should we parse anyway and return an error on chunked encoding ?
6394 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006395 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6396 txn->status == 101)) {
6397 /* Either we've established an explicit tunnel, or we're
6398 * switching the protocol. In both cases, we're very unlikely
6399 * to understand the next protocols. We have to switch to tunnel
6400 * mode, so that we transfer the request and responses then let
6401 * this protocol pass unmodified. When we later implement specific
6402 * parsers for such protocols, we'll want to check the Upgrade
6403 * header which contains information about that protocol for
6404 * responses with status 101 (eg: see RFC2817 about TLS).
6405 */
6406 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6407 msg->flags |= HTTP_MSGF_XFER_LEN;
6408 goto end;
6409 }
6410
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006411 if (txn->meth == HTTP_METH_HEAD ||
6412 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006413 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006414 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006415 goto skip_content_length;
6416 }
6417
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006418 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006419 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006420 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006421 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006422 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6423 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006424 /* bad transfer-encoding (chunked followed by something else) */
6425 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006426 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006427 break;
6428 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006429 }
6430
Willy Tarreau1c913912015-04-30 10:57:51 +02006431 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006432 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006433 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006434 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6435 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6436 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006437 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006438 signed long long cl;
6439
Willy Tarreauad14f752011-09-02 20:33:27 +02006440 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006441 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006442 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006443 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006444
Willy Tarreauad14f752011-09-02 20:33:27 +02006445 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006446 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006447 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006448 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006449
Willy Tarreauad14f752011-09-02 20:33:27 +02006450 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006451 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006452 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006453 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006454
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006455 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006456 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006457 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006458 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006459
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006460 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006461 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006462 }
6463
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006464 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006465 /* Now we have to check if we need to modify the Connection header.
6466 * This is more difficult on the response than it is on the request,
6467 * because we can have two different HTTP versions and we don't know
6468 * how the client will interprete a response. For instance, let's say
6469 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6470 * HTTP/1.1 response without any header. Maybe it will bound itself to
6471 * HTTP/1.0 because it only knows about it, and will consider the lack
6472 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6473 * the lack of header as a keep-alive. Thus we will use two flags
6474 * indicating how a request MAY be understood by the client. In case
6475 * of multiple possibilities, we'll fix the header to be explicit. If
6476 * ambiguous cases such as both close and keepalive are seen, then we
6477 * will fall back to explicit close. Note that we won't take risks with
6478 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006479 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006480 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006481 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6482 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6483 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6484 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006485 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006486
Willy Tarreau70dffda2014-01-30 03:07:23 +01006487 /* this situation happens when combining pretend-keepalive with httpclose. */
6488 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006489 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006491 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6492
Willy Tarreau60466522010-01-18 19:08:45 +01006493 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006494 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006495 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6496 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006497
Willy Tarreau60466522010-01-18 19:08:45 +01006498 /* now adjust header transformations depending on current state */
6499 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6500 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6501 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006502 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006503 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006504 }
Willy Tarreau60466522010-01-18 19:08:45 +01006505 else { /* SCL / KAL */
6506 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006507 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006508 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006509 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006510
Willy Tarreau60466522010-01-18 19:08:45 +01006511 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006512 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006513
Willy Tarreau60466522010-01-18 19:08:45 +01006514 /* Some keep-alive responses are converted to Server-close if
6515 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006516 */
Willy Tarreau60466522010-01-18 19:08:45 +01006517 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6518 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006519 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006520 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006521 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006522 }
6523
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006524 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006525 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006526 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006527
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006528 /* end of job, return OK */
6529 rep->analysers &= ~an_bit;
6530 rep->analyse_exp = TICK_ETERNITY;
6531 channel_auto_close(rep);
6532 return 1;
6533
6534 abort_keep_alive:
6535 /* A keep-alive request to the server failed on a network error.
6536 * The client is required to retry. We need to close without returning
6537 * any other information so that the client retries.
6538 */
6539 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006540 rep->analysers &= AN_RES_FLT_END;
6541 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006542 channel_auto_close(rep);
6543 s->logs.logwait = 0;
6544 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006545 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006546 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006547 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006548 return 0;
6549}
6550
6551/* This function performs all the processing enabled for the current response.
6552 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006553 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006554 * other functions. It works like process_request (see indications above).
6555 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006556int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006557{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006558 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006559 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006560 struct http_msg *msg = &txn->rsp;
6561 struct proxy *cur_proxy;
6562 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006563 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006564
Willy Tarreau87b09662015-04-03 00:22:06 +02006565 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 +02006566 now_ms, __FUNCTION__,
6567 s,
6568 rep,
6569 rep->rex, rep->wex,
6570 rep->flags,
6571 rep->buf->i,
6572 rep->analysers);
6573
6574 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6575 return 0;
6576
Willy Tarreau70730dd2014-04-24 18:06:27 +02006577 /* The stats applet needs to adjust the Connection header but we don't
6578 * apply any filter there.
6579 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006580 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6581 rep->analysers &= ~an_bit;
6582 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006583 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006584 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006585
Willy Tarreau58975672014-04-24 21:13:57 +02006586 /*
6587 * We will have to evaluate the filters.
6588 * As opposed to version 1.2, now they will be evaluated in the
6589 * filters order and not in the header order. This means that
6590 * each filter has to be validated among all headers.
6591 *
6592 * Filters are tried with ->be first, then with ->fe if it is
6593 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006594 *
6595 * Maybe we are in resume condiion. In this case I choose the
6596 * "struct proxy" which contains the rule list matching the resume
6597 * pointer. If none of theses "struct proxy" match, I initialise
6598 * the process with the first one.
6599 *
6600 * In fact, I check only correspondance betwwen the current list
6601 * pointer and the ->fe rule list. If it doesn't match, I initialize
6602 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006603 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006604 if (s->current_rule_list == &sess->fe->http_res_rules)
6605 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006606 else
6607 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006608 while (1) {
6609 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006610
Willy Tarreau58975672014-04-24 21:13:57 +02006611 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006612 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006613 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006614
Willy Tarreau51d861a2015-05-22 17:30:48 +02006615 if (ret == HTTP_RULE_RES_BADREQ)
6616 goto return_srv_prx_502;
6617
6618 if (ret == HTTP_RULE_RES_DONE) {
6619 rep->analysers &= ~an_bit;
6620 rep->analyse_exp = TICK_ETERNITY;
6621 return 1;
6622 }
6623 }
6624
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006625 /* we need to be called again. */
6626 if (ret == HTTP_RULE_RES_YIELD) {
6627 channel_dont_close(rep);
6628 return 0;
6629 }
6630
Willy Tarreau58975672014-04-24 21:13:57 +02006631 /* try headers filters */
6632 if (rule_set->rsp_exp != NULL) {
6633 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6634 return_bad_resp:
6635 if (objt_server(s->target)) {
6636 objt_server(s->target)->counters.failed_resp++;
6637 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006638 }
Willy Tarreau58975672014-04-24 21:13:57 +02006639 s->be->be_counters.failed_resp++;
6640 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006641 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006642 txn->status = 502;
6643 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006644 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006645 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006646 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006647 if (!(s->flags & SF_ERR_MASK))
6648 s->flags |= SF_ERR_PRXCOND;
6649 if (!(s->flags & SF_FINST_MASK))
6650 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006651 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006652 }
Willy Tarreau58975672014-04-24 21:13:57 +02006653 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006654
Willy Tarreau58975672014-04-24 21:13:57 +02006655 /* has the response been denied ? */
6656 if (txn->flags & TX_SVDENY) {
6657 if (objt_server(s->target))
6658 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006659
Willy Tarreau58975672014-04-24 21:13:57 +02006660 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006661 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006662 if (sess->listener->counters)
6663 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006664
Willy Tarreau58975672014-04-24 21:13:57 +02006665 goto return_srv_prx_502;
6666 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006667
Willy Tarreau58975672014-04-24 21:13:57 +02006668 /* add response headers from the rule sets in the same order */
6669 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006670 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006671 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006672 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006673 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006674 ret = acl_pass(ret);
6675 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6676 ret = !ret;
6677 if (!ret)
6678 continue;
6679 }
6680 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6681 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006682 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006683
Willy Tarreau58975672014-04-24 21:13:57 +02006684 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006685 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006686 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006687 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006688 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006689
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006690 /* After this point, this anayzer can't return yield, so we can
6691 * remove the bit corresponding to this analyzer from the list.
6692 *
6693 * Note that the intermediate returns and goto found previously
6694 * reset the analyzers.
6695 */
6696 rep->analysers &= ~an_bit;
6697 rep->analyse_exp = TICK_ETERNITY;
6698
Willy Tarreau58975672014-04-24 21:13:57 +02006699 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006700 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006701 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006702
Willy Tarreau58975672014-04-24 21:13:57 +02006703 /*
6704 * Now check for a server cookie.
6705 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006706 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006707 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006708
Willy Tarreau58975672014-04-24 21:13:57 +02006709 /*
6710 * Check for cache-control or pragma headers if required.
6711 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006712 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 +02006713 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006714
Willy Tarreau58975672014-04-24 21:13:57 +02006715 /*
6716 * Add server cookie in the response if needed
6717 */
6718 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6719 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006720 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006721 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6722 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6723 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6724 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6725 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006726 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006727 /* the server is known, it's not the one the client requested, or the
6728 * cookie's last seen date needs to be refreshed. We have to
6729 * insert a set-cookie here, except if we want to insert only on POST
6730 * requests and this one isn't. Note that servers which don't have cookies
6731 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732 */
Willy Tarreau58975672014-04-24 21:13:57 +02006733 if (!objt_server(s->target)->cookie) {
6734 chunk_printf(&trash,
6735 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6736 s->be->cookie_name);
6737 }
6738 else {
6739 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
Willy Tarreau58975672014-04-24 21:13:57 +02006741 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6742 /* emit last_date, which is mandatory */
6743 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6744 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6745 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006746
Willy Tarreau58975672014-04-24 21:13:57 +02006747 if (s->be->cookie_maxlife) {
6748 /* emit first_date, which is either the original one or
6749 * the current date.
6750 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006751 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006752 s30tob64(txn->cookie_first_date ?
6753 txn->cookie_first_date >> 2 :
6754 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006755 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006756 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006757 }
Willy Tarreau58975672014-04-24 21:13:57 +02006758 chunk_appendf(&trash, "; path=/");
6759 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006760
Willy Tarreau58975672014-04-24 21:13:57 +02006761 if (s->be->cookie_domain)
6762 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006763
Willy Tarreau58975672014-04-24 21:13:57 +02006764 if (s->be->ck_opts & PR_CK_HTTPONLY)
6765 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006766
Willy Tarreau58975672014-04-24 21:13:57 +02006767 if (s->be->ck_opts & PR_CK_SECURE)
6768 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006769
Willy Tarreau58975672014-04-24 21:13:57 +02006770 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6771 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006772
Willy Tarreau58975672014-04-24 21:13:57 +02006773 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006774 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006775 /* the server did not change, only the date was updated */
6776 txn->flags |= TX_SCK_UPDATED;
6777 else
6778 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006779
Willy Tarreau58975672014-04-24 21:13:57 +02006780 /* Here, we will tell an eventual cache on the client side that we don't
6781 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6782 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6783 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006784 */
Willy Tarreau58975672014-04-24 21:13:57 +02006785 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006786
Willy Tarreau58975672014-04-24 21:13:57 +02006787 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006788
Willy Tarreau58975672014-04-24 21:13:57 +02006789 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6790 "Cache-control: private", 22) < 0))
6791 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006792 }
Willy Tarreau58975672014-04-24 21:13:57 +02006793 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794
Willy Tarreau58975672014-04-24 21:13:57 +02006795 /*
6796 * Check if result will be cacheable with a cookie.
6797 * We'll block the response if security checks have caught
6798 * nasty things such as a cacheable cookie.
6799 */
6800 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6801 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6802 (s->be->options & PR_O_CHK_CACHE)) {
6803 /* we're in presence of a cacheable response containing
6804 * a set-cookie header. We'll block it as requested by
6805 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806 */
Willy Tarreau58975672014-04-24 21:13:57 +02006807 if (objt_server(s->target))
6808 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006809
Willy Tarreau58975672014-04-24 21:13:57 +02006810 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006811 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006812 if (sess->listener->counters)
6813 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814
Willy Tarreau58975672014-04-24 21:13:57 +02006815 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6816 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6817 send_log(s->be, LOG_ALERT,
6818 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6819 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6820 goto return_srv_prx_502;
6821 }
Willy Tarreau03945942009-12-22 16:50:27 +01006822
Willy Tarreau70730dd2014-04-24 18:06:27 +02006823 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006824 /*
6825 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6826 * If an "Upgrade" token is found, the header is left untouched in order
6827 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006828 * if anything but "Upgrade" is present in the Connection header. We don't
6829 * want to touch any 101 response either since it's switching to another
6830 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006831 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006832 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006833 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006834 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006835 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6836 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006837
Willy Tarreau58975672014-04-24 21:13:57 +02006838 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6839 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6840 /* we want a keep-alive response here. Keep-alive header
6841 * required if either side is not 1.1.
6842 */
6843 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6844 want_flags |= TX_CON_KAL_SET;
6845 }
6846 else {
6847 /* we want a close response here. Close header required if
6848 * the server is 1.1, regardless of the client.
6849 */
6850 if (msg->flags & HTTP_MSGF_VER_11)
6851 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006852 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853
Willy Tarreau58975672014-04-24 21:13:57 +02006854 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6855 http_change_connection_header(txn, msg, want_flags);
6856 }
6857
6858 skip_header_mangling:
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006859 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, rep) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006860 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01006861 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006862 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006863 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864
Willy Tarreau58975672014-04-24 21:13:57 +02006865 /* if the user wants to log as soon as possible, without counting
6866 * bytes from the server, then this is the right moment. We have
6867 * to temporarily assign bytes_out to log what we currently have.
6868 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006869 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006870 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6871 s->logs.bytes_out = txn->rsp.eoh;
6872 s->do_log(s);
6873 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006874 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006875 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006876}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877
Willy Tarreaud98cf932009-12-27 22:54:55 +01006878/* This function is an analyser which forwards response body (including chunk
6879 * sizes if any). It is called as soon as we must forward, even if we forward
6880 * zero byte. The only situation where it must not be called is when we're in
6881 * tunnel mode and we want to forward till the close. It's used both to forward
6882 * remaining data and to resync after end of body. It expects the msg_state to
6883 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006884 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006885 *
6886 * It is capable of compressing response data both in content-length mode and
6887 * in chunked mode. The state machines follows different flows depending on
6888 * whether content-length and chunked modes are used, since there are no
6889 * trailers in content-length :
6890 *
6891 * chk-mode cl-mode
6892 * ,----- BODY -----.
6893 * / \
6894 * V size > 0 V chk-mode
6895 * .--> SIZE -------------> DATA -------------> CRLF
6896 * | | size == 0 | last byte |
6897 * | v final crlf v inspected |
6898 * | TRAILERS -----------> DONE |
6899 * | |
6900 * `----------------------------------------------'
6901 *
6902 * Compression only happens in the DATA state, and must be flushed in final
6903 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6904 * is performed at once on final states for all bytes parsed, or when leaving
6905 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006906 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006907int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006908{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006909 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006910 struct http_txn *txn = s->txn;
6911 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006912 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006913
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006914 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6915 return 0;
6916
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006917 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006918 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006919 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006920 /* Output closed while we were sending data. We must abort and
6921 * wake the other side up.
6922 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006923 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006924 msg->msg_state = HTTP_MSG_ERROR;
6925 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006926 return 1;
6927 }
6928
Willy Tarreau4fe41902010-06-07 22:27:41 +02006929 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006930 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006931
Christopher Fauletd7c91962015-04-30 11:48:27 +02006932 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006933 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6934 ? HTTP_MSG_CHUNK_SIZE
6935 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006936 }
6937
Willy Tarreauefdf0942014-04-24 20:08:57 +02006938 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006939 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006940 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006941 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006942 }
6943
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006944 if (msg->msg_state < HTTP_MSG_DONE) {
6945 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6946 ? http_msg_forward_chunked_body(s, msg)
6947 : http_msg_forward_body(s, msg));
6948 if (!ret)
6949 goto missing_data_or_waiting;
6950 if (ret < 0)
6951 goto return_bad_res;
6952 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006953
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006954 /* other states, DONE...TUNNEL */
6955 /* for keep-alive we don't want to forward closes on DONE */
6956 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6957 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6958 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006959
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006960 if (http_resync_states(s)) {
6961 /* some state changes occurred, maybe the analyser was disabled
6962 * too. */
6963 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6964 if (res->flags & CF_SHUTW) {
6965 /* response errors are most likely due to the
6966 * client aborting the transfer. */
6967 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006968 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006969 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006970 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006971 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006972 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006973 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006974 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006975 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006976
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006977 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006978 if (res->flags & CF_SHUTW)
6979 goto aborted_xfer;
6980
6981 /* stop waiting for data if the input is closed before the end. If the
6982 * client side was already closed, it means that the client has aborted,
6983 * so we don't want to count this as a server abort. Otherwise it's a
6984 * server abort.
6985 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006986 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006987 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006988 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006989 /* If we have some pending data, we continue the processing */
6990 if (!buffer_pending(res->buf)) {
6991 if (!(s->flags & SF_ERR_MASK))
6992 s->flags |= SF_ERR_SRVCL;
6993 s->be->be_counters.srv_aborts++;
6994 if (objt_server(s->target))
6995 objt_server(s->target)->counters.srv_aborts++;
6996 goto return_bad_res_stats_ok;
6997 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006998 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006999
Willy Tarreau40dba092010-03-04 18:14:51 +01007000 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007001 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007002 goto return_bad_res;
7003
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007004 /* When TE: chunked is used, we need to get there again to parse
7005 * remaining chunks even if the server has closed, so we don't want to
7006 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
7007 * keep-alive is set on the client side or if there are filters
7008 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007009 */
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007010 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !(msg->flags & HTTP_MSGF_XFER_LEN) ||
7011 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007012 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7013 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007014 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007015
Willy Tarreau5c620922011-05-11 19:56:11 +02007016 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007017 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007018 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007019 * modes are already handled by the stream sock layer. We must not do
7020 * this in content-length mode because it could present the MSG_MORE
7021 * flag with the last block of forwarded data, which would cause an
7022 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007023 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007024 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007025 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007026
Willy Tarreau87b09662015-04-03 00:22:06 +02007027 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007028 return 0;
7029
Willy Tarreau40dba092010-03-04 18:14:51 +01007030 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007031 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007032 if (objt_server(s->target))
7033 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007034
7035 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007036 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007037 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007038 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007039 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007040 res->analysers &= AN_RES_FLT_END;
7041 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007042 if (objt_server(s->target))
7043 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007044
Willy Tarreaue7dff022015-04-03 01:14:29 +02007045 if (!(s->flags & SF_ERR_MASK))
7046 s->flags |= SF_ERR_PRXCOND;
7047 if (!(s->flags & SF_FINST_MASK))
7048 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007049 return 0;
7050
7051 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007052 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007053 txn->rsp.msg_state = HTTP_MSG_ERROR;
7054 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007055 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007056 res->analysers &= AN_RES_FLT_END;
7057 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007058
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007059 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007060 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007061 if (objt_server(s->target))
7062 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007063
Willy Tarreaue7dff022015-04-03 01:14:29 +02007064 if (!(s->flags & SF_ERR_MASK))
7065 s->flags |= SF_ERR_CLICL;
7066 if (!(s->flags & SF_FINST_MASK))
7067 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007068 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007069}
7070
7071
7072static inline int
7073http_msg_forward_body(struct stream *s, struct http_msg *msg)
7074{
7075 struct channel *chn = msg->chn;
7076 int ret;
7077
7078 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7079
7080 if (msg->msg_state == HTTP_MSG_ENDING)
7081 goto ending;
7082
7083 /* Neither content-length, nor transfer-encoding was found, so we must
7084 * read the body until the server connection is closed. In that case, we
7085 * eat data as they come. Of course, this happens for response only. */
7086 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7087 unsigned long long len = (chn->buf->i - msg->next);
7088 msg->chunk_len += len;
7089 msg->body_len += len;
7090 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007091 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7092 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7093 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007094 msg->next += ret;
7095 msg->chunk_len -= ret;
7096 if (msg->chunk_len) {
7097 /* input empty or output full */
7098 if (chn->buf->i > msg->next)
7099 chn->flags |= CF_WAKE_WRITE;
7100 goto missing_data_or_waiting;
7101 }
7102
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007103 /* The server still sending data that should be filtered */
7104 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007105 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007106
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007107 msg->msg_state = HTTP_MSG_ENDING;
7108
7109 ending:
7110 /* we may have some pending data starting at res->buf->p such as a last
7111 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007112 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7113 /* default_ret */ msg->next,
7114 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007115 b_adv(chn->buf, ret);
7116 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007117 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7118 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007119 if (msg->next)
7120 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007121
Christopher Fauletda02e172015-12-04 09:25:05 +01007122 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7123 /* default_ret */ 1,
7124 /* on_error */ goto error,
7125 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007126 msg->msg_state = HTTP_MSG_DONE;
7127 return 1;
7128
7129 missing_data_or_waiting:
7130 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007131 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7132 /* default_ret */ msg->next,
7133 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007134 b_adv(chn->buf, ret);
7135 msg->next -= ret;
7136 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7137 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007138 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007139 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007140 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007141 return 0;
7142 error:
7143 return -1;
7144}
7145
7146static inline int
7147http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7148{
7149 struct channel *chn = msg->chn;
7150 int ret;
7151
7152 /* Here we have the guarantee to be in one of the following state:
7153 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7154 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7155
7156 switch_states:
7157 switch (msg->msg_state) {
7158 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007159 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7160 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7161 /* on_error */ goto error);
7162 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007163 msg->chunk_len -= ret;
7164 if (msg->chunk_len) {
7165 /* input empty or output full */
7166 if (chn->buf->i > msg->next)
7167 chn->flags |= CF_WAKE_WRITE;
7168 goto missing_data_or_waiting;
7169 }
7170
7171 /* nothing left to forward for this chunk*/
7172 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7173 /* fall through for HTTP_MSG_CHUNK_CRLF */
7174
7175 case HTTP_MSG_CHUNK_CRLF:
7176 /* we want the CRLF after the data */
7177 ret = http_skip_chunk_crlf(msg);
7178 if (ret == 0)
7179 goto missing_data_or_waiting;
7180 if (ret < 0)
7181 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007182 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007183 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7184 /* fall through for HTTP_MSG_CHUNK_SIZE */
7185
7186 case HTTP_MSG_CHUNK_SIZE:
7187 /* read the chunk size and assign it to ->chunk_len,
7188 * then set ->next to point to the body and switch to
7189 * DATA or TRAILERS state.
7190 */
7191 ret = http_parse_chunk_size(msg);
7192 if (ret == 0)
7193 goto missing_data_or_waiting;
7194 if (ret < 0)
7195 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007196 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007197 if (msg->chunk_len) {
7198 msg->msg_state = HTTP_MSG_DATA;
7199 goto switch_states;
7200 }
7201 msg->msg_state = HTTP_MSG_TRAILERS;
7202 /* fall through for HTTP_MSG_TRAILERS */
7203
7204 case HTTP_MSG_TRAILERS:
7205 ret = http_forward_trailers(msg);
7206 if (ret < 0)
7207 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007208 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7209 /* default_ret */ 1,
7210 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007211 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007212 if (!ret)
7213 goto missing_data_or_waiting;
7214 break;
7215
7216 case HTTP_MSG_ENDING:
7217 goto ending;
7218
7219 default:
7220 /* This should no happen in this function */
7221 goto error;
7222 }
7223
7224 msg->msg_state = HTTP_MSG_ENDING;
7225 ending:
7226 /* we may have some pending data starting at res->buf->p such as a last
7227 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007228 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007229 /* default_ret */ msg->next,
7230 /* on_error */ goto error);
7231 b_adv(chn->buf, ret);
7232 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007233 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7234 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007235 if (msg->next)
7236 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007237
Christopher Fauletda02e172015-12-04 09:25:05 +01007238 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007239 /* default_ret */ 1,
7240 /* on_error */ goto error,
7241 /* on_wait */ goto waiting);
7242 msg->msg_state = HTTP_MSG_DONE;
7243 return 1;
7244
7245 missing_data_or_waiting:
7246 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007247 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007248 /* default_ret */ msg->next,
7249 /* on_error */ goto error);
7250 b_adv(chn->buf, ret);
7251 msg->next -= ret;
7252 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7253 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007254 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007255 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007256 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007257 return 0;
7258
7259 chunk_parsing_error:
7260 if (msg->err_pos >= 0) {
7261 if (chn->flags & CF_ISRESP)
7262 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7263 msg->msg_state, strm_fe(s));
7264 else
7265 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7266 msg, msg->msg_state, s->be);
7267 }
7268 error:
7269 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007270}
7271
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273/* Iterate the same filter through all request headers.
7274 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 * Since it can manage the switch to another backend, it updates the per-proxy
7276 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007277 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007278int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 char *cur_ptr, *cur_end, *cur_next;
7281 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007282 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007284 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 last_hdr = 0;
7287
Willy Tarreau9b28e032012-10-12 23:49:43 +02007288 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 old_idx = 0;
7290
7291 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007292 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007294 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007295 (exp->action == ACT_ALLOW ||
7296 exp->action == ACT_DENY ||
7297 exp->action == ACT_TARPIT))
7298 return 0;
7299
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007300 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007301 if (!cur_idx)
7302 break;
7303
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007304 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 cur_ptr = cur_next;
7306 cur_end = cur_ptr + cur_hdr->len;
7307 cur_next = cur_end + cur_hdr->cr + 1;
7308
7309 /* Now we have one header between cur_ptr and cur_end,
7310 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311 */
7312
Willy Tarreau15a53a42015-01-21 13:39:42 +01007313 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007316 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007317 last_hdr = 1;
7318 break;
7319
7320 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007321 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 break;
7324
7325 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007326 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007328 break;
7329
7330 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007331 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7332 if (trash.len < 0)
7333 return -1;
7334
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007335 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 /* FIXME: if the user adds a newline in the replacement, the
7337 * index will not be recalculated for now, and the new line
7338 * will not be counted as a new header.
7339 */
7340
7341 cur_end += delta;
7342 cur_next += delta;
7343 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007344 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 break;
7346
7347 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007348 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007349 cur_next += delta;
7350
Willy Tarreaufa355d42009-11-29 18:12:29 +01007351 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007352 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7353 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007354 cur_hdr->len = 0;
7355 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007356 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007357 break;
7358
7359 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360 }
7361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 /* keep the link from this header to next one in case of later
7363 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007364 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 old_idx = cur_idx;
7366 }
7367 return 0;
7368}
7369
7370
7371/* Apply the filter to the request line.
7372 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7373 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007374 * Since it can manage the switch to another backend, it updates the per-proxy
7375 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007377int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007379 char *cur_ptr, *cur_end;
7380 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007381 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007382 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007383
Willy Tarreau3d300592007-03-18 18:34:41 +01007384 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007386 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 (exp->action == ACT_ALLOW ||
7388 exp->action == ACT_DENY ||
7389 exp->action == ACT_TARPIT))
7390 return 0;
7391 else if (exp->action == ACT_REMOVE)
7392 return 0;
7393
7394 done = 0;
7395
Willy Tarreau9b28e032012-10-12 23:49:43 +02007396 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007397 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007398
7399 /* Now we have the request line between cur_ptr and cur_end */
7400
Willy Tarreau15a53a42015-01-21 13:39:42 +01007401 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007402 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007404 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007405 done = 1;
7406 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007408 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007409 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007410 done = 1;
7411 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007413 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007414 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415 done = 1;
7416 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007418 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007419 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7420 if (trash.len < 0)
7421 return -1;
7422
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007423 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 /* FIXME: if the user adds a newline in the replacement, the
7425 * index will not be recalculated for now, and the new line
7426 * will not be counted as a new header.
7427 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007428
Willy Tarreaufa355d42009-11-29 18:12:29 +01007429 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007430 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007431 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007432 HTTP_MSG_RQMETH,
7433 cur_ptr, cur_end + 1,
7434 NULL, NULL);
7435 if (unlikely(!cur_end))
7436 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007438 /* we have a full request and we know that we have either a CR
7439 * or an LF at <ptr>.
7440 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007441 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7442 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007443 /* there is no point trying this regex on headers */
7444 return 1;
7445 }
7446 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007447 return done;
7448}
Willy Tarreau97de6242006-12-27 17:18:38 +01007449
Willy Tarreau58f10d72006-12-04 02:26:12 +01007450
Willy Tarreau58f10d72006-12-04 02:26:12 +01007451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007452/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007453 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007454 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455 * unparsable request. Since it can manage the switch to another backend, it
7456 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007457 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007458int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007459{
Willy Tarreau192252e2015-04-04 01:47:55 +02007460 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007461 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007462 struct hdr_exp *exp;
7463
7464 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007467 /*
7468 * The interleaving of transformations and verdicts
7469 * makes it difficult to decide to continue or stop
7470 * the evaluation.
7471 */
7472
Willy Tarreau6c123b12010-01-28 20:22:06 +01007473 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7474 break;
7475
Willy Tarreau3d300592007-03-18 18:34:41 +01007476 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007477 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007478 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007479 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007480
7481 /* if this filter had a condition, evaluate it now and skip to
7482 * next filter if the condition does not match.
7483 */
7484 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007485 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007486 ret = acl_pass(ret);
7487 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7488 ret = !ret;
7489
7490 if (!ret)
7491 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007492 }
7493
7494 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007495 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007496 if (unlikely(ret < 0))
7497 return -1;
7498
7499 if (likely(ret == 0)) {
7500 /* The filter did not match the request, it can be
7501 * iterated through all headers.
7502 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007503 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7504 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007505 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007507 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007508}
7509
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007510
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007511/* Find the end of a cookie value contained between <s> and <e>. It works the
7512 * same way as with headers above except that the semi-colon also ends a token.
7513 * See RFC2965 for more information. Note that it requires a valid header to
7514 * return a valid result.
7515 */
7516char *find_cookie_value_end(char *s, const char *e)
7517{
7518 int quoted, qdpair;
7519
7520 quoted = qdpair = 0;
7521 for (; s < e; s++) {
7522 if (qdpair) qdpair = 0;
7523 else if (quoted) {
7524 if (*s == '\\') qdpair = 1;
7525 else if (*s == '"') quoted = 0;
7526 }
7527 else if (*s == '"') quoted = 1;
7528 else if (*s == ',' || *s == ';') return s;
7529 }
7530 return s;
7531}
7532
7533/* Delete a value in a header between delimiters <from> and <next> in buffer
7534 * <buf>. The number of characters displaced is returned, and the pointer to
7535 * the first delimiter is updated if required. The function tries as much as
7536 * possible to respect the following principles :
7537 * - replace <from> delimiter by the <next> one unless <from> points to a
7538 * colon, in which case <next> is simply removed
7539 * - set exactly one space character after the new first delimiter, unless
7540 * there are not enough characters in the block being moved to do so.
7541 * - remove unneeded spaces before the previous delimiter and after the new
7542 * one.
7543 *
7544 * It is the caller's responsibility to ensure that :
7545 * - <from> points to a valid delimiter or the colon ;
7546 * - <next> points to a valid delimiter or the final CR/LF ;
7547 * - there are non-space chars before <from> ;
7548 * - there is a CR/LF at or after <next>.
7549 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007550int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551{
7552 char *prev = *from;
7553
7554 if (*prev == ':') {
7555 /* We're removing the first value, preserve the colon and add a
7556 * space if possible.
7557 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007558 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007559 next++;
7560 prev++;
7561 if (prev < next)
7562 *prev++ = ' ';
7563
Willy Tarreau2235b262016-11-05 15:50:20 +01007564 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007565 next++;
7566 } else {
7567 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007568 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007569 prev--;
7570 *from = prev;
7571
7572 /* copy the delimiter and if possible a space if we're
7573 * not at the end of the line.
7574 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007575 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 *prev++ = *next++;
7577 if (prev + 1 < next)
7578 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007579 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 next++;
7581 }
7582 }
7583 return buffer_replace2(buf, prev, next, NULL, 0);
7584}
7585
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007586/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007587 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 * desirable to call it only when needed. This code is quite complex because
7589 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7590 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007592void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007593{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007594 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007595 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007597 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7599 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007600
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007602 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007603 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007605 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007606 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007607 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007609 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007610 hdr_beg = hdr_next;
7611 hdr_end = hdr_beg + cur_hdr->len;
7612 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 /* We have one full header between hdr_beg and hdr_end, and the
7615 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 * "Cookie:" headers.
7617 */
7618
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007619 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007620 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007621 old_idx = cur_idx;
7622 continue;
7623 }
7624
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007625 del_from = NULL; /* nothing to be deleted */
7626 preserve_hdr = 0; /* assume we may kill the whole header */
7627
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628 /* Now look for cookies. Conforming to RFC2109, we have to support
7629 * attributes whose name begin with a '$', and associate them with
7630 * the right cookie, if we want to delete this cookie.
7631 * So there are 3 cases for each cookie read :
7632 * 1) it's a special attribute, beginning with a '$' : ignore it.
7633 * 2) it's a server id cookie that we *MAY* want to delete : save
7634 * some pointers on it (last semi-colon, beginning of cookie...)
7635 * 3) it's an application cookie : we *MAY* have to delete a previous
7636 * "special" cookie.
7637 * At the end of loop, if a "special" cookie remains, we may have to
7638 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 * *MUST* delete it.
7640 *
7641 * Note: RFC2965 is unclear about the processing of spaces around
7642 * the equal sign in the ATTR=VALUE form. A careful inspection of
7643 * the RFC explicitly allows spaces before it, and not within the
7644 * tokens (attrs or values). An inspection of RFC2109 allows that
7645 * too but section 10.1.3 lets one think that spaces may be allowed
7646 * after the equal sign too, resulting in some (rare) buggy
7647 * implementations trying to do that. So let's do what servers do.
7648 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7649 * allowed quoted strings in values, with any possible character
7650 * after a backslash, including control chars and delimitors, which
7651 * causes parsing to become ambiguous. Browsers also allow spaces
7652 * within values even without quotes.
7653 *
7654 * We have to keep multiple pointers in order to support cookie
7655 * removal at the beginning, middle or end of header without
7656 * corrupting the header. All of these headers are valid :
7657 *
7658 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7659 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7660 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7661 * | | | | | | | | |
7662 * | | | | | | | | hdr_end <--+
7663 * | | | | | | | +--> next
7664 * | | | | | | +----> val_end
7665 * | | | | | +-----------> val_beg
7666 * | | | | +--------------> equal
7667 * | | | +----------------> att_end
7668 * | | +---------------------> att_beg
7669 * | +--------------------------> prev
7670 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007671 */
7672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7674 /* Iterate through all cookies on this line */
7675
7676 /* find att_beg */
7677 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007678 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 att_beg++;
7680
7681 /* find att_end : this is the first character after the last non
7682 * space before the equal. It may be equal to hdr_end.
7683 */
7684 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 while (equal < hdr_end) {
7687 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007688 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007689 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 continue;
7691 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692 }
7693
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7695 * is between <att_beg> and <equal>, both may be identical.
7696 */
7697
7698 /* look for end of cookie if there is an equal sign */
7699 if (equal < hdr_end && *equal == '=') {
7700 /* look for the beginning of the value */
7701 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007702 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 val_beg++;
7704
7705 /* find the end of the value, respecting quotes */
7706 next = find_cookie_value_end(val_beg, hdr_end);
7707
7708 /* make val_end point to the first white space or delimitor after the value */
7709 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007710 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 val_end--;
7712 } else {
7713 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007714 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 /* We have nothing to do with attributes beginning with '$'. However,
7717 * they will automatically be removed if a header before them is removed,
7718 * since they're supposed to be linked together.
7719 */
7720 if (*att_beg == '$')
7721 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 /* Ignore cookies with no equal sign */
7724 if (equal == next) {
7725 /* This is not our cookie, so we must preserve it. But if we already
7726 * scheduled another cookie for removal, we cannot remove the
7727 * complete header, but we can remove the previous block itself.
7728 */
7729 preserve_hdr = 1;
7730 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007731 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 val_end += delta;
7733 next += delta;
7734 hdr_end += delta;
7735 hdr_next += delta;
7736 cur_hdr->len += delta;
7737 http_msg_move_end(&txn->req, delta);
7738 prev = del_from;
7739 del_from = NULL;
7740 }
7741 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007742 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007743
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007744 /* if there are spaces around the equal sign, we need to
7745 * strip them otherwise we'll get trouble for cookie captures,
7746 * or even for rewrites. Since this happens extremely rarely,
7747 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007748 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7750 int stripped_before = 0;
7751 int stripped_after = 0;
7752
7753 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007754 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 equal += stripped_before;
7756 val_beg += stripped_before;
7757 }
7758
7759 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007760 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007761 val_beg += stripped_after;
7762 stripped_before += stripped_after;
7763 }
7764
7765 val_end += stripped_before;
7766 next += stripped_before;
7767 hdr_end += stripped_before;
7768 hdr_next += stripped_before;
7769 cur_hdr->len += stripped_before;
7770 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007771 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007773
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007774 /* First, let's see if we want to capture this cookie. We check
7775 * that we don't already have a client side cookie, because we
7776 * can only capture one. Also as an optimisation, we ignore
7777 * cookies shorter than the declared name.
7778 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007779 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7780 (val_end - att_beg >= sess->fe->capture_namelen) &&
7781 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 int log_len = val_end - att_beg;
7783
7784 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7785 Alert("HTTP logging : out of memory.\n");
7786 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007787 if (log_len > sess->fe->capture_len)
7788 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007789 memcpy(txn->cli_cookie, att_beg, log_len);
7790 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007791 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007793
Willy Tarreaubca99692010-10-06 19:25:55 +02007794 /* Persistence cookies in passive, rewrite or insert mode have the
7795 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007796 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007797 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007798 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007799 * For cookies in prefix mode, the form is :
7800 *
7801 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007802 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007803 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7804 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7805 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007806 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007807
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7809 * have the server ID between val_beg and delim, and the original cookie between
7810 * delim+1 and val_end. Otherwise, delim==val_end :
7811 *
7812 * Cookie: NAME=SRV; # in all but prefix modes
7813 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7814 * | || || | |+-> next
7815 * | || || | +--> val_end
7816 * | || || +---------> delim
7817 * | || |+------------> val_beg
7818 * | || +-------------> att_end = equal
7819 * | |+-----------------> att_beg
7820 * | +------------------> prev
7821 * +-------------------------> hdr_beg
7822 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007823
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007824 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007825 for (delim = val_beg; delim < val_end; delim++)
7826 if (*delim == COOKIE_DELIM)
7827 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007828 } else {
7829 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007830 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007831 /* Now check if the cookie contains a date field, which would
7832 * appear after a vertical bar ('|') just after the server name
7833 * and before the delimiter.
7834 */
7835 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7836 if (vbar1) {
7837 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007838 * right is the last seen date. It is a base64 encoded
7839 * 30-bit value representing the UNIX date since the
7840 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007841 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007842 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007843 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007844 if (val_end - vbar1 >= 5) {
7845 val = b64tos30(vbar1);
7846 if (val > 0)
7847 txn->cookie_last_date = val << 2;
7848 }
7849 /* look for a second vertical bar */
7850 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7851 if (vbar1 && (val_end - vbar1 > 5)) {
7852 val = b64tos30(vbar1 + 1);
7853 if (val > 0)
7854 txn->cookie_first_date = val << 2;
7855 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007856 }
7857 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007858
Willy Tarreauf64d1412010-10-07 20:06:11 +02007859 /* if the cookie has an expiration date and the proxy wants to check
7860 * it, then we do that now. We first check if the cookie is too old,
7861 * then only if it has expired. We detect strict overflow because the
7862 * time resolution here is not great (4 seconds). Cookies with dates
7863 * in the future are ignored if their offset is beyond one day. This
7864 * allows an admin to fix timezone issues without expiring everyone
7865 * and at the same time avoids keeping unwanted side effects for too
7866 * long.
7867 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007868 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7869 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007870 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007871 txn->flags &= ~TX_CK_MASK;
7872 txn->flags |= TX_CK_OLD;
7873 delim = val_beg; // let's pretend we have not found the cookie
7874 txn->cookie_first_date = 0;
7875 txn->cookie_last_date = 0;
7876 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007877 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7878 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007879 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007880 txn->flags &= ~TX_CK_MASK;
7881 txn->flags |= TX_CK_EXPIRED;
7882 delim = val_beg; // let's pretend we have not found the cookie
7883 txn->cookie_first_date = 0;
7884 txn->cookie_last_date = 0;
7885 }
7886
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007887 /* Here, we'll look for the first running server which supports the cookie.
7888 * This allows to share a same cookie between several servers, for example
7889 * to dedicate backup servers to specific servers only.
7890 * However, to prevent clients from sticking to cookie-less backup server
7891 * when they have incidentely learned an empty cookie, we simply ignore
7892 * empty cookies and mark them as invalid.
7893 * The same behaviour is applied when persistence must be ignored.
7894 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007895 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007896 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007898 while (srv) {
7899 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7900 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007901 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007902 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007903 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 /* we found the server and we can use it */
7905 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007906 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007907 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007908 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 break;
7910 } else {
7911 /* we found a server, but it's down,
7912 * mark it as such and go on in case
7913 * another one is available.
7914 */
7915 txn->flags &= ~TX_CK_MASK;
7916 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007917 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007919 srv = srv->next;
7920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921
Willy Tarreauf64d1412010-10-07 20:06:11 +02007922 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007923 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007925 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007926 txn->flags |= TX_CK_UNUSED;
7927 else
7928 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007929 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007931 /* depending on the cookie mode, we may have to either :
7932 * - delete the complete cookie if we're in insert+indirect mode, so that
7933 * the server never sees it ;
7934 * - remove the server id from the cookie value, and tag the cookie as an
7935 * application cookie so that it does not get accidentely removed later,
7936 * if we're in cookie prefix mode
7937 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007938 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940
Willy Tarreau9b28e032012-10-12 23:49:43 +02007941 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 val_end += delta;
7943 next += delta;
7944 hdr_end += delta;
7945 hdr_next += delta;
7946 cur_hdr->len += delta;
7947 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007949 del_from = NULL;
7950 preserve_hdr = 1; /* we want to keep this cookie */
7951 }
7952 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007953 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007954 del_from = prev;
7955 }
7956 } else {
7957 /* This is not our cookie, so we must preserve it. But if we already
7958 * scheduled another cookie for removal, we cannot remove the
7959 * complete header, but we can remove the previous block itself.
7960 */
7961 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007962
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007963 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007964 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007965 if (att_beg >= del_from)
7966 att_beg += delta;
7967 if (att_end >= del_from)
7968 att_end += delta;
7969 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 val_end += delta;
7971 next += delta;
7972 hdr_end += delta;
7973 hdr_next += delta;
7974 cur_hdr->len += delta;
7975 http_msg_move_end(&txn->req, delta);
7976 prev = del_from;
7977 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007978 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007979 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007980
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007981 /* continue with next cookie on this header line */
7982 att_beg = next;
7983 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007984
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007985 /* There are no more cookies on this line.
7986 * We may still have one (or several) marked for deletion at the
7987 * end of the line. We must do this now in two ways :
7988 * - if some cookies must be preserved, we only delete from the
7989 * mark to the end of line ;
7990 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007991 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007992 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007993 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007994 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007995 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007996 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 cur_hdr->len += delta;
7998 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008000
8001 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01008002 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8003 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008004 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008005 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008006 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008007 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008008 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008009 }
8010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008011 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01008012 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008013 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008014}
8015
8016
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017/* Iterate the same filter through all response headers contained in <rtr>.
8018 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8019 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008020int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 char *cur_ptr, *cur_end, *cur_next;
8023 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008024 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008026 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027
8028 last_hdr = 0;
8029
Willy Tarreau9b28e032012-10-12 23:49:43 +02008030 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031 old_idx = 0;
8032
8033 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008034 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008036 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 (exp->action == ACT_ALLOW ||
8038 exp->action == ACT_DENY))
8039 return 0;
8040
8041 cur_idx = txn->hdr_idx.v[old_idx].next;
8042 if (!cur_idx)
8043 break;
8044
8045 cur_hdr = &txn->hdr_idx.v[cur_idx];
8046 cur_ptr = cur_next;
8047 cur_end = cur_ptr + cur_hdr->len;
8048 cur_next = cur_end + cur_hdr->cr + 1;
8049
8050 /* Now we have one header between cur_ptr and cur_end,
8051 * and the next header starts at cur_next.
8052 */
8053
Willy Tarreau15a53a42015-01-21 13:39:42 +01008054 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 switch (exp->action) {
8056 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008057 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058 last_hdr = 1;
8059 break;
8060
8061 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008062 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 last_hdr = 1;
8064 break;
8065
8066 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008067 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8068 if (trash.len < 0)
8069 return -1;
8070
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008071 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072 /* FIXME: if the user adds a newline in the replacement, the
8073 * index will not be recalculated for now, and the new line
8074 * will not be counted as a new header.
8075 */
8076
8077 cur_end += delta;
8078 cur_next += delta;
8079 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008080 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081 break;
8082
8083 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008084 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 cur_next += delta;
8086
Willy Tarreaufa355d42009-11-29 18:12:29 +01008087 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8089 txn->hdr_idx.used--;
8090 cur_hdr->len = 0;
8091 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008092 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 break;
8094
8095 }
8096 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097
8098 /* keep the link from this header to next one in case of later
8099 * removal of next header.
8100 */
8101 old_idx = cur_idx;
8102 }
8103 return 0;
8104}
8105
8106
8107/* Apply the filter to the status line in the response buffer <rtr>.
8108 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8109 * or -1 if a replacement resulted in an invalid status line.
8110 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008111int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 char *cur_ptr, *cur_end;
8114 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008115 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008116 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117
8118
Willy Tarreau3d300592007-03-18 18:34:41 +01008119 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008121 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122 (exp->action == ACT_ALLOW ||
8123 exp->action == ACT_DENY))
8124 return 0;
8125 else if (exp->action == ACT_REMOVE)
8126 return 0;
8127
8128 done = 0;
8129
Willy Tarreau9b28e032012-10-12 23:49:43 +02008130 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008131 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132
8133 /* Now we have the status line between cur_ptr and cur_end */
8134
Willy Tarreau15a53a42015-01-21 13:39:42 +01008135 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136 switch (exp->action) {
8137 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008138 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 done = 1;
8140 break;
8141
8142 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008143 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 done = 1;
8145 break;
8146
8147 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008148 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8149 if (trash.len < 0)
8150 return -1;
8151
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008152 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153 /* FIXME: if the user adds a newline in the replacement, the
8154 * index will not be recalculated for now, and the new line
8155 * will not be counted as a new header.
8156 */
8157
Willy Tarreaufa355d42009-11-29 18:12:29 +01008158 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008160 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008161 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 cur_ptr, cur_end + 1,
8163 NULL, NULL);
8164 if (unlikely(!cur_end))
8165 return -1;
8166
8167 /* we have a full respnse and we know that we have either a CR
8168 * or an LF at <ptr>.
8169 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008170 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008171 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172 /* there is no point trying this regex on headers */
8173 return 1;
8174 }
8175 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176 return done;
8177}
8178
8179
8180
8181/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008182 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008183 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8184 * unparsable response.
8185 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008186int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008187{
Willy Tarreau192252e2015-04-04 01:47:55 +02008188 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008189 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008190 struct hdr_exp *exp;
8191
8192 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193 int ret;
8194
8195 /*
8196 * The interleaving of transformations and verdicts
8197 * makes it difficult to decide to continue or stop
8198 * the evaluation.
8199 */
8200
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008201 if (txn->flags & TX_SVDENY)
8202 break;
8203
Willy Tarreau3d300592007-03-18 18:34:41 +01008204 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8206 exp->action == ACT_PASS)) {
8207 exp = exp->next;
8208 continue;
8209 }
8210
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008211 /* if this filter had a condition, evaluate it now and skip to
8212 * next filter if the condition does not match.
8213 */
8214 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008215 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008216 ret = acl_pass(ret);
8217 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8218 ret = !ret;
8219 if (!ret)
8220 continue;
8221 }
8222
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008224 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 if (unlikely(ret < 0))
8226 return -1;
8227
8228 if (likely(ret == 0)) {
8229 /* The filter did not match the response, it can be
8230 * iterated through all headers.
8231 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008232 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8233 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008235 }
8236 return 0;
8237}
8238
8239
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008241 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008242 * desirable to call it only when needed. This function is also used when we
8243 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008245void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008247 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008248 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008249 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008252 char *hdr_beg, *hdr_end, *hdr_next;
8253 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 /* Iterate through the headers.
8256 * we start with the start line.
8257 */
8258 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008259 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260
8261 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8262 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008263 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264
8265 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 hdr_beg = hdr_next;
8267 hdr_end = hdr_beg + cur_hdr->len;
8268 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008269
Willy Tarreau24581ba2010-08-31 22:39:35 +02008270 /* We have one full header between hdr_beg and hdr_end, and the
8271 * next header starts at hdr_next. We're only interested in
8272 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273 */
8274
Willy Tarreau24581ba2010-08-31 22:39:35 +02008275 is_cookie2 = 0;
8276 prev = hdr_beg + 10;
8277 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008278 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008279 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8280 if (!val) {
8281 old_idx = cur_idx;
8282 continue;
8283 }
8284 is_cookie2 = 1;
8285 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008286 }
8287
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8289 * <prev> points to the colon.
8290 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008291 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8294 * check-cache is enabled) and we are not interested in checking
8295 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008296 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008297 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008298 return;
8299
Willy Tarreau24581ba2010-08-31 22:39:35 +02008300 /* OK so now we know we have to process this response cookie.
8301 * The format of the Set-Cookie header is slightly different
8302 * from the format of the Cookie header in that it does not
8303 * support the comma as a cookie delimiter (thus the header
8304 * cannot be folded) because the Expires attribute described in
8305 * the original Netscape's spec may contain an unquoted date
8306 * with a comma inside. We have to live with this because
8307 * many browsers don't support Max-Age and some browsers don't
8308 * support quoted strings. However the Set-Cookie2 header is
8309 * clean.
8310 *
8311 * We have to keep multiple pointers in order to support cookie
8312 * removal at the beginning, middle or end of header without
8313 * corrupting the header (in case of set-cookie2). A special
8314 * pointer, <scav> points to the beginning of the set-cookie-av
8315 * fields after the first semi-colon. The <next> pointer points
8316 * either to the end of line (set-cookie) or next unquoted comma
8317 * (set-cookie2). All of these headers are valid :
8318 *
8319 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8320 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8321 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8322 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8323 * | | | | | | | | | |
8324 * | | | | | | | | +-> next hdr_end <--+
8325 * | | | | | | | +------------> scav
8326 * | | | | | | +--------------> val_end
8327 * | | | | | +--------------------> val_beg
8328 * | | | | +----------------------> equal
8329 * | | | +------------------------> att_end
8330 * | | +----------------------------> att_beg
8331 * | +------------------------------> prev
8332 * +-----------------------------------------> hdr_beg
8333 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008334
Willy Tarreau24581ba2010-08-31 22:39:35 +02008335 for (; prev < hdr_end; prev = next) {
8336 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008337
Willy Tarreau24581ba2010-08-31 22:39:35 +02008338 /* find att_beg */
8339 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008340 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008341 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008342
Willy Tarreau24581ba2010-08-31 22:39:35 +02008343 /* find att_end : this is the first character after the last non
8344 * space before the equal. It may be equal to hdr_end.
8345 */
8346 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008347
Willy Tarreau24581ba2010-08-31 22:39:35 +02008348 while (equal < hdr_end) {
8349 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8350 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008351 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 continue;
8353 att_end = equal;
8354 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008355
Willy Tarreau24581ba2010-08-31 22:39:35 +02008356 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8357 * is between <att_beg> and <equal>, both may be identical.
8358 */
8359
8360 /* look for end of cookie if there is an equal sign */
8361 if (equal < hdr_end && *equal == '=') {
8362 /* look for the beginning of the value */
8363 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008364 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366
Willy Tarreau24581ba2010-08-31 22:39:35 +02008367 /* find the end of the value, respecting quotes */
8368 next = find_cookie_value_end(val_beg, hdr_end);
8369
8370 /* make val_end point to the first white space or delimitor after the value */
8371 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008372 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008373 val_end--;
8374 } else {
8375 /* <equal> points to next comma, semi-colon or EOL */
8376 val_beg = val_end = next = equal;
8377 }
8378
8379 if (next < hdr_end) {
8380 /* Set-Cookie2 supports multiple cookies, and <next> points to
8381 * a colon or semi-colon before the end. So skip all attr-value
8382 * pairs and look for the next comma. For Set-Cookie, since
8383 * commas are permitted in values, skip to the end.
8384 */
8385 if (is_cookie2)
8386 next = find_hdr_value_end(next, hdr_end);
8387 else
8388 next = hdr_end;
8389 }
8390
8391 /* Now everything is as on the diagram above */
8392
8393 /* Ignore cookies with no equal sign */
8394 if (equal == val_end)
8395 continue;
8396
8397 /* If there are spaces around the equal sign, we need to
8398 * strip them otherwise we'll get trouble for cookie captures,
8399 * or even for rewrites. Since this happens extremely rarely,
8400 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008401 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008402 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8403 int stripped_before = 0;
8404 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405
Willy Tarreau24581ba2010-08-31 22:39:35 +02008406 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008407 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008408 equal += stripped_before;
8409 val_beg += stripped_before;
8410 }
8411
8412 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008413 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008414 val_beg += stripped_after;
8415 stripped_before += stripped_after;
8416 }
8417
8418 val_end += stripped_before;
8419 next += stripped_before;
8420 hdr_end += stripped_before;
8421 hdr_next += stripped_before;
8422 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008423 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008424 }
8425
8426 /* First, let's see if we want to capture this cookie. We check
8427 * that we don't already have a server side cookie, because we
8428 * can only capture one. Also as an optimisation, we ignore
8429 * cookies shorter than the declared name.
8430 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008431 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008432 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008433 (val_end - att_beg >= sess->fe->capture_namelen) &&
8434 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008435 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008436 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437 Alert("HTTP logging : out of memory.\n");
8438 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008439 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008440 if (log_len > sess->fe->capture_len)
8441 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008442 memcpy(txn->srv_cookie, att_beg, log_len);
8443 txn->srv_cookie[log_len] = 0;
8444 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008445 }
8446
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008447 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008448 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008449 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008450 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8451 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008452 /* assume passive cookie by default */
8453 txn->flags &= ~TX_SCK_MASK;
8454 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008455
8456 /* If the cookie is in insert mode on a known server, we'll delete
8457 * this occurrence because we'll insert another one later.
8458 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008459 * a direct access.
8460 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008461 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008462 /* The "preserve" flag was set, we don't want to touch the
8463 * server's cookie.
8464 */
8465 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008466 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008467 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 /* this cookie must be deleted */
8469 if (*prev == ':' && next == hdr_end) {
8470 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008471 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008472 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8473 txn->hdr_idx.used--;
8474 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008475 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008476 hdr_next += delta;
8477 http_msg_move_end(&txn->rsp, delta);
8478 /* note: while both invalid now, <next> and <hdr_end>
8479 * are still equal, so the for() will stop as expected.
8480 */
8481 } else {
8482 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008483 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008484 next = prev;
8485 hdr_end += delta;
8486 hdr_next += delta;
8487 cur_hdr->len += delta;
8488 http_msg_move_end(&txn->rsp, delta);
8489 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008490 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008491 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008492 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008494 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008495 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008496 * with this server since we know it.
8497 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008498 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008499 next += delta;
8500 hdr_end += delta;
8501 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008502 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008503 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504
Willy Tarreauf1348312010-10-07 15:54:11 +02008505 txn->flags &= ~TX_SCK_MASK;
8506 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008508 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008510 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008511 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008512 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008513 next += delta;
8514 hdr_end += delta;
8515 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008517 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008518
Willy Tarreau827aee92011-03-10 16:55:02 +01008519 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008520 txn->flags &= ~TX_SCK_MASK;
8521 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522 }
8523 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008524 /* that's done for this cookie, check the next one on the same
8525 * line when next != hdr_end (only if is_cookie2).
8526 */
8527 }
8528 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008529 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008530 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531}
8532
8533
Willy Tarreaua15645d2007-03-18 16:22:39 +01008534/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008535 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008536 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008537void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008539 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008540 char *p1, *p2;
8541
8542 char *cur_ptr, *cur_end, *cur_next;
8543 int cur_idx;
8544
Willy Tarreau5df51872007-11-25 16:20:08 +01008545 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008546 return;
8547
8548 /* Iterate through the headers.
8549 * we start with the start line.
8550 */
8551 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008552 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008553
8554 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8555 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008556 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008557
8558 cur_hdr = &txn->hdr_idx.v[cur_idx];
8559 cur_ptr = cur_next;
8560 cur_end = cur_ptr + cur_hdr->len;
8561 cur_next = cur_end + cur_hdr->cr + 1;
8562
8563 /* We have one full header between cur_ptr and cur_end, and the
8564 * next header starts at cur_next. We're only interested in
8565 * "Cookie:" headers.
8566 */
8567
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008568 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8569 if (val) {
8570 if ((cur_end - (cur_ptr + val) >= 8) &&
8571 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8572 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8573 return;
8574 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008575 }
8576
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008577 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8578 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008579 continue;
8580
8581 /* OK, right now we know we have a cache-control header at cur_ptr */
8582
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008583 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008584
8585 if (p1 >= cur_end) /* no more info */
8586 continue;
8587
8588 /* p1 is at the beginning of the value */
8589 p2 = p1;
8590
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008591 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008592 p2++;
8593
8594 /* we have a complete value between p1 and p2 */
8595 if (p2 < cur_end && *p2 == '=') {
8596 /* we have something of the form no-cache="set-cookie" */
8597 if ((cur_end - p1 >= 21) &&
8598 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8599 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008600 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008601 continue;
8602 }
8603
8604 /* OK, so we know that either p2 points to the end of string or to a comma */
8605 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008606 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008607 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8608 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8609 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008610 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008611 return;
8612 }
8613
8614 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008615 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008616 continue;
8617 }
8618 }
8619}
8620
Willy Tarreau58f10d72006-12-04 02:26:12 +01008621
Willy Tarreaub2513902006-12-17 14:52:38 +01008622/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008623 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008624 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008625 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008626 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008627 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008628 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008629 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008630 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008631int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008632{
8633 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008634 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008635 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008636
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008637 if (!uri_auth)
8638 return 0;
8639
Cyril Bonté70be45d2010-10-12 00:14:35 +02008640 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008641 return 0;
8642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008643 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008644 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008645 return 0;
8646
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008647 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008648 return 0;
8649
Willy Tarreaub2513902006-12-17 14:52:38 +01008650 return 1;
8651}
8652
Willy Tarreau4076a152009-04-02 15:18:36 +02008653/*
8654 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008655 * By default it tries to report the error position as msg->err_pos. However if
8656 * this one is not set, it will then report msg->next, which is the last known
8657 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008658 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008659 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008660void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008661 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008662 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008663{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008664 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008665 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008666 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008667
Willy Tarreauf3764b72016-03-31 13:45:10 +02008668 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008669 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008670 len1 = MIN(len1, es->len);
8671 len2 = es->len - len1; /* remaining data if buffer wraps */
8672
Willy Tarreauf3764b72016-03-31 13:45:10 +02008673 if (!es->buf)
8674 es->buf = malloc(global.tune.bufsize);
8675
8676 if (es->buf) {
8677 memcpy(es->buf, chn->buf->p, len1);
8678 if (len2)
8679 memcpy(es->buf + len1, chn->buf->data, len2);
8680 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008681
Willy Tarreau4076a152009-04-02 15:18:36 +02008682 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008683 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008684 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008685 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008686
Willy Tarreau4076a152009-04-02 15:18:36 +02008687 es->when = date; // user-visible date
8688 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008689 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008690 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008691 if (objt_conn(sess->origin))
8692 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008693 else
8694 memset(&es->src, 0, sizeof(es->src));
8695
Willy Tarreau078272e2010-12-12 12:46:33 +01008696 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008697 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008698 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008699 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008700 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008701 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008702 es->b_out = chn->buf->o;
8703 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008704 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008705 es->m_clen = msg->chunk_len;
8706 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008707}
Willy Tarreaub2513902006-12-17 14:52:38 +01008708
Willy Tarreau294c4732011-12-16 21:35:50 +01008709/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8710 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8711 * performed over the whole headers. Otherwise it must contain a valid header
8712 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8713 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8714 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8715 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008716 * -1. The value fetch stops at commas, so this function is suited for use with
8717 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008718 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008719 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008720unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008721 struct hdr_idx *idx, int occ,
8722 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008723{
Willy Tarreau294c4732011-12-16 21:35:50 +01008724 struct hdr_ctx local_ctx;
8725 char *ptr_hist[MAX_HDR_HISTORY];
8726 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008727 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008728 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008729
Willy Tarreau294c4732011-12-16 21:35:50 +01008730 if (!ctx) {
8731 local_ctx.idx = 0;
8732 ctx = &local_ctx;
8733 }
8734
Willy Tarreaubce70882009-09-07 11:51:47 +02008735 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008736 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008737 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008738 occ--;
8739 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008740 *vptr = ctx->line + ctx->val;
8741 *vlen = ctx->vlen;
8742 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008743 }
8744 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008745 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008746 }
8747
8748 /* negative occurrence, we scan all the list then walk back */
8749 if (-occ > MAX_HDR_HISTORY)
8750 return 0;
8751
Willy Tarreau294c4732011-12-16 21:35:50 +01008752 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008753 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008754 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8755 len_hist[hist_ptr] = ctx->vlen;
8756 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008757 hist_ptr = 0;
8758 found++;
8759 }
8760 if (-occ > found)
8761 return 0;
8762 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008763 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8764 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8765 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008766 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008767 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008768 if (hist_ptr >= MAX_HDR_HISTORY)
8769 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008770 *vptr = ptr_hist[hist_ptr];
8771 *vlen = len_hist[hist_ptr];
8772 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008773}
8774
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008775/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8776 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8777 * performed over the whole headers. Otherwise it must contain a valid header
8778 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8779 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8780 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8781 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8782 * -1. This function differs from http_get_hdr() in that it only returns full
8783 * line header values and does not stop at commas.
8784 * The return value is 0 if nothing was found, or non-zero otherwise.
8785 */
8786unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8787 struct hdr_idx *idx, int occ,
8788 struct hdr_ctx *ctx, char **vptr, int *vlen)
8789{
8790 struct hdr_ctx local_ctx;
8791 char *ptr_hist[MAX_HDR_HISTORY];
8792 int len_hist[MAX_HDR_HISTORY];
8793 unsigned int hist_ptr;
8794 int found;
8795
8796 if (!ctx) {
8797 local_ctx.idx = 0;
8798 ctx = &local_ctx;
8799 }
8800
8801 if (occ >= 0) {
8802 /* search from the beginning */
8803 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8804 occ--;
8805 if (occ <= 0) {
8806 *vptr = ctx->line + ctx->val;
8807 *vlen = ctx->vlen;
8808 return 1;
8809 }
8810 }
8811 return 0;
8812 }
8813
8814 /* negative occurrence, we scan all the list then walk back */
8815 if (-occ > MAX_HDR_HISTORY)
8816 return 0;
8817
8818 found = hist_ptr = 0;
8819 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8820 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8821 len_hist[hist_ptr] = ctx->vlen;
8822 if (++hist_ptr >= MAX_HDR_HISTORY)
8823 hist_ptr = 0;
8824 found++;
8825 }
8826 if (-occ > found)
8827 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008828
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008829 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008830 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8831 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8832 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008833 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008834 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008835 if (hist_ptr >= MAX_HDR_HISTORY)
8836 hist_ptr -= MAX_HDR_HISTORY;
8837 *vptr = ptr_hist[hist_ptr];
8838 *vlen = len_hist[hist_ptr];
8839 return 1;
8840}
8841
Willy Tarreaubaaee002006-06-26 02:48:02 +02008842/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008843 * Print a debug line with a header. Always stop at the first CR or LF char,
8844 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8845 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008846 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008847void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008848{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008849 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008850 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008851
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008852 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008853 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008854 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008855 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 +02008856
8857 for (max = 0; start + max < end; max++)
8858 if (start[max] == '\r' || start[max] == '\n')
8859 break;
8860
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008861 UBOUND(max, trash.size - trash.len - 3);
8862 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8863 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008864 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008865}
8866
Willy Tarreaueee5b512015-04-03 23:46:31 +02008867
8868/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8869 * The hdr_idx is allocated as well. In case of allocation failure, everything
8870 * allocated is freed and NULL is returned. Otherwise the new transaction is
8871 * assigned to the stream and returned.
8872 */
8873struct http_txn *http_alloc_txn(struct stream *s)
8874{
8875 struct http_txn *txn = s->txn;
8876
8877 if (txn)
8878 return txn;
8879
8880 txn = pool_alloc2(pool2_http_txn);
8881 if (!txn)
8882 return txn;
8883
8884 txn->hdr_idx.size = global.tune.max_http_hdr;
8885 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8886 if (!txn->hdr_idx.v) {
8887 pool_free2(pool2_http_txn, txn);
8888 return NULL;
8889 }
8890
8891 s->txn = txn;
8892 return txn;
8893}
8894
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008895void http_txn_reset_req(struct http_txn *txn)
8896{
8897 txn->req.flags = 0;
8898 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8899 txn->req.next = 0;
8900 txn->req.chunk_len = 0LL;
8901 txn->req.body_len = 0LL;
8902 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8903}
8904
8905void http_txn_reset_res(struct http_txn *txn)
8906{
8907 txn->rsp.flags = 0;
8908 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8909 txn->rsp.next = 0;
8910 txn->rsp.chunk_len = 0LL;
8911 txn->rsp.body_len = 0LL;
8912 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8913}
8914
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008916 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008917 * the required fields are properly allocated and that we only need to (re)init
8918 * them. This should be used before processing any new request.
8919 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008920void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008921{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008922 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008923 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008924
8925 txn->flags = 0;
8926 txn->status = -1;
8927
Willy Tarreauf64d1412010-10-07 20:06:11 +02008928 txn->cookie_first_date = 0;
8929 txn->cookie_last_date = 0;
8930
Willy Tarreaueee5b512015-04-03 23:46:31 +02008931 txn->srv_cookie = NULL;
8932 txn->cli_cookie = NULL;
8933 txn->uri = NULL;
8934
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008935 http_txn_reset_req(txn);
8936 http_txn_reset_res(txn);
8937
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008938 txn->req.chn = &s->req;
8939 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008940
8941 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008942
8943 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8944 if (fe->options2 & PR_O2_REQBUG_OK)
8945 txn->req.err_pos = -1; /* let buggy requests pass */
8946
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947 if (txn->hdr_idx.v)
8948 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008949
8950 vars_init(&s->vars_txn, SCOPE_TXN);
8951 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008952}
8953
8954/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008955void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008957 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008958 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008959
8960 /* these ones will have been dynamically allocated */
8961 pool_free2(pool2_requri, txn->uri);
8962 pool_free2(pool2_capture, txn->cli_cookie);
8963 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008964 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008965
William Lallemanda73203e2012-03-12 12:48:57 +01008966 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008967 txn->uri = NULL;
8968 txn->srv_cookie = NULL;
8969 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008970
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008971 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008972 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008973 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008974 pool_free2(h->pool, s->req_cap[h->index]);
8975 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008976 }
8977
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008978 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008979 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008980 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008981 pool_free2(h->pool, s->res_cap[h->index]);
8982 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008983 }
8984
Willy Tarreau6204cd92016-03-10 16:33:04 +01008985 vars_prune(&s->vars_txn, s->sess, s);
8986 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008987}
8988
8989/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008990void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008991{
8992 http_end_txn(s);
8993 http_init_txn(s);
8994
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008995 /* reinitialise the current rule list pointer to NULL. We are sure that
8996 * any rulelist match the NULL pointer.
8997 */
8998 s->current_rule_list = NULL;
8999
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009000 s->be = strm_fe(s);
9001 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009002 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009003 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009004 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009005 /* re-init store persistence */
9006 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009007 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009008
Willy Tarreau0937bc42009-12-22 15:03:09 +01009009 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009010
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009011 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009012
Willy Tarreau739cfba2010-01-25 23:11:14 +01009013 /* We must trim any excess data from the response buffer, because we
9014 * may have blocked an invalid response from a server that we don't
9015 * want to accidentely forward once we disable the analysers, nor do
9016 * we want those data to come along with next response. A typical
9017 * example of such data would be from a buggy server responding to
9018 * a HEAD with some data, or sending more than the advertised
9019 * content-length.
9020 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009021 if (unlikely(s->res.buf->i))
9022 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009023
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009024 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009025 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009026
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009027 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009028 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009029
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009030 s->req.rex = TICK_ETERNITY;
9031 s->req.wex = TICK_ETERNITY;
9032 s->req.analyse_exp = TICK_ETERNITY;
9033 s->res.rex = TICK_ETERNITY;
9034 s->res.wex = TICK_ETERNITY;
9035 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009036}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009037
Sasha Pachev218f0642014-06-16 12:05:59 -06009038void free_http_res_rules(struct list *r)
9039{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009040 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009041
9042 list_for_each_entry_safe(pr, tr, r, list) {
9043 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009044 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009045 free(pr);
9046 }
9047}
9048
9049void free_http_req_rules(struct list *r)
9050{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009051 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009052
9053 list_for_each_entry_safe(pr, tr, r, list) {
9054 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009055 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009056 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009057
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009058 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 free(pr);
9060 }
9061}
9062
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009063/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009064struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009065{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009066 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009067 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009068 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009069 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009070
Vincent Bernat02779b62016-04-03 13:48:43 +02009071 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009072 if (!rule) {
9073 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009074 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009075 }
9076
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009077 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009078 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009079 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009080 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009081 int code;
9082 int hc;
9083
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009084 if (!strcmp(args[0], "tarpit")) {
9085 rule->action = ACT_HTTP_REQ_TARPIT;
9086 rule->deny_status = HTTP_ERR_500;
9087 }
9088 else {
9089 rule->action = ACT_ACTION_DENY;
9090 rule->deny_status = HTTP_ERR_403;
9091 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009092 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009093 if (strcmp(args[cur_arg], "deny_status") == 0) {
9094 cur_arg++;
9095 if (!args[cur_arg]) {
9096 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9097 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9098 goto out_err;
9099 }
9100
9101 code = atol(args[cur_arg]);
9102 cur_arg++;
9103 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9104 if (http_err_codes[hc] == code) {
9105 rule->deny_status = hc;
9106 break;
9107 }
9108 }
9109
9110 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009111 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9112 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009113 }
9114 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009115 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009116 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009117 cur_arg = 1;
9118
9119 while(*args[cur_arg]) {
9120 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009121 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009122 cur_arg+=2;
9123 continue;
9124 } else
9125 break;
9126 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009127 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009128 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009129 cur_arg = 1;
9130
9131 if (!*args[cur_arg] ||
9132 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9133 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9134 file, linenum, args[0]);
9135 goto out_err;
9136 }
9137 rule->arg.nice = atoi(args[cur_arg]);
9138 if (rule->arg.nice < -1024)
9139 rule->arg.nice = -1024;
9140 else if (rule->arg.nice > 1024)
9141 rule->arg.nice = 1024;
9142 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009143 } else if (!strcmp(args[0], "set-tos")) {
9144#ifdef IP_TOS
9145 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009146 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009147 cur_arg = 1;
9148
9149 if (!*args[cur_arg] ||
9150 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9151 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9152 file, linenum, args[0]);
9153 goto out_err;
9154 }
9155
9156 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9157 if (err && *err != '\0') {
9158 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9159 file, linenum, err, args[0]);
9160 goto out_err;
9161 }
9162 cur_arg++;
9163#else
9164 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9165 goto out_err;
9166#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009167 } else if (!strcmp(args[0], "set-mark")) {
9168#ifdef SO_MARK
9169 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009170 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009171 cur_arg = 1;
9172
9173 if (!*args[cur_arg] ||
9174 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9175 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9176 file, linenum, args[0]);
9177 goto out_err;
9178 }
9179
9180 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9181 if (err && *err != '\0') {
9182 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9183 file, linenum, err, args[0]);
9184 goto out_err;
9185 }
9186 cur_arg++;
9187 global.last_checks |= LSTCHK_NETADM;
9188#else
9189 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9190 goto out_err;
9191#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009192 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009193 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009194 cur_arg = 1;
9195
9196 if (!*args[cur_arg] ||
9197 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9198 bad_log_level:
9199 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9200 file, linenum, args[0]);
9201 goto out_err;
9202 }
9203 if (strcmp(args[cur_arg], "silent") == 0)
9204 rule->arg.loglevel = -1;
9205 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9206 goto bad_log_level;
9207 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009208 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009209 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009210 cur_arg = 1;
9211
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009212 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9213 (*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 +01009214 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9215 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009216 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009217 }
9218
9219 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9220 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9221 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009222
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009223 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009224 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009225 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 +01009226 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9227 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9228 file, linenum, args[0], error);
9229 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009230 goto out_err;
9231 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009232 free(proxy->conf.lfs_file);
9233 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9234 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009235 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009236 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009237 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009238 cur_arg = 1;
9239
9240 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009241 (*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 -06009242 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9243 file, linenum, args[0]);
9244 goto out_err;
9245 }
9246
9247 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9248 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9249 LIST_INIT(&rule->arg.hdr_add.fmt);
9250
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009251 error = NULL;
9252 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9253 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9254 args[cur_arg + 1], error);
9255 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009256 goto out_err;
9257 }
9258
9259 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009260 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009261 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 +01009262 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9263 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9264 file, linenum, args[0], error);
9265 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009266 goto out_err;
9267 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009268
9269 free(proxy->conf.lfs_file);
9270 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9271 proxy->conf.lfs_line = proxy->conf.args.line;
9272 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009273 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009274 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009275 cur_arg = 1;
9276
9277 if (!*args[cur_arg] ||
9278 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9279 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9280 file, linenum, args[0]);
9281 goto out_err;
9282 }
9283
9284 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9285 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9286
9287 proxy->conf.args.ctx = ARGC_HRQ;
9288 free(proxy->conf.lfs_file);
9289 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9290 proxy->conf.lfs_line = proxy->conf.args.line;
9291 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009292 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9293 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009294 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009295 struct sample_expr *expr;
9296 unsigned int where;
9297 char *err = NULL;
9298
9299 cur_arg = 1;
9300 proxy->conf.args.ctx = ARGC_TRK;
9301
9302 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9303 if (!expr) {
9304 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9305 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9306 free(err);
9307 goto out_err;
9308 }
9309
9310 where = 0;
9311 if (proxy->cap & PR_CAP_FE)
9312 where |= SMP_VAL_FE_HRQ_HDR;
9313 if (proxy->cap & PR_CAP_BE)
9314 where |= SMP_VAL_BE_HRQ_HDR;
9315
9316 if (!(expr->fetch->val & where)) {
9317 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9318 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9319 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9320 args[cur_arg-1], sample_src_names(expr->fetch->use));
9321 free(expr);
9322 goto out_err;
9323 }
9324
9325 if (strcmp(args[cur_arg], "table") == 0) {
9326 cur_arg++;
9327 if (!args[cur_arg]) {
9328 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9329 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9330 free(expr);
9331 goto out_err;
9332 }
9333 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009334 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009335 cur_arg++;
9336 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009337 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009338 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009339 } else if (strcmp(args[0], "redirect") == 0) {
9340 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009341 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009342
Willy Tarreaube4653b2015-05-28 15:26:58 +02009343 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009344 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9345 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9346 goto out_err;
9347 }
9348
9349 /* this redirect rule might already contain a parsed condition which
9350 * we'll pass to the http-request rule.
9351 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009352 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009353 rule->arg.redir = redir;
9354 rule->cond = redir->cond;
9355 redir->cond = NULL;
9356 cur_arg = 2;
9357 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009358 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9359 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009360 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009361 /*
9362 * '+ 8' for 'add-acl('
9363 * '- 9' for 'add-acl(' + trailing ')'
9364 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009365 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009366
9367 cur_arg = 1;
9368
9369 if (!*args[cur_arg] ||
9370 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9371 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9372 file, linenum, args[0]);
9373 goto out_err;
9374 }
9375
9376 LIST_INIT(&rule->arg.map.key);
9377 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009378 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009379 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009380 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9381 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9382 file, linenum, args[0], error);
9383 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009384 goto out_err;
9385 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009386 free(proxy->conf.lfs_file);
9387 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9388 proxy->conf.lfs_line = proxy->conf.args.line;
9389 cur_arg += 1;
9390 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9391 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009392 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009393 /*
9394 * '+ 8' for 'del-acl('
9395 * '- 9' for 'del-acl(' + trailing ')'
9396 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009397 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009398
9399 cur_arg = 1;
9400
9401 if (!*args[cur_arg] ||
9402 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9403 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9404 file, linenum, args[0]);
9405 goto out_err;
9406 }
9407
9408 LIST_INIT(&rule->arg.map.key);
9409 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009410 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009411 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009412 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9413 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9414 file, linenum, args[0], error);
9415 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009416 goto out_err;
9417 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009418 free(proxy->conf.lfs_file);
9419 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9420 proxy->conf.lfs_line = proxy->conf.args.line;
9421 cur_arg += 1;
9422 } else if (strncmp(args[0], "del-map", 7) == 0) {
9423 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009424 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009425 /*
9426 * '+ 8' for 'del-map('
9427 * '- 9' for 'del-map(' + trailing ')'
9428 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009429 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009430
9431 cur_arg = 1;
9432
9433 if (!*args[cur_arg] ||
9434 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9435 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9436 file, linenum, args[0]);
9437 goto out_err;
9438 }
9439
9440 LIST_INIT(&rule->arg.map.key);
9441 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009442 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009443 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009444 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9445 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9446 file, linenum, args[0], error);
9447 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009448 goto out_err;
9449 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009450 free(proxy->conf.lfs_file);
9451 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9452 proxy->conf.lfs_line = proxy->conf.args.line;
9453 cur_arg += 1;
9454 } else if (strncmp(args[0], "set-map", 7) == 0) {
9455 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009456 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009457 /*
9458 * '+ 8' for 'set-map('
9459 * '- 9' for 'set-map(' + trailing ')'
9460 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009461 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009462
9463 cur_arg = 1;
9464
9465 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9466 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9467 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9468 file, linenum, args[0]);
9469 goto out_err;
9470 }
9471
9472 LIST_INIT(&rule->arg.map.key);
9473 LIST_INIT(&rule->arg.map.value);
9474 proxy->conf.args.ctx = ARGC_HRQ;
9475
9476 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009477 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009478 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009479 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9480 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9481 file, linenum, args[0], error);
9482 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009483 goto out_err;
9484 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009485
9486 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009487 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009488 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 +01009489 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9490 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9491 file, linenum, args[0], error);
9492 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009493 goto out_err;
9494 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009495 free(proxy->conf.lfs_file);
9496 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9497 proxy->conf.lfs_line = proxy->conf.args.line;
9498
9499 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009500 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9501 char *errmsg = NULL;
9502 cur_arg = 1;
9503 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009504 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009505 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009506 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009507 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9508 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9509 free(errmsg);
9510 goto out_err;
9511 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009512 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009513 action_build_list(&http_req_keywords.list, &trash);
9514 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9515 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009516 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009517 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009518 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009519 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009520 }
9521
9522 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9523 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009524 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009525
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009526 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9527 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9528 file, linenum, args[0], errmsg);
9529 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009530 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009531 }
9532 rule->cond = cond;
9533 }
9534 else if (*args[cur_arg]) {
9535 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9536 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9537 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009538 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009539 }
9540
9541 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009542 out_err:
9543 free(rule);
9544 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009545}
9546
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009547/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009548struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009549{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009550 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009551 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009552 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009553 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009554
9555 rule = calloc(1, sizeof(*rule));
9556 if (!rule) {
9557 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9558 goto out_err;
9559 }
9560
9561 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009562 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009563 cur_arg = 1;
9564 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009565 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009566 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009567 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009568 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009569 cur_arg = 1;
9570
9571 if (!*args[cur_arg] ||
9572 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9573 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9574 file, linenum, args[0]);
9575 goto out_err;
9576 }
9577 rule->arg.nice = atoi(args[cur_arg]);
9578 if (rule->arg.nice < -1024)
9579 rule->arg.nice = -1024;
9580 else if (rule->arg.nice > 1024)
9581 rule->arg.nice = 1024;
9582 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009583 } else if (!strcmp(args[0], "set-tos")) {
9584#ifdef IP_TOS
9585 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009586 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009587 cur_arg = 1;
9588
9589 if (!*args[cur_arg] ||
9590 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9591 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9592 file, linenum, args[0]);
9593 goto out_err;
9594 }
9595
9596 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9597 if (err && *err != '\0') {
9598 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9599 file, linenum, err, args[0]);
9600 goto out_err;
9601 }
9602 cur_arg++;
9603#else
9604 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9605 goto out_err;
9606#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009607 } else if (!strcmp(args[0], "set-mark")) {
9608#ifdef SO_MARK
9609 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009610 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009611 cur_arg = 1;
9612
9613 if (!*args[cur_arg] ||
9614 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9615 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9616 file, linenum, args[0]);
9617 goto out_err;
9618 }
9619
9620 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9621 if (err && *err != '\0') {
9622 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9623 file, linenum, err, args[0]);
9624 goto out_err;
9625 }
9626 cur_arg++;
9627 global.last_checks |= LSTCHK_NETADM;
9628#else
9629 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9630 goto out_err;
9631#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009632 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009633 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009634 cur_arg = 1;
9635
9636 if (!*args[cur_arg] ||
9637 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9638 bad_log_level:
9639 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9640 file, linenum, args[0]);
9641 goto out_err;
9642 }
9643 if (strcmp(args[cur_arg], "silent") == 0)
9644 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009645 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009646 goto bad_log_level;
9647 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009648 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009649 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009650 cur_arg = 1;
9651
9652 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9653 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9654 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9655 file, linenum, args[0]);
9656 goto out_err;
9657 }
9658
9659 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9660 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9661 LIST_INIT(&rule->arg.hdr_add.fmt);
9662
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009663 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009664 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009665 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 +01009666 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9667 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9668 file, linenum, args[0], error);
9669 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009670 goto out_err;
9671 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009672 free(proxy->conf.lfs_file);
9673 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9674 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009675 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009676 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009677 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009678 cur_arg = 1;
9679
9680 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009681 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9682 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009683 file, linenum, args[0]);
9684 goto out_err;
9685 }
9686
9687 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9688 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9689 LIST_INIT(&rule->arg.hdr_add.fmt);
9690
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009691 error = NULL;
9692 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9693 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9694 args[cur_arg + 1], error);
9695 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009696 goto out_err;
9697 }
9698
9699 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009700 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009701 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 +01009702 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9703 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9704 file, linenum, args[0], error);
9705 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009706 goto out_err;
9707 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009708
9709 free(proxy->conf.lfs_file);
9710 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9711 proxy->conf.lfs_line = proxy->conf.args.line;
9712 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009713 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009714 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009715 cur_arg = 1;
9716
9717 if (!*args[cur_arg] ||
9718 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9719 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9720 file, linenum, args[0]);
9721 goto out_err;
9722 }
9723
9724 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9725 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9726
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009727 proxy->conf.args.ctx = ARGC_HRS;
9728 free(proxy->conf.lfs_file);
9729 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9730 proxy->conf.lfs_line = proxy->conf.args.line;
9731 cur_arg += 1;
9732 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9733 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009734 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009735 /*
9736 * '+ 8' for 'add-acl('
9737 * '- 9' for 'add-acl(' + trailing ')'
9738 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009739 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009740
9741 cur_arg = 1;
9742
9743 if (!*args[cur_arg] ||
9744 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9745 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9746 file, linenum, args[0]);
9747 goto out_err;
9748 }
9749
9750 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009751 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009752 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009753 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009754 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9755 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9756 file, linenum, args[0], error);
9757 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009758 goto out_err;
9759 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009760 free(proxy->conf.lfs_file);
9761 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9762 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009764 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009765 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9766 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009767 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009768 /*
9769 * '+ 8' for 'del-acl('
9770 * '- 9' for 'del-acl(' + trailing ')'
9771 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009772 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009773
9774 cur_arg = 1;
9775
9776 if (!*args[cur_arg] ||
9777 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9778 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9779 file, linenum, args[0]);
9780 goto out_err;
9781 }
9782
9783 LIST_INIT(&rule->arg.map.key);
9784 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009785 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009786 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009787 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9788 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9789 file, linenum, args[0], error);
9790 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009791 goto out_err;
9792 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009793 free(proxy->conf.lfs_file);
9794 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9795 proxy->conf.lfs_line = proxy->conf.args.line;
9796 cur_arg += 1;
9797 } else if (strncmp(args[0], "del-map", 7) == 0) {
9798 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009799 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009800 /*
9801 * '+ 8' for 'del-map('
9802 * '- 9' for 'del-map(' + trailing ')'
9803 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009804 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009805
9806 cur_arg = 1;
9807
9808 if (!*args[cur_arg] ||
9809 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9810 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9811 file, linenum, args[0]);
9812 goto out_err;
9813 }
9814
9815 LIST_INIT(&rule->arg.map.key);
9816 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009817 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009818 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009819 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9820 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9821 file, linenum, args[0], error);
9822 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009823 goto out_err;
9824 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009825 free(proxy->conf.lfs_file);
9826 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9827 proxy->conf.lfs_line = proxy->conf.args.line;
9828 cur_arg += 1;
9829 } else if (strncmp(args[0], "set-map", 7) == 0) {
9830 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009831 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009832 /*
9833 * '+ 8' for 'set-map('
9834 * '- 9' for 'set-map(' + trailing ')'
9835 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009836 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009837
9838 cur_arg = 1;
9839
9840 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9841 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9842 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9843 file, linenum, args[0]);
9844 goto out_err;
9845 }
9846
9847 LIST_INIT(&rule->arg.map.key);
9848 LIST_INIT(&rule->arg.map.value);
9849
9850 proxy->conf.args.ctx = ARGC_HRS;
9851
9852 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009853 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009854 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009855 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9856 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9857 file, linenum, args[0], error);
9858 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009859 goto out_err;
9860 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009861
9862 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009863 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009864 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 +01009865 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9866 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9867 file, linenum, args[0], error);
9868 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009869 goto out_err;
9870 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009871
9872 free(proxy->conf.lfs_file);
9873 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9874 proxy->conf.lfs_line = proxy->conf.args.line;
9875
9876 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009877 } else if (strcmp(args[0], "redirect") == 0) {
9878 struct redirect_rule *redir;
9879 char *errmsg = NULL;
9880
9881 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9882 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9883 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9884 goto out_err;
9885 }
9886
9887 /* this redirect rule might already contain a parsed condition which
9888 * we'll pass to the http-request rule.
9889 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009890 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009891 rule->arg.redir = redir;
9892 rule->cond = redir->cond;
9893 redir->cond = NULL;
9894 cur_arg = 2;
9895 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009896 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9897 args[0][9] == '\0' && args[0][8] >= '0' &&
9898 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9899 struct sample_expr *expr;
9900 unsigned int where;
9901 char *err = NULL;
9902
9903 cur_arg = 1;
9904 proxy->conf.args.ctx = ARGC_TRK;
9905
9906 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9907 if (!expr) {
9908 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9909 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9910 free(err);
9911 goto out_err;
9912 }
9913
9914 where = 0;
9915 if (proxy->cap & PR_CAP_FE)
9916 where |= SMP_VAL_FE_HRS_HDR;
9917 if (proxy->cap & PR_CAP_BE)
9918 where |= SMP_VAL_BE_HRS_HDR;
9919
9920 if (!(expr->fetch->val & where)) {
9921 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9922 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9923 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9924 args[cur_arg-1], sample_src_names(expr->fetch->use));
9925 free(expr);
9926 goto out_err;
9927 }
9928
9929 if (strcmp(args[cur_arg], "table") == 0) {
9930 cur_arg++;
9931 if (!args[cur_arg]) {
9932 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9933 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9934 free(expr);
9935 goto out_err;
9936 }
9937 /* we copy the table name for now, it will be resolved later */
9938 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9939 cur_arg++;
9940 }
9941 rule->arg.trk_ctr.expr = expr;
9942 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009943 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9944 char *errmsg = NULL;
9945 cur_arg = 1;
9946 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009947 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009948 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009949 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009950 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9951 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9952 free(errmsg);
9953 goto out_err;
9954 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009955 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009956 action_build_list(&http_res_keywords.list, &trash);
9957 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9958 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009959 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009960 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009961 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009962 goto out_err;
9963 }
9964
9965 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9966 struct acl_cond *cond;
9967 char *errmsg = NULL;
9968
9969 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9970 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9971 file, linenum, args[0], errmsg);
9972 free(errmsg);
9973 goto out_err;
9974 }
9975 rule->cond = cond;
9976 }
9977 else if (*args[cur_arg]) {
9978 Alert("parsing [%s:%d]: 'http-response %s' expects"
9979 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9980 file, linenum, args[0], args[cur_arg]);
9981 goto out_err;
9982 }
9983
9984 return rule;
9985 out_err:
9986 free(rule);
9987 return NULL;
9988}
9989
Willy Tarreau4baae242012-12-27 12:00:31 +01009990/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009991 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009992 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9993 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009994 */
9995struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009996 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009997{
9998 struct redirect_rule *rule;
9999 int cur_arg;
10000 int type = REDIRECT_TYPE_NONE;
10001 int code = 302;
10002 const char *destination = NULL;
10003 const char *cookie = NULL;
10004 int cookie_set = 0;
10005 unsigned int flags = REDIRECT_FLAG_NONE;
10006 struct acl_cond *cond = NULL;
10007
10008 cur_arg = 0;
10009 while (*(args[cur_arg])) {
10010 if (strcmp(args[cur_arg], "location") == 0) {
10011 if (!*args[cur_arg + 1])
10012 goto missing_arg;
10013
10014 type = REDIRECT_TYPE_LOCATION;
10015 cur_arg++;
10016 destination = args[cur_arg];
10017 }
10018 else if (strcmp(args[cur_arg], "prefix") == 0) {
10019 if (!*args[cur_arg + 1])
10020 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010021 type = REDIRECT_TYPE_PREFIX;
10022 cur_arg++;
10023 destination = args[cur_arg];
10024 }
10025 else if (strcmp(args[cur_arg], "scheme") == 0) {
10026 if (!*args[cur_arg + 1])
10027 goto missing_arg;
10028
10029 type = REDIRECT_TYPE_SCHEME;
10030 cur_arg++;
10031 destination = args[cur_arg];
10032 }
10033 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10034 if (!*args[cur_arg + 1])
10035 goto missing_arg;
10036
10037 cur_arg++;
10038 cookie = args[cur_arg];
10039 cookie_set = 1;
10040 }
10041 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10042 if (!*args[cur_arg + 1])
10043 goto missing_arg;
10044
10045 cur_arg++;
10046 cookie = args[cur_arg];
10047 cookie_set = 0;
10048 }
10049 else if (strcmp(args[cur_arg], "code") == 0) {
10050 if (!*args[cur_arg + 1])
10051 goto missing_arg;
10052
10053 cur_arg++;
10054 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010055 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010056 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010057 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010058 args[cur_arg - 1], args[cur_arg]);
10059 return NULL;
10060 }
10061 }
10062 else if (!strcmp(args[cur_arg],"drop-query")) {
10063 flags |= REDIRECT_FLAG_DROP_QS;
10064 }
10065 else if (!strcmp(args[cur_arg],"append-slash")) {
10066 flags |= REDIRECT_FLAG_APPEND_SLASH;
10067 }
10068 else if (strcmp(args[cur_arg], "if") == 0 ||
10069 strcmp(args[cur_arg], "unless") == 0) {
10070 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10071 if (!cond) {
10072 memprintf(errmsg, "error in condition: %s", *errmsg);
10073 return NULL;
10074 }
10075 break;
10076 }
10077 else {
10078 memprintf(errmsg,
10079 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10080 args[cur_arg]);
10081 return NULL;
10082 }
10083 cur_arg++;
10084 }
10085
10086 if (type == REDIRECT_TYPE_NONE) {
10087 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10088 return NULL;
10089 }
10090
Willy Tarreaube4653b2015-05-28 15:26:58 +020010091 if (dir && type != REDIRECT_TYPE_LOCATION) {
10092 memprintf(errmsg, "response only supports redirect type 'location'");
10093 return NULL;
10094 }
10095
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010096 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010097 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010098 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010099
10100 if (!use_fmt) {
10101 /* old-style static redirect rule */
10102 rule->rdr_str = strdup(destination);
10103 rule->rdr_len = strlen(destination);
10104 }
10105 else {
10106 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010107
10108 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10109 * if prefix == "/", we don't want to add anything, otherwise it
10110 * makes it hard for the user to configure a self-redirection.
10111 */
Godbachd9722032014-12-18 15:44:58 +080010112 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010113 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010114 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10115 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 +010010116 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10117 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010118 return NULL;
10119 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010120 free(curproxy->conf.lfs_file);
10121 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10122 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010123 }
10124 }
10125
Willy Tarreau4baae242012-12-27 12:00:31 +010010126 if (cookie) {
10127 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10128 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10129 */
10130 rule->cookie_len = strlen(cookie);
10131 if (cookie_set) {
10132 rule->cookie_str = malloc(rule->cookie_len + 10);
10133 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10134 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10135 rule->cookie_len += 9;
10136 } else {
10137 rule->cookie_str = malloc(rule->cookie_len + 21);
10138 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10139 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10140 rule->cookie_len += 20;
10141 }
10142 }
10143 rule->type = type;
10144 rule->code = code;
10145 rule->flags = flags;
10146 LIST_INIT(&rule->list);
10147 return rule;
10148
10149 missing_arg:
10150 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10151 return NULL;
10152}
10153
Willy Tarreau8797c062007-05-07 00:55:35 +020010154/************************************************************************/
10155/* The code below is dedicated to ACL parsing and matching */
10156/************************************************************************/
10157
10158
Willy Tarreau14174bc2012-04-16 14:34:04 +020010159/* This function ensures that the prerequisites for an L7 fetch are ready,
10160 * which means that a request or response is ready. If some data is missing,
10161 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010162 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10163 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010164 *
10165 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010166 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10167 * decide whether or not an HTTP message is present ;
10168 * 0 if the requested data cannot be fetched or if it is certain that
10169 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010170 * 1 if an HTTP message is ready
10171 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010172int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010173 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174{
Willy Tarreau192252e2015-04-04 01:47:55 +020010175 struct http_txn *txn;
10176 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010177
Willy Tarreaube508f12016-03-10 11:47:01 +010010178 /* Note: it is possible that <s> is NULL when called before stream
10179 * initialization (eg: tcp-request connection), so this function is the
10180 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010181 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010182 if (!s)
10183 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010184
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010185 if (!s->txn) {
10186 if (unlikely(!http_alloc_txn(s)))
10187 return 0; /* not enough memory */
10188 http_init_txn(s);
10189 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010190 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010191 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010192
10193 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010194 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010195
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010196 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010197 /* If the buffer does not leave enough free space at the end,
10198 * we must first realign it.
10199 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010200 if (s->req.buf->p > s->req.buf->data &&
10201 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10202 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010203
Willy Tarreau14174bc2012-04-16 14:34:04 +020010204 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010205 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010206 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010207
10208 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010209 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010210 http_msg_analyzer(msg, &txn->hdr_idx);
10211
10212 /* Still no valid request ? */
10213 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010214 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010215 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010216 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010217 }
10218 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010219 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010220 return 0;
10221 }
10222
10223 /* OK we just got a valid HTTP request. We have some minor
10224 * preparation to perform so that further checks can rely
10225 * on HTTP tests.
10226 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010227
10228 /* If the request was parsed but was too large, we must absolutely
10229 * return an error so that it is not processed. At the moment this
10230 * cannot happen, but if the parsers are to change in the future,
10231 * we want this check to be maintained.
10232 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010233 if (unlikely(s->req.buf->i + s->req.buf->p >
10234 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010235 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010236 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010237 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010238 return 1;
10239 }
10240
Willy Tarreau9b28e032012-10-12 23:49:43 +020010241 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010242 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010243 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010244
Willy Tarreau506d0502013-07-06 13:29:24 +020010245 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10246 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010247 }
10248
Willy Tarreau506d0502013-07-06 13:29:24 +020010249 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010250 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010251 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010252
10253 /* otherwise everything's ready for the request */
10254 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010255 else {
10256 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010257 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10258 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010259 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010260 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010261 }
10262
10263 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010264 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010265 return 1;
10266}
Willy Tarreau8797c062007-05-07 00:55:35 +020010267
Willy Tarreau8797c062007-05-07 00:55:35 +020010268/* 1. Check on METHOD
10269 * We use the pre-parsed method if it is known, and store its number as an
10270 * integer. If it is unknown, we use the pointer and the length.
10271 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010272static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010273{
10274 int len, meth;
10275
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010276 len = strlen(text);
10277 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010278
10279 pattern->val.i = meth;
10280 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010281 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010282 pattern->len = len;
10283 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010284 else {
10285 pattern->ptr.str = NULL;
10286 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010287 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010288 return 1;
10289}
10290
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010291/* This function fetches the method of current HTTP request and stores
10292 * it in the global pattern struct as a chunk. There are two possibilities :
10293 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10294 * in <len> and <ptr> is NULL ;
10295 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10296 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010297 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010298 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010299static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010300smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010301{
10302 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010303 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010304
Willy Tarreau24e32d82012-04-23 23:55:44 +020010305 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010306
Willy Tarreaube508f12016-03-10 11:47:01 +010010307 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010309 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010310 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010311 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010312 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10313 /* ensure the indexes are not affected */
10314 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010315 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010316 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10317 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010318 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010319 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010320 return 1;
10321}
10322
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010323/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010324static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010325{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010326 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010327 struct pattern_list *lst;
10328 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010329
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010330 list_for_each_entry(lst, &expr->patterns, list) {
10331 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010332
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010333 /* well-known method */
10334 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010335 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010336 return pattern;
10337 else
10338 continue;
10339 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010340
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010341 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010342 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010343 continue;
10344
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010345 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010346 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10347 (!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 +010010348 return pattern;
10349 }
10350 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010351}
10352
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010353static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010354smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010355{
Willy Tarreaube508f12016-03-10 11:47:01 +010010356 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010357 char *ptr;
10358 int len;
10359
Willy Tarreauc0239e02012-04-16 14:42:55 +020010360 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010361
Willy Tarreaube508f12016-03-10 11:47:01 +010010362 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010363 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010364 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010365
10366 while ((len-- > 0) && (*ptr++ != '/'));
10367 if (len <= 0)
10368 return 0;
10369
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010370 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010371 smp->data.u.str.str = ptr;
10372 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010373
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010374 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010375 return 1;
10376}
10377
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010378static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010379smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010380{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010381 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010382 char *ptr;
10383 int len;
10384
Willy Tarreauc0239e02012-04-16 14:42:55 +020010385 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010386
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010387 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010388 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10389 return 0;
10390
Willy Tarreau8797c062007-05-07 00:55:35 +020010391 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010392 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010393
10394 while ((len-- > 0) && (*ptr++ != '/'));
10395 if (len <= 0)
10396 return 0;
10397
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010398 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010399 smp->data.u.str.str = ptr;
10400 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010401
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010402 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010403 return 1;
10404}
10405
10406/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010407static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010408smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010409{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010410 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010411 char *ptr;
10412 int len;
10413
Willy Tarreauc0239e02012-04-16 14:42:55 +020010414 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010415
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010416 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010417 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10418 return 0;
10419
Willy Tarreau8797c062007-05-07 00:55:35 +020010420 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010421 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010422
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010423 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010424 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010425 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010426 return 1;
10427}
10428
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010429static int
10430smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10431{
10432 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10433 return 0;
10434
10435 if (!smp->strm->unique_id) {
10436 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10437 return 0;
10438 smp->strm->unique_id[0] = '\0';
10439 }
10440 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10441 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10442
10443 smp->data.type = SMP_T_STR;
10444 smp->data.u.str.str = smp->strm->unique_id;
10445 smp->flags = SMP_F_CONST;
10446 return 1;
10447}
10448
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010449/* returns the longest available part of the body. This requires that the body
10450 * has been waited for using http-buffer-request.
10451 */
10452static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010453smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010454{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010455 struct http_msg *msg;
10456 unsigned long len;
10457 unsigned long block1;
10458 char *body;
10459 struct chunk *temp;
10460
10461 CHECK_HTTP_MESSAGE_FIRST();
10462
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010463 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010464 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010465 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010466 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010467
10468 len = http_body_bytes(msg);
10469 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10470
10471 block1 = len;
10472 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10473 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10474
10475 if (block1 == len) {
10476 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010477 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010478 smp->data.u.str.str = body;
10479 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010480 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10481 }
10482 else {
10483 /* buffer is wrapped, we need to defragment it */
10484 temp = get_trash_chunk();
10485 memcpy(temp->str, body, block1);
10486 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010487 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010488 smp->data.u.str.str = temp->str;
10489 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010490 smp->flags = SMP_F_VOL_TEST;
10491 }
10492 return 1;
10493}
10494
10495
10496/* returns the available length of the body. This requires that the body
10497 * has been waited for using http-buffer-request.
10498 */
10499static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010500smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010501{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010502 struct http_msg *msg;
10503
10504 CHECK_HTTP_MESSAGE_FIRST();
10505
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010506 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010507 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010508 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010509 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010510
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010511 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010512 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010513
10514 smp->flags = SMP_F_VOL_TEST;
10515 return 1;
10516}
10517
10518
10519/* returns the advertised length of the body, or the advertised size of the
10520 * chunks available in the buffer. This requires that the body has been waited
10521 * for using http-buffer-request.
10522 */
10523static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010524smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010525{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010526 struct http_msg *msg;
10527
10528 CHECK_HTTP_MESSAGE_FIRST();
10529
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010530 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010531 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010532 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010533 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010534
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010535 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010536 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010537
10538 smp->flags = SMP_F_VOL_TEST;
10539 return 1;
10540}
10541
10542
Willy Tarreau8797c062007-05-07 00:55:35 +020010543/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010544static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010545smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010546{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010547 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010548
Willy Tarreauc0239e02012-04-16 14:42:55 +020010549 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010550
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010551 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010552 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010553 smp->data.u.str.len = txn->req.sl.rq.u_l;
10554 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010555 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010556 return 1;
10557}
10558
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010560smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010561{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010562 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010563 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010564
Willy Tarreauc0239e02012-04-16 14:42:55 +020010565 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010566
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010567 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010568 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 +020010569 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010570 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010571
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010572 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010573 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010574 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010575 return 1;
10576}
10577
10578static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010579smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010580{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010581 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010582 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010583
Willy Tarreauc0239e02012-04-16 14:42:55 +020010584 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010585
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010586 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010587 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 +020010588 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10589 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010590
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010591 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010592 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010593 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010594 return 1;
10595}
10596
Willy Tarreau185b5c42012-04-26 15:11:51 +020010597/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10598 * Accepts an optional argument of type string containing the header field name,
10599 * and an optional argument of type signed or unsigned integer to request an
10600 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010601 * headers are considered from the first one. It does not stop on commas and
10602 * returns full lines instead (useful for User-Agent or Date for example).
10603 */
10604static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010605smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010606{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010607 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010608 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010609 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010610 int occ = 0;
10611 const char *name_str = NULL;
10612 int name_len = 0;
10613
10614 if (!ctx) {
10615 /* first call */
10616 ctx = &static_hdr_ctx;
10617 ctx->idx = 0;
10618 smp->ctx.a[0] = ctx;
10619 }
10620
10621 if (args) {
10622 if (args[0].type != ARGT_STR)
10623 return 0;
10624 name_str = args[0].data.str.str;
10625 name_len = args[0].data.str.len;
10626
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010627 if (args[1].type == ARGT_SINT)
10628 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010629 }
10630
10631 CHECK_HTTP_MESSAGE_FIRST();
10632
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010633 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010634 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 +020010635
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010636 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10637 /* search for header from the beginning */
10638 ctx->idx = 0;
10639
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010640 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010641 /* no explicit occurrence and single fetch => last header by default */
10642 occ = -1;
10643
10644 if (!occ)
10645 /* prepare to report multiple occurrences for ACL fetches */
10646 smp->flags |= SMP_F_NOT_LAST;
10647
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010648 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010649 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010650 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 +020010651 return 1;
10652
10653 smp->flags &= ~SMP_F_NOT_LAST;
10654 return 0;
10655}
10656
10657/* 6. Check on HTTP header count. The number of occurrences is returned.
10658 * Accepts exactly 1 argument of type string. It does not stop on commas and
10659 * returns full lines instead (useful for User-Agent or Date for example).
10660 */
10661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010663{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010664 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010665 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010666 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010667 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010668 const char *name = NULL;
10669 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010670
Willy Tarreau601a4d12015-04-01 19:16:09 +020010671 if (args && args->type == ARGT_STR) {
10672 name = args->data.str.str;
10673 len = args->data.str.len;
10674 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010675
10676 CHECK_HTTP_MESSAGE_FIRST();
10677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010678 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010679 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 +020010680
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010681 ctx.idx = 0;
10682 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010683 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010684 cnt++;
10685
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010686 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010687 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010688 smp->flags = SMP_F_VOL_HDR;
10689 return 1;
10690}
10691
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010692static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010693smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010694{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010695 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010696 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010697 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010698 struct chunk *temp;
10699 char del = ',';
10700
10701 if (args && args->type == ARGT_STR)
10702 del = *args[0].data.str.str;
10703
10704 CHECK_HTTP_MESSAGE_FIRST();
10705
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010706 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010707 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 +020010708
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010709 temp = get_trash_chunk();
10710
10711 ctx.idx = 0;
10712 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10713 if (temp->len)
10714 temp->str[temp->len++] = del;
10715 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10716 temp->len += ctx.del;
10717 }
10718
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010719 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010720 smp->data.u.str.str = temp->str;
10721 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010722 smp->flags = SMP_F_VOL_HDR;
10723 return 1;
10724}
10725
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010726/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10727 * Accepts an optional argument of type string containing the header field name,
10728 * and an optional argument of type signed or unsigned integer to request an
10729 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010730 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010731 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010732static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010733smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010734{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010735 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010736 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010737 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010738 int occ = 0;
10739 const char *name_str = NULL;
10740 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010741
Willy Tarreaua890d072013-04-02 12:01:06 +020010742 if (!ctx) {
10743 /* first call */
10744 ctx = &static_hdr_ctx;
10745 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010746 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010747 }
10748
Willy Tarreau185b5c42012-04-26 15:11:51 +020010749 if (args) {
10750 if (args[0].type != ARGT_STR)
10751 return 0;
10752 name_str = args[0].data.str.str;
10753 name_len = args[0].data.str.len;
10754
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010755 if (args[1].type == ARGT_SINT)
10756 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010757 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010758
Willy Tarreaue333ec92012-04-16 16:26:40 +020010759 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010760
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010761 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010762 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 +020010763
Willy Tarreau185b5c42012-04-26 15:11:51 +020010764 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010765 /* search for header from the beginning */
10766 ctx->idx = 0;
10767
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010768 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010769 /* no explicit occurrence and single fetch => last header by default */
10770 occ = -1;
10771
10772 if (!occ)
10773 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010774 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010775
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010776 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010777 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010778 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 +020010779 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010780
Willy Tarreau37406352012-04-23 16:16:37 +020010781 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010782 return 0;
10783}
10784
Willy Tarreauc11416f2007-06-17 16:58:38 +020010785/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010786 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010787 */
10788static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010789smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010790{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010791 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010792 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010793 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010794 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010795 const char *name = NULL;
10796 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010797
Willy Tarreau601a4d12015-04-01 19:16:09 +020010798 if (args && args->type == ARGT_STR) {
10799 name = args->data.str.str;
10800 len = args->data.str.len;
10801 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010802
Willy Tarreaue333ec92012-04-16 16:26:40 +020010803 CHECK_HTTP_MESSAGE_FIRST();
10804
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010805 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010806 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 +020010807
Willy Tarreau33a7e692007-06-10 19:45:56 +020010808 ctx.idx = 0;
10809 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010810 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010811 cnt++;
10812
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010813 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010814 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010815 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010816 return 1;
10817}
10818
Willy Tarreau185b5c42012-04-26 15:11:51 +020010819/* Fetch an HTTP header's integer value. The integer value is returned. It
10820 * takes a mandatory argument of type string and an optional one of type int
10821 * to designate a specific occurrence. It returns an unsigned integer, which
10822 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010823 */
10824static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010825smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010826{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010827 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010828
Willy Tarreauf853c462012-04-23 18:53:56 +020010829 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010830 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010831 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010832 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010833
Willy Tarreaud53e2422012-04-16 17:21:11 +020010834 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010835}
10836
Cyril Bonté69fa9922012-10-25 00:01:06 +020010837/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10838 * and an optional one of type int to designate a specific occurrence.
10839 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010840 */
10841static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010842smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010843{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010844 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010845
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010846 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010847 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010848 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010849 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010850 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010851 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010852 if (smp->data.u.str.len < temp->size - 1) {
10853 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10854 temp->str[smp->data.u.str.len] = '\0';
10855 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010856 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010857 break;
10858 }
10859 }
10860 }
10861
Willy Tarreaud53e2422012-04-16 17:21:11 +020010862 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010863 if (!(smp->flags & SMP_F_NOT_LAST))
10864 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010865 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010866 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010867}
10868
Willy Tarreau737b0c12007-06-10 21:28:46 +020010869/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10870 * the first '/' after the possible hostname, and ends before the possible '?'.
10871 */
10872static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010873smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010874{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010875 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010876 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010877
Willy Tarreauc0239e02012-04-16 14:42:55 +020010878 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010879
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010880 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010881 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010882 ptr = http_get_path(txn);
10883 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010884 return 0;
10885
10886 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010887 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010888 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010889
10890 while (ptr < end && *ptr != '?')
10891 ptr++;
10892
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010893 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010894 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010895 return 1;
10896}
10897
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010898/* This produces a concatenation of the first occurrence of the Host header
10899 * followed by the path component if it begins with a slash ('/'). This means
10900 * that '*' will not be added, resulting in exactly the first Host entry.
10901 * If no Host header is found, then the path is returned as-is. The returned
10902 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010903 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010904 */
10905static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010906smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010907{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010908 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010909 char *ptr, *end, *beg;
10910 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010911 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010912
10913 CHECK_HTTP_MESSAGE_FIRST();
10914
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010915 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010916 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010917 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010918 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010919
10920 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010921 temp = get_trash_chunk();
10922 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010923 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010924 smp->data.u.str.str = temp->str;
10925 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010926
10927 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010928 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010929 beg = http_get_path(txn);
10930 if (!beg)
10931 beg = end;
10932
10933 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10934
10935 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010936 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10937 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010938 }
10939
10940 smp->flags = SMP_F_VOL_1ST;
10941 return 1;
10942}
10943
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010944/* This produces a 32-bit hash of the concatenation of the first occurrence of
10945 * the Host header followed by the path component if it begins with a slash ('/').
10946 * This means that '*' will not be added, resulting in exactly the first Host
10947 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010948 * is hashed using the path hash followed by a full avalanche hash and provides a
10949 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010950 * high-traffic sites without having to store whole paths.
10951 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010952int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010953smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010954{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010955 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010956 struct hdr_ctx ctx;
10957 unsigned int hash = 0;
10958 char *ptr, *beg, *end;
10959 int len;
10960
10961 CHECK_HTTP_MESSAGE_FIRST();
10962
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010963 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010964 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010965 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010966 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10967 ptr = ctx.line + ctx.val;
10968 len = ctx.vlen;
10969 while (len--)
10970 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10971 }
10972
10973 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010974 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010975 beg = http_get_path(txn);
10976 if (!beg)
10977 beg = end;
10978
10979 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10980
10981 if (beg < ptr && *beg == '/') {
10982 while (beg < ptr)
10983 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10984 }
10985 hash = full_hash(hash);
10986
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010987 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010988 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010989 smp->flags = SMP_F_VOL_1ST;
10990 return 1;
10991}
10992
Willy Tarreau4a550602012-12-09 14:53:32 +010010993/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010994 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10995 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10996 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010997 * that in environments where IPv6 is insignificant, truncating the output to
10998 * 8 bytes would still work.
10999 */
11000static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011001smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011002{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011003 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011004 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011005
11006 if (!cli_conn)
11007 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011008
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011009 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011010 return 0;
11011
Willy Tarreau47ca5452012-12-23 20:22:19 +010011012 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011013 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011014 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011015
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011016 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011017 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011018 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011019 temp->len += 4;
11020 break;
11021 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011022 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011023 temp->len += 16;
11024 break;
11025 default:
11026 return 0;
11027 }
11028
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011029 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011030 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011031 return 1;
11032}
11033
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011034/* Extracts the query string, which comes after the question mark '?'. If no
11035 * question mark is found, nothing is returned. Otherwise it returns a sample
11036 * of type string carrying the whole query string.
11037 */
11038static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011039smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011040{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011041 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011042 char *ptr, *end;
11043
11044 CHECK_HTTP_MESSAGE_FIRST();
11045
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011046 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011047 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11048 end = ptr + txn->req.sl.rq.u_l;
11049
11050 /* look up the '?' */
11051 do {
11052 if (ptr == end)
11053 return 0;
11054 } while (*ptr++ != '?');
11055
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011056 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011057 smp->data.u.str.str = ptr;
11058 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011059 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11060 return 1;
11061}
11062
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011063static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011064smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011065{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011066 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11067 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11068 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011069
Willy Tarreau24e32d82012-04-23 23:55:44 +020011070 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011071
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011072 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011073 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011074 return 1;
11075}
11076
Willy Tarreau7f18e522010-10-22 20:04:13 +020011077/* return a valid test if the current request is the first one on the connection */
11078static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011079smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011080{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011081 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011082 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011083 return 1;
11084}
11085
Willy Tarreau34db1082012-04-19 17:16:54 +020011086/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011087static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011088smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011089{
11090
Willy Tarreau24e32d82012-04-23 23:55:44 +020011091 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011092 return 0;
11093
Willy Tarreauc0239e02012-04-16 14:42:55 +020011094 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011095
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011096 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011097 return 0;
11098
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011099 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011100 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011101 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011102 return 1;
11103}
Willy Tarreau8797c062007-05-07 00:55:35 +020011104
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011105/* Accepts exactly 1 argument of type userlist */
11106static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011107smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011108{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011109 if (!args || args->type != ARGT_USR)
11110 return 0;
11111
11112 CHECK_HTTP_MESSAGE_FIRST();
11113
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011114 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011115 return 0;
11116
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011117 /* if the user does not belong to the userlist or has a wrong password,
11118 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011119 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011120 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011121 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11122 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011123 return 0;
11124
11125 /* pat_match_auth() will need the user list */
11126 smp->ctx.a[0] = args->data.usr;
11127
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011128 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011129 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011130 smp->data.u.str.str = smp->strm->txn->auth.user;
11131 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011132
11133 return 1;
11134}
11135
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011136/* Try to find the next occurrence of a cookie name in a cookie header value.
11137 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11138 * the cookie value is returned into *value and *value_l, and the function
11139 * returns a pointer to the next pointer to search from if the value was found.
11140 * Otherwise if the cookie was not found, NULL is returned and neither value
11141 * nor value_l are touched. The input <hdr> string should first point to the
11142 * header's value, and the <hdr_end> pointer must point to the first character
11143 * not part of the value. <list> must be non-zero if value may represent a list
11144 * of values (cookie headers). This makes it faster to abort parsing when no
11145 * list is expected.
11146 */
David Carlier4686f792015-09-25 14:10:50 +010011147char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011148extract_cookie_value(char *hdr, const char *hdr_end,
11149 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011150 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011151{
11152 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11153 char *next;
11154
11155 /* we search at least a cookie name followed by an equal, and more
11156 * generally something like this :
11157 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11158 */
11159 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11160 /* Iterate through all cookies on this line */
11161
Willy Tarreau2235b262016-11-05 15:50:20 +010011162 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011163 att_beg++;
11164
11165 /* find att_end : this is the first character after the last non
11166 * space before the equal. It may be equal to hdr_end.
11167 */
11168 equal = att_end = att_beg;
11169
11170 while (equal < hdr_end) {
11171 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11172 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011173 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011174 continue;
11175 att_end = equal;
11176 }
11177
11178 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11179 * is between <att_beg> and <equal>, both may be identical.
11180 */
11181
11182 /* look for end of cookie if there is an equal sign */
11183 if (equal < hdr_end && *equal == '=') {
11184 /* look for the beginning of the value */
11185 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011186 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011187 val_beg++;
11188
11189 /* find the end of the value, respecting quotes */
11190 next = find_cookie_value_end(val_beg, hdr_end);
11191
11192 /* make val_end point to the first white space or delimitor after the value */
11193 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011194 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011195 val_end--;
11196 } else {
11197 val_beg = val_end = next = equal;
11198 }
11199
11200 /* We have nothing to do with attributes beginning with '$'. However,
11201 * they will automatically be removed if a header before them is removed,
11202 * since they're supposed to be linked together.
11203 */
11204 if (*att_beg == '$')
11205 continue;
11206
11207 /* Ignore cookies with no equal sign */
11208 if (equal == next)
11209 continue;
11210
11211 /* Now we have the cookie name between att_beg and att_end, and
11212 * its value between val_beg and val_end.
11213 */
11214
11215 if (att_end - att_beg == cookie_name_l &&
11216 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11217 /* let's return this value and indicate where to go on from */
11218 *value = val_beg;
11219 *value_l = val_end - val_beg;
11220 return next + 1;
11221 }
11222
11223 /* Set-Cookie headers only have the name in the first attr=value part */
11224 if (!list)
11225 break;
11226 }
11227
11228 return NULL;
11229}
11230
William Lallemanda43ba4e2014-01-28 18:14:25 +010011231/* Fetch a captured HTTP request header. The index is the position of
11232 * the "capture" option in the configuration file
11233 */
11234static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011235smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011236{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011237 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011238 int idx;
11239
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011240 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011241 return 0;
11242
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011243 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011244
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011245 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 +010011246 return 0;
11247
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011248 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011249 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011250 smp->data.u.str.str = smp->strm->req_cap[idx];
11251 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011252
11253 return 1;
11254}
11255
11256/* Fetch a captured HTTP response header. The index is the position of
11257 * the "capture" option in the configuration file
11258 */
11259static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011260smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011261{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011262 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011263 int idx;
11264
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011265 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011266 return 0;
11267
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011268 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011269
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011270 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 +010011271 return 0;
11272
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011273 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011274 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011275 smp->data.u.str.str = smp->strm->res_cap[idx];
11276 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011277
11278 return 1;
11279}
11280
William Lallemand65ad6e12014-01-31 15:08:02 +010011281/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11282static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011283smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011284{
11285 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011286 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011287 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011288
Willy Tarreau15e91e12015-04-04 00:52:09 +020011289 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011290 return 0;
11291
William Lallemand96a77852014-02-05 00:30:02 +010011292 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011293
William Lallemand96a77852014-02-05 00:30:02 +010011294 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11295 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011296
William Lallemand96a77852014-02-05 00:30:02 +010011297 temp = get_trash_chunk();
11298 temp->str = txn->uri;
11299 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011300 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011301 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011302 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011303
11304 return 1;
11305
11306}
11307
11308/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11309static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011310smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011311{
11312 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011313 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011314 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011315
Willy Tarreau15e91e12015-04-04 00:52:09 +020011316 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011317 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011318
William Lallemand65ad6e12014-01-31 15:08:02 +010011319 ptr = txn->uri;
11320
11321 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11322 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011323
William Lallemand65ad6e12014-01-31 15:08:02 +010011324 if (!*ptr)
11325 return 0;
11326
11327 ptr++; /* skip the space */
11328
11329 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011330 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011331 if (!ptr)
11332 return 0;
11333 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11334 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011335
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011336 smp->data.u.str = *temp;
11337 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011338 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011339 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011340
11341 return 1;
11342}
11343
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011344/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11345 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11346 */
11347static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011348smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011349{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011350 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011351
Willy Tarreau15e91e12015-04-04 00:52:09 +020011352 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011353 return 0;
11354
11355 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011356 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011357 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011358 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011359
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011360 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011361 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011362 smp->flags = SMP_F_CONST;
11363 return 1;
11364
11365}
11366
11367/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11368 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11369 */
11370static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011371smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011372{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011373 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011374
Willy Tarreau15e91e12015-04-04 00:52:09 +020011375 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011376 return 0;
11377
11378 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011379 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011380 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011381 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011382
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011383 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011384 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011385 smp->flags = SMP_F_CONST;
11386 return 1;
11387
11388}
11389
William Lallemand65ad6e12014-01-31 15:08:02 +010011390
Willy Tarreaue333ec92012-04-16 16:26:40 +020011391/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011392 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011393 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011394 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011395 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011396 * Accepts exactly 1 argument of type string. If the input options indicate
11397 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011398 * The returned sample is of type CSTR. Can be used to parse cookies in other
11399 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011400 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011401int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011402{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011403 struct http_txn *txn;
11404 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011405 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011406 const struct http_msg *msg;
11407 const char *hdr_name;
11408 int hdr_name_len;
11409 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011410 int occ = 0;
11411 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011412
Willy Tarreau24e32d82012-04-23 23:55:44 +020011413 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011414 return 0;
11415
Willy Tarreaua890d072013-04-02 12:01:06 +020011416 if (!ctx) {
11417 /* first call */
11418 ctx = &static_hdr_ctx;
11419 ctx->idx = 0;
11420 smp->ctx.a[2] = ctx;
11421 }
11422
Willy Tarreaue333ec92012-04-16 16:26:40 +020011423 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011424
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011425 txn = smp->strm->txn;
11426 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011427
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011428 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011429 msg = &txn->req;
11430 hdr_name = "Cookie";
11431 hdr_name_len = 6;
11432 } else {
11433 msg = &txn->rsp;
11434 hdr_name = "Set-Cookie";
11435 hdr_name_len = 10;
11436 }
11437
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011438 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011439 /* no explicit occurrence and single fetch => last cookie by default */
11440 occ = -1;
11441
11442 /* OK so basically here, either we want only one value and it's the
11443 * last one, or we want to iterate over all of them and we fetch the
11444 * next one.
11445 */
11446
Willy Tarreau9b28e032012-10-12 23:49:43 +020011447 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011448 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011449 /* search for the header from the beginning, we must first initialize
11450 * the search parameters.
11451 */
Willy Tarreau37406352012-04-23 16:16:37 +020011452 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011453 ctx->idx = 0;
11454 }
11455
Willy Tarreau28376d62012-04-26 21:26:10 +020011456 smp->flags |= SMP_F_VOL_HDR;
11457
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011458 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011459 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11460 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011461 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11462 goto out;
11463
Willy Tarreau24e32d82012-04-23 23:55:44 +020011464 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011465 continue;
11466
Willy Tarreau37406352012-04-23 16:16:37 +020011467 smp->ctx.a[0] = ctx->line + ctx->val;
11468 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011469 }
11470
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011471 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011472 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011473 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011474 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011475 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011476 &smp->data.u.str.str,
11477 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011478 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011479 found = 1;
11480 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011481 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011482 smp->flags |= SMP_F_NOT_LAST;
11483 return 1;
11484 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011485 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011486 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011487 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011488 /* all cookie headers and values were scanned. If we're looking for the
11489 * last occurrence, we may return it now.
11490 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011491 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011492 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011493 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011494}
11495
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011496/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011497 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011498 * multiple cookies may be parsed on the same line. The returned sample is of
11499 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011500 */
11501static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011502smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011503{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011504 struct http_txn *txn;
11505 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011506 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011507 const struct http_msg *msg;
11508 const char *hdr_name;
11509 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011510 int cnt;
11511 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011512 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011513
Willy Tarreau24e32d82012-04-23 23:55:44 +020011514 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011515 return 0;
11516
Willy Tarreaue333ec92012-04-16 16:26:40 +020011517 CHECK_HTTP_MESSAGE_FIRST();
11518
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011519 txn = smp->strm->txn;
11520 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011521
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011522 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011523 msg = &txn->req;
11524 hdr_name = "Cookie";
11525 hdr_name_len = 6;
11526 } else {
11527 msg = &txn->rsp;
11528 hdr_name = "Set-Cookie";
11529 hdr_name_len = 10;
11530 }
11531
Willy Tarreau9b28e032012-10-12 23:49:43 +020011532 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011533 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011534 ctx.idx = 0;
11535 cnt = 0;
11536
11537 while (1) {
11538 /* Note: val_beg == NULL every time we need to fetch a new header */
11539 if (!val_beg) {
11540 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11541 break;
11542
Willy Tarreau24e32d82012-04-23 23:55:44 +020011543 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011544 continue;
11545
11546 val_beg = ctx.line + ctx.val;
11547 val_end = val_beg + ctx.vlen;
11548 }
11549
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011550 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011551 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011552 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011553 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011554 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011555 &smp->data.u.str.str,
11556 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011557 cnt++;
11558 }
11559 }
11560
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011561 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011562 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011563 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011564 return 1;
11565}
11566
Willy Tarreau51539362012-05-08 12:46:28 +020011567/* Fetch an cookie's integer value. The integer value is returned. It
11568 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11569 */
11570static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011571smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011572{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011573 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011574
11575 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011576 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011577 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011578 }
11579
11580 return ret;
11581}
11582
Willy Tarreau8797c062007-05-07 00:55:35 +020011583/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011584/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011585/************************************************************************/
11586
David Cournapeau16023ee2010-12-23 20:55:41 +090011587/*
11588 * Given a path string and its length, find the position of beginning of the
11589 * query string. Returns NULL if no query string is found in the path.
11590 *
11591 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11592 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011593 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011594 */
bedis4c75cca2012-10-05 08:38:24 +020011595static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011596{
11597 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011598
bedis4c75cca2012-10-05 08:38:24 +020011599 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011600 return p ? p + 1 : NULL;
11601}
11602
bedis4c75cca2012-10-05 08:38:24 +020011603static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011604{
bedis4c75cca2012-10-05 08:38:24 +020011605 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011606}
11607
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011608/* after increasing a pointer value, it can exceed the first buffer
11609 * size. This function transform the value of <ptr> according with
11610 * the expected position. <chunks> is an array of the one or two
11611 * avalaible chunks. The first value is the start of the first chunk,
11612 * the second value if the end+1 of the first chunks. The third value
11613 * is NULL or the start of the second chunk and the fourth value is
11614 * the end+1 of the second chunk. The function returns 1 if does a
11615 * wrap, else returns 0.
11616 */
11617static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11618{
11619 if (*ptr < chunks[1])
11620 return 0;
11621 if (!chunks[2])
11622 return 0;
11623 *ptr = chunks[2] + ( *ptr - chunks[1] );
11624 return 1;
11625}
11626
David Cournapeau16023ee2010-12-23 20:55:41 +090011627/*
11628 * Given a url parameter, find the starting position of the first occurence,
11629 * or NULL if the parameter is not found.
11630 *
11631 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11632 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011633 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011634 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011635 * or the second chunk. The caller must be check the position before using the
11636 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011637 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011638static const char *
11639find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011640 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011641 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011642{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011643 const char *pos, *last, *equal;
11644 const char **bufs = chunks;
11645 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011646
David Cournapeau16023ee2010-12-23 20:55:41 +090011647
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011648 pos = bufs[0];
11649 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011650 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011651 /* Check the equal. */
11652 equal = pos + url_param_name_l;
11653 if (fix_pointer_if_wrap(chunks, &equal)) {
11654 if (equal >= chunks[3])
11655 return NULL;
11656 } else {
11657 if (equal >= chunks[1])
11658 return NULL;
11659 }
11660 if (*equal == '=') {
11661 if (pos + url_param_name_l > last) {
11662 /* process wrap case, we detect a wrap. In this case, the
11663 * comparison is performed in two parts.
11664 */
11665
11666 /* This is the end, we dont have any other chunk. */
11667 if (bufs != chunks || !bufs[2])
11668 return NULL;
11669
11670 /* Compute the length of each part of the comparison. */
11671 l1 = last - pos;
11672 l2 = url_param_name_l - l1;
11673
11674 /* The second buffer is too short to contain the compared string. */
11675 if (bufs[2] + l2 > bufs[3])
11676 return NULL;
11677
11678 if (memcmp(pos, url_param_name, l1) == 0 &&
11679 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11680 return pos;
11681
11682 /* Perform wrapping and jump the string who fail the comparison. */
11683 bufs += 2;
11684 pos = bufs[0] + l2;
11685 last = bufs[1];
11686
11687 } else {
11688 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011689 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11690 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011691 pos += url_param_name_l + 1;
11692 if (fix_pointer_if_wrap(chunks, &pos))
11693 last = bufs[2];
11694 }
11695 }
11696
11697 while (1) {
11698 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011699 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011700 pos++;
11701 if (pos < last)
11702 break;
11703 /* process buffer wrapping. */
11704 if (bufs != chunks || !bufs[2])
11705 return NULL;
11706 bufs += 2;
11707 pos = bufs[0];
11708 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011709 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011710 pos++;
11711 }
11712 return NULL;
11713}
11714
11715/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011716 * Given a url parameter name and a query string, find the next value.
11717 * An empty url_param_name matches the first available parameter.
11718 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11719 * respectively provide a pointer to the value and its end.
11720 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011721 */
11722static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011723find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011724 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011725 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011726{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011727 const char *arg_start, *qs_end;
11728 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011729
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011730 arg_start = chunks[0];
11731 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011732 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011733 /* Looks for an argument name. */
11734 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011735 url_param_name, url_param_name_l,
11736 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011737 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011738 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011739 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011740 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011741 if (!arg_start)
11742 return 0;
11743
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011744 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011745 while (1) {
11746 /* looks for the first argument. */
11747 value_start = memchr(arg_start, '=', qs_end - arg_start);
11748 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011749 /* Check for wrapping. */
11750 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011751 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011752 chunks[2]) {
11753 arg_start = chunks[2];
11754 qs_end = chunks[3];
11755 continue;
11756 }
11757 return 0;
11758 }
11759 break;
11760 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011761 value_start++;
11762 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011763 else {
11764 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011765 value_start = arg_start + url_param_name_l + 1;
11766
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011767 /* Check for pointer wrapping. */
11768 if (fix_pointer_if_wrap(chunks, &value_start)) {
11769 /* Update the end pointer. */
11770 qs_end = chunks[3];
11771
11772 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011773 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011774 return 0;
11775 }
11776 }
11777
David Cournapeau16023ee2010-12-23 20:55:41 +090011778 value_end = value_start;
11779
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011780 while (1) {
11781 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11782 value_end++;
11783 if (value_end < qs_end)
11784 break;
11785 /* process buffer wrapping. */
11786 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011787 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011788 chunks[2]) {
11789 value_end = chunks[2];
11790 qs_end = chunks[3];
11791 continue;
11792 }
11793 break;
11794 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011795
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011796 *vstart = value_start;
11797 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011798 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011799}
11800
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011801/* This scans a URL-encoded query string. It takes an optionally wrapping
11802 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11803 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11804 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011805 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011806static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011807smp_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 +090011808{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011809 const char *vstart, *vend;
11810 struct chunk *temp;
11811 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011812
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011813 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011814 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011815 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011816 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011817 return 0;
11818
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011819 /* Create sample. If the value is contiguous, return the pointer as CONST,
11820 * if the value is wrapped, copy-it in a buffer.
11821 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011822 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011823 if (chunks[2] &&
11824 vstart >= chunks[0] && vstart <= chunks[1] &&
11825 vend >= chunks[2] && vend <= chunks[3]) {
11826 /* Wrapped case. */
11827 temp = get_trash_chunk();
11828 memcpy(temp->str, vstart, chunks[1] - vstart);
11829 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011830 smp->data.u.str.str = temp->str;
11831 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011832 } else {
11833 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011834 smp->data.u.str.str = (char *)vstart;
11835 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011836 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11837 }
11838
11839 /* Update context, check wrapping. */
11840 chunks[0] = vend;
11841 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11842 chunks[1] = chunks[3];
11843 chunks[2] = NULL;
11844 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011845
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011846 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011847 smp->flags |= SMP_F_NOT_LAST;
11848
David Cournapeau16023ee2010-12-23 20:55:41 +090011849 return 1;
11850}
11851
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011852/* This function iterates over each parameter of the query string. It uses
11853 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011854 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11855 * An optional parameter name is passed in args[0], otherwise any parameter is
11856 * considered. It supports an optional delimiter argument for the beginning of
11857 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011858 */
11859static int
11860smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11861{
11862 struct http_msg *msg;
11863 char delim = '?';
11864 const char *name;
11865 int name_len;
11866
Dragan Dosen26f77e52015-05-25 10:02:11 +020011867 if (!args ||
11868 (args[0].type && args[0].type != ARGT_STR) ||
11869 (args[1].type && args[1].type != ARGT_STR))
11870 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011871
Dragan Dosen26f77e52015-05-25 10:02:11 +020011872 name = "";
11873 name_len = 0;
11874 if (args->type == ARGT_STR) {
11875 name = args->data.str.str;
11876 name_len = args->data.str.len;
11877 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011878
Dragan Dosen26f77e52015-05-25 10:02:11 +020011879 if (args[1].type)
11880 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011881
Dragan Dosen26f77e52015-05-25 10:02:11 +020011882 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011883 CHECK_HTTP_MESSAGE_FIRST();
11884
11885 msg = &smp->strm->txn->req;
11886
11887 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11888 msg->sl.rq.u_l, delim);
11889 if (!smp->ctx.a[0])
11890 return 0;
11891
11892 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011893
11894 /* Assume that the context is filled with NULL pointer
11895 * before the first call.
11896 * smp->ctx.a[2] = NULL;
11897 * smp->ctx.a[3] = NULL;
11898 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011899 }
11900
11901 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11902}
11903
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011904/* This function iterates over each parameter of the body. This requires
11905 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011906 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11907 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11908 * optional second part if the body wraps at the end of the buffer. An optional
11909 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011910 */
11911static int
11912smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11913{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011914 struct http_msg *msg;
11915 unsigned long len;
11916 unsigned long block1;
11917 char *body;
11918 const char *name;
11919 int name_len;
11920
11921 if (!args || (args[0].type && args[0].type != ARGT_STR))
11922 return 0;
11923
11924 name = "";
11925 name_len = 0;
11926 if (args[0].type == ARGT_STR) {
11927 name = args[0].data.str.str;
11928 name_len = args[0].data.str.len;
11929 }
11930
11931 if (!smp->ctx.a[0]) { // first call, find the query string
11932 CHECK_HTTP_MESSAGE_FIRST();
11933
11934 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011935 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011936 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011937 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011938
11939 len = http_body_bytes(msg);
11940 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11941
11942 block1 = len;
11943 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11944 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11945
11946 if (block1 == len) {
11947 /* buffer is not wrapped (or empty) */
11948 smp->ctx.a[0] = body;
11949 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011950
11951 /* Assume that the context is filled with NULL pointer
11952 * before the first call.
11953 * smp->ctx.a[2] = NULL;
11954 * smp->ctx.a[3] = NULL;
11955 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011956 }
11957 else {
11958 /* buffer is wrapped, we need to defragment it */
11959 smp->ctx.a[0] = body;
11960 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011961 smp->ctx.a[2] = msg->chn->buf->data;
11962 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011963 }
11964 }
11965 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11966}
11967
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011968/* Return the signed integer value for the specified url parameter (see url_param
11969 * above).
11970 */
11971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011972smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011973{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011974 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011975
11976 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011977 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011978 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011979 }
11980
11981 return ret;
11982}
11983
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011984/* This produces a 32-bit hash of the concatenation of the first occurrence of
11985 * the Host header followed by the path component if it begins with a slash ('/').
11986 * This means that '*' will not be added, resulting in exactly the first Host
11987 * entry. If no Host header is found, then the path is used. The resulting value
11988 * is hashed using the url hash followed by a full avalanche hash and provides a
11989 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11990 * high-traffic sites without having to store whole paths.
11991 * this differs from the base32 functions in that it includes the url parameters
11992 * as well as the path
11993 */
11994static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011995smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011996{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011997 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011998 struct hdr_ctx ctx;
11999 unsigned int hash = 0;
12000 char *ptr, *beg, *end;
12001 int len;
12002
12003 CHECK_HTTP_MESSAGE_FIRST();
12004
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012005 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012006 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012007 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012008 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12009 ptr = ctx.line + ctx.val;
12010 len = ctx.vlen;
12011 while (len--)
12012 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12013 }
12014
12015 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012016 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 +000012017 beg = http_get_path(txn);
12018 if (!beg)
12019 beg = end;
12020
12021 for (ptr = beg; ptr < end ; ptr++);
12022
12023 if (beg < ptr && *beg == '/') {
12024 while (beg < ptr)
12025 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12026 }
12027 hash = full_hash(hash);
12028
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012029 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012030 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012031 smp->flags = SMP_F_VOL_1ST;
12032 return 1;
12033}
12034
12035/* This concatenates the source address with the 32-bit hash of the Host and
12036 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12037 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12038 * on the source address length. The URL hash is stored before the address so
12039 * that in environments where IPv6 is insignificant, truncating the output to
12040 * 8 bytes would still work.
12041 */
12042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012043smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012044{
12045 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012046 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012047
Dragan Dosendb5af612016-06-16 11:23:01 +020012048 if (!cli_conn)
12049 return 0;
12050
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012051 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012052 return 0;
12053
12054 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012055 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12056 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012057
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012058 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012059 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012060 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012061 temp->len += 4;
12062 break;
12063 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012064 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012065 temp->len += 16;
12066 break;
12067 default:
12068 return 0;
12069 }
12070
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012071 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012072 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012073 return 1;
12074}
12075
Willy Tarreau185b5c42012-04-26 15:11:51 +020012076/* This function is used to validate the arguments passed to any "hdr" fetch
12077 * keyword. These keywords support an optional positive or negative occurrence
12078 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12079 * is assumed that the types are already the correct ones. Returns 0 on error,
12080 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12081 * error message in case of error, that the caller is responsible for freeing.
12082 * The initial location must either be freeable or NULL.
12083 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012084int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012085{
12086 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012087 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012088 return 0;
12089 }
12090 return 1;
12091}
12092
Willy Tarreau276fae92013-07-25 14:36:01 +020012093/* takes an UINT value on input supposed to represent the time since EPOCH,
12094 * adds an optional offset found in args[0] and emits a string representing
12095 * the date in RFC-1123/5322 format.
12096 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012097static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012098{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012099 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012100 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12101 struct chunk *temp;
12102 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012103 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012104 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012105
12106 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012107 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012108 curr_date += args[0].data.sint;
12109
12110 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012111 if (!tm)
12112 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012113
12114 temp = get_trash_chunk();
12115 temp->len = snprintf(temp->str, temp->size - temp->len,
12116 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12117 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12118 tm->tm_hour, tm->tm_min, tm->tm_sec);
12119
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012120 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012121 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012122 return 1;
12123}
12124
Thierry FOURNIERad903512014-04-11 17:51:01 +020012125/* Match language range with language tag. RFC2616 14.4:
12126 *
12127 * A language-range matches a language-tag if it exactly equals
12128 * the tag, or if it exactly equals a prefix of the tag such
12129 * that the first tag character following the prefix is "-".
12130 *
12131 * Return 1 if the strings match, else return 0.
12132 */
12133static inline int language_range_match(const char *range, int range_len,
12134 const char *tag, int tag_len)
12135{
12136 const char *end = range + range_len;
12137 const char *tend = tag + tag_len;
12138 while (range < end) {
12139 if (*range == '-' && tag == tend)
12140 return 1;
12141 if (*range != *tag || tag == tend)
12142 return 0;
12143 range++;
12144 tag++;
12145 }
12146 /* Return true only if the last char of the tag is matched. */
12147 return tag == tend;
12148}
12149
12150/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012151static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012152{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012153 const char *al = smp->data.u.str.str;
12154 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012155 const char *token;
12156 int toklen;
12157 int qvalue;
12158 const char *str;
12159 const char *w;
12160 int best_q = 0;
12161
12162 /* Set the constant to the sample, because the output of the
12163 * function will be peek in the constant configuration string.
12164 */
12165 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012166 smp->data.u.str.size = 0;
12167 smp->data.u.str.str = "";
12168 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012169
12170 /* Parse the accept language */
12171 while (1) {
12172
12173 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012174 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012175 al++;
12176 if (al >= end)
12177 break;
12178
12179 /* Start of the fisrt word. */
12180 token = al;
12181
12182 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012183 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012184 al++;
12185 if (al == token)
12186 goto expect_comma;
12187
12188 /* Length of the token. */
12189 toklen = al - token;
12190 qvalue = 1000;
12191
12192 /* Check if the token exists in the list. If the token not exists,
12193 * jump to the next token.
12194 */
12195 str = args[0].data.str.str;
12196 w = str;
12197 while (1) {
12198 if (*str == ';' || *str == '\0') {
12199 if (language_range_match(token, toklen, w, str-w))
12200 goto look_for_q;
12201 if (*str == '\0')
12202 goto expect_comma;
12203 w = str + 1;
12204 }
12205 str++;
12206 }
12207 goto expect_comma;
12208
12209look_for_q:
12210
12211 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012212 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012213 al++;
12214 if (al >= end)
12215 goto process_value;
12216
12217 /* If ',' is found, process the result */
12218 if (*al == ',')
12219 goto process_value;
12220
12221 /* If the character is different from ';', look
12222 * for the end of the header part in best effort.
12223 */
12224 if (*al != ';')
12225 goto expect_comma;
12226
12227 /* Assumes that the char is ';', now expect "q=". */
12228 al++;
12229
12230 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012231 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012232 al++;
12233 if (al >= end)
12234 goto process_value;
12235
12236 /* Expect 'q'. If no 'q', continue in best effort */
12237 if (*al != 'q')
12238 goto process_value;
12239 al++;
12240
12241 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012242 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012243 al++;
12244 if (al >= end)
12245 goto process_value;
12246
12247 /* Expect '='. If no '=', continue in best effort */
12248 if (*al != '=')
12249 goto process_value;
12250 al++;
12251
12252 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012253 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012254 al++;
12255 if (al >= end)
12256 goto process_value;
12257
12258 /* Parse the q value. */
12259 qvalue = parse_qvalue(al, &al);
12260
12261process_value:
12262
12263 /* If the new q value is the best q value, then store the associated
12264 * language in the response. If qvalue is the biggest value (1000),
12265 * break the process.
12266 */
12267 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012268 smp->data.u.str.str = (char *)w;
12269 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012270 if (qvalue >= 1000)
12271 break;
12272 best_q = qvalue;
12273 }
12274
12275expect_comma:
12276
12277 /* Expect comma or end. If the end is detected, quit the loop. */
12278 while (al < end && *al != ',')
12279 al++;
12280 if (al >= end)
12281 break;
12282
12283 /* Comma is found, jump it and restart the analyzer. */
12284 al++;
12285 }
12286
12287 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012288 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12289 smp->data.u.str.str = args[1].data.str.str;
12290 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012291 }
12292
12293 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012294 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012295}
12296
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012297/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012298static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012299{
12300 /* If the constant flag is set or if not size is avalaible at
12301 * the end of the buffer, copy the string in other buffer
12302 * before decoding.
12303 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012304 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012305 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012306 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12307 smp->data.u.str.str = str->str;
12308 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012309 smp->flags &= ~SMP_F_CONST;
12310 }
12311
12312 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012313 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12314 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012315 return 1;
12316}
12317
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012318static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12319{
12320 struct proxy *fe = strm_fe(smp->strm);
12321 int idx, i;
12322 struct cap_hdr *hdr;
12323 int len;
12324
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012325 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012326 return 0;
12327
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012328 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012329
12330 /* Check the availibity of the capture id. */
12331 if (idx > fe->nb_req_cap - 1)
12332 return 0;
12333
12334 /* Look for the original configuration. */
12335 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12336 hdr != NULL && i != idx ;
12337 i--, hdr = hdr->next);
12338 if (!hdr)
12339 return 0;
12340
12341 /* check for the memory allocation */
12342 if (smp->strm->req_cap[hdr->index] == NULL)
12343 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12344 if (smp->strm->req_cap[hdr->index] == NULL)
12345 return 0;
12346
12347 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012348 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012349 if (len > hdr->len)
12350 len = hdr->len;
12351
12352 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012353 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012354 smp->strm->req_cap[idx][len] = '\0';
12355
12356 return 1;
12357}
12358
12359static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12360{
12361 struct proxy *fe = strm_fe(smp->strm);
12362 int idx, i;
12363 struct cap_hdr *hdr;
12364 int len;
12365
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012366 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012367 return 0;
12368
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012369 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012370
12371 /* Check the availibity of the capture id. */
12372 if (idx > fe->nb_rsp_cap - 1)
12373 return 0;
12374
12375 /* Look for the original configuration. */
12376 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12377 hdr != NULL && i != idx ;
12378 i--, hdr = hdr->next);
12379 if (!hdr)
12380 return 0;
12381
12382 /* check for the memory allocation */
12383 if (smp->strm->res_cap[hdr->index] == NULL)
12384 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12385 if (smp->strm->res_cap[hdr->index] == NULL)
12386 return 0;
12387
12388 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012389 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012390 if (len > hdr->len)
12391 len = hdr->len;
12392
12393 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012394 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012395 smp->strm->res_cap[idx][len] = '\0';
12396
12397 return 1;
12398}
12399
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012400/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012401 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012402 * the relevant part of the request line accordingly. Then it updates various
12403 * pointers to the next elements which were moved, and the total buffer length.
12404 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012405 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12406 * error, though this can be revisited when this code is finally exploited.
12407 *
12408 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12409 * query string and 3 to replace uri.
12410 *
12411 * In query string case, the mark question '?' must be set at the start of the
12412 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012413 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012414int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012415 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012417 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012418 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012419 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012420 int delta;
12421
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012422 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012424 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012425 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12426
12427 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012428 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012429 txn->req.sl.rq.m_l += delta;
12430 txn->req.sl.rq.u += delta;
12431 txn->req.sl.rq.v += delta;
12432 break;
12433
12434 case 1: // path
12435 cur_ptr = http_get_path(txn);
12436 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012437 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012438
12439 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012440 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 +010012441 cur_end++;
12442
12443 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012444 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012445 txn->req.sl.rq.u_l += delta;
12446 txn->req.sl.rq.v += delta;
12447 break;
12448
12449 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012450 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012451 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012452 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12453 while (cur_ptr < cur_end && *cur_ptr != '?')
12454 cur_ptr++;
12455
12456 /* skip the question mark or indicate that we must insert it
12457 * (but only if the format string is not empty then).
12458 */
12459 if (cur_ptr < cur_end)
12460 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012461 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012462 offset = 0;
12463
12464 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012465 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012466 txn->req.sl.rq.u_l += delta;
12467 txn->req.sl.rq.v += delta;
12468 break;
12469
12470 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012471 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012472 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12473
12474 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012475 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012476 txn->req.sl.rq.u_l += delta;
12477 txn->req.sl.rq.v += delta;
12478 break;
12479
12480 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012481 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012482 }
12483
12484 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012485 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012486 txn->req.sl.rq.l += delta;
12487 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012488 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012489 return 0;
12490}
12491
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012492/* This function replace the HTTP status code and the associated message. The
12493 * variable <status> contains the new status code. This function never fails.
12494 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012495void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012496{
12497 struct http_txn *txn = s->txn;
12498 char *cur_ptr, *cur_end;
12499 int delta;
12500 char *res;
12501 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012502 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012503 int msg_len;
12504
12505 chunk_reset(&trash);
12506
12507 res = ultoa_o(status, trash.str, trash.size);
12508 c_l = res - trash.str;
12509
12510 trash.str[c_l] = ' ';
12511 trash.len = c_l + 1;
12512
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012513 /* Do we have a custom reason format string? */
12514 if (msg == NULL)
12515 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012516 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012517 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12518 trash.len += msg_len;
12519
12520 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12521 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12522
12523 /* commit changes and adjust message */
12524 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12525
12526 /* adjust res line offsets and lengths */
12527 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12528 txn->rsp.sl.st.c_l = c_l;
12529 txn->rsp.sl.st.r_l = msg_len;
12530
12531 delta = trash.len - (cur_end - cur_ptr);
12532 txn->rsp.sl.st.l += delta;
12533 txn->hdr_idx.v[0].len += delta;
12534 http_msg_move_end(&txn->rsp, delta);
12535}
12536
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012537/* This function executes one of the set-{method,path,query,uri} actions. It
12538 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012539 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012540 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012541 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12542 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012543 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012544enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012545 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012546{
12547 chunk_reset(&trash);
12548
12549 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012550 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012551 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012552 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012553
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012554 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012555 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012556}
12557
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012558/* This function is just a compliant action wrapper for "set-status". */
12559enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012560 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012561{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012562 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012563 return ACT_RET_CONT;
12564}
12565
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012566/* parse an http-request action among :
12567 * set-method
12568 * set-path
12569 * set-query
12570 * set-uri
12571 *
12572 * All of them accept a single argument of type string representing a log-format.
12573 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12574 * 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 +020012575 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012576 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012577enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12578 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012579{
12580 int cur_arg = *orig_arg;
12581
Thierry FOURNIER42148732015-09-02 17:17:33 +020012582 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012583
12584 switch (args[0][4]) {
12585 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012586 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012587 rule->action_ptr = http_action_set_req_line;
12588 break;
12589 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012590 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012591 rule->action_ptr = http_action_set_req_line;
12592 break;
12593 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012594 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012595 rule->action_ptr = http_action_set_req_line;
12596 break;
12597 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012598 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012599 rule->action_ptr = http_action_set_req_line;
12600 break;
12601 default:
12602 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012603 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012604 }
12605
12606 if (!*args[cur_arg] ||
12607 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12608 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012609 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012610 }
12611
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012612 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012613 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012614 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012615 (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 +010012616 return ACT_RET_PRS_ERR;
12617 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012618
12619 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012620 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012621}
12622
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012623/* parse set-status action:
12624 * This action accepts a single argument of type int representing
12625 * an http status code. It returns ACT_RET_PRS_OK on success,
12626 * ACT_RET_PRS_ERR on error.
12627 */
12628enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12629 struct act_rule *rule, char **err)
12630{
12631 char *error;
12632
Thierry FOURNIER42148732015-09-02 17:17:33 +020012633 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012634 rule->action_ptr = action_http_set_status;
12635
12636 /* Check if an argument is available */
12637 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012638 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012639 return ACT_RET_PRS_ERR;
12640 }
12641
12642 /* convert status code as integer */
12643 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12644 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12645 memprintf(err, "expects an integer status code between 100 and 999");
12646 return ACT_RET_PRS_ERR;
12647 }
12648
12649 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012650
12651 /* set custom reason string */
12652 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12653 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12654 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12655 (*orig_arg)++;
12656 rule->arg.status.reason = strdup(args[*orig_arg]);
12657 (*orig_arg)++;
12658 }
12659
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012660 return ACT_RET_PRS_OK;
12661}
12662
Willy Tarreaua9083d02015-05-08 15:27:59 +020012663/* This function executes the "capture" action. It executes a fetch expression,
12664 * turns the result into a string and puts it in a capture slot. It always
12665 * returns 1. If an error occurs the action is cancelled, but the rule
12666 * processing continues.
12667 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012668enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012669 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012670{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012671 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012672 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012673 char **cap = s->req_cap;
12674 int len;
12675
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012676 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 +020012677 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012678 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012679
12680 if (cap[h->index] == NULL)
12681 cap[h->index] = pool_alloc2(h->pool);
12682
12683 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012684 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012685
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012686 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012687 if (len > h->len)
12688 len = h->len;
12689
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012690 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012691 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012692 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012693}
12694
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012695/* This function executes the "capture" action and store the result in a
12696 * capture slot if exists. It executes a fetch expression, turns the result
12697 * into a string and puts it in a capture slot. It always returns 1. If an
12698 * error occurs the action is cancelled, but the rule processing continues.
12699 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012700enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012701 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012702{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012703 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012704 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012705 char **cap = s->req_cap;
12706 struct proxy *fe = strm_fe(s);
12707 int len;
12708 int i;
12709
12710 /* Look for the original configuration. */
12711 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012712 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012713 i--, h = h->next);
12714 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012715 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012716
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012717 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 +020012718 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012719 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012720
12721 if (cap[h->index] == NULL)
12722 cap[h->index] = pool_alloc2(h->pool);
12723
12724 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012725 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012726
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012727 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012728 if (len > h->len)
12729 len = h->len;
12730
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012731 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012732 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012733 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012734}
12735
Willy Tarreaua9083d02015-05-08 15:27:59 +020012736/* parse an "http-request capture" action. It takes a single argument which is
12737 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012738 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012739 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012740 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012741enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12742 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012743{
12744 struct sample_expr *expr;
12745 struct cap_hdr *hdr;
12746 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012747 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012748
12749 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12750 if (strcmp(args[cur_arg], "if") == 0 ||
12751 strcmp(args[cur_arg], "unless") == 0)
12752 break;
12753
12754 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012755 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012756 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012757 }
12758
Willy Tarreaua9083d02015-05-08 15:27:59 +020012759 cur_arg = *orig_arg;
12760 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12761 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012762 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012763
12764 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12765 memprintf(err,
12766 "fetch method '%s' extracts information from '%s', none of which is available here",
12767 args[cur_arg-1], sample_src_names(expr->fetch->use));
12768 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012769 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012770 }
12771
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012772 if (!args[cur_arg] || !*args[cur_arg]) {
12773 memprintf(err, "expects 'len or 'id'");
12774 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012775 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012776 }
12777
Willy Tarreaua9083d02015-05-08 15:27:59 +020012778 if (strcmp(args[cur_arg], "len") == 0) {
12779 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012780
12781 if (!(px->cap & PR_CAP_FE)) {
12782 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012783 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012784 }
12785
12786 proxy->conf.args.ctx = ARGC_CAP;
12787
Willy Tarreaua9083d02015-05-08 15:27:59 +020012788 if (!args[cur_arg]) {
12789 memprintf(err, "missing length value");
12790 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012791 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012792 }
12793 /* we copy the table name for now, it will be resolved later */
12794 len = atoi(args[cur_arg]);
12795 if (len <= 0) {
12796 memprintf(err, "length must be > 0");
12797 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012798 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012799 }
12800 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012801
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012802 if (!len) {
12803 memprintf(err, "a positive 'len' argument is mandatory");
12804 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012806 }
12807
Vincent Bernat02779b62016-04-03 13:48:43 +020012808 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012809 hdr->next = px->req_cap;
12810 hdr->name = NULL; /* not a header capture */
12811 hdr->namelen = 0;
12812 hdr->len = len;
12813 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12814 hdr->index = px->nb_req_cap++;
12815
12816 px->req_cap = hdr;
12817 px->to_log |= LW_REQHDR;
12818
Thierry FOURNIER42148732015-09-02 17:17:33 +020012819 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012820 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012821 rule->arg.cap.expr = expr;
12822 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012823 }
12824
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012825 else if (strcmp(args[cur_arg], "id") == 0) {
12826 int id;
12827 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012828
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012829 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012830
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012831 if (!args[cur_arg]) {
12832 memprintf(err, "missing id value");
12833 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012834 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012835 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012836
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012837 id = strtol(args[cur_arg], &error, 10);
12838 if (*error != '\0') {
12839 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12840 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012841 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012842 }
12843 cur_arg++;
12844
12845 proxy->conf.args.ctx = ARGC_CAP;
12846
Thierry FOURNIER42148732015-09-02 17:17:33 +020012847 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012848 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012849 rule->arg.capid.expr = expr;
12850 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012851 }
12852
12853 else {
12854 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12855 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012856 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012857 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012858
12859 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012860 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012861}
12862
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012863/* This function executes the "capture" action and store the result in a
12864 * capture slot if exists. It executes a fetch expression, turns the result
12865 * into a string and puts it in a capture slot. It always returns 1. If an
12866 * error occurs the action is cancelled, but the rule processing continues.
12867 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012868enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012869 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012870{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012871 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012872 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012873 char **cap = s->res_cap;
12874 struct proxy *fe = strm_fe(s);
12875 int len;
12876 int i;
12877
12878 /* Look for the original configuration. */
12879 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012880 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012881 i--, h = h->next);
12882 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012883 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012884
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012885 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 +020012886 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012887 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012888
12889 if (cap[h->index] == NULL)
12890 cap[h->index] = pool_alloc2(h->pool);
12891
12892 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012893 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012894
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012895 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012896 if (len > h->len)
12897 len = h->len;
12898
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012899 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012900 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012901 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012902}
12903
12904/* parse an "http-response capture" action. It takes a single argument which is
12905 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12906 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012907 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012908 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012909enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12910 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012911{
12912 struct sample_expr *expr;
12913 int cur_arg;
12914 int id;
12915 char *error;
12916
12917 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12918 if (strcmp(args[cur_arg], "if") == 0 ||
12919 strcmp(args[cur_arg], "unless") == 0)
12920 break;
12921
12922 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012923 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012924 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012925 }
12926
12927 cur_arg = *orig_arg;
12928 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12929 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012930 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012931
12932 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12933 memprintf(err,
12934 "fetch method '%s' extracts information from '%s', none of which is available here",
12935 args[cur_arg-1], sample_src_names(expr->fetch->use));
12936 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012937 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012938 }
12939
12940 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012941 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012942 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012943 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012944 }
12945
12946 if (strcmp(args[cur_arg], "id") != 0) {
12947 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12948 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012949 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012950 }
12951
12952 cur_arg++;
12953
12954 if (!args[cur_arg]) {
12955 memprintf(err, "missing id value");
12956 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012957 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012958 }
12959
12960 id = strtol(args[cur_arg], &error, 10);
12961 if (*error != '\0') {
12962 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12963 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012964 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012965 }
12966 cur_arg++;
12967
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012968 proxy->conf.args.ctx = ARGC_CAP;
12969
Thierry FOURNIER42148732015-09-02 17:17:33 +020012970 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012971 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012972 rule->arg.capid.expr = expr;
12973 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012974
12975 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012976 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012977}
12978
William Lallemand73025dd2014-04-24 14:38:37 +020012979/*
12980 * Return the struct http_req_action_kw associated to a keyword.
12981 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012982struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012983{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012984 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012985}
12986
12987/*
12988 * Return the struct http_res_action_kw associated to a keyword.
12989 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012990struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012991{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012992 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012993}
12994
Willy Tarreau12207b32016-11-22 19:48:51 +010012995
12996/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12997 * now.
12998 */
12999static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
13000{
13001 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13002 return 1;
13003
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013004 if (*args[2]) {
13005 struct proxy *px;
13006
13007 px = proxy_find_by_name(args[2], 0, 0);
13008 if (px)
13009 appctx->ctx.errors.iid = px->uuid;
13010 else
13011 appctx->ctx.errors.iid = atoi(args[2]);
13012
13013 if (!appctx->ctx.errors.iid) {
13014 appctx->ctx.cli.msg = "No such proxy.\n";
13015 appctx->st0 = CLI_ST_PRINT;
13016 return 1;
13017 }
13018 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013019 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013020 appctx->ctx.errors.iid = -1; // dump all proxies
13021
Willy Tarreau35069f82016-11-25 09:16:37 +010013022 appctx->ctx.errors.flag = 0;
13023 if (strcmp(args[3], "request") == 0)
13024 appctx->ctx.errors.flag |= 4; // ignore response
13025 else if (strcmp(args[3], "response") == 0)
13026 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013027 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013028 return 0;
13029}
13030
13031/* This function dumps all captured errors onto the stream interface's
13032 * read buffer. It returns 0 if the output buffer is full and it needs
13033 * to be called again, otherwise non-zero.
13034 */
13035static int cli_io_handler_show_errors(struct appctx *appctx)
13036{
13037 struct stream_interface *si = appctx->owner;
13038 extern const char *monthname[12];
13039
13040 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13041 return 1;
13042
13043 chunk_reset(&trash);
13044
13045 if (!appctx->ctx.errors.px) {
13046 /* the function had not been called yet, let's prepare the
13047 * buffer for a response.
13048 */
13049 struct tm tm;
13050
13051 get_localtime(date.tv_sec, &tm);
13052 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13053 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13054 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13055 error_snapshot_id);
13056
13057 if (bi_putchk(si_ic(si), &trash) == -1) {
13058 /* Socket buffer full. Let's try again later from the same point */
13059 si_applet_cant_put(si);
13060 return 0;
13061 }
13062
13063 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013064 appctx->ctx.errors.bol = 0;
13065 appctx->ctx.errors.ptr = -1;
13066 }
13067
13068 /* we have two inner loops here, one for the proxy, the other one for
13069 * the buffer.
13070 */
13071 while (appctx->ctx.errors.px) {
13072 struct error_snapshot *es;
13073
Willy Tarreau35069f82016-11-25 09:16:37 +010013074 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013075 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013076 if (appctx->ctx.errors.flag & 2) // skip req
13077 goto next;
13078 }
13079 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013080 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013081 if (appctx->ctx.errors.flag & 4) // skip resp
13082 goto next;
13083 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013084
13085 if (!es->when.tv_sec)
13086 goto next;
13087
13088 if (appctx->ctx.errors.iid >= 0 &&
13089 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13090 es->oe->uuid != appctx->ctx.errors.iid)
13091 goto next;
13092
13093 if (appctx->ctx.errors.ptr < 0) {
13094 /* just print headers now */
13095
13096 char pn[INET6_ADDRSTRLEN];
13097 struct tm tm;
13098 int port;
13099
13100 get_localtime(es->when.tv_sec, &tm);
13101 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13102 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13103 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13104
13105 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13106 case AF_INET:
13107 case AF_INET6:
13108 port = get_host_port(&es->src);
13109 break;
13110 default:
13111 port = 0;
13112 }
13113
Willy Tarreau35069f82016-11-25 09:16:37 +010013114 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013115 case 0:
13116 chunk_appendf(&trash,
13117 " frontend %s (#%d): invalid request\n"
13118 " backend %s (#%d)",
13119 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13120 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13121 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13122 break;
13123 case 1:
13124 chunk_appendf(&trash,
13125 " backend %s (#%d): invalid response\n"
13126 " frontend %s (#%d)",
13127 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13128 es->oe->id, es->oe->uuid);
13129 break;
13130 }
13131
13132 chunk_appendf(&trash,
13133 ", server %s (#%d), event #%u\n"
13134 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013135 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013136 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13137 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13138 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13139 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13140 es->ev_id,
13141 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013142 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013143 es->m_clen, es->m_blen,
13144 es->b_flags, es->b_out, es->b_tot,
13145 es->len, es->b_wrap, es->pos);
13146
13147 if (bi_putchk(si_ic(si), &trash) == -1) {
13148 /* Socket buffer full. Let's try again later from the same point */
13149 si_applet_cant_put(si);
13150 return 0;
13151 }
13152 appctx->ctx.errors.ptr = 0;
13153 appctx->ctx.errors.sid = es->sid;
13154 }
13155
13156 if (appctx->ctx.errors.sid != es->sid) {
13157 /* the snapshot changed while we were dumping it */
13158 chunk_appendf(&trash,
13159 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13160 if (bi_putchk(si_ic(si), &trash) == -1) {
13161 si_applet_cant_put(si);
13162 return 0;
13163 }
13164 goto next;
13165 }
13166
13167 /* OK, ptr >= 0, so we have to dump the current line */
13168 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13169 int newptr;
13170 int newline;
13171
13172 newline = appctx->ctx.errors.bol;
13173 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13174 if (newptr == appctx->ctx.errors.ptr)
13175 return 0;
13176
13177 if (bi_putchk(si_ic(si), &trash) == -1) {
13178 /* Socket buffer full. Let's try again later from the same point */
13179 si_applet_cant_put(si);
13180 return 0;
13181 }
13182 appctx->ctx.errors.ptr = newptr;
13183 appctx->ctx.errors.bol = newline;
13184 };
13185 next:
13186 appctx->ctx.errors.bol = 0;
13187 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013188 appctx->ctx.errors.flag ^= 1;
13189 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013190 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013191 }
13192
13193 /* dump complete */
13194 return 1;
13195}
13196
13197/* register cli keywords */
13198static struct cli_kw_list cli_kws = {{ },{
13199 { { "show", "errors", NULL },
13200 "show errors : report last request and response errors for each proxy",
13201 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13202 },
13203 {{},}
13204}};
13205
Willy Tarreau4a568972010-05-12 08:08:50 +020013206/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013207/* All supported ACL keywords must be declared here. */
13208/************************************************************************/
13209
13210/* Note: must not be declared <const> as its list will be overwritten.
13211 * Please take care of keeping this list alphabetically sorted.
13212 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013213static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013214 { "base", "base", PAT_MATCH_STR },
13215 { "base_beg", "base", PAT_MATCH_BEG },
13216 { "base_dir", "base", PAT_MATCH_DIR },
13217 { "base_dom", "base", PAT_MATCH_DOM },
13218 { "base_end", "base", PAT_MATCH_END },
13219 { "base_len", "base", PAT_MATCH_LEN },
13220 { "base_reg", "base", PAT_MATCH_REG },
13221 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013222
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013223 { "cook", "req.cook", PAT_MATCH_STR },
13224 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13225 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13226 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13227 { "cook_end", "req.cook", PAT_MATCH_END },
13228 { "cook_len", "req.cook", PAT_MATCH_LEN },
13229 { "cook_reg", "req.cook", PAT_MATCH_REG },
13230 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013231
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013232 { "hdr", "req.hdr", PAT_MATCH_STR },
13233 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13234 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13235 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13236 { "hdr_end", "req.hdr", PAT_MATCH_END },
13237 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13238 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13239 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013240
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013241 /* these two declarations uses strings with list storage (in place
13242 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13243 * and delete functions are relative to the list management. The parse
13244 * and match method are related to the corresponding fetch methods. This
13245 * is very particular ACL declaration mode.
13246 */
13247 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13248 { "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 +020013249
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013250 { "path", "path", PAT_MATCH_STR },
13251 { "path_beg", "path", PAT_MATCH_BEG },
13252 { "path_dir", "path", PAT_MATCH_DIR },
13253 { "path_dom", "path", PAT_MATCH_DOM },
13254 { "path_end", "path", PAT_MATCH_END },
13255 { "path_len", "path", PAT_MATCH_LEN },
13256 { "path_reg", "path", PAT_MATCH_REG },
13257 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013258
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013259 { "req_ver", "req.ver", PAT_MATCH_STR },
13260 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013261
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013262 { "scook", "res.cook", PAT_MATCH_STR },
13263 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13264 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13265 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13266 { "scook_end", "res.cook", PAT_MATCH_END },
13267 { "scook_len", "res.cook", PAT_MATCH_LEN },
13268 { "scook_reg", "res.cook", PAT_MATCH_REG },
13269 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013270
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013271 { "shdr", "res.hdr", PAT_MATCH_STR },
13272 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13273 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13274 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13275 { "shdr_end", "res.hdr", PAT_MATCH_END },
13276 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13277 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13278 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013279
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013280 { "url", "url", PAT_MATCH_STR },
13281 { "url_beg", "url", PAT_MATCH_BEG },
13282 { "url_dir", "url", PAT_MATCH_DIR },
13283 { "url_dom", "url", PAT_MATCH_DOM },
13284 { "url_end", "url", PAT_MATCH_END },
13285 { "url_len", "url", PAT_MATCH_LEN },
13286 { "url_reg", "url", PAT_MATCH_REG },
13287 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013288
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013289 { "urlp", "urlp", PAT_MATCH_STR },
13290 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13291 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13292 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13293 { "urlp_end", "urlp", PAT_MATCH_END },
13294 { "urlp_len", "urlp", PAT_MATCH_LEN },
13295 { "urlp_reg", "urlp", PAT_MATCH_REG },
13296 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013297
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013298 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013299}};
13300
13301/************************************************************************/
13302/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013303/************************************************************************/
13304/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013305static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013306 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013307 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013308 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13309
Willy Tarreau87b09662015-04-03 00:22:06 +020013310 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013311 { "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 +020013312
13313 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013314 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13315 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13316 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013317
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013318 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13319 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013320
Willy Tarreau409bcde2013-01-08 00:31:00 +010013321 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13322 * are only here to match the ACL's name, are request-only and are used
13323 * for ACL compatibility only.
13324 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013325 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13326 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013327 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13328 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013329
13330 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13331 * only here to match the ACL's name, are request-only and are used for
13332 * ACL compatibility only.
13333 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013334 { "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 +020013335 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013336 { "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 +020013337 { "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 +010013338
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013339 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013340 { "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 +010013341 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013342 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013343 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013344 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013345
13346 /* HTTP protocol on the request path */
13347 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013348 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013349
13350 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013351 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13352 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013353
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013354 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013355 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13356 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013357 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013358
Willy Tarreau18ed2562013-01-14 15:56:36 +010013359 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013360 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13361 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013362
Willy Tarreau18ed2562013-01-14 15:56:36 +010013363 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013364 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013365 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13366 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013367
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013368 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013369 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013370 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013371 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013372 { "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 +010013373 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013374 { "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 +010013375
13376 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013377 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013378 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13379 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013380
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013381 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013382 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013383 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013384 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013385 { "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 +010013386 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013387 { "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 +010013388
Willy Tarreau409bcde2013-01-08 00:31:00 +010013389 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013390 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013391 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13392 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013393 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013394
13395 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013396 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013397 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013398 { "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 +020013399 { "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 +010013400
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013401 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013402 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013403 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013404 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013405 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013406 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013407 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013408 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13409 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013410 { "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 +010013411 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013412}};
13413
Willy Tarreau8797c062007-05-07 00:55:35 +020013414
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013415/************************************************************************/
13416/* All supported converter keywords must be declared here. */
13417/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013418/* Note: must not be declared <const> as its list will be overwritten */
13419static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013420 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013421 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013422 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13423 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013424 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013425 { NULL, NULL, 0, 0, 0 },
13426}};
13427
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013428
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013429/************************************************************************/
13430/* All supported http-request action keywords must be declared here. */
13431/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013432struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013433 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013434 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013435 { "set-method", parse_set_req_line },
13436 { "set-path", parse_set_req_line },
13437 { "set-query", parse_set_req_line },
13438 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013439 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013440 }
13441};
13442
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013443struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013444 .kw = {
13445 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013446 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013447 { NULL, NULL }
13448 }
13449};
13450
Willy Tarreau8797c062007-05-07 00:55:35 +020013451__attribute__((constructor))
13452static void __http_protocol_init(void)
13453{
13454 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013455 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013456 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013457 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013458 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013459 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013460}
13461
13462
Willy Tarreau58f10d72006-12-04 02:26:12 +010013463/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013464 * Local variables:
13465 * c-indent-level: 8
13466 * c-basic-offset: 8
13467 * End:
13468 */