blob: e72e7e9620ed677f61cd1a252980d0b93e40b173 [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);
William Lallemanda73203e2012-03-12 12:48:57 +0100462 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100463}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
Willy Tarreau53b6c742006-12-17 13:37:46 +0100465/*
466 * We have 26 list of methods (1 per first letter), each of which can have
467 * up to 3 entries (2 valid, 1 null).
468 */
469struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100470 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100471 int len;
472 const char text[8];
473};
474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100475const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100476 ['C' - 'A'] = {
477 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
478 },
479 ['D' - 'A'] = {
480 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
481 },
482 ['G' - 'A'] = {
483 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
484 },
485 ['H' - 'A'] = {
486 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
487 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200488 ['O' - 'A'] = {
489 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
490 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100491 ['P' - 'A'] = {
492 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
493 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
494 },
495 ['T' - 'A'] = {
496 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
497 },
498 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200499 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100500 */
501};
502
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100503const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
505 [HTTP_METH_GET] = { "GET", 3 },
506 [HTTP_METH_HEAD] = { "HEAD", 4 },
507 [HTTP_METH_POST] = { "POST", 4 },
508 [HTTP_METH_PUT] = { "PUT", 3 },
509 [HTTP_METH_DELETE] = { "DELETE", 6 },
510 [HTTP_METH_TRACE] = { "TRACE", 5 },
511 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
512};
513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100514/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200515 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100516 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
517 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
518 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
519 * digit. Note: please do not overwrite values in assignment since gcc-2.95
520 * will not handle them correctly. It's worth noting that chars 128..255 are
521 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100522 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100523const unsigned char http_char_classes[256] = {
524 [ 0] = HTTP_FLG_CTL,
525 [ 1] = HTTP_FLG_CTL,
526 [ 2] = HTTP_FLG_CTL,
527 [ 3] = HTTP_FLG_CTL,
528 [ 4] = HTTP_FLG_CTL,
529 [ 5] = HTTP_FLG_CTL,
530 [ 6] = HTTP_FLG_CTL,
531 [ 7] = HTTP_FLG_CTL,
532 [ 8] = HTTP_FLG_CTL,
533 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
534 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
535 [ 11] = HTTP_FLG_CTL,
536 [ 12] = HTTP_FLG_CTL,
537 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
538 [ 14] = HTTP_FLG_CTL,
539 [ 15] = HTTP_FLG_CTL,
540 [ 16] = HTTP_FLG_CTL,
541 [ 17] = HTTP_FLG_CTL,
542 [ 18] = HTTP_FLG_CTL,
543 [ 19] = HTTP_FLG_CTL,
544 [ 20] = HTTP_FLG_CTL,
545 [ 21] = HTTP_FLG_CTL,
546 [ 22] = HTTP_FLG_CTL,
547 [ 23] = HTTP_FLG_CTL,
548 [ 24] = HTTP_FLG_CTL,
549 [ 25] = HTTP_FLG_CTL,
550 [ 26] = HTTP_FLG_CTL,
551 [ 27] = HTTP_FLG_CTL,
552 [ 28] = HTTP_FLG_CTL,
553 [ 29] = HTTP_FLG_CTL,
554 [ 30] = HTTP_FLG_CTL,
555 [ 31] = HTTP_FLG_CTL,
556 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
557 ['!'] = HTTP_FLG_TOK,
558 ['"'] = HTTP_FLG_SEP,
559 ['#'] = HTTP_FLG_TOK,
560 ['$'] = HTTP_FLG_TOK,
561 ['%'] = HTTP_FLG_TOK,
562 ['&'] = HTTP_FLG_TOK,
563 [ 39] = HTTP_FLG_TOK,
564 ['('] = HTTP_FLG_SEP,
565 [')'] = HTTP_FLG_SEP,
566 ['*'] = HTTP_FLG_TOK,
567 ['+'] = HTTP_FLG_TOK,
568 [','] = HTTP_FLG_SEP,
569 ['-'] = HTTP_FLG_TOK,
570 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
571 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
572 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
573 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
574 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
576 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
577 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
579 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
580 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
581 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
582 [':'] = HTTP_FLG_SEP,
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 ['A'] = HTTP_FLG_TOK,
590 ['B'] = HTTP_FLG_TOK,
591 ['C'] = HTTP_FLG_TOK,
592 ['D'] = HTTP_FLG_TOK,
593 ['E'] = HTTP_FLG_TOK,
594 ['F'] = HTTP_FLG_TOK,
595 ['G'] = HTTP_FLG_TOK,
596 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
597 ['I'] = HTTP_FLG_TOK,
598 ['J'] = HTTP_FLG_TOK,
599 ['K'] = HTTP_FLG_TOK,
600 ['L'] = HTTP_FLG_TOK,
601 ['M'] = HTTP_FLG_TOK,
602 ['N'] = HTTP_FLG_TOK,
603 ['O'] = HTTP_FLG_TOK,
604 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
605 ['Q'] = HTTP_FLG_TOK,
606 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
607 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
608 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
609 ['U'] = HTTP_FLG_TOK,
610 ['V'] = HTTP_FLG_TOK,
611 ['W'] = HTTP_FLG_TOK,
612 ['X'] = HTTP_FLG_TOK,
613 ['Y'] = HTTP_FLG_TOK,
614 ['Z'] = HTTP_FLG_TOK,
615 ['['] = HTTP_FLG_SEP,
616 [ 92] = HTTP_FLG_SEP,
617 [']'] = HTTP_FLG_SEP,
618 ['^'] = HTTP_FLG_TOK,
619 ['_'] = HTTP_FLG_TOK,
620 ['`'] = HTTP_FLG_TOK,
621 ['a'] = HTTP_FLG_TOK,
622 ['b'] = HTTP_FLG_TOK,
623 ['c'] = HTTP_FLG_TOK,
624 ['d'] = HTTP_FLG_TOK,
625 ['e'] = HTTP_FLG_TOK,
626 ['f'] = HTTP_FLG_TOK,
627 ['g'] = HTTP_FLG_TOK,
628 ['h'] = HTTP_FLG_TOK,
629 ['i'] = HTTP_FLG_TOK,
630 ['j'] = HTTP_FLG_TOK,
631 ['k'] = HTTP_FLG_TOK,
632 ['l'] = HTTP_FLG_TOK,
633 ['m'] = HTTP_FLG_TOK,
634 ['n'] = HTTP_FLG_TOK,
635 ['o'] = HTTP_FLG_TOK,
636 ['p'] = HTTP_FLG_TOK,
637 ['q'] = HTTP_FLG_TOK,
638 ['r'] = HTTP_FLG_TOK,
639 ['s'] = HTTP_FLG_TOK,
640 ['t'] = HTTP_FLG_TOK,
641 ['u'] = HTTP_FLG_TOK,
642 ['v'] = HTTP_FLG_TOK,
643 ['w'] = HTTP_FLG_TOK,
644 ['x'] = HTTP_FLG_TOK,
645 ['y'] = HTTP_FLG_TOK,
646 ['z'] = HTTP_FLG_TOK,
647 ['{'] = HTTP_FLG_SEP,
648 ['|'] = HTTP_FLG_TOK,
649 ['}'] = HTTP_FLG_SEP,
650 ['~'] = HTTP_FLG_TOK,
651 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100652};
653
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100654/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100655 * Adds a header and its CRLF at the tail of the message's buffer, just before
656 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100657 * The header is also automatically added to the index <hdr_idx>, and the end
658 * of headers is automatically adjusted. The number of bytes added is returned
659 * on success, otherwise <0 is returned indicating an error.
660 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100661int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100662{
663 int bytes, len;
664
665 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200666 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100667 if (!bytes)
668 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100669 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100670 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
671}
672
673/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100674 * Adds a header and its CRLF at the tail of the message's buffer, just before
675 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100676 * the buffer is only opened and the space reserved, but nothing is copied.
677 * The header is also automatically added to the index <hdr_idx>, and the end
678 * of headers is automatically adjusted. The number of bytes added is returned
679 * on success, otherwise <0 is returned indicating an error.
680 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100681int http_header_add_tail2(struct http_msg *msg,
682 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100683{
684 int bytes;
685
Willy Tarreau9b28e032012-10-12 23:49:43 +0200686 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100687 if (!bytes)
688 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100689 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100690 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
691}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692
693/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100694 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
695 * If so, returns the position of the first non-space character relative to
696 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
697 * to return a pointer to the place after the first space. Returns 0 if the
698 * header name does not match. Checks are case-insensitive.
699 */
700int http_header_match2(const char *hdr, const char *end,
701 const char *name, int len)
702{
703 const char *val;
704
705 if (hdr + len >= end)
706 return 0;
707 if (hdr[len] != ':')
708 return 0;
709 if (strncasecmp(hdr, name, len) != 0)
710 return 0;
711 val = hdr + len + 1;
712 while (val < end && HTTP_IS_SPHT(*val))
713 val++;
714 if ((val >= end) && (len + 2 <= end - hdr))
715 return len + 2; /* we may replace starting from second space */
716 return val - hdr;
717}
718
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200719/* Find the first or next occurrence of header <name> in message buffer <sol>
720 * using headers index <idx>, and return it in the <ctx> structure. This
721 * structure holds everything necessary to use the header and find next
722 * occurrence. If its <idx> member is 0, the header is searched from the
723 * beginning. Otherwise, the next occurrence is returned. The function returns
724 * 1 when it finds a value, and 0 when there is no more. It is very similar to
725 * http_find_header2() except that it is designed to work with full-line headers
726 * whose comma is not a delimiter but is part of the syntax. As a special case,
727 * if ctx->val is NULL when searching for a new values of a header, the current
728 * header is rescanned. This allows rescanning after a header deletion.
729 */
730int http_find_full_header2(const char *name, int len,
731 char *sol, struct hdr_idx *idx,
732 struct hdr_ctx *ctx)
733{
734 char *eol, *sov;
735 int cur_idx, old_idx;
736
737 cur_idx = ctx->idx;
738 if (cur_idx) {
739 /* We have previously returned a header, let's search another one */
740 sol = ctx->line;
741 eol = sol + idx->v[cur_idx].len;
742 goto next_hdr;
743 }
744
745 /* first request for this header */
746 sol += hdr_idx_first_pos(idx);
747 old_idx = 0;
748 cur_idx = hdr_idx_first_idx(idx);
749 while (cur_idx) {
750 eol = sol + idx->v[cur_idx].len;
751
752 if (len == 0) {
753 /* No argument was passed, we want any header.
754 * To achieve this, we simply build a fake request. */
755 while (sol + len < eol && sol[len] != ':')
756 len++;
757 name = sol;
758 }
759
760 if ((len < eol - sol) &&
761 (sol[len] == ':') &&
762 (strncasecmp(sol, name, len) == 0)) {
763 ctx->del = len;
764 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100765 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200766 sov++;
767
768 ctx->line = sol;
769 ctx->prev = old_idx;
770 ctx->idx = cur_idx;
771 ctx->val = sov - sol;
772 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100773 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200774 eol--;
775 ctx->tws++;
776 }
777 ctx->vlen = eol - sov;
778 return 1;
779 }
780 next_hdr:
781 sol = eol + idx->v[cur_idx].cr + 1;
782 old_idx = cur_idx;
783 cur_idx = idx->v[cur_idx].next;
784 }
785 return 0;
786}
787
Willy Tarreauc90dc232015-02-20 13:51:36 +0100788/* Find the first or next header field in message buffer <sol> using headers
789 * index <idx>, and return it in the <ctx> structure. This structure holds
790 * everything necessary to use the header and find next occurrence. If its
791 * <idx> member is 0, the first header is retrieved. Otherwise, the next
792 * occurrence is returned. The function returns 1 when it finds a value, and
793 * 0 when there is no more. It is equivalent to http_find_full_header2() with
794 * no header name.
795 */
796int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
797{
798 char *eol, *sov;
799 int cur_idx, old_idx;
800 int len;
801
802 cur_idx = ctx->idx;
803 if (cur_idx) {
804 /* We have previously returned a header, let's search another one */
805 sol = ctx->line;
806 eol = sol + idx->v[cur_idx].len;
807 goto next_hdr;
808 }
809
810 /* first request for this header */
811 sol += hdr_idx_first_pos(idx);
812 old_idx = 0;
813 cur_idx = hdr_idx_first_idx(idx);
814 while (cur_idx) {
815 eol = sol + idx->v[cur_idx].len;
816
817 len = 0;
818 while (1) {
819 if (len >= eol - sol)
820 goto next_hdr;
821 if (sol[len] == ':')
822 break;
823 len++;
824 }
825
826 ctx->del = len;
827 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100828 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100829 sov++;
830
831 ctx->line = sol;
832 ctx->prev = old_idx;
833 ctx->idx = cur_idx;
834 ctx->val = sov - sol;
835 ctx->tws = 0;
836
Willy Tarreau2235b262016-11-05 15:50:20 +0100837 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100838 eol--;
839 ctx->tws++;
840 }
841 ctx->vlen = eol - sov;
842 return 1;
843
844 next_hdr:
845 sol = eol + idx->v[cur_idx].cr + 1;
846 old_idx = cur_idx;
847 cur_idx = idx->v[cur_idx].next;
848 }
849 return 0;
850}
851
Lukas Tribus23953682017-04-28 13:24:30 +0000852/* Find the end of the header value contained between <s> and <e>. See RFC7230,
853 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100854 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200855 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100856char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200857{
858 int quoted, qdpair;
859
860 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100861
862#if defined(__x86_64__) || \
863 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
864 defined(__ARM_ARCH_7A__)
865 /* speedup: skip everything not a comma nor a double quote */
866 for (; s <= e - sizeof(int); s += sizeof(int)) {
867 unsigned int c = *(int *)s; // comma
868 unsigned int q = c; // quote
869
870 c ^= 0x2c2c2c2c; // contains one zero on a comma
871 q ^= 0x22222222; // contains one zero on a quote
872
873 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
874 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
875
876 if ((c | q) & 0x80808080)
877 break; // found a comma or a quote
878 }
879#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200880 for (; s < e; s++) {
881 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200882 else if (quoted) {
883 if (*s == '\\') qdpair = 1;
884 else if (*s == '"') quoted = 0;
885 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200886 else if (*s == '"') quoted = 1;
887 else if (*s == ',') return s;
888 }
889 return s;
890}
891
892/* Find the first or next occurrence of header <name> in message buffer <sol>
893 * using headers index <idx>, and return it in the <ctx> structure. This
894 * structure holds everything necessary to use the header and find next
895 * occurrence. If its <idx> member is 0, the header is searched from the
896 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100897 * 1 when it finds a value, and 0 when there is no more. It is designed to work
898 * with headers defined as comma-separated lists. As a special case, if ctx->val
899 * is NULL when searching for a new values of a header, the current header is
900 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200901 */
902int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100903 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200904 struct hdr_ctx *ctx)
905{
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 char *eol, *sov;
907 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200908
Willy Tarreau68085d82010-01-18 14:54:04 +0100909 cur_idx = ctx->idx;
910 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200911 /* We have previously returned a value, let's search
912 * another one on the same line.
913 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200914 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200915 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200917 eol = sol + idx->v[cur_idx].len;
918
919 if (sov >= eol)
920 /* no more values in this header */
921 goto next_hdr;
922
Willy Tarreau68085d82010-01-18 14:54:04 +0100923 /* values remaining for this header, skip the comma but save it
924 * for later use (eg: for header deletion).
925 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200926 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100927 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200928 sov++;
929
930 goto return_hdr;
931 }
932
933 /* first request for this header */
934 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100935 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200936 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 while (cur_idx) {
938 eol = sol + idx->v[cur_idx].len;
939
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200940 if (len == 0) {
941 /* No argument was passed, we want any header.
942 * To achieve this, we simply build a fake request. */
943 while (sol + len < eol && sol[len] != ':')
944 len++;
945 name = sol;
946 }
947
Willy Tarreau33a7e692007-06-10 19:45:56 +0200948 if ((len < eol - sol) &&
949 (sol[len] == ':') &&
950 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100951 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200952 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100953 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200954 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100955
Willy Tarreau33a7e692007-06-10 19:45:56 +0200956 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100957 ctx->prev = old_idx;
958 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200959 ctx->idx = cur_idx;
960 ctx->val = sov - sol;
961
962 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200963 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100964 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200965 eol--;
966 ctx->tws++;
967 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200968 ctx->vlen = eol - sov;
969 return 1;
970 }
971 next_hdr:
972 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100973 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200974 cur_idx = idx->v[cur_idx].next;
975 }
976 return 0;
977}
978
979int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100980 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200981 struct hdr_ctx *ctx)
982{
983 return http_find_header2(name, strlen(name), sol, idx, ctx);
984}
985
Willy Tarreau68085d82010-01-18 14:54:04 +0100986/* Remove one value of a header. This only works on a <ctx> returned by one of
987 * the http_find_header functions. The value is removed, as well as surrounding
988 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100989 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100990 * message <msg>. The new index is returned. If it is zero, it means there is
991 * no more header, so any processing may stop. The ctx is always left in a form
992 * that can be handled by http_find_header2() to find next occurrence.
993 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100994int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100995{
996 int cur_idx = ctx->idx;
997 char *sol = ctx->line;
998 struct hdr_idx_elem *hdr;
999 int delta, skip_comma;
1000
1001 if (!cur_idx)
1002 return 0;
1003
1004 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001005 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +01001006 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001007 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +01001008 http_msg_move_end(msg, delta);
1009 idx->used--;
1010 hdr->len = 0; /* unused entry */
1011 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +01001012 if (idx->tail == ctx->idx)
1013 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +01001014 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +01001015 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +01001016 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001017 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001018 return ctx->idx;
1019 }
1020
1021 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001022 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1023 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001024 */
1025
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001026 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001027 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001028 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001029 NULL, 0);
1030 hdr->len += delta;
1031 http_msg_move_end(msg, delta);
1032 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001033 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001034 return ctx->idx;
1035}
1036
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001037/* This function handles a server error at the stream interface level. The
1038 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +01001039 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001040 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001041 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001043static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +01001044 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045{
Willy Tarreau2019f952017-03-14 11:07:31 +01001046 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001047 channel_auto_read(si_oc(si));
1048 channel_abort(si_oc(si));
1049 channel_auto_close(si_oc(si));
1050 channel_erase(si_oc(si));
1051 channel_auto_close(si_ic(si));
1052 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001053 if (msg)
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001054 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001055 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001056 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001057 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001058 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059}
1060
Willy Tarreau87b09662015-04-03 00:22:06 +02001061/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001062 * and message.
1063 */
1064
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001065struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +01001066{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001067 const int msgnum = http_get_status_idx(s->txn->status);
1068
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001069 if (s->be->errmsg[msgnum].str)
1070 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001071 else if (strm_fe(s)->errmsg[msgnum].str)
1072 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001073 else
1074 return &http_err_chunks[msgnum];
1075}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001076
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001077void
1078http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1079{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001080 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001081 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001082 stream_int_retnclose(&s->si[0], msg);
1083}
1084
Willy Tarreau53b6c742006-12-17 13:37:46 +01001085/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001086 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1087 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001088 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001089enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001090{
1091 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001093
1094 m = ((unsigned)*str - 'A');
1095
1096 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 for (h = http_methods[m]; h->len > 0; h++) {
1098 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001099 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001101 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001102 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001103 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001104 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001105}
1106
Willy Tarreau21d2af32008-02-14 20:25:24 +01001107/* Parse the URI from the given transaction (which is assumed to be in request
1108 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1109 * It is returned otherwise.
1110 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001111char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001112{
1113 char *ptr, *end;
1114
Willy Tarreau9b28e032012-10-12 23:49:43 +02001115 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001116 end = ptr + txn->req.sl.rq.u_l;
1117
1118 if (ptr >= end)
1119 return NULL;
1120
Lukas Tribus23953682017-04-28 13:24:30 +00001121 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +01001122 * Request-URI = "*" | absuri | abspath | authority
1123 */
1124
1125 if (*ptr == '*')
1126 return NULL;
1127
1128 if (isalpha((unsigned char)*ptr)) {
1129 /* this is a scheme as described by RFC3986, par. 3.1 */
1130 ptr++;
1131 while (ptr < end &&
1132 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1133 ptr++;
1134 /* skip '://' */
1135 if (ptr == end || *ptr++ != ':')
1136 return NULL;
1137 if (ptr == end || *ptr++ != '/')
1138 return NULL;
1139 if (ptr == end || *ptr++ != '/')
1140 return NULL;
1141 }
1142 /* skip [user[:passwd]@]host[:[port]] */
1143
1144 while (ptr < end && *ptr != '/')
1145 ptr++;
1146
1147 if (ptr == end)
1148 return NULL;
1149
1150 /* OK, we got the '/' ! */
1151 return ptr;
1152}
1153
William Lallemand65ad6e12014-01-31 15:08:02 +01001154/* Parse the URI from the given string and look for the "/" beginning the PATH.
1155 * If not found, return NULL. It is returned otherwise.
1156 */
1157static char *
1158http_get_path_from_string(char *str)
1159{
1160 char *ptr = str;
1161
1162 /* RFC2616, par. 5.1.2 :
1163 * Request-URI = "*" | absuri | abspath | authority
1164 */
1165
1166 if (*ptr == '*')
1167 return NULL;
1168
1169 if (isalpha((unsigned char)*ptr)) {
1170 /* this is a scheme as described by RFC3986, par. 3.1 */
1171 ptr++;
1172 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1173 ptr++;
1174 /* skip '://' */
1175 if (*ptr == '\0' || *ptr++ != ':')
1176 return NULL;
1177 if (*ptr == '\0' || *ptr++ != '/')
1178 return NULL;
1179 if (*ptr == '\0' || *ptr++ != '/')
1180 return NULL;
1181 }
1182 /* skip [user[:passwd]@]host[:[port]] */
1183
1184 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1185 ptr++;
1186
1187 if (*ptr == '\0' || *ptr == ' ')
1188 return NULL;
1189
1190 /* OK, we got the '/' ! */
1191 return ptr;
1192}
1193
Willy Tarreau71241ab2012-12-27 11:30:54 +01001194/* Returns a 302 for a redirectable request that reaches a server working in
1195 * in redirect mode. This may only be called just after the stream interface
1196 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1197 * follow normal proxy processing. NOTE: this function is designed to support
1198 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001199 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001200void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001201{
1202 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001203 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001204 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001205 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001206
1207 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001208 trash.len = strlen(HTTP_302);
1209 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001210
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001211 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001212
Willy Tarreauefb453c2008-10-26 20:49:47 +01001213 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001214 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001215 return;
1216
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001217 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001218 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001219 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1220 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001221 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001222
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001223 /* 3: add the request URI. Since it was already forwarded, we need
1224 * to temporarily rewind the buffer.
1225 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001226 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001227 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001228
Willy Tarreauefb453c2008-10-26 20:49:47 +01001229 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001230 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 +02001231
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001232 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001233
Willy Tarreauefb453c2008-10-26 20:49:47 +01001234 if (!path)
1235 return;
1236
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001237 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001238 return;
1239
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001240 memcpy(trash.str + trash.len, path, len);
1241 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001242
1243 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001244 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1245 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001246 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001247 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1248 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001249 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001250
1251 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001252 si_shutr(si);
1253 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001254 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001255 si->state = SI_ST_CLO;
1256
1257 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001258 txn->status = 302;
1259 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001260
1261 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001262 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001263 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001264}
1265
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001266/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001267 * that the server side is closed. Note that err_type is actually a
1268 * bitmask, where almost only aborts may be cumulated with other
1269 * values. We consider that aborted operations are more important
1270 * than timeouts or errors due to the fact that nobody else in the
1271 * logs might explain incomplete retries. All others should avoid
1272 * being cumulated. It should normally not be possible to have multiple
1273 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001274 * Note that connection errors appearing on the second request of a keep-alive
1275 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001276 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001277void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001278{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001279 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001280
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001281 /* set s->txn->status for http_error_message(s) */
1282 s->txn->status = 503;
1283
Willy Tarreauefb453c2008-10-26 20:49:47 +01001284 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001285 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001286 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001287 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001288 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001289 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001290 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001291 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001292 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001293 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001294 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001295 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001296 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001297 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001298 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001299 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001300 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001301 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001302 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001303 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001304 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001305 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001306 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001307 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001308 http_error_message(s));
1309 else { /* SI_ET_CONN_OTHER and others */
1310 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001311 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001312 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001313 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001314}
1315
Willy Tarreau42250582007-04-01 01:30:43 +02001316extern const char sess_term_cond[8];
1317extern const char sess_fin_state[8];
1318extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001319struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001320struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001321struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001322struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323
Willy Tarreau117f59e2007-03-04 18:17:17 +01001324/*
1325 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001326 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001327 */
1328void capture_headers(char *som, struct hdr_idx *idx,
1329 char **cap, struct cap_hdr *cap_hdr)
1330{
1331 char *eol, *sol, *col, *sov;
1332 int cur_idx;
1333 struct cap_hdr *h;
1334 int len;
1335
1336 sol = som + hdr_idx_first_pos(idx);
1337 cur_idx = hdr_idx_first_idx(idx);
1338
1339 while (cur_idx) {
1340 eol = sol + idx->v[cur_idx].len;
1341
1342 col = sol;
1343 while (col < eol && *col != ':')
1344 col++;
1345
1346 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001347 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001348 sov++;
1349
1350 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001351 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001352 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1353 if (cap[h->index] == NULL)
1354 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001355 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001356
1357 if (cap[h->index] == NULL) {
1358 Alert("HTTP capture : out of memory.\n");
1359 continue;
1360 }
1361
1362 len = eol - sov;
1363 if (len > h->len)
1364 len = h->len;
1365
1366 memcpy(cap[h->index], sov, len);
1367 cap[h->index][len]=0;
1368 }
1369 }
1370 sol = eol + idx->v[cur_idx].cr + 1;
1371 cur_idx = idx->v[cur_idx].next;
1372 }
1373}
1374
1375
Willy Tarreau42250582007-04-01 01:30:43 +02001376/* either we find an LF at <ptr> or we jump to <bad>.
1377 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001378#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 +02001379
1380/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1381 * otherwise to <http_msg_ood> with <state> set to <st>.
1382 */
1383#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1384 ptr++; \
1385 if (likely(ptr < end)) \
1386 goto good; \
1387 else { \
1388 state = (st); \
1389 goto http_msg_ood; \
1390 } \
1391 } while (0)
1392
1393
Willy Tarreaubaaee002006-06-26 02:48:02 +02001394/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001395 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001396 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1397 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1398 * will give undefined results.
1399 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1400 * and that msg->sol points to the beginning of the response.
1401 * If a complete line is found (which implies that at least one CR or LF is
1402 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1403 * returned indicating an incomplete line (which does not mean that parts have
1404 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1405 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1406 * upon next call.
1407 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001408 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1410 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001411 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001412 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001413const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001414 enum ht_state state, const char *ptr, const char *end,
1415 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001416{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001417 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001418
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001420 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001421 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001422 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1424
1425 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001426 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1428 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001429 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001430 state = HTTP_MSG_ERROR;
1431 break;
1432
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001434 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001436 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 goto http_msg_rpcode;
1438 }
1439 if (likely(HTTP_IS_SPHT(*ptr)))
1440 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1441 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001442 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001443 state = HTTP_MSG_ERROR;
1444 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001445
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001447 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 if (likely(!HTTP_IS_LWS(*ptr)))
1449 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1450
1451 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001452 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1454 }
1455
1456 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001457 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001458 http_msg_rsp_reason:
1459 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001460 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 msg->sl.st.r_l = 0;
1462 goto http_msg_rpline_eol;
1463
Willy Tarreau8973c702007-01-21 23:58:29 +01001464 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001465 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001467 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001468 goto http_msg_rpreason;
1469 }
1470 if (likely(HTTP_IS_SPHT(*ptr)))
1471 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1472 /* so it's a CR/LF, so there is no reason phrase */
1473 goto http_msg_rsp_reason;
1474
Willy Tarreau8973c702007-01-21 23:58:29 +01001475 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001476 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 if (likely(!HTTP_IS_CRLF(*ptr)))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001479 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 http_msg_rpline_eol:
1481 /* We have seen the end of line. Note that we do not
1482 * necessarily have the \n yet, but at least we know that we
1483 * have EITHER \r OR \n, otherwise the response would not be
1484 * complete. We can then record the response length and return
1485 * to the caller which will be able to register it.
1486 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001487 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 return ptr;
1489
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001491#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001492 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1493 exit(1);
1494#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001495 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001496 }
1497
1498 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001499 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001500 if (ret_state)
1501 *ret_state = state;
1502 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001503 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001504 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001505}
1506
Willy Tarreau8973c702007-01-21 23:58:29 +01001507/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 * This function parses a request line between <ptr> and <end>, starting with
1509 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1510 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1511 * will give undefined results.
1512 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1513 * and that msg->sol points to the beginning of the request.
1514 * If a complete line is found (which implies that at least one CR or LF is
1515 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1516 * returned indicating an incomplete line (which does not mean that parts have
1517 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1518 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1519 * upon next call.
1520 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001521 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1523 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001524 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001525 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001526const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001527 enum ht_state state, const char *ptr, const char *end,
1528 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001529{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001530 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001534 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 if (likely(HTTP_IS_TOKEN(*ptr)))
1536 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001539 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 if (likely(HTTP_IS_CRLF(*ptr))) {
1544 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001545 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001547 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001549 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001551 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 msg->sl.rq.v_l = 0;
1553 goto http_msg_rqline_eol;
1554 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001555 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001556 state = HTTP_MSG_ERROR;
1557 break;
1558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001560 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001562 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 goto http_msg_rquri;
1564 }
1565 if (likely(HTTP_IS_SPHT(*ptr)))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1567 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1568 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001572#if defined(__x86_64__) || \
1573 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1574 defined(__ARM_ARCH_7A__)
1575 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1576 while (ptr <= end - sizeof(int)) {
1577 int x = *(int *)ptr - 0x21212121;
1578 if (x & 0x80808080)
1579 break;
1580
1581 x -= 0x5e5e5e5e;
1582 if (!(x & 0x80808080))
1583 break;
1584
1585 ptr += sizeof(int);
1586 }
1587#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001588 if (ptr >= end) {
1589 state = HTTP_MSG_RQURI;
1590 goto http_msg_ood;
1591 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001592 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001593 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001594 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001597 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1599 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001600
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001601 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001602 /* non-ASCII chars are forbidden unless option
1603 * accept-invalid-http-request is enabled in the frontend.
1604 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001605 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001606 if (msg->err_pos < -1)
1607 goto invalid_char;
1608 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001609 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001610 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1611 }
1612
1613 if (likely(HTTP_IS_CRLF(*ptr))) {
1614 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1615 goto http_msg_req09_uri_e;
1616 }
1617
1618 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001619 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001620 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001621 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001622 state = HTTP_MSG_ERROR;
1623 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001626 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001628 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 goto http_msg_rqver;
1630 }
1631 if (likely(HTTP_IS_SPHT(*ptr)))
1632 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1633 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1634 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001638 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001640
1641 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001642 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001643 http_msg_rqline_eol:
1644 /* We have seen the end of line. Note that we do not
1645 * necessarily have the \n yet, but at least we know that we
1646 * have EITHER \r OR \n, otherwise the request would not be
1647 * complete. We can then record the request length and return
1648 * to the caller which will be able to register it.
1649 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001650 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001651 return ptr;
1652 }
1653
1654 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001655 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001656 state = HTTP_MSG_ERROR;
1657 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001660#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1662 exit(1);
1663#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001664 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001668 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 if (ret_state)
1670 *ret_state = state;
1671 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001672 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001675
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001676/*
1677 * Returns the data from Authorization header. Function may be called more
1678 * than once so data is stored in txn->auth_data. When no header is found
1679 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001680 * searching again for something we are unable to find anyway. However, if
1681 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001682 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001683 */
1684
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001685/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1686 * set according to global.tune.bufsize.
1687 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001688char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001689
1690int
Willy Tarreau87b09662015-04-03 00:22:06 +02001691get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001692{
1693
Willy Tarreaueee5b512015-04-03 23:46:31 +02001694 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001695 struct chunk auth_method;
1696 struct hdr_ctx ctx;
1697 char *h, *p;
1698 int len;
1699
1700#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001701 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001702#endif
1703
1704 if (txn->auth.method == HTTP_AUTH_WRONG)
1705 return 0;
1706
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001707 txn->auth.method = HTTP_AUTH_WRONG;
1708
1709 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001710
1711 if (txn->flags & TX_USE_PX_CONN) {
1712 h = "Proxy-Authorization";
1713 len = strlen(h);
1714 } else {
1715 h = "Authorization";
1716 len = strlen(h);
1717 }
1718
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001719 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001720 return 0;
1721
1722 h = ctx.line + ctx.val;
1723
1724 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001725 len = p - h;
1726 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001727 return 0;
1728
David Carlier7365f7d2016-04-04 11:54:42 +01001729 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1730 return 0;
1731
Willy Tarreau5c557d12016-03-13 08:17:02 +01001732 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001733
1734 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1735
1736 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001737 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001738
1739 if (len < 0)
1740 return 0;
1741
1742
1743 get_http_auth_buff[len] = '\0';
1744
1745 p = strchr(get_http_auth_buff, ':');
1746
1747 if (!p)
1748 return 0;
1749
1750 txn->auth.user = get_http_auth_buff;
1751 *p = '\0';
1752 txn->auth.pass = p+1;
1753
1754 txn->auth.method = HTTP_AUTH_BASIC;
1755 return 1;
1756 }
1757
1758 return 0;
1759}
1760
Willy Tarreau58f10d72006-12-04 02:26:12 +01001761
Willy Tarreau8973c702007-01-21 23:58:29 +01001762/*
1763 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001764 * depending on the initial msg->msg_state. The caller is responsible for
1765 * ensuring that the message does not wrap. The function can be preempted
1766 * everywhere when data are missing and recalled at the exact same location
1767 * with no information loss. The message may even be realigned between two
1768 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001769 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001770 * fields. Note that msg->sol will be initialized after completing the first
1771 * state, so that none of the msg pointers has to be initialized prior to the
1772 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001773 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001774void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775{
Willy Tarreau3770f232013-12-07 00:01:53 +01001776 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001778 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001779
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001780 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001781 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001782 ptr = buf->p + msg->next;
1783 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 if (unlikely(ptr >= end))
1786 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001789 /*
1790 * First, states that are specific to the response only.
1791 * We check them first so that request and headers are
1792 * closer to each other (accessed more often).
1793 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001794 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001795 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001796 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001797 /* we have a start of message, but we have to check
1798 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001799 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001800 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001801 if (unlikely(ptr != buf->p)) {
1802 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001803 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001804 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001805 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001806 }
Willy Tarreau26927362012-05-18 23:22:52 +02001807 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001808 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001809 hdr_idx_init(idx);
1810 state = HTTP_MSG_RPVER;
1811 goto http_msg_rpver;
1812 }
1813
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001814 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1815 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001816 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001817 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001818
1819 if (unlikely(*ptr == '\n'))
1820 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1821 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1822 /* stop here */
1823
Willy Tarreau8973c702007-01-21 23:58:29 +01001824 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001825 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001826 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001827 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1828 /* stop here */
1829
Willy Tarreau8973c702007-01-21 23:58:29 +01001830 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001831 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001832 case HTTP_MSG_RPVER_SP:
1833 case HTTP_MSG_RPCODE:
1834 case HTTP_MSG_RPCODE_SP:
1835 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001836 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001837 state, ptr, end,
1838 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001839 if (unlikely(!ptr))
1840 return;
1841
1842 /* we have a full response and we know that we have either a CR
1843 * or an LF at <ptr>.
1844 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001845 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1846
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001848 if (likely(*ptr == '\r'))
1849 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1850 goto http_msg_rpline_end;
1851
Willy Tarreau8973c702007-01-21 23:58:29 +01001852 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001853 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001854 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001855 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001856 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1857 /* stop here */
1858
1859 /*
1860 * Second, states that are specific to the request only
1861 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001863 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001865 /* we have a start of message, but we have to check
1866 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001867 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001868 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001869 if (likely(ptr != buf->p)) {
1870 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001871 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001872 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001873 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 }
Willy Tarreau26927362012-05-18 23:22:52 +02001875 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001876 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001877 state = HTTP_MSG_RQMETH;
1878 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001880
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001881 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1882 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001884 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 if (unlikely(*ptr == '\n'))
1887 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1888 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001889 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001892 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001893 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001895 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001896
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001897 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001898 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 case HTTP_MSG_RQMETH_SP:
1900 case HTTP_MSG_RQURI:
1901 case HTTP_MSG_RQURI_SP:
1902 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001903 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001904 state, ptr, end,
1905 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001906 if (unlikely(!ptr))
1907 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 /* we have a full request and we know that we have either a CR
1910 * or an LF at <ptr>.
1911 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001913
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 if (likely(*ptr == '\r'))
1916 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001920 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 /* check for HTTP/0.9 request : no version information available.
1922 * msg->sol must point to the first of CR or LF.
1923 */
1924 if (unlikely(msg->sl.rq.v_l == 0))
1925 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001926
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001927 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001929 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001930
Willy Tarreau8973c702007-01-21 23:58:29 +01001931 /*
1932 * Common states below
1933 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001934 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001935 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001936 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 if (likely(!HTTP_IS_CRLF(*ptr))) {
1938 goto http_msg_hdr_name;
1939 }
1940
1941 if (likely(*ptr == '\r'))
1942 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1943 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001946 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001947 /* assumes msg->sol points to the first char */
1948 if (likely(HTTP_IS_TOKEN(*ptr)))
1949 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001950
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001951 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001952 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001953
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001954 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1955 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001956 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001957 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001958
1959 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001960 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001961
1962 /* and we still accept this non-token character */
1963 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001964
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001965 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001966 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001967 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001968 if (likely(HTTP_IS_SPHT(*ptr)))
1969 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001971 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001973
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 if (likely(!HTTP_IS_CRLF(*ptr))) {
1975 goto http_msg_hdr_val;
1976 }
1977
1978 if (likely(*ptr == '\r'))
1979 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1980 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001982 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001983 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001984 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001985 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001987 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001988 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989 if (likely(HTTP_IS_SPHT(*ptr))) {
1990 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 for (; buf->p + msg->sov < ptr; msg->sov++)
1992 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001993 goto http_msg_hdr_l1_sp;
1994 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001995 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001996 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001997 goto http_msg_complete_header;
1998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002000 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002001 /* assumes msg->sol points to the first char, and msg->sov
2002 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002004
2005 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
2006 * and lower. In fact since most of the time is spent in the loop, we
2007 * also remove the sign bit test so that bytes 0x8e..0x0d break the
2008 * loop, but we don't care since they're very rare in header values.
2009 */
2010#if defined(__x86_64__)
2011 while (ptr <= end - sizeof(long)) {
2012 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
2013 goto http_msg_hdr_val2;
2014 ptr += sizeof(long);
2015 }
2016#endif
2017#if defined(__x86_64__) || \
2018 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
2019 defined(__ARM_ARCH_7A__)
2020 while (ptr <= end - sizeof(int)) {
2021 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
2022 goto http_msg_hdr_val2;
2023 ptr += sizeof(int);
2024 }
2025#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002026 if (ptr >= end) {
2027 state = HTTP_MSG_HDR_VAL;
2028 goto http_msg_ood;
2029 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002030 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002031 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002032 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002033
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002034 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002035 /* Note: we could also copy eol into ->eoh so that we have the
2036 * real header end in case it ends with lots of LWS, but is this
2037 * really needed ?
2038 */
2039 if (likely(*ptr == '\r'))
2040 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2041 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002043 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002044 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002045 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002048 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002049 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002050 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2051 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002052 for (; buf->p + msg->eol < ptr; msg->eol++)
2053 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002054 goto http_msg_hdr_val;
2055 }
2056 http_msg_complete_header:
2057 /*
2058 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002059 * Assumes msg->sol points to the first char, msg->sov points
2060 * to the first character of the value and msg->eol to the
2061 * first CR or LF so we know how the line ends. We insert last
2062 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002065 idx, idx->tail) < 0)) {
2066 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002067 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002068 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002069
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002071 if (likely(!HTTP_IS_CRLF(*ptr))) {
2072 goto http_msg_hdr_name;
2073 }
2074
2075 if (likely(*ptr == '\r'))
2076 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2077 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002079 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002080 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002081 /* Assumes msg->sol points to the first of either CR or LF.
2082 * Sets ->sov and ->next to the total header length, ->eoh to
2083 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2084 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002085 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002086 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002088 msg->eoh = msg->sol;
2089 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002090 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002091 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002092 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002093
2094 case HTTP_MSG_ERROR:
2095 /* this may only happen if we call http_msg_analyser() twice with an error */
2096 break;
2097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002098 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002099#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2101 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002102#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002103 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002104 }
2105 http_msg_ood:
2106 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002107 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002108 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002109 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002110
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002111 http_msg_invalid:
2112 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002113 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002114 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002115 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002116 return;
2117}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002118
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002119/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2120 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2121 * nothing is done and 1 is returned.
2122 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002123static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002124{
2125 int delta;
2126 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002127 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002128
2129 if (msg->sl.rq.v_l != 0)
2130 return 1;
2131
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002132 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2133 if (txn->meth != HTTP_METH_GET)
2134 return 0;
2135
Willy Tarreau9b28e032012-10-12 23:49:43 +02002136 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002137
2138 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002139 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2140 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002141 }
2142 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002143 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002144 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002145 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002146 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002147 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002148 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002149 NULL, NULL);
2150 if (unlikely(!cur_end))
2151 return 0;
2152
2153 /* we have a full HTTP/1.0 request now and we know that
2154 * we have either a CR or an LF at <ptr>.
2155 */
2156 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2157 return 1;
2158}
2159
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002160/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002161 * and "keep-alive" values. If we already know that some headers may safely
2162 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002163 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2164 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002165 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002166 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2167 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2168 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002169 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002170 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002171void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002172{
Willy Tarreau5b154472009-12-21 20:11:07 +01002173 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002174 const char *hdr_val = "Connection";
2175 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002176
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002177 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002178 return;
2179
Willy Tarreau88d349d2010-01-25 12:15:43 +01002180 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2181 hdr_val = "Proxy-Connection";
2182 hdr_len = 16;
2183 }
2184
Willy Tarreau5b154472009-12-21 20:11:07 +01002185 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002186 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002187 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002188 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2189 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002190 if (to_del & 2)
2191 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002192 else
2193 txn->flags |= TX_CON_KAL_SET;
2194 }
2195 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2196 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002197 if (to_del & 1)
2198 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002199 else
2200 txn->flags |= TX_CON_CLO_SET;
2201 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002202 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2203 txn->flags |= TX_HDR_CONN_UPG;
2204 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002205 }
2206
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002207 txn->flags |= TX_HDR_CONN_PRS;
2208 return;
2209}
Willy Tarreau5b154472009-12-21 20:11:07 +01002210
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002211/* Apply desired changes on the Connection: header. Values may be removed and/or
2212 * added depending on the <wanted> flags, which are exclusively composed of
2213 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2214 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2215 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002216void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002217{
2218 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002219 const char *hdr_val = "Connection";
2220 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002221
2222 ctx.idx = 0;
2223
Willy Tarreau88d349d2010-01-25 12:15:43 +01002224
2225 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2226 hdr_val = "Proxy-Connection";
2227 hdr_len = 16;
2228 }
2229
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002230 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002231 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002232 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2233 if (wanted & TX_CON_KAL_SET)
2234 txn->flags |= TX_CON_KAL_SET;
2235 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002236 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002237 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002238 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2239 if (wanted & TX_CON_CLO_SET)
2240 txn->flags |= TX_CON_CLO_SET;
2241 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002242 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002243 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002244 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002245
2246 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2247 return;
2248
2249 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2250 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002251 hdr_val = "Connection: close";
2252 hdr_len = 17;
2253 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2254 hdr_val = "Proxy-Connection: close";
2255 hdr_len = 23;
2256 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002257 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002258 }
2259
2260 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2261 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002262 hdr_val = "Connection: keep-alive";
2263 hdr_len = 22;
2264 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2265 hdr_val = "Proxy-Connection: keep-alive";
2266 hdr_len = 28;
2267 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002268 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002269 }
2270 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002271}
2272
Christopher Faulet113f7de2015-12-14 14:52:13 +01002273/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2274 * point to the first byte of data after the chunk size, so that we know we can
2275 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2276 * forming the chunk size. That way it is always possible to differentiate
2277 * between the start of the body and the start of the data. Return the number
2278 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2279 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002280 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002281static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002282{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002283 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002284 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002285 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002286 const char *end = buf->data + buf->size;
2287 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002288 unsigned int chunk = 0;
2289
2290 /* The chunk size is in the following form, though we are only
2291 * interested in the size and CRLF :
2292 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2293 */
2294 while (1) {
2295 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002296 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002297 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002298 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002299 if (c < 0) /* not a hex digit anymore */
2300 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002301 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002302 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002303 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002304 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002305 chunk = (chunk << 4) + c;
2306 }
2307
Willy Tarreaud98cf932009-12-27 22:54:55 +01002308 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002309 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002310 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002311
Willy Tarreau2235b262016-11-05 15:50:20 +01002312 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002313 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002314 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002315 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002316 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002317 }
2318
Willy Tarreaud98cf932009-12-27 22:54:55 +01002319 /* Up to there, we know that at least one byte is present at *ptr. Check
2320 * for the end of chunk size.
2321 */
2322 while (1) {
2323 if (likely(HTTP_IS_CRLF(*ptr))) {
2324 /* we now have a CR or an LF at ptr */
2325 if (likely(*ptr == '\r')) {
2326 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002327 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002328 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002329 return 0;
2330 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002331
Willy Tarreaud98cf932009-12-27 22:54:55 +01002332 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002333 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002334 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002335 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002336 /* done */
2337 break;
2338 }
2339 else if (*ptr == ';') {
2340 /* chunk extension, ends at next CRLF */
2341 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002342 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002343 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002344 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002345
2346 while (!HTTP_IS_CRLF(*ptr)) {
2347 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002348 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002349 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002350 return 0;
2351 }
2352 /* we have a CRLF now, loop above */
2353 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002354 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002355 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002356 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002357 }
2358
Christopher Faulet113f7de2015-12-14 14:52:13 +01002359 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2360 * or may not be present. We save the number of bytes parsed into
2361 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002362 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002363 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002364 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002365 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002366 msg->chunk_len = chunk;
2367 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002368 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002369 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002370 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002371 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002372}
2373
Christopher Faulet113f7de2015-12-14 14:52:13 +01002374/* This function skips trailers in the buffer associated with HTTP message
2375 * <msg>. The first visited position is msg->next. If the end of the trailers is
2376 * found, the function returns >0. So, the caller can automatically schedul it
2377 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2378 * data are available, the function does not change anything except maybe
2379 * msg->sol if it could parse some lines, and returns zero. If a parse error
2380 * is encountered, the function returns < 0 and does not change anything except
2381 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2382 * state before calling this function, which implies that all non-trailers data
2383 * have already been scheduled for forwarding, and that msg->next exactly
2384 * matches the length of trailers already parsed and not forwarded. It is also
2385 * important to note that this function is designed to be able to parse wrapped
2386 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002387 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002388static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002389{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002390 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002391
Christopher Faulet113f7de2015-12-14 14:52:13 +01002392 /* we have msg->next which points to next line. Look for CRLF. But
2393 * first, we reset msg->sol */
2394 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002395 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002396 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002397 const char *start = b_ptr(buf, msg->next + msg->sol);
2398 const char *stop = bi_end(buf);
2399 const char *ptr = start;
2400 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002401
2402 /* scan current line and stop at LF or CRLF */
2403 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002404 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002405 return 0;
2406
2407 if (*ptr == '\n') {
2408 if (!p1)
2409 p1 = ptr;
2410 p2 = ptr;
2411 break;
2412 }
2413
2414 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002415 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002416 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002417 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002418 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002419 p1 = ptr;
2420 }
2421
2422 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002423 if (ptr >= buf->data + buf->size)
2424 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002425 }
2426
2427 /* after LF; point to beginning of next line */
2428 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002429 if (p2 >= buf->data + buf->size)
2430 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002431
Christopher Faulet2fb28802015-12-01 10:40:57 +01002432 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002433 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002434 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002435 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002436
Christopher Fauletd7c91962015-04-30 11:48:27 +02002437 /* LF/CRLF at beginning of line => end of trailers at p2.
2438 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002439 * from this message. */
2440 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002441 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002442
Willy Tarreaud98cf932009-12-27 22:54:55 +01002443 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002444 }
2445}
2446
Christopher Faulet113f7de2015-12-14 14:52:13 +01002447/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2448 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2449 * in order to include this part into the next forwarding phase. Note that the
2450 * caller must ensure that ->p points to the first byte to parse. It returns
2451 * the number of bytes parsed on success, so the caller can set msg_state to
2452 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2453 * change anything and returns zero. If a parse error is encountered, the
2454 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2455 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002456 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002457static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002458{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002459 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002460 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002461 int bytes;
2462
2463 /* NB: we'll check data availabilty at the end. It's not a
2464 * problem because whatever we match first will be checked
2465 * against the correct length.
2466 */
2467 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002468 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002469 if (*ptr == '\r') {
2470 bytes++;
2471 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002472 if (ptr >= buf->data + buf->size)
2473 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002474 }
2475
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002476 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002477 return 0;
2478
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002479 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002480 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002481 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002482 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002483 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002484}
Willy Tarreau5b154472009-12-21 20:11:07 +01002485
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002486/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2487 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2488 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2489 * Unparsable qvalues return 1000 as "q=1.000".
2490 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002491int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002492{
2493 int q = 1000;
2494
Willy Tarreau506c69a2014-07-08 00:59:48 +02002495 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002496 goto out;
2497 q = (*qvalue++ - '0') * 1000;
2498
2499 if (*qvalue++ != '.')
2500 goto out;
2501
Willy Tarreau506c69a2014-07-08 00:59:48 +02002502 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002503 goto out;
2504 q += (*qvalue++ - '0') * 100;
2505
Willy Tarreau506c69a2014-07-08 00:59:48 +02002506 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002507 goto out;
2508 q += (*qvalue++ - '0') * 10;
2509
Willy Tarreau506c69a2014-07-08 00:59:48 +02002510 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002511 goto out;
2512 q += (*qvalue++ - '0') * 1;
2513 out:
2514 if (q > 1000)
2515 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002516 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002517 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002518 return q;
2519}
William Lallemand82fe75c2012-10-23 10:25:10 +02002520
Willy Tarreau87b09662015-04-03 00:22:06 +02002521void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002522{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002523 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002524 int tmp = TX_CON_WANT_KAL;
2525
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002526 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2527 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002528 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2529 tmp = TX_CON_WANT_TUN;
2530
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002531 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002532 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2533 tmp = TX_CON_WANT_TUN;
2534 }
2535
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002536 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002537 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2538 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002539 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002540 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2541 tmp = TX_CON_WANT_CLO;
2542 else
2543 tmp = TX_CON_WANT_SCL;
2544 }
2545
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002546 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002547 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2548 tmp = TX_CON_WANT_CLO;
2549
2550 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2551 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2552
2553 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2554 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2555 /* parse the Connection header and possibly clean it */
2556 int to_del = 0;
2557 if ((msg->flags & HTTP_MSGF_VER_11) ||
2558 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002559 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002560 to_del |= 2; /* remove "keep-alive" */
2561 if (!(msg->flags & HTTP_MSGF_VER_11))
2562 to_del |= 1; /* remove "close" */
2563 http_parse_connection_header(txn, msg, to_del);
2564 }
2565
2566 /* check if client or config asks for explicit close in KAL/SCL */
2567 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2568 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2569 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2570 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2571 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002572 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002573 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2574}
William Lallemand82fe75c2012-10-23 10:25:10 +02002575
Willy Tarreaud787e662009-07-07 10:14:51 +02002576/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2577 * processing can continue on next analysers, or zero if it either needs more
2578 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002579 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002580 * when it has nothing left to do, and may remove any analyser when it wants to
2581 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002582 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002583int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002584{
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 /*
2586 * We will parse the partial (or complete) lines.
2587 * We will check the request syntax, and also join multi-line
2588 * headers. An index of all the lines will be elaborated while
2589 * parsing.
2590 *
2591 * For the parsing, we use a 28 states FSM.
2592 *
2593 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002594 * req->buf->p = beginning of request
2595 * req->buf->p + msg->eoh = end of processed headers / start of current one
2596 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002597 * msg->eol = end of current header or line (LF or CRLF)
2598 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002599 *
2600 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002601 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002602 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2603 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002605
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002607 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002608 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002610 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002611
Willy Tarreau87b09662015-04-03 00:22:06 +02002612 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 +01002613 now_ms, __FUNCTION__,
2614 s,
2615 req,
2616 req->rex, req->wex,
2617 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002618 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002619 req->analysers);
2620
Willy Tarreau52a0c602009-08-16 22:45:38 +02002621 /* we're speaking HTTP here, so let's speak HTTP to the client */
2622 s->srv_error = http_return_srv_error;
2623
Willy Tarreau83e3af02009-12-28 17:39:57 +01002624 /* There's a protected area at the end of the buffer for rewriting
2625 * purposes. We don't want to start to parse the request if the
2626 * protected area is affected, because we may have to move processed
2627 * data later, which is much more complicated.
2628 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002629 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002630
2631 /* This point is executed when some data is avalaible for analysis,
2632 * so we log the end of the idle time. */
2633 if (s->logs.t_idle == -1)
2634 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2635
Willy Tarreau379357a2013-06-08 12:55:46 +02002636 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002637 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002638 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002639 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002640 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002641 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002642 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002643 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002644 return 0;
2645 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002646 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2647 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2648 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002649 }
2650
Willy Tarreau9b28e032012-10-12 23:49:43 +02002651 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002652 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002653 }
2654
Willy Tarreau59234e92008-11-30 23:51:27 +01002655 /* 1: we might have to print this header in debug mode */
2656 if (unlikely((global.mode & MODE_DEBUG) &&
2657 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002658 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660
Willy Tarreau9b28e032012-10-12 23:49:43 +02002661 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002662 /* this is a bit complex : in case of error on the request line,
2663 * we know that rq.l is still zero, so we display only the part
2664 * up to the end of the line (truncated by debug_hdr).
2665 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002666 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002668
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 sol += hdr_idx_first_pos(&txn->hdr_idx);
2670 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 while (cur_idx) {
2673 eol = sol + txn->hdr_idx.v[cur_idx].len;
2674 debug_hdr("clihdr", s, sol, eol);
2675 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2676 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002677 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 }
2679
Willy Tarreau58f10d72006-12-04 02:26:12 +01002680
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /*
2682 * Now we quickly check if we have found a full valid request.
2683 * If not so, we check the FD and buffer states before leaving.
2684 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002685 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002686 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002687 * on a keep-alive stream, if we encounter and error, close, t/o,
2688 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002689 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002690 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002691 * Last, we may increase some tracked counters' http request errors on
2692 * the cases that are deliberately the client's fault. For instance,
2693 * a timeout or connection reset is not counted as an error. However
2694 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696
Willy Tarreau655dce92009-11-08 13:10:58 +01002697 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002698 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002699 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002700 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002701 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002702 stream_inc_http_req_ctr(s);
2703 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002704 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002706 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002707
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 /* 1: Since we are in header mode, if there's no space
2709 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002710 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 * must terminate it now.
2712 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002713 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002714 /* FIXME: check if URI is set and return Status
2715 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002716 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002717 stream_inc_http_req_ctr(s);
2718 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002719 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002720 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 goto return_bad_req;
2723 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002726 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002727 if (!(s->flags & SF_ERR_MASK))
2728 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002729
Willy Tarreaufcffa692010-01-10 14:21:19 +01002730 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002731 goto failed_keep_alive;
2732
Willy Tarreau0f228a02015-05-01 15:37:53 +02002733 if (sess->fe->options & PR_O_IGNORE_PRB)
2734 goto failed_keep_alive;
2735
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002737 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002738 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002739 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002740 }
2741
Willy Tarreaudc979f22012-12-04 10:39:01 +01002742 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002743 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002744 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002745 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002746 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002747 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002748 proxy_inc_fe_req_ctr(sess->fe);
2749 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002750 if (sess->listener->counters)
2751 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002752
Willy Tarreaue7dff022015-04-03 01:14:29 +02002753 if (!(s->flags & SF_FINST_MASK))
2754 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002755 return 0;
2756 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002757
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002759 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002760 if (!(s->flags & SF_ERR_MASK))
2761 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002762
Willy Tarreaufcffa692010-01-10 14:21:19 +01002763 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002764 goto failed_keep_alive;
2765
Willy Tarreau0f228a02015-05-01 15:37:53 +02002766 if (sess->fe->options & PR_O_IGNORE_PRB)
2767 goto failed_keep_alive;
2768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002770 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002771 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002773 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002774 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002775 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002777 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002778 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002779
Willy Tarreau87b09662015-04-03 00:22:06 +02002780 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002781 proxy_inc_fe_req_ctr(sess->fe);
2782 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002783 if (sess->listener->counters)
2784 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002785
Willy Tarreaue7dff022015-04-03 01:14:29 +02002786 if (!(s->flags & SF_FINST_MASK))
2787 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 return 0;
2789 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002790
Willy Tarreau59234e92008-11-30 23:51:27 +01002791 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002792 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002793 if (!(s->flags & SF_ERR_MASK))
2794 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002795
Willy Tarreaufcffa692010-01-10 14:21:19 +01002796 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002797 goto failed_keep_alive;
2798
Willy Tarreau0f228a02015-05-01 15:37:53 +02002799 if (sess->fe->options & PR_O_IGNORE_PRB)
2800 goto failed_keep_alive;
2801
Willy Tarreau4076a152009-04-02 15:18:36 +02002802 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002803 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002805 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002807 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002808 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002809 stream_inc_http_err_ctr(s);
2810 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002811 proxy_inc_fe_req_ctr(sess->fe);
2812 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002813 if (sess->listener->counters)
2814 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002815
Willy Tarreaue7dff022015-04-03 01:14:29 +02002816 if (!(s->flags & SF_FINST_MASK))
2817 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002818 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002819 }
2820
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002821 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002822 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002823 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002824#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002825 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2826 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002827 /* We need more data, we have to re-enable quick-ack in case we
2828 * previously disabled it, otherwise we might cause the client
2829 * to delay next data.
2830 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002831 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002832 }
2833#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002834
Willy Tarreaufcffa692010-01-10 14:21:19 +01002835 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2836 /* If the client starts to talk, let's fall back to
2837 * request timeout processing.
2838 */
2839 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002840 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002841 }
2842
Willy Tarreau59234e92008-11-30 23:51:27 +01002843 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002844 if (!tick_isset(req->analyse_exp)) {
2845 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2846 (txn->flags & TX_WAIT_NEXT_RQ) &&
2847 tick_isset(s->be->timeout.httpka))
2848 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2849 else
2850 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2851 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002852
Willy Tarreau59234e92008-11-30 23:51:27 +01002853 /* we're not ready yet */
2854 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002855
2856 failed_keep_alive:
2857 /* Here we process low-level errors for keep-alive requests. In
2858 * short, if the request is not the first one and it experiences
2859 * a timeout, read error or shutdown, we just silently close so
2860 * that the client can try again.
2861 */
2862 txn->status = 0;
2863 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002864 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002865 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002866 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002867 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002868 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002869 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002871
Willy Tarreaud787e662009-07-07 10:14:51 +02002872 /* OK now we have a complete HTTP request with indexed headers. Let's
2873 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002874 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002875 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002876 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002877 * byte after the last LF. msg->sov points to the first byte of data.
2878 * msg->eol cannot be trusted because it may have been left uninitialized
2879 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002880 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002881
Willy Tarreau87b09662015-04-03 00:22:06 +02002882 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002883 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002884
Willy Tarreaub16a5742010-01-10 14:46:16 +01002885 if (txn->flags & TX_WAIT_NEXT_RQ) {
2886 /* kill the pending keep-alive timeout */
2887 txn->flags &= ~TX_WAIT_NEXT_RQ;
2888 req->analyse_exp = TICK_ETERNITY;
2889 }
2890
2891
Willy Tarreaud787e662009-07-07 10:14:51 +02002892 /* Maybe we found in invalid header name while we were configured not
2893 * to block on that, so we have to capture it now.
2894 */
2895 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002896 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002897
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 /*
2899 * 1: identify the method
2900 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002901 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002902
2903 /* we can make use of server redirect on GET and HEAD */
2904 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002905 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 /*
2908 * 2: check if the URI matches the monitor_uri.
2909 * We have to do this for every request which gets in, because
2910 * the monitor-uri is defined by the frontend.
2911 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002912 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2913 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002914 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002915 sess->fe->monitor_uri,
2916 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002917 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002919 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002920 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002921
Willy Tarreaue7dff022015-04-03 01:14:29 +02002922 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002923 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002924
Willy Tarreau59234e92008-11-30 23:51:27 +01002925 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002926 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002927 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002928
Willy Tarreau59234e92008-11-30 23:51:27 +01002929 ret = acl_pass(ret);
2930 if (cond->pol == ACL_COND_UNLESS)
2931 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002932
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 if (ret) {
2934 /* we fail this request, let's return 503 service unavail */
2935 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002936 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002937 if (!(s->flags & SF_ERR_MASK))
2938 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002940 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002941 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002942
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 /* nothing to fail, let's reply normaly */
2944 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002945 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002946 if (!(s->flags & SF_ERR_MASK))
2947 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 goto return_prx_cond;
2949 }
2950
2951 /*
2952 * 3: Maybe we have to copy the original REQURI for the logs ?
2953 * Note: we cannot log anymore if the request has been
2954 * classified as invalid.
2955 */
2956 if (unlikely(s->logs.logwait & LW_REQ)) {
2957 /* we have a complete HTTP request that we must log */
2958 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2959 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002960
Stéphane Cottin23e9e932017-05-18 08:58:41 +02002961 if (urilen >= global.tune.requri_len )
2962 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002963 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002965
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002966 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 s->do_log(s);
2968 } else {
2969 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002970 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002971 }
Willy Tarreau06619262006-12-17 08:37:22 +01002972
Willy Tarreau91852eb2015-05-01 13:26:00 +02002973 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2974 * exactly one digit "." one digit. This check may be disabled using
2975 * option accept-invalid-http-request.
2976 */
2977 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2978 if (msg->sl.rq.v_l != 8) {
2979 msg->err_pos = msg->sl.rq.v;
2980 goto return_bad_req;
2981 }
2982
2983 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2984 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2985 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2986 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2987 msg->err_pos = msg->sl.rq.v + 4;
2988 goto return_bad_req;
2989 }
2990 }
Willy Tarreau13317662015-05-01 13:47:08 +02002991 else {
2992 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2993 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2994 goto return_bad_req;
2995 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002996
Willy Tarreau5b154472009-12-21 20:11:07 +01002997 /* ... and check if the request is HTTP/1.1 or above */
2998 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002999 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3000 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3001 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003002 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003003
3004 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003005 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 +01003006
Willy Tarreau88d349d2010-01-25 12:15:43 +01003007 /* if the frontend has "option http-use-proxy-header", we'll check if
3008 * we have what looks like a proxied connection instead of a connection,
3009 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3010 * Note that this is *not* RFC-compliant, however browsers and proxies
3011 * happen to do that despite being non-standard :-(
3012 * We consider that a request not beginning with either '/' or '*' is
3013 * a proxied connection, which covers both "scheme://location" and
3014 * CONNECT ip:port.
3015 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003016 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003017 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003018 txn->flags |= TX_USE_PX_CONN;
3019
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003020 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003021 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003022
Willy Tarreau59234e92008-11-30 23:51:27 +01003023 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003024 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003025 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003026 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003027
Willy Tarreau557f1992015-05-01 10:05:17 +02003028 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3029 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003030 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003031 * The length of a message body is determined by one of the following
3032 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003033 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003034 * 1. Any response to a HEAD request and any response with a 1xx
3035 * (Informational), 204 (No Content), or 304 (Not Modified) status
3036 * code is always terminated by the first empty line after the
3037 * header fields, regardless of the header fields present in the
3038 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003039 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003040 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3041 * the connection will become a tunnel immediately after the empty
3042 * line that concludes the header fields. A client MUST ignore any
3043 * Content-Length or Transfer-Encoding header fields received in
3044 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003045 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003046 * 3. If a Transfer-Encoding header field is present and the chunked
3047 * transfer coding (Section 4.1) is the final encoding, the message
3048 * body length is determined by reading and decoding the chunked
3049 * data until the transfer coding indicates the data is complete.
3050 *
3051 * If a Transfer-Encoding header field is present in a response and
3052 * the chunked transfer coding is not the final encoding, the
3053 * message body length is determined by reading the connection until
3054 * it is closed by the server. If a Transfer-Encoding header field
3055 * is present in a request and the chunked transfer coding is not
3056 * the final encoding, the message body length cannot be determined
3057 * reliably; the server MUST respond with the 400 (Bad Request)
3058 * status code and then close the connection.
3059 *
3060 * If a message is received with both a Transfer-Encoding and a
3061 * Content-Length header field, the Transfer-Encoding overrides the
3062 * Content-Length. Such a message might indicate an attempt to
3063 * perform request smuggling (Section 9.5) or response splitting
3064 * (Section 9.4) and ought to be handled as an error. A sender MUST
3065 * remove the received Content-Length field prior to forwarding such
3066 * a message downstream.
3067 *
3068 * 4. If a message is received without Transfer-Encoding and with
3069 * either multiple Content-Length header fields having differing
3070 * field-values or a single Content-Length header field having an
3071 * invalid value, then the message framing is invalid and the
3072 * recipient MUST treat it as an unrecoverable error. If this is a
3073 * request message, the server MUST respond with a 400 (Bad Request)
3074 * status code and then close the connection. If this is a response
3075 * message received by a proxy, the proxy MUST close the connection
3076 * to the server, discard the received response, and send a 502 (Bad
3077 * Gateway) response to the client. If this is a response message
3078 * received by a user agent, the user agent MUST close the
3079 * connection to the server and discard the received response.
3080 *
3081 * 5. If a valid Content-Length header field is present without
3082 * Transfer-Encoding, its decimal value defines the expected message
3083 * body length in octets. If the sender closes the connection or
3084 * the recipient times out before the indicated number of octets are
3085 * received, the recipient MUST consider the message to be
3086 * incomplete and close the connection.
3087 *
3088 * 6. If this is a request message and none of the above are true, then
3089 * the message body length is zero (no message body is present).
3090 *
3091 * 7. Otherwise, this is a response message without a declared message
3092 * body length, so the message body length is determined by the
3093 * number of octets received prior to the server closing the
3094 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003095 */
3096
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003098 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003099 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003100 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02003101 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003102 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003103 /* chunked not last, return badreq */
3104 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003105 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003106 }
3107
Willy Tarreau1c913912015-04-30 10:57:51 +02003108 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003109 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003110 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3111 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3112 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3113 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003114 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003115 signed long long cl;
3116
Willy Tarreauad14f752011-09-02 20:33:27 +02003117 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003118 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003119 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003120 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003121
Willy Tarreauad14f752011-09-02 20:33:27 +02003122 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003123 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003124 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003125 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003126
Willy Tarreauad14f752011-09-02 20:33:27 +02003127 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003128 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003129 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003130 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003131
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003132 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003133 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003134 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003135 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003136
Christopher Fauletbe821b92017-03-30 11:21:53 +02003137 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003138 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003139 }
3140
Willy Tarreau34dfc602015-05-01 10:09:49 +02003141 /* even bodyless requests have a known length */
3142 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003143
Willy Tarreau179085c2014-04-28 16:48:56 +02003144 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3145 * only change if both the request and the config reference something else.
3146 * Option httpclose by itself sets tunnel mode where headers are mangled.
3147 * However, if another mode is set, it will affect it (eg: server-close/
3148 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3149 * if FE and BE have the same settings (common). The method consists in
3150 * checking if options changed between the two calls (implying that either
3151 * one is non-null, or one of them is non-null and we are there for the first
3152 * time.
3153 */
3154 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003155 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003156 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003157
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003158 /* we may have to wait for the request's body */
3159 if ((s->be->options & PR_O_WREQ_BODY) &&
3160 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3161 req->analysers |= AN_REQ_HTTP_BODY;
3162
Willy Tarreaud787e662009-07-07 10:14:51 +02003163 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003164 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003165 req->analyse_exp = TICK_ETERNITY;
3166 return 1;
3167
3168 return_bad_req:
3169 /* We centralize bad requests processing here */
3170 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3171 /* we detected a parsing error. We want to archive this request
3172 * in the dedicated proxy area for later troubleshooting.
3173 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003174 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003175 }
3176
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003177 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003178 txn->req.msg_state = HTTP_MSG_ERROR;
3179 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003180 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003181
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003182 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003183 if (sess->listener->counters)
3184 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003185
3186 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003187 if (!(s->flags & SF_ERR_MASK))
3188 s->flags |= SF_ERR_PRXCOND;
3189 if (!(s->flags & SF_FINST_MASK))
3190 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003191
Christopher Faulet0184ea72017-01-05 14:06:34 +01003192 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003193 req->analyse_exp = TICK_ETERNITY;
3194 return 0;
3195}
3196
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003197
Willy Tarreau347a35d2013-11-22 17:51:09 +01003198/* This function prepares an applet to handle the stats. It can deal with the
3199 * "100-continue" expectation, check that admin rules are met for POST requests,
3200 * and program a response message if something was unexpected. It cannot fail
3201 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003202 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003204 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003205 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003206int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003207{
3208 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003209 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003210 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003211 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003212 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003213 struct uri_auth *uri_auth = s->be->uri_auth;
3214 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003215 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003217 appctx = si_appctx(si);
3218 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3219 appctx->st1 = appctx->st2 = 0;
3220 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3221 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003222 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003223 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003224
3225 uri = msg->chn->buf->p + msg->sl.rq.u;
3226 lookup = uri + uri_auth->uri_len;
3227
3228 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3229 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003230 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003231 break;
3232 }
3233 }
3234
3235 if (uri_auth->refresh) {
3236 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3237 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003238 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003239 break;
3240 }
3241 }
3242 }
3243
3244 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3245 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003246 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003247 break;
3248 }
3249 }
3250
Willy Tarreau1e62df92016-01-11 18:57:53 +01003251 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3252 if (memcmp(h, ";typed", 6) == 0) {
3253 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3254 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3255 break;
3256 }
3257 }
3258
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003259 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3260 if (memcmp(h, ";st=", 4) == 0) {
3261 int i;
3262 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003264 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3265 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003266 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003267 break;
3268 }
3269 }
3270 break;
3271 }
3272 }
3273
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003274 appctx->ctx.stats.scope_str = 0;
3275 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003276 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3277 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3278 int itx = 0;
3279 const char *h2;
3280 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3281 const char *err;
3282
3283 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3284 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003286 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3287 itx++;
3288 h++;
3289 }
3290
3291 if (itx > STAT_SCOPE_TXT_MAXLEN)
3292 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003294
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003295 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003296 memcpy(scope_txt, h2, itx);
3297 scope_txt[itx] = '\0';
3298 err = invalid_char(scope_txt);
3299 if (err) {
3300 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003301 appctx->ctx.stats.scope_str = 0;
3302 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003303 }
3304 break;
3305 }
3306 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003307
3308 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003309 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003310 int ret = 1;
3311
3312 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003313 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 +01003314 ret = acl_pass(ret);
3315 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3316 ret = !ret;
3317 }
3318
3319 if (ret) {
3320 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003321 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003322 break;
3323 }
3324 }
3325
3326 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003327 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003328 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003329 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003330 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3331 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003332 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003333 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003334 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003335 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3336 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003337 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003338 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003339 else {
3340 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003341 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003342 }
3343
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003344 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003345 return 1;
3346}
3347
Lukas Tribus67db8df2013-06-23 17:37:13 +02003348/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3349 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3350 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003351void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003352{
3353#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003354 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003355 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3356#endif
3357#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003358 if (from->ss_family == AF_INET6) {
3359 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003360 /* v4-mapped addresses need IP_TOS */
3361 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3362 else
3363 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3364 }
3365#endif
3366}
3367
Willy Tarreau87b09662015-04-03 00:22:06 +02003368int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003369 const char* name, unsigned int name_len,
3370 const char *str, struct my_regex *re,
3371 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003372{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003373 struct hdr_ctx ctx;
3374 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003375 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003376 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3377 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003378 struct chunk *output = get_trash_chunk();
3379
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003380 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003381
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003382 /* Choose the header browsing function. */
3383 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003384 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003385 http_find_hdr_func = http_find_header2;
3386 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003387 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003388 http_find_hdr_func = http_find_full_header2;
3389 break;
3390 default: /* impossible */
3391 return -1;
3392 }
3393
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003394 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3395 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003396 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003397 char *val = ctx.line + ctx.val;
3398 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003399
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003400 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3401 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003402
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003403 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003404 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003405 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003406
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003407 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003408
3409 hdr->len += delta;
3410 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003411
3412 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003413 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003414 }
3415
3416 return 0;
3417}
3418
Willy Tarreau87b09662015-04-03 00:22:06 +02003419static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003420 const char* name, unsigned int name_len,
3421 struct list *fmt, struct my_regex *re,
3422 int action)
3423{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003424 struct chunk *replace;
3425 int ret = -1;
3426
3427 replace = alloc_trash_chunk();
3428 if (!replace)
3429 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003430
3431 replace->len = build_logline(s, replace->str, replace->size, fmt);
3432 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003433 goto leave;
3434
3435 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003436
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003437 leave:
3438 free_trash_chunk(replace);
3439 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003440}
3441
Willy Tarreau87b09662015-04-03 00:22:06 +02003442/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003443 * transaction <txn>. Returns the verdict of the first rule that prevents
3444 * further processing of the request (auth, deny, ...), and defaults to
3445 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3446 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003447 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3448 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3449 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003450 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003451enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003452http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003453{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003454 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003455 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003456 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003457 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003458 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003459 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003460 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003461 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003462
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003463 /* If "the current_rule_list" match the executed rule list, we are in
3464 * resume condition. If a resume is needed it is always in the action
3465 * and never in the ACL or converters. In this case, we initialise the
3466 * current rule, and go to the action execution point.
3467 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003468 if (s->current_rule) {
3469 rule = s->current_rule;
3470 s->current_rule = NULL;
3471 if (s->current_rule_list == rules)
3472 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003473 }
3474 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003475
Willy Tarreauff011f22011-01-06 17:51:27 +01003476 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003477
Willy Tarreau96257ec2012-12-27 10:46:37 +01003478 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003479 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003480 int ret;
3481
Willy Tarreau192252e2015-04-04 01:47:55 +02003482 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003483 ret = acl_pass(ret);
3484
Willy Tarreauff011f22011-01-06 17:51:27 +01003485 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003486 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003487
3488 if (!ret) /* condition not matched */
3489 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003490 }
3491
Willy Tarreauacc98002015-09-27 23:34:39 +02003492 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003493resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003494 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003495 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003496 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003497
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003498 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003499 if (deny_status)
3500 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003501 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003502
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003503 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003504 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003505 if (deny_status)
3506 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003507 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003508
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003509 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003510 /* Auth might be performed on regular http-req rules as well as on stats */
3511 auth_realm = rule->arg.auth.realm;
3512 if (!auth_realm) {
3513 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3514 auth_realm = STATS_DEFAULT_REALM;
3515 else
3516 auth_realm = px->id;
3517 }
3518 /* send 401/407 depending on whether we use a proxy or not. We still
3519 * count one error, because normal browsing won't significantly
3520 * increase the counter but brute force attempts will.
3521 */
3522 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3523 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003524 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003525 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003526 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003527
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003528 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003529 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3530 return HTTP_RULE_RES_BADREQ;
3531 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003533 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003534 s->task->nice = rule->arg.nice;
3535 break;
3536
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003537 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003538 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003539 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003540 break;
3541
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003542 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003543#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003544 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003545 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003546#endif
3547 break;
3548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003549 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003550 s->logs.level = rule->arg.loglevel;
3551 break;
3552
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003553 case ACT_HTTP_REPLACE_HDR:
3554 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003555 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3556 rule->arg.hdr_add.name_len,
3557 &rule->arg.hdr_add.fmt,
3558 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003559 return HTTP_RULE_RES_BADREQ;
3560 break;
3561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003562 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003563 ctx.idx = 0;
3564 /* remove all occurrences of the header */
3565 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3566 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3567 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003568 }
Willy Tarreau85603282015-01-21 20:39:27 +01003569 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003570
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003571 case ACT_HTTP_SET_HDR:
3572 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003573 /* The scope of the trash buffer must be limited to this function. The
3574 * build_logline() function can execute a lot of other function which
3575 * can use the trash buffer. So for limiting the scope of this global
3576 * buffer, we build first the header value using build_logline, and
3577 * after we store the header name.
3578 */
3579 len = rule->arg.hdr_add.name_len + 2,
3580 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003581 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003582 trash.str[rule->arg.hdr_add.name_len] = ':';
3583 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3584 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003585
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003586 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003587 /* remove all occurrences of the header */
3588 ctx.idx = 0;
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);
3592 }
3593 }
3594
Willy Tarreau96257ec2012-12-27 10:46:37 +01003595 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3596 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003597
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003598 case ACT_HTTP_DEL_ACL:
3599 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003600 struct pat_ref *ref;
3601 char *key;
3602 int len;
3603
3604 /* collect reference */
3605 ref = pat_ref_lookup(rule->arg.map.ref);
3606 if (!ref)
3607 continue;
3608
3609 /* collect key */
3610 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3611 key = trash.str;
3612 key[len] = '\0';
3613
3614 /* perform update */
3615 /* returned code: 1=ok, 0=ko */
3616 pat_ref_delete(ref, key);
3617
3618 break;
3619 }
3620
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003621 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003622 struct pat_ref *ref;
3623 char *key;
3624 struct chunk *trash_key;
3625 int len;
3626
3627 trash_key = get_trash_chunk();
3628
3629 /* collect reference */
3630 ref = pat_ref_lookup(rule->arg.map.ref);
3631 if (!ref)
3632 continue;
3633
3634 /* collect key */
3635 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3636 key = trash_key->str;
3637 key[len] = '\0';
3638
3639 /* perform update */
3640 /* add entry only if it does not already exist */
3641 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003642 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003643
3644 break;
3645 }
3646
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003647 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003648 struct pat_ref *ref;
3649 char *key, *value;
3650 struct chunk *trash_key, *trash_value;
3651 int len;
3652
3653 trash_key = get_trash_chunk();
3654 trash_value = get_trash_chunk();
3655
3656 /* collect reference */
3657 ref = pat_ref_lookup(rule->arg.map.ref);
3658 if (!ref)
3659 continue;
3660
3661 /* collect key */
3662 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3663 key = trash_key->str;
3664 key[len] = '\0';
3665
3666 /* collect value */
3667 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3668 value = trash_value->str;
3669 value[len] = '\0';
3670
3671 /* perform update */
3672 if (pat_ref_find_elt(ref, key) != NULL)
3673 /* update entry if it exists */
3674 pat_ref_set(ref, key, value, NULL);
3675 else
3676 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003677 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003678
3679 break;
3680 }
William Lallemand73025dd2014-04-24 14:38:37 +02003681
Thierry FOURNIER42148732015-09-02 17:17:33 +02003682 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003683 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3684 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003685
Willy Tarreauacc98002015-09-27 23:34:39 +02003686 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003687 case ACT_RET_ERR:
3688 case ACT_RET_CONT:
3689 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003690 case ACT_RET_STOP:
3691 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003692 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003693 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003694 return HTTP_RULE_RES_YIELD;
3695 }
William Lallemand73025dd2014-04-24 14:38:37 +02003696 break;
3697
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003698 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003699 /* Note: only the first valid tracking parameter of each
3700 * applies.
3701 */
3702
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003703 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003704 struct stktable *t;
3705 struct stksess *ts;
3706 struct stktable_key *key;
3707 void *ptr;
3708
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003709 t = rule->arg.trk_ctr.table.t;
3710 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 +02003711
3712 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003713 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003714
3715 /* let's count a new HTTP request as it's the first time we do it */
3716 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3717 if (ptr)
3718 stktable_data_cast(ptr, http_req_cnt)++;
3719
3720 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3721 if (ptr)
3722 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3723 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3724
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003725 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003726 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003727 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003728 }
3729 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003730 break;
3731
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003732 /* other flags exists, but normaly, they never be matched. */
3733 default:
3734 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003735 }
3736 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003737
3738 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003739 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003740}
3741
Willy Tarreau71241ab2012-12-27 11:30:54 +01003742
Willy Tarreau51d861a2015-05-22 17:30:48 +02003743/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3744 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3745 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3746 * is returned, the process can continue the evaluation of next rule list. If
3747 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3748 * is returned, it means the operation could not be processed and a server error
3749 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3750 * deny rule. If *YIELD is returned, the caller must call again the function
3751 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003752 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003753static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003754http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003755{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003756 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003757 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003758 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003759 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003760 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003761 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003762
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003763 /* If "the current_rule_list" match the executed rule list, we are in
3764 * resume condition. If a resume is needed it is always in the action
3765 * and never in the ACL or converters. In this case, we initialise the
3766 * current rule, and go to the action execution point.
3767 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003768 if (s->current_rule) {
3769 rule = s->current_rule;
3770 s->current_rule = NULL;
3771 if (s->current_rule_list == rules)
3772 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003773 }
3774 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003775
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003776 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003777
3778 /* check optional condition */
3779 if (rule->cond) {
3780 int ret;
3781
Willy Tarreau192252e2015-04-04 01:47:55 +02003782 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003783 ret = acl_pass(ret);
3784
3785 if (rule->cond->pol == ACL_COND_UNLESS)
3786 ret = !ret;
3787
3788 if (!ret) /* condition not matched */
3789 continue;
3790 }
3791
Willy Tarreauacc98002015-09-27 23:34:39 +02003792 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003793resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003794 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003795 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003796 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003797
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003798 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003799 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003800 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003802 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003803 s->task->nice = rule->arg.nice;
3804 break;
3805
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003806 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003807 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003808 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003809 break;
3810
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003811 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003812#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003813 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003814 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003815#endif
3816 break;
3817
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003818 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003819 s->logs.level = rule->arg.loglevel;
3820 break;
3821
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003822 case ACT_HTTP_REPLACE_HDR:
3823 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003824 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3825 rule->arg.hdr_add.name_len,
3826 &rule->arg.hdr_add.fmt,
3827 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003828 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003829 break;
3830
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003831 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003832 ctx.idx = 0;
3833 /* remove all occurrences of the header */
3834 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3835 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3836 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3837 }
Willy Tarreau85603282015-01-21 20:39:27 +01003838 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003839
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003840 case ACT_HTTP_SET_HDR:
3841 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003842 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3843 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3844 trash.len = rule->arg.hdr_add.name_len;
3845 trash.str[trash.len++] = ':';
3846 trash.str[trash.len++] = ' ';
3847 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 +01003848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003849 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003850 /* remove all occurrences of the header */
3851 ctx.idx = 0;
3852 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3853 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3854 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3855 }
3856 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003857 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3858 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003860 case ACT_HTTP_DEL_ACL:
3861 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003862 struct pat_ref *ref;
3863 char *key;
3864 int len;
3865
3866 /* collect reference */
3867 ref = pat_ref_lookup(rule->arg.map.ref);
3868 if (!ref)
3869 continue;
3870
3871 /* collect key */
3872 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3873 key = trash.str;
3874 key[len] = '\0';
3875
3876 /* perform update */
3877 /* returned code: 1=ok, 0=ko */
3878 pat_ref_delete(ref, key);
3879
3880 break;
3881 }
3882
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003883 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003884 struct pat_ref *ref;
3885 char *key;
3886 struct chunk *trash_key;
3887 int len;
3888
3889 trash_key = get_trash_chunk();
3890
3891 /* collect reference */
3892 ref = pat_ref_lookup(rule->arg.map.ref);
3893 if (!ref)
3894 continue;
3895
3896 /* collect key */
3897 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3898 key = trash_key->str;
3899 key[len] = '\0';
3900
3901 /* perform update */
3902 /* check if the entry already exists */
3903 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003904 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003905
3906 break;
3907 }
3908
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003909 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003910 struct pat_ref *ref;
3911 char *key, *value;
3912 struct chunk *trash_key, *trash_value;
3913 int len;
3914
3915 trash_key = get_trash_chunk();
3916 trash_value = get_trash_chunk();
3917
3918 /* collect reference */
3919 ref = pat_ref_lookup(rule->arg.map.ref);
3920 if (!ref)
3921 continue;
3922
3923 /* collect key */
3924 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3925 key = trash_key->str;
3926 key[len] = '\0';
3927
3928 /* collect value */
3929 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3930 value = trash_value->str;
3931 value[len] = '\0';
3932
3933 /* perform update */
3934 if (pat_ref_find_elt(ref, key) != NULL)
3935 /* update entry if it exists */
3936 pat_ref_set(ref, key, value, NULL);
3937 else
3938 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003939 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003940
3941 break;
3942 }
William Lallemand73025dd2014-04-24 14:38:37 +02003943
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003944 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003945 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3946 return HTTP_RULE_RES_BADREQ;
3947 return HTTP_RULE_RES_DONE;
3948
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003949 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3950 /* Note: only the first valid tracking parameter of each
3951 * applies.
3952 */
3953
3954 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3955 struct stktable *t;
3956 struct stksess *ts;
3957 struct stktable_key *key;
3958 void *ptr;
3959
3960 t = rule->arg.trk_ctr.table.t;
3961 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3962
3963 if (key && (ts = stktable_get_entry(t, key))) {
3964 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3965
3966 /* let's count a new HTTP request as it's the first time we do it */
3967 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3968 if (ptr)
3969 stktable_data_cast(ptr, http_req_cnt)++;
3970
3971 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3972 if (ptr)
3973 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3974 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3975
3976 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3977 if (sess->fe != s->be)
3978 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3979
3980 /* When the client triggers a 4xx from the server, it's most often due
3981 * to a missing object or permission. These events should be tracked
3982 * because if they happen often, it may indicate a brute force or a
3983 * vulnerability scan. Normally this is done when receiving the response
3984 * but here we're tracking after this ought to have been done so we have
3985 * to do it on purpose.
3986 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003987 if ((unsigned)(txn->status - 400) < 100) {
3988 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3989 if (ptr)
3990 stktable_data_cast(ptr, http_err_cnt)++;
3991
3992 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3993 if (ptr)
3994 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3995 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3996 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003997 }
3998 }
3999 break;
4000
Thierry FOURNIER42148732015-09-02 17:17:33 +02004001 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004002 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4003 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004004
Willy Tarreauacc98002015-09-27 23:34:39 +02004005 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004006 case ACT_RET_ERR:
4007 case ACT_RET_CONT:
4008 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004009 case ACT_RET_STOP:
4010 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004011 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004012 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004013 return HTTP_RULE_RES_YIELD;
4014 }
William Lallemand73025dd2014-04-24 14:38:37 +02004015 break;
4016
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004017 /* other flags exists, but normaly, they never be matched. */
4018 default:
4019 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004020 }
4021 }
4022
4023 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004024 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004025}
4026
4027
Willy Tarreau71241ab2012-12-27 11:30:54 +01004028/* Perform an HTTP redirect based on the information in <rule>. The function
4029 * returns non-zero on success, or zero in case of a, irrecoverable error such
4030 * as too large a request to build a valid response.
4031 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004032static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004033{
Willy Tarreaub329a312015-05-22 16:27:37 +02004034 struct http_msg *req = &txn->req;
4035 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004036 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004037 struct chunk *chunk;
4038 int ret = 0;
4039
4040 chunk = alloc_trash_chunk();
4041 if (!chunk)
4042 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004043
4044 /* build redirect message */
4045 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004046 case 308:
4047 msg_fmt = HTTP_308;
4048 break;
4049 case 307:
4050 msg_fmt = HTTP_307;
4051 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004052 case 303:
4053 msg_fmt = HTTP_303;
4054 break;
4055 case 301:
4056 msg_fmt = HTTP_301;
4057 break;
4058 case 302:
4059 default:
4060 msg_fmt = HTTP_302;
4061 break;
4062 }
4063
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004064 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4065 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066
4067 switch(rule->type) {
4068 case REDIRECT_TYPE_SCHEME: {
4069 const char *path;
4070 const char *host;
4071 struct hdr_ctx ctx;
4072 int pathlen;
4073 int hostlen;
4074
4075 host = "";
4076 hostlen = 0;
4077 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004078 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004079 host = ctx.line + ctx.val;
4080 hostlen = ctx.vlen;
4081 }
4082
4083 path = http_get_path(txn);
4084 /* build message using path */
4085 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004086 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004087 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4088 int qs = 0;
4089 while (qs < pathlen) {
4090 if (path[qs] == '?') {
4091 pathlen = qs;
4092 break;
4093 }
4094 qs++;
4095 }
4096 }
4097 } else {
4098 path = "/";
4099 pathlen = 1;
4100 }
4101
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004102 if (rule->rdr_str) { /* this is an old "redirect" rule */
4103 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004104 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4105 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004106
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004107 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004108 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4109 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004110 }
4111 else {
4112 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004113 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004114
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004115 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004116 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4117 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004118 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004120 memcpy(chunk->str + chunk->len, "://", 3);
4121 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004122
4123 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004124 memcpy(chunk->str + chunk->len, host, hostlen);
4125 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004126
4127 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004128 memcpy(chunk->str + chunk->len, path, pathlen);
4129 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004130
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004131 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004132 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004133 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004134 if (chunk->len > chunk->size - 5)
4135 goto leave;
4136 chunk->str[chunk->len] = '/';
4137 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004138 }
4139
4140 break;
4141 }
4142 case REDIRECT_TYPE_PREFIX: {
4143 const char *path;
4144 int pathlen;
4145
4146 path = http_get_path(txn);
4147 /* build message using path */
4148 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004149 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004150 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4151 int qs = 0;
4152 while (qs < pathlen) {
4153 if (path[qs] == '?') {
4154 pathlen = qs;
4155 break;
4156 }
4157 qs++;
4158 }
4159 }
4160 } else {
4161 path = "/";
4162 pathlen = 1;
4163 }
4164
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004165 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004166 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4167 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004168
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004169 /* add prefix. Note that if prefix == "/", we don't want to
4170 * add anything, otherwise it makes it hard for the user to
4171 * configure a self-redirection.
4172 */
4173 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004174 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4175 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004176 }
4177 }
4178 else {
4179 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004180 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004181
4182 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004183 if (chunk->len + pathlen > chunk->size - 4)
4184 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004185 }
4186
4187 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004188 memcpy(chunk->str + chunk->len, path, pathlen);
4189 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004190
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004191 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004192 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004193 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004194 if (chunk->len > chunk->size - 5)
4195 goto leave;
4196 chunk->str[chunk->len] = '/';
4197 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004198 }
4199
4200 break;
4201 }
4202 case REDIRECT_TYPE_LOCATION:
4203 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004204 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004205 if (chunk->len + rule->rdr_len > chunk->size - 4)
4206 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004207
4208 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004209 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4210 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004211 }
4212 else {
4213 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004214 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004215
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004216 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004217 if (chunk->len > chunk->size - 4)
4218 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004219 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 break;
4221 }
4222
4223 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004224 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4225 chunk->len += 14;
4226 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4227 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004228 }
4229
Willy Tarreau19b14122017-02-28 09:48:11 +01004230 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004231 txn->status = rule->code;
4232 /* let's log the request time */
4233 s->logs.tv_request = now;
4234
Christopher Fauletbe821b92017-03-30 11:21:53 +02004235 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004236 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4237 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4238 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004239 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004241 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4242 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004243 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004244 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4245 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004246 }
4247 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004248 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4249 chunk->len += 4;
4250 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4251 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004252 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004253 bi_fast_delete(req->chn->buf, req->sov);
4254 req->next -= req->sov;
4255 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004256 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01004257 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004258 req->msg_state = HTTP_MSG_CLOSED;
4259 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004260 /* Trim any possible response */
4261 res->chn->buf->i = 0;
4262 res->next = res->sov = 0;
Christopher Faulet9f724ed2017-04-20 14:16:13 +02004263 /* If not already done, don't perform any connection establishment */
4264 channel_dont_connect(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004265 } else {
4266 /* keep-alive not possible */
4267 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004268 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4269 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004270 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004271 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4272 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004273 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004274 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004275 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004276 }
4277
Willy Tarreaue7dff022015-04-03 01:14:29 +02004278 if (!(s->flags & SF_ERR_MASK))
4279 s->flags |= SF_ERR_LOCAL;
4280 if (!(s->flags & SF_FINST_MASK))
4281 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004282
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004283 ret = 1;
4284 leave:
4285 free_trash_chunk(chunk);
4286 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004287}
4288
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004289/* This stream analyser runs all HTTP request processing which is common to
4290 * frontends and backends, which means blocking ACLs, filters, connection-close,
4291 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004292 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004293 * either needs more data or wants to immediately abort the request (eg: deny,
4294 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004295 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004296int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004297{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004298 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004299 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004300 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004301 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004302 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004303 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004304 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004305
Willy Tarreau655dce92009-11-08 13:10:58 +01004306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004307 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004308 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004309 }
4310
Willy Tarreau87b09662015-04-03 00:22:06 +02004311 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 +02004312 now_ms, __FUNCTION__,
4313 s,
4314 req,
4315 req->rex, req->wex,
4316 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004317 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004318 req->analysers);
4319
Willy Tarreau65410832014-04-28 21:25:43 +02004320 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004321 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004322
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004323 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004324 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004325 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004328 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4329 goto return_prx_yield;
4330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_CONT:
4332 case HTTP_RULE_RES_STOP: /* nothing to do */
4333 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004334
Willy Tarreau0b748332014-04-29 00:13:29 +02004335 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4336 if (txn->flags & TX_CLTARPIT)
4337 goto tarpit;
4338 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4341 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004342
Willy Tarreau0b748332014-04-29 00:13:29 +02004343 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004344 goto done;
4345
Willy Tarreau0b748332014-04-29 00:13:29 +02004346 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4347 goto return_bad_req;
4348 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004349 }
4350
Willy Tarreau52542592014-04-28 18:33:26 +02004351 /* OK at this stage, we know that the request was accepted according to
4352 * the http-request rules, we can check for the stats. Note that the
4353 * URI is detected *before* the req* rules in order not to be affected
4354 * by a possible reqrep, while they are processed *after* so that a
4355 * reqdeny can still block them. This clearly needs to change in 1.6!
4356 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004357 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004358 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004360 txn->status = 500;
4361 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004362 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363
Willy Tarreaue7dff022015-04-03 01:14:29 +02004364 if (!(s->flags & SF_ERR_MASK))
4365 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004366 goto return_prx_cond;
4367 }
4368
4369 /* parse the whole stats request and extract the relevant information */
4370 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004371 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004372 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373
Willy Tarreau0b748332014-04-29 00:13:29 +02004374 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4375 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004376
Willy Tarreau0b748332014-04-29 00:13:29 +02004377 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4378 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004379 }
4380
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004381 /* evaluate the req* rules except reqadd */
4382 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004383 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004384 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004385
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386 if (txn->flags & TX_CLDENY)
4387 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004388
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004389 if (txn->flags & TX_CLTARPIT) {
4390 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004391 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004392 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004393 }
Willy Tarreau06619262006-12-17 08:37:22 +01004394
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004395 /* add request headers from the rule sets in the same order */
4396 list_for_each_entry(wl, &px->req_add, list) {
4397 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004398 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004399 ret = acl_pass(ret);
4400 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4401 ret = !ret;
4402 if (!ret)
4403 continue;
4404 }
4405
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004406 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004407 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004408 }
4409
Willy Tarreau52542592014-04-28 18:33:26 +02004410
4411 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004412 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004413 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004414 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4415 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004416
Willy Tarreaue7dff022015-04-03 01:14:29 +02004417 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4418 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4419 if (!(s->flags & SF_FINST_MASK))
4420 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004421
Willy Tarreau70730dd2014-04-24 18:06:27 +02004422 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004423 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4424 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004425 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004426 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004427 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004428
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004429 /* check whether we have some ACLs set to redirect this request */
4430 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004431 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004432 int ret;
4433
Willy Tarreau192252e2015-04-04 01:47:55 +02004434 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004435 ret = acl_pass(ret);
4436 if (rule->cond->pol == ACL_COND_UNLESS)
4437 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 if (!ret)
4439 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004440 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004441 if (!http_apply_redirect_rule(rule, s, txn))
4442 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004443 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004444 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004445
Willy Tarreau2be39392010-01-03 17:24:51 +01004446 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4447 * If this happens, then the data will not come immediately, so we must
4448 * send all what we have without waiting. Note that due to the small gain
4449 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004451 * itself once used.
4452 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004453 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004454
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004455 done: /* done with this analyser, continue with next ones that the calling
4456 * points will have set, if any.
4457 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004458 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004459 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4460 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004461 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004462
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004463 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02004464 /* Allow cookie logging
4465 */
4466 if (s->be->cookie_name || sess->fe->capture_name)
4467 manage_client_side_cookies(s, req);
4468
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004469 /* When a connection is tarpitted, we use the tarpit timeout,
4470 * which may be the same as the connect timeout if unspecified.
4471 * If unset, then set it to zero because we really want it to
4472 * eventually expire. We build the tarpit as an analyser.
4473 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004474 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004475
4476 /* wipe the request out so that we can drop the connection early
4477 * if the client closes first.
4478 */
4479 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004480
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004481 txn->status = http_err_codes[deny_status];
4482
Christopher Faulet0184ea72017-01-05 14:06:34 +01004483 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004484 req->analysers |= AN_REQ_HTTP_TARPIT;
4485 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4486 if (!req->analyse_exp)
4487 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004488 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004489 sess->fe->fe_counters.denied_req++;
4490 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004491 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004492 if (sess->listener->counters)
4493 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004494 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004495
4496 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004497
4498 /* Allow cookie logging
4499 */
4500 if (s->be->cookie_name || sess->fe->capture_name)
4501 manage_client_side_cookies(s, req);
4502
Willy Tarreau0b748332014-04-29 00:13:29 +02004503 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004504 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004505 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004506 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02004507 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004508 sess->fe->fe_counters.denied_req++;
4509 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004510 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004511 if (sess->listener->counters)
4512 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004513 goto return_prx_cond;
4514
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004515 return_bad_req:
4516 /* We centralize bad requests processing here */
4517 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4518 /* we detected a parsing error. We want to archive this request
4519 * in the dedicated proxy area for later troubleshooting.
4520 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004521 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004522 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004523
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004524 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004525 txn->req.msg_state = HTTP_MSG_ERROR;
4526 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004527 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004528
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004529 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004530 if (sess->listener->counters)
4531 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004532
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004533 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004534 if (!(s->flags & SF_ERR_MASK))
4535 s->flags |= SF_ERR_PRXCOND;
4536 if (!(s->flags & SF_FINST_MASK))
4537 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004538
Christopher Faulet0184ea72017-01-05 14:06:34 +01004539 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004540 req->analyse_exp = TICK_ETERNITY;
4541 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004542
4543 return_prx_yield:
4544 channel_dont_connect(req);
4545 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004546}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004547
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004548/* This function performs all the processing enabled for the current request.
4549 * It returns 1 if the processing can continue on next analysers, or zero if it
4550 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004551 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004552 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004553int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004554{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004555 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004556 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004557 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004558 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004559
Willy Tarreau655dce92009-11-08 13:10:58 +01004560 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004561 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004562 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004563 return 0;
4564 }
4565
Willy Tarreau87b09662015-04-03 00:22:06 +02004566 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 +02004567 now_ms, __FUNCTION__,
4568 s,
4569 req,
4570 req->rex, req->wex,
4571 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004572 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004573 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004574
Willy Tarreau59234e92008-11-30 23:51:27 +01004575 /*
4576 * Right now, we know that we have processed the entire headers
4577 * and that unwanted requests have been filtered out. We can do
4578 * whatever we want with the remaining request. Also, now we
4579 * may have separate values for ->fe, ->be.
4580 */
Willy Tarreau06619262006-12-17 08:37:22 +01004581
Willy Tarreau59234e92008-11-30 23:51:27 +01004582 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004583 * If HTTP PROXY is set we simply get remote server address parsing
4584 * incoming request. Note that this requires that a connection is
4585 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004586 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004587 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004588 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004589 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004590
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004591 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004592 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004593 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004594 txn->req.msg_state = HTTP_MSG_ERROR;
4595 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004596 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004597 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004598
Willy Tarreaue7dff022015-04-03 01:14:29 +02004599 if (!(s->flags & SF_ERR_MASK))
4600 s->flags |= SF_ERR_RESOURCE;
4601 if (!(s->flags & SF_FINST_MASK))
4602 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004603
4604 return 0;
4605 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004606
4607 path = http_get_path(txn);
4608 url2sa(req->buf->p + msg->sl.rq.u,
4609 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004610 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004611 /* if the path was found, we have to remove everything between
4612 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4613 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4614 * u_l characters by a single "/".
4615 */
4616 if (path) {
4617 char *cur_ptr = req->buf->p;
4618 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4619 int delta;
4620
4621 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4622 http_msg_move_end(&txn->req, delta);
4623 cur_end += delta;
4624 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4625 goto return_bad_req;
4626 }
4627 else {
4628 char *cur_ptr = req->buf->p;
4629 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4630 int delta;
4631
4632 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4633 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4634 http_msg_move_end(&txn->req, delta);
4635 cur_end += delta;
4636 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4637 goto return_bad_req;
4638 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004639 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004640
Willy Tarreau59234e92008-11-30 23:51:27 +01004641 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004642 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004643 * Note that doing so might move headers in the request, but
4644 * the fields will stay coherent and the URI will not move.
4645 * This should only be performed in the backend.
4646 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004647 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004648 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004649
William Lallemanda73203e2012-03-12 12:48:57 +01004650 /* add unique-id if "header-unique-id" is specified */
4651
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004652 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004653 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4654 goto return_bad_req;
4655 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004656 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004657 }
William Lallemanda73203e2012-03-12 12:48:57 +01004658
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004659 if (sess->fe->header_unique_id && s->unique_id) {
4660 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004661 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004662 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004663 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004664 goto return_bad_req;
4665 }
4666
Cyril Bontéb21570a2009-11-29 20:04:48 +01004667 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004668 * 9: add X-Forwarded-For if either the frontend or the backend
4669 * asks for it.
4670 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004671 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004672 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004673 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4674 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4675 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004676 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004677 /* The header is set to be added only if none is present
4678 * and we found it, so don't do anything.
4679 */
4680 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004682 /* Add an X-Forwarded-For header unless the source IP is
4683 * in the 'except' network range.
4684 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004685 if ((!sess->fe->except_mask.s_addr ||
4686 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4687 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004688 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004689 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004690 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004691 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004693 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004694
4695 /* Note: we rely on the backend to get the header name to be used for
4696 * x-forwarded-for, because the header is really meant for the backends.
4697 * However, if the backend did not specify any option, we have to rely
4698 * on the frontend's header name.
4699 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004700 if (s->be->fwdfor_hdr_len) {
4701 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004702 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004703 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004704 len = sess->fe->fwdfor_hdr_len;
4705 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004706 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004707 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 +01004708
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004709 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004710 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004711 }
4712 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004713 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004714 /* FIXME: for the sake of completeness, we should also support
4715 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004716 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004717 int len;
4718 char pn[INET6_ADDRSTRLEN];
4719 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004720 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004721 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004722
Willy Tarreau59234e92008-11-30 23:51:27 +01004723 /* Note: we rely on the backend to get the header name to be used for
4724 * x-forwarded-for, because the header is really meant for the backends.
4725 * However, if the backend did not specify any option, we have to rely
4726 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004727 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004728 if (s->be->fwdfor_hdr_len) {
4729 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004730 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004731 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004732 len = sess->fe->fwdfor_hdr_len;
4733 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004734 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004735 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004736
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004737 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004738 goto return_bad_req;
4739 }
4740 }
4741
4742 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004743 * 10: add X-Original-To if either the frontend or the backend
4744 * asks for it.
4745 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004746 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004747
4748 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004749 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004750 /* Add an X-Original-To header unless the destination IP is
4751 * in the 'except' network range.
4752 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004753 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004754
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004755 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004756 ((!sess->fe->except_mask_to.s_addr ||
4757 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4758 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004759 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004760 (((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 +02004761 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004762 int len;
4763 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004764 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004765
4766 /* Note: we rely on the backend to get the header name to be used for
4767 * x-original-to, because the header is really meant for the backends.
4768 * However, if the backend did not specify any option, we have to rely
4769 * on the frontend's header name.
4770 */
4771 if (s->be->orgto_hdr_len) {
4772 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004773 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004774 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004775 len = sess->fe->orgto_hdr_len;
4776 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004777 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004778 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 +02004779
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004780 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004781 goto return_bad_req;
4782 }
4783 }
4784 }
4785
Willy Tarreau50fc7772012-11-11 22:19:57 +01004786 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4787 * If an "Upgrade" token is found, the header is left untouched in order not to have
4788 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4789 * "Upgrade" is present in the Connection header.
4790 */
4791 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4792 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004793 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004794 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004795 unsigned int want_flags = 0;
4796
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004798 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004799 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004800 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004801 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004802 want_flags |= TX_CON_CLO_SET;
4803 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004804 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004805 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004806 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004807 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004808 want_flags |= TX_CON_KAL_SET;
4809 }
4810
4811 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004812 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004814
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004815
Willy Tarreau522d6c02009-12-06 18:49:18 +01004816 /* If we have no server assigned yet and we're balancing on url_param
4817 * with a POST request, we may be interested in checking the body for
4818 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004819 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004820 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004821 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004822 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004823 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004824 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004825 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004826
Christopher Fauletbe821b92017-03-30 11:21:53 +02004827 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
4828 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004829#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02004830 /* We expect some data from the client. Unless we know for sure
4831 * we already have a full request, we have to re-enable quick-ack
4832 * in case we previously disabled it, otherwise we might cause
4833 * the client to delay further data.
4834 */
4835 if ((sess->listener->options & LI_O_NOQUICKACK) &&
4836 cli_conn && conn_ctrl_ready(cli_conn) &&
4837 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
4838 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
4839 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004840#endif
Willy Tarreau03945942009-12-22 16:50:27 +01004841
Willy Tarreau59234e92008-11-30 23:51:27 +01004842 /*************************************************************
4843 * OK, that's finished for the headers. We have done what we *
4844 * could. Let's switch to the DATA state. *
4845 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004846 req->analyse_exp = TICK_ETERNITY;
4847 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004848
Willy Tarreau59234e92008-11-30 23:51:27 +01004849 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004850 /* OK let's go on with the BODY now */
4851 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004852
Willy Tarreau59234e92008-11-30 23:51:27 +01004853 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004854 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004855 /* we detected a parsing error. We want to archive this request
4856 * in the dedicated proxy area for later troubleshooting.
4857 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004858 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004859 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004860
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004861 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004862 txn->req.msg_state = HTTP_MSG_ERROR;
4863 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004864 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004865 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004866
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004867 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004868 if (sess->listener->counters)
4869 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004870
Willy Tarreaue7dff022015-04-03 01:14:29 +02004871 if (!(s->flags & SF_ERR_MASK))
4872 s->flags |= SF_ERR_PRXCOND;
4873 if (!(s->flags & SF_FINST_MASK))
4874 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004875 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004876}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004877
Willy Tarreau60b85b02008-11-30 23:28:40 +01004878/* This function is an analyser which processes the HTTP tarpit. It always
4879 * returns zero, at the beginning because it prevents any other processing
4880 * from occurring, and at the end because it terminates the request.
4881 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004882int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004884 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004885
4886 /* This connection is being tarpitted. The CLIENT side has
4887 * already set the connect expiration date to the right
4888 * timeout. We just have to check that the client is still
4889 * there and that the timeout has not expired.
4890 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004891 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004892 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004893 !tick_is_expired(req->analyse_exp, now_ms))
4894 return 0;
4895
4896 /* We will set the queue timer to the time spent, just for
4897 * logging purposes. We fake a 500 server error, so that the
4898 * attacker will not suspect his connection has been tarpitted.
4899 * It will not cause trouble to the logs because we can exclude
4900 * the tarpitted connections by filtering on the 'PT' status flags.
4901 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004902 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4903
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004904 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004905 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004906
Christopher Faulet0184ea72017-01-05 14:06:34 +01004907 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004908 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004909
Willy Tarreaue7dff022015-04-03 01:14:29 +02004910 if (!(s->flags & SF_ERR_MASK))
4911 s->flags |= SF_ERR_PRXCOND;
4912 if (!(s->flags & SF_FINST_MASK))
4913 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004914 return 0;
4915}
4916
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004917/* This function is an analyser which waits for the HTTP request body. It waits
4918 * for either the buffer to be full, or the full advertised contents to have
4919 * reached the buffer. It must only be called after the standard HTTP request
4920 * processing has occurred, because it expects the request to be parsed and will
4921 * look for the Expect header. It may send a 100-Continue interim response. It
4922 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4923 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4924 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004925 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004926int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004927{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004928 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004929 struct http_txn *txn = s->txn;
4930 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004931
4932 /* We have to parse the HTTP request body to find any required data.
4933 * "balance url_param check_post" should have been the only way to get
4934 * into this. We were brought here after HTTP header analysis, so all
4935 * related structures are ready.
4936 */
4937
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4939 /* This is the first call */
4940 if (msg->msg_state < HTTP_MSG_BODY)
4941 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004942
Willy Tarreau890988f2014-04-10 11:59:33 +02004943 if (msg->msg_state < HTTP_MSG_100_SENT) {
4944 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4945 * send an HTTP/1.1 100 Continue intermediate response.
4946 */
4947 if (msg->flags & HTTP_MSGF_VER_11) {
4948 struct hdr_ctx ctx;
4949 ctx.idx = 0;
4950 /* Expect is allowed in 1.1, look for it */
4951 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4952 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004953 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004954 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004955 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004956 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004957 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004958 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004959
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004960 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004961 * req->buf->p still points to the beginning of the message. We
4962 * must save the body in msg->next because it survives buffer
4963 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004965 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004966
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004967 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4969 else
4970 msg->msg_state = HTTP_MSG_DATA;
4971 }
4972
Willy Tarreau890988f2014-04-10 11:59:33 +02004973 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4974 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004975 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004976 goto missing_data;
4977
4978 /* OK we have everything we need now */
4979 goto http_end;
4980 }
4981
4982 /* OK here we're parsing a chunked-encoded message */
4983
Willy Tarreau522d6c02009-12-06 18:49:18 +01004984 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004985 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004986 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004987 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004988 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004989 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004990
Willy Tarreau115acb92009-12-26 13:56:06 +01004991 if (!ret)
4992 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004993 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004994 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004995 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004996 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004997 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004998 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004999 }
5000
Willy Tarreaud98cf932009-12-27 22:54:55 +01005001 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005002 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5003 * for at least a whole chunk or the whole content length bytes after
5004 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005005 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005006 if (msg->msg_state == HTTP_MSG_TRAILERS)
5007 goto http_end;
5008
Willy Tarreaue115b492015-05-01 23:05:14 +02005009 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005010 goto http_end;
5011
5012 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005013 /* we get here if we need to wait for more data. If the buffer is full,
5014 * we have the maximum we can expect.
5015 */
5016 if (buffer_full(req->buf, global.tune.maxrewrite))
5017 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005018
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005019 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005020 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005021 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005022
Willy Tarreaue7dff022015-04-03 01:14:29 +02005023 if (!(s->flags & SF_ERR_MASK))
5024 s->flags |= SF_ERR_CLITO;
5025 if (!(s->flags & SF_FINST_MASK))
5026 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005027 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005028 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005029
5030 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005031 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005032 /* Not enough data. We'll re-use the http-request
5033 * timeout here. Ideally, we should set the timeout
5034 * relative to the accept() date. We just set the
5035 * request timeout once at the beginning of the
5036 * request.
5037 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005038 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005039 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005040 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005041 return 0;
5042 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005043
5044 http_end:
5045 /* The situation will not evolve, so let's give up on the analysis. */
5046 s->logs.tv_request = now; /* update the request timer to reflect full request */
5047 req->analysers &= ~an_bit;
5048 req->analyse_exp = TICK_ETERNITY;
5049 return 1;
5050
5051 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005052 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053 txn->req.msg_state = HTTP_MSG_ERROR;
5054 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005055 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005056
Willy Tarreaue7dff022015-04-03 01:14:29 +02005057 if (!(s->flags & SF_ERR_MASK))
5058 s->flags |= SF_ERR_PRXCOND;
5059 if (!(s->flags & SF_FINST_MASK))
5060 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005061
Willy Tarreau522d6c02009-12-06 18:49:18 +01005062 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005063 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005064 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005065 if (sess->listener->counters)
5066 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005067 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005068}
5069
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005070/* send a server's name with an outgoing request over an established connection.
5071 * Note: this function is designed to be called once the request has been scheduled
5072 * for being forwarded. This is the reason why it rewinds the buffer before
5073 * proceeding.
5074 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005075int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005076
5077 struct hdr_ctx ctx;
5078
Mark Lamourinec2247f02012-01-04 13:02:01 -05005079 char *hdr_name = be->server_id_hdr_name;
5080 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005081 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005082 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005083 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005084
William Lallemandd9e90662012-01-30 17:27:17 +01005085 ctx.idx = 0;
5086
Willy Tarreau211cdec2014-04-17 20:18:08 +02005087 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005088 if (old_o) {
5089 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005090 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005091 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005092 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005093 }
5094
Willy Tarreau9b28e032012-10-12 23:49:43 +02005095 old_i = chn->buf->i;
5096 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 -05005097 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005098 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005099 }
5100
5101 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005102 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005103 memcpy(hdr_val, hdr_name, hdr_name_len);
5104 hdr_val += hdr_name_len;
5105 *hdr_val++ = ':';
5106 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005107 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5108 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005109
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005110 if (old_o) {
5111 /* If this was a forwarded request, we must readjust the amount of
5112 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005113 * variations. Note that the current state is >= HTTP_MSG_BODY,
5114 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005115 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005116 old_o += chn->buf->i - old_i;
5117 b_adv(chn->buf, old_o);
5118 txn->req.next -= old_o;
5119 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005120 }
5121
Mark Lamourinec2247f02012-01-04 13:02:01 -05005122 return 0;
5123}
5124
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125/* Terminate current transaction and prepare a new one. This is very tricky
5126 * right now but it works.
5127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005128void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005130 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005131 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005132 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005133 struct connection *srv_conn;
5134 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005135 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005136
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 /* FIXME: We need a more portable way of releasing a backend's and a
5138 * server's connections. We need a safer way to reinitialize buffer
5139 * flags. We also need a more accurate method for computing per-request
5140 * data.
5141 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005142 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
Willy Tarreau4213a112013-12-15 10:25:42 +01005144 /* unless we're doing keep-alive, we want to quickly close the connection
5145 * to the server.
5146 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005147 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005148 !si_conn_ready(&s->si[1])) {
5149 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5150 si_shutr(&s->si[1]);
5151 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005152 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153
Willy Tarreaue7dff022015-04-03 01:14:29 +02005154 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005155 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005156 if (unlikely(s->srv_conn))
5157 sess_change_server(s, NULL);
5158 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159
5160 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005161 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005162
Willy Tarreaueee5b512015-04-03 23:46:31 +02005163 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 int n;
5165
Willy Tarreaueee5b512015-04-03 23:46:31 +02005166 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005167 if (n < 1 || n > 5)
5168 n = 0;
5169
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005170 if (fe->mode == PR_MODE_HTTP) {
5171 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005172 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005173 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005174 (be->mode == PR_MODE_HTTP)) {
5175 be->be_counters.p.http.rsp[n]++;
5176 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005177 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
5179
5180 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005181 s->logs.bytes_in -= s->req.buf->i;
5182 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183
5184 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005185 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005186 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005187 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 s->do_log(s);
5189 }
5190
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005191 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005192 stream_stop_content_counters(s);
5193 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005194
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195 s->logs.accept_date = date; /* user-visible date for logging */
5196 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005197 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5198 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 tv_zero(&s->logs.tv_request);
5200 s->logs.t_queue = -1;
5201 s->logs.t_connect = -1;
5202 s->logs.t_data = -1;
5203 s->logs.t_close = 0;
5204 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5205 s->logs.srv_queue_size = 0; /* we will get this number soon */
5206
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005207 s->logs.bytes_in = s->req.total = s->req.buf->i;
5208 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209
5210 if (s->pend_pos)
5211 pendconn_free(s->pend_pos);
5212
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005213 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005214 if (s->flags & SF_CURR_SESS) {
5215 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005216 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005218 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005219 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 }
5221
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005222 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005223
Willy Tarreau4213a112013-12-15 10:25:42 +01005224 /* only release our endpoint if we don't intend to reuse the
5225 * connection.
5226 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005227 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005228 !si_conn_ready(&s->si[1])) {
5229 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005230 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005231 }
5232
Willy Tarreau350f4872014-11-28 14:42:25 +01005233 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5234 s->si[1].err_type = SI_ET_NONE;
5235 s->si[1].conn_retries = 0; /* used for logging too */
5236 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005237 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 +01005238 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);
5239 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 +02005240 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5241 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5242 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005243
Willy Tarreaueee5b512015-04-03 23:46:31 +02005244 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005245 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005246 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005247
5248 if (prev_status == 401 || prev_status == 407) {
5249 /* In HTTP keep-alive mode, if we receive a 401, we still have
5250 * a chance of being able to send the visitor again to the same
5251 * server over the same connection. This is required by some
5252 * broken protocols such as NTLM, and anyway whenever there is
5253 * an opportunity for sending the challenge to the proper place,
5254 * it's better to do it (at least it helps with debugging).
5255 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005256 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005257 if (srv_conn)
5258 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005259 }
5260
Willy Tarreau53f96852016-02-02 18:50:47 +01005261 /* Never ever allow to reuse a connection from a non-reuse backend */
5262 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5263 srv_conn->flags |= CO_FL_PRIVATE;
5264
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005265 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005266 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005268 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005269 s->req.flags |= CF_NEVER_WAIT;
5270 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005271 }
5272
Willy Tarreau714ea782015-11-25 20:11:11 +01005273 /* we're removing the analysers, we MUST re-enable events detection.
5274 * We don't enable close on the response channel since it's either
5275 * already closed, or in keep-alive with an idle connection handler.
5276 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005277 channel_auto_read(&s->req);
5278 channel_auto_close(&s->req);
5279 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005281 /* we're in keep-alive with an idle connection, monitor it if not already done */
5282 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005283 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005284 if (!srv)
5285 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005286 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005287 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005288 else if (prev_flags & TX_NOT_FIRST)
5289 /* note: we check the request, not the connection, but
5290 * this is valid for strategies SAFE and AGGR, and in
5291 * case of ALWS, we don't care anyway.
5292 */
5293 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005294 else
5295 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005296 }
Christopher Faulete6006242017-03-10 11:52:44 +01005297 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5298 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299}
5300
5301
5302/* This function updates the request state machine according to the response
5303 * state machine and buffer flags. It returns 1 if it changes anything (flag
5304 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5305 * it is only used to find when a request/response couple is complete. Both
5306 * this function and its equivalent should loop until both return zero. It
5307 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5308 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005309int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005311 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005312 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005313 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 unsigned int old_state = txn->req.msg_state;
5315
Christopher Faulet4be98032017-07-18 10:48:24 +02005316 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 return 0;
5318
5319 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005320 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005321 * We can shut the read side unless we want to abort_on_close,
5322 * or we have a POST request. The issue with POST requests is
5323 * that some browsers still send a CRLF after the request, and
5324 * this CRLF must be read so that it does not remain in the kernel
5325 * buffers, otherwise a close could cause an RST on some systems
5326 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005327 * Note that if we're using keep-alive on the client side, we'd
5328 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005329 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005330 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005331 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005332 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5333 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5334 !(s->be->options & PR_O_ABRT_CLOSE) &&
5335 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005336 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337
Willy Tarreau40f151a2012-12-20 12:10:09 +01005338 /* if the server closes the connection, we want to immediately react
5339 * and close the socket to save packets and syscalls.
5340 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005341 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005342
Willy Tarreau7f876a12015-11-18 11:59:55 +01005343 /* In any case we've finished parsing the request so we must
5344 * disable Nagle when sending data because 1) we're not going
5345 * to shut this side, and 2) the server is waiting for us to
5346 * send pending data.
5347 */
5348 chn->flags |= CF_NEVER_WAIT;
5349
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5351 goto wait_other_side;
5352
5353 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5354 /* The server has not finished to respond, so we
5355 * don't want to move in order not to upset it.
5356 */
5357 goto wait_other_side;
5358 }
5359
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360 /* When we get here, it means that both the request and the
5361 * response have finished receiving. Depending on the connection
5362 * mode, we'll have to wait for the last bytes to leave in either
5363 * direction, and sometimes for a close to be effective.
5364 */
5365
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005366 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5367 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005368 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5369 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005370 }
5371 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5372 /* Option forceclose is set, or either side wants to close,
5373 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005374 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005375 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005376 *
5377 * However, there is an exception if the response
5378 * length is undefined. In this case, we need to wait
5379 * the close from the server. The response will be
5380 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005381 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005382 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
5383 txn->rsp.msg_state != HTTP_MSG_CLOSED)
5384 goto check_channel_flags;
5385
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005386 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5387 channel_shutr_now(chn);
5388 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005389 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005390 }
5391 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005392 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5393 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005394 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005395 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5396 channel_auto_read(chn);
5397 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005398 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 }
5400
Christopher Faulet4be98032017-07-18 10:48:24 +02005401 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005402 }
5403
5404 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5405 http_msg_closing:
5406 /* nothing else to forward, just waiting for the output buffer
5407 * to be empty and for the shutw_now to take effect.
5408 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005409 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410 txn->req.msg_state = HTTP_MSG_CLOSED;
5411 goto http_msg_closed;
5412 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005413 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005414 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005416 }
Christopher Faulet56d26092017-07-20 11:05:10 +02005417 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 }
5419
5420 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5421 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005422 /* see above in MSG_DONE why we only do this in these states */
5423 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5424 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5425 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005426 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427 goto wait_other_side;
5428 }
5429
Christopher Faulet4be98032017-07-18 10:48:24 +02005430 check_channel_flags:
5431 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5432 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5433 /* if we've just closed an output, let's switch */
5434 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
5435 txn->req.msg_state = HTTP_MSG_CLOSING;
5436 goto http_msg_closing;
5437 }
5438
5439
Willy Tarreau610ecce2010-01-04 21:15:02 +01005440 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005441 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442}
5443
5444
5445/* This function updates the response state machine according to the request
5446 * state machine and buffer flags. It returns 1 if it changes anything (flag
5447 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5448 * it is only used to find when a request/response couple is complete. Both
5449 * this function and its equivalent should loop until both return zero. It
5450 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005452int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005454 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005455 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005456 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 unsigned int old_state = txn->rsp.msg_state;
5458
Christopher Faulet4be98032017-07-18 10:48:24 +02005459 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460 return 0;
5461
5462 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5463 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005464 * still monitor the server connection for a possible close
5465 * while the request is being uploaded, so we don't disable
5466 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005467 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005468 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469
5470 if (txn->req.msg_state == HTTP_MSG_ERROR)
5471 goto wait_other_side;
5472
5473 if (txn->req.msg_state < HTTP_MSG_DONE) {
5474 /* The client seems to still be sending data, probably
5475 * because we got an error response during an upload.
5476 * We have the choice of either breaking the connection
5477 * or letting it pass through. Let's do the later.
5478 */
5479 goto wait_other_side;
5480 }
5481
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 /* When we get here, it means that both the request and the
5483 * response have finished receiving. Depending on the connection
5484 * mode, we'll have to wait for the last bytes to leave in either
5485 * direction, and sometimes for a close to be effective.
5486 */
5487
5488 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5489 /* Server-close mode : shut read and wait for the request
5490 * side to close its output buffer. The caller will detect
5491 * when we're in DONE and the other is in CLOSED and will
5492 * catch that for the final cleanup.
5493 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005494 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5495 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005496 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005497 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5498 /* Option forceclose is set, or either side wants to close,
5499 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005500 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005501 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005502 *
5503 * However, there is an exception if the response length
5504 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005505 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005506 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
5507 channel_auto_read(chn);
5508 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5509 chn->flags |= CF_NEVER_WAIT;
5510 }
5511 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005512 channel_shutr_now(chn);
5513 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005514 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515 }
5516 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005517 /* The last possible modes are keep-alive and tunnel. Tunnel will
5518 * need to forward remaining data. Keep-alive will need to monitor
5519 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005520 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005521 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005522 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005523 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5524 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005525 }
5526
Christopher Faulet4be98032017-07-18 10:48:24 +02005527 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 }
5529
5530 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5531 http_msg_closing:
5532 /* nothing else to forward, just waiting for the output buffer
5533 * to be empty and for the shutw_now to take effect.
5534 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005535 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5537 goto http_msg_closed;
5538 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005539 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02005540 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005542 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005543 if (objt_server(s->target))
5544 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005545 }
Christopher Faulet56d26092017-07-20 11:05:10 +02005546 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005547 }
5548
5549 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5550 http_msg_closed:
5551 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005552 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005553 channel_auto_close(chn);
5554 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005555 goto wait_other_side;
5556 }
5557
Christopher Faulet4be98032017-07-18 10:48:24 +02005558 check_channel_flags:
5559 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5560 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5561 /* if we've just closed an output, let's switch */
5562 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5563 goto http_msg_closing;
5564 }
5565
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005567 /* We force the response to leave immediately if we're waiting for the
5568 * other side, since there is no pending shutdown to push it out.
5569 */
5570 if (!channel_is_empty(chn))
5571 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005572 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573}
5574
5575
Christopher Faulet894da4c2017-07-18 11:29:07 +02005576/* Resync the request and response state machines. */
5577void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005578{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005579 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02005580#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01005581 int old_req_state = txn->req.msg_state;
5582 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02005583#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01005584
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 http_sync_req_state(s);
5586 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005587 if (!http_sync_res_state(s))
5588 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 if (!http_sync_req_state(s))
5590 break;
5591 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005592
Christopher Faulet894da4c2017-07-18 11:29:07 +02005593 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
5594 "req->analysers=0x%08x res->analysers=0x%08x\n",
5595 now_ms, __FUNCTION__, s,
5596 http_msg_state_str(old_req_state), http_msg_state_str(old_res_state),
5597 http_msg_state_str(txn->req.msg_state), http_msg_state_str(txn->rsp.msg_state),
5598 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02005599
5600
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 /* OK, both state machines agree on a compatible state.
5602 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5604 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02005605 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
5606 * means we must abort the request.
5607 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
5608 * corresponding channel.
5609 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
5610 * on the response with server-close mode means we've completed one
5611 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005612 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02005613 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
5614 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005615 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005616 channel_auto_close(&s->req);
5617 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005618 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005619 channel_auto_close(&s->res);
5620 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005621 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02005622 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
5623 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005624 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005625 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005626 channel_auto_close(&s->res);
5627 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005628 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005629 channel_abort(&s->req);
5630 channel_auto_close(&s->req);
5631 channel_auto_read(&s->req);
5632 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005633 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02005634 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5635 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5636 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5637 s->req.analysers &= AN_REQ_FLT_END;
5638 if (HAS_REQ_DATA_FILTERS(s))
5639 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
5640 }
5641 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5642 s->res.analysers &= AN_RES_FLT_END;
5643 if (HAS_RSP_DATA_FILTERS(s))
5644 s->res.analysers |= AN_RES_FLT_XFER_DATA;
5645 }
5646 channel_auto_close(&s->req);
5647 channel_auto_read(&s->req);
5648 channel_auto_close(&s->res);
5649 channel_auto_read(&s->res);
5650 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005651 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5652 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005653 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005654 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5655 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5656 /* server-close/keep-alive: terminate this transaction,
5657 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005658 * a fresh-new transaction, but only once we're sure there's
5659 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02005660 * another request. They must not hold any pending output data
5661 * and the response buffer must realigned
5662 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01005663 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02005664 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005665 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02005666 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005667 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02005668 else {
5669 s->req.analysers = AN_REQ_FLT_END;
5670 s->res.analysers = AN_RES_FLT_END;
5671 txn->flags |= TX_WAIT_CLEANUP;
5672 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005673 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005674}
5675
Willy Tarreaud98cf932009-12-27 22:54:55 +01005676/* This function is an analyser which forwards request body (including chunk
5677 * sizes if any). It is called as soon as we must forward, even if we forward
5678 * zero byte. The only situation where it must not be called is when we're in
5679 * tunnel mode and we want to forward till the close. It's used both to forward
5680 * remaining data and to resync after end of body. It expects the msg_state to
5681 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005682 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005683 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005684 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005685 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005686int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005688 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005689 struct http_txn *txn = s->txn;
5690 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005691 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005692
Christopher Faulet814d2702017-03-30 11:33:44 +02005693 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5694 now_ms, __FUNCTION__,
5695 s,
5696 req,
5697 req->rex, req->wex,
5698 req->flags,
5699 req->buf->i,
5700 req->analysers);
5701
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005702 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5703 return 0;
5704
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005705 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005706 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005707 /* Output closed while we were sending data. We must abort and
5708 * wake the other side up.
5709 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005710 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005711 msg->msg_state = HTTP_MSG_ERROR;
5712 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005713 return 1;
5714 }
5715
Willy Tarreaud98cf932009-12-27 22:54:55 +01005716 /* Note that we don't have to send 100-continue back because we don't
5717 * need the data to complete our job, and it's up to the server to
5718 * decide whether to return 100, 417 or anything else in return of
5719 * an "Expect: 100-continue" header.
5720 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005721 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005722 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5723 ? HTTP_MSG_CHUNK_SIZE
5724 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005725
5726 /* TODO/filters: when http-buffer-request option is set or if a
5727 * rule on url_param exists, the first chunk size could be
5728 * already parsed. In that case, msg->next is after the chunk
5729 * size (including the CRLF after the size). So this case should
5730 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005731 }
5732
Willy Tarreau7ba23542014-04-17 21:50:00 +02005733 /* Some post-connect processing might want us to refrain from starting to
5734 * forward data. Currently, the only reason for this is "balance url_param"
5735 * whichs need to parse/process the request after we've enabled forwarding.
5736 */
5737 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005738 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005739 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005740 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005741 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005742 }
5743 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5744 }
5745
Willy Tarreau80a92c02014-03-12 10:41:13 +01005746 /* in most states, we should abort in case of early close */
5747 channel_auto_close(req);
5748
Willy Tarreauefdf0942014-04-24 20:08:57 +02005749 if (req->to_forward) {
5750 /* We can't process the buffer's contents yet */
5751 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005753 }
5754
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005755 if (msg->msg_state < HTTP_MSG_DONE) {
5756 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5757 ? http_msg_forward_chunked_body(s, msg)
5758 : http_msg_forward_body(s, msg));
5759 if (!ret)
5760 goto missing_data_or_waiting;
5761 if (ret < 0)
5762 goto return_bad_req;
5763 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005764
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005765 /* other states, DONE...TUNNEL */
5766 /* we don't want to forward closes on DONE except in tunnel mode. */
5767 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5768 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005769
Christopher Faulet894da4c2017-07-18 11:29:07 +02005770 http_resync_states(s);
5771 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005772 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5773 if (req->flags & CF_SHUTW) {
5774 /* request errors are most likely due to the
5775 * server aborting the transfer. */
5776 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005777 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005779 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005780 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005783 }
5784
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005785 /* If "option abortonclose" is set on the backend, we want to monitor
5786 * the client's connection and forward any shutdown notification to the
5787 * server, which will decide whether to close or to go on processing the
5788 * request. We only do that in tunnel mode, and not in other modes since
5789 * it can be abused to exhaust source ports. */
5790 if (s->be->options & PR_O_ABRT_CLOSE) {
5791 channel_auto_read(req);
5792 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5793 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5794 s->si[1].flags |= SI_FL_NOLINGER;
5795 channel_auto_close(req);
5796 }
5797 else if (s->txn->meth == HTTP_METH_POST) {
5798 /* POST requests may require to read extra CRLF sent by broken
5799 * browsers and which could cause an RST to be sent upon close
5800 * on some systems (eg: Linux). */
5801 channel_auto_read(req);
5802 }
5803 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005804
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005805 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005806 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005807 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005808 if (!(s->flags & SF_ERR_MASK))
5809 s->flags |= SF_ERR_CLICL;
5810 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005811 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005812 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005813 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005814 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005815 }
5816
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005817 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005818 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005819 if (objt_server(s->target))
5820 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005821
5822 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005823 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005824
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005825 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005826 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005827 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005828
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005829 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005830 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005831 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005832 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005833 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005834
Willy Tarreau5c620922011-05-11 19:56:11 +02005835 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005836 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005837 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005838 * modes are already handled by the stream sock layer. We must not do
5839 * this in content-length mode because it could present the MSG_MORE
5840 * flag with the last block of forwarded data, which would cause an
5841 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005842 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005843 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005844 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005845
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005846 return 0;
5847
Willy Tarreaud98cf932009-12-27 22:54:55 +01005848 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005849 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005850 if (sess->listener->counters)
5851 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005852
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005853 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005854 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 txn->req.msg_state = HTTP_MSG_ERROR;
5856 if (txn->status) {
5857 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005858 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859 } else {
5860 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005861 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005862 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005863 req->analysers &= AN_REQ_FLT_END;
5864 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005865
Willy Tarreaue7dff022015-04-03 01:14:29 +02005866 if (!(s->flags & SF_ERR_MASK))
5867 s->flags |= SF_ERR_PRXCOND;
5868 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005869 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005870 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005871 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005872 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005873 }
5874 return 0;
5875
5876 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005877 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005878 txn->req.msg_state = HTTP_MSG_ERROR;
5879 if (txn->status) {
5880 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005881 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005882 } else {
5883 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005884 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005885 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005886 req->analysers &= AN_REQ_FLT_END;
5887 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005888
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005889 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005890 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005891 if (objt_server(s->target))
5892 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005893
Willy Tarreaue7dff022015-04-03 01:14:29 +02005894 if (!(s->flags & SF_ERR_MASK))
5895 s->flags |= SF_ERR_SRVCL;
5896 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005897 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005898 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005899 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005900 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005901 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005902 return 0;
5903}
5904
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005905/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5906 * processing can continue on next analysers, or zero if it either needs more
5907 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005908 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005909 * when it has nothing left to do, and may remove any analyser when it wants to
5910 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005911 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005912int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005913{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005914 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005915 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005916 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005917 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005918 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005919 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005920 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005921
Willy Tarreau87b09662015-04-03 00:22:06 +02005922 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005923 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005925 rep,
5926 rep->rex, rep->wex,
5927 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005928 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005929 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005930
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 /*
5932 * Now parse the partial (or complete) lines.
5933 * We will check the response syntax, and also join multi-line
5934 * headers. An index of all the lines will be elaborated while
5935 * parsing.
5936 *
5937 * For the parsing, we use a 28 states FSM.
5938 *
5939 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005940 * rep->buf->p = beginning of response
5941 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5942 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005943 * msg->eol = end of current header or line (LF or CRLF)
5944 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945 */
5946
Willy Tarreau628c40c2014-04-24 19:11:26 +02005947 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005948 /* There's a protected area at the end of the buffer for rewriting
5949 * purposes. We don't want to start to parse the request if the
5950 * protected area is affected, because we may have to move processed
5951 * data later, which is much more complicated.
5952 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005953 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005954 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005955 /* some data has still not left the buffer, wake us once that's done */
5956 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5957 goto abort_response;
5958 channel_dont_close(rep);
5959 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005960 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005961 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005962 }
5963
Willy Tarreau379357a2013-06-08 12:55:46 +02005964 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5965 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5966 buffer_slow_realign(rep->buf);
5967
Willy Tarreau9b28e032012-10-12 23:49:43 +02005968 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005969 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005970 }
5971
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 /* 1: we might have to print this header in debug mode */
5973 if (unlikely((global.mode & MODE_DEBUG) &&
5974 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005975 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977
Willy Tarreau9b28e032012-10-12 23:49:43 +02005978 sol = rep->buf->p;
5979 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 sol += hdr_idx_first_pos(&txn->hdr_idx);
5983 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005984
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 while (cur_idx) {
5986 eol = sol + txn->hdr_idx.v[cur_idx].len;
5987 debug_hdr("srvhdr", s, sol, eol);
5988 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5989 cur_idx = txn->hdr_idx.v[cur_idx].next;
5990 }
5991 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005992
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 /*
5994 * Now we quickly check if we have found a full valid response.
5995 * If not so, we check the FD and buffer states before leaving.
5996 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005997 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998 * responses are checked first.
5999 *
6000 * Depending on whether the client is still there or not, we
6001 * may send an error response back or not. Note that normally
6002 * we should only check for HTTP status there, and check I/O
6003 * errors somewhere else.
6004 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006005
Willy Tarreau655dce92009-11-08 13:10:58 +01006006 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 /* Invalid response */
6008 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6009 /* we detected a parsing error. We want to archive this response
6010 * in the dedicated proxy area for later troubleshooting.
6011 */
6012 hdr_response_bad:
6013 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006014 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006016 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006017 if (objt_server(s->target)) {
6018 objt_server(s->target)->counters.failed_resp++;
6019 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006020 }
Willy Tarreau64648412010-03-05 10:41:54 +01006021 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006022 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006023 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006025 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006026 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006027 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006028
Willy Tarreaue7dff022015-04-03 01:14:29 +02006029 if (!(s->flags & SF_ERR_MASK))
6030 s->flags |= SF_ERR_PRXCOND;
6031 if (!(s->flags & SF_FINST_MASK))
6032 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033
6034 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006035 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006036
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006038 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006039 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006040 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006041 goto hdr_response_bad;
6042 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006043
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006044 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006045 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006047 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006048 else if (txn->flags & TX_NOT_FIRST)
6049 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006050
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006051 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006052 if (objt_server(s->target)) {
6053 objt_server(s->target)->counters.failed_resp++;
6054 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006055 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006056
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006057 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006058 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006059 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006060 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006061 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006062 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02006063
Willy Tarreaue7dff022015-04-03 01:14:29 +02006064 if (!(s->flags & SF_ERR_MASK))
6065 s->flags |= SF_ERR_SRVCL;
6066 if (!(s->flags & SF_FINST_MASK))
6067 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006068 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006069 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006070
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006071 /* read timeout : return a 504 to the client. */
6072 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006074 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006075
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006076 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006077 if (objt_server(s->target)) {
6078 objt_server(s->target)->counters.failed_resp++;
6079 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006080 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006081
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006082 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006083 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006085 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006086 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006087 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02006088
Willy Tarreaue7dff022015-04-03 01:14:29 +02006089 if (!(s->flags & SF_ERR_MASK))
6090 s->flags |= SF_ERR_SRVTO;
6091 if (!(s->flags & SF_FINST_MASK))
6092 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093 return 0;
6094 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006095
Willy Tarreauf003d372012-11-26 13:35:37 +01006096 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006097 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006098 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006099 s->be->be_counters.cli_aborts++;
6100 if (objt_server(s->target))
6101 objt_server(s->target)->counters.cli_aborts++;
6102
Christopher Faulet0184ea72017-01-05 14:06:34 +01006103 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006104 channel_auto_close(rep);
6105
6106 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006107 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006108 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01006109
Willy Tarreaue7dff022015-04-03 01:14:29 +02006110 if (!(s->flags & SF_ERR_MASK))
6111 s->flags |= SF_ERR_CLICL;
6112 if (!(s->flags & SF_FINST_MASK))
6113 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006114
Willy Tarreau87b09662015-04-03 00:22:06 +02006115 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006116 return 0;
6117 }
6118
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006119 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006120 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006121 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006122 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006123 else if (txn->flags & TX_NOT_FIRST)
6124 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006125
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006126 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006127 if (objt_server(s->target)) {
6128 objt_server(s->target)->counters.failed_resp++;
6129 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006130 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006131
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006132 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006133 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006134 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006135 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006136 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006137 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006138
Willy Tarreaue7dff022015-04-03 01:14:29 +02006139 if (!(s->flags & SF_ERR_MASK))
6140 s->flags |= SF_ERR_SRVCL;
6141 if (!(s->flags & SF_FINST_MASK))
6142 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006143 return 0;
6144 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006146 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006147 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006148 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006149 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006150 else if (txn->flags & TX_NOT_FIRST)
6151 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006153 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006154 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006155 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006156
Willy Tarreaue7dff022015-04-03 01:14:29 +02006157 if (!(s->flags & SF_ERR_MASK))
6158 s->flags |= SF_ERR_CLICL;
6159 if (!(s->flags & SF_FINST_MASK))
6160 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006161
Willy Tarreau87b09662015-04-03 00:22:06 +02006162 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006163 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006164 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006165
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006166 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006167 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006168 return 0;
6169 }
6170
6171 /* More interesting part now : we know that we have a complete
6172 * response which at least looks like HTTP. We have an indicator
6173 * of each header's length, so we can parse them quickly.
6174 */
6175
6176 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006177 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006178
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006179 /*
6180 * 1: get the status code
6181 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006182 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006183 if (n < 1 || n > 5)
6184 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006185 /* when the client triggers a 4xx from the server, it's most often due
6186 * to a missing object or permission. These events should be tracked
6187 * because if they happen often, it may indicate a brute force or a
6188 * vulnerability scan.
6189 */
6190 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006191 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006192
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006193 if (objt_server(s->target))
6194 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006195
Willy Tarreau91852eb2015-05-01 13:26:00 +02006196 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6197 * exactly one digit "." one digit. This check may be disabled using
6198 * option accept-invalid-http-response.
6199 */
6200 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6201 if (msg->sl.st.v_l != 8) {
6202 msg->err_pos = 0;
6203 goto hdr_response_bad;
6204 }
6205
6206 if (rep->buf->p[4] != '/' ||
6207 !isdigit((unsigned char)rep->buf->p[5]) ||
6208 rep->buf->p[6] != '.' ||
6209 !isdigit((unsigned char)rep->buf->p[7])) {
6210 msg->err_pos = 4;
6211 goto hdr_response_bad;
6212 }
6213 }
6214
Willy Tarreau5b154472009-12-21 20:11:07 +01006215 /* check if the response is HTTP/1.1 or above */
6216 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006217 ((rep->buf->p[5] > '1') ||
6218 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006219 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006220
6221 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006222 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01006223
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006224 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006225 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006226
Willy Tarreau9b28e032012-10-12 23:49:43 +02006227 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006228
Willy Tarreau39650402010-03-15 19:44:39 +01006229 /* Adjust server's health based on status code. Note: status codes 501
6230 * and 505 are triggered on demand by client request, so we must not
6231 * count them as server failures.
6232 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006233 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006234 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006235 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006236 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006237 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006238 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006239
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006240 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02006241 * We may be facing a 100-continue response, or any other informational
6242 * 1xx response which is non-final, in which case this is not the right
6243 * response, and we're waiting for the next one. Let's allow this response
6244 * to go to the client and wait for the next one. There's an exception for
6245 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006246 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02006247 if (txn->status < 200 &&
6248 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006249 hdr_idx_init(&txn->hdr_idx);
6250 msg->next -= channel_forward(rep, msg->next);
6251 msg->msg_state = HTTP_MSG_RPBEFORE;
6252 txn->status = 0;
6253 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006254 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006255 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02006256 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02006257
Willy Tarreaua14ad722017-07-07 11:36:32 +02006258 /*
6259 * 2: check for cacheability.
6260 */
6261
6262 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006263 case 200:
6264 case 203:
6265 case 206:
6266 case 300:
6267 case 301:
6268 case 410:
6269 /* RFC2616 @13.4:
6270 * "A response received with a status code of
6271 * 200, 203, 206, 300, 301 or 410 MAY be stored
6272 * by a cache (...) unless a cache-control
6273 * directive prohibits caching."
6274 *
6275 * RFC2616 @9.5: POST method :
6276 * "Responses to this method are not cacheable,
6277 * unless the response includes appropriate
6278 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006279 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006280 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006281 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006282 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6283 break;
6284 default:
6285 break;
6286 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006287
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006288 /*
6289 * 3: we may need to capture headers
6290 */
6291 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006292 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006293 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006294 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006295
Willy Tarreau557f1992015-05-01 10:05:17 +02006296 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6297 * by RFC7230#3.3.3 :
6298 *
6299 * The length of a message body is determined by one of the following
6300 * (in order of precedence):
6301 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006302 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6303 * the connection will become a tunnel immediately after the empty
6304 * line that concludes the header fields. A client MUST ignore
6305 * any Content-Length or Transfer-Encoding header fields received
6306 * in such a message. Any 101 response (Switching Protocols) is
6307 * managed in the same manner.
6308 *
6309 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006310 * (Informational), 204 (No Content), or 304 (Not Modified) status
6311 * code is always terminated by the first empty line after the
6312 * header fields, regardless of the header fields present in the
6313 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006315 * 3. If a Transfer-Encoding header field is present and the chunked
6316 * transfer coding (Section 4.1) is the final encoding, the message
6317 * body length is determined by reading and decoding the chunked
6318 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006319 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006320 * If a Transfer-Encoding header field is present in a response and
6321 * the chunked transfer coding is not the final encoding, the
6322 * message body length is determined by reading the connection until
6323 * it is closed by the server. If a Transfer-Encoding header field
6324 * is present in a request and the chunked transfer coding is not
6325 * the final encoding, the message body length cannot be determined
6326 * reliably; the server MUST respond with the 400 (Bad Request)
6327 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006328 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006329 * If a message is received with both a Transfer-Encoding and a
6330 * Content-Length header field, the Transfer-Encoding overrides the
6331 * Content-Length. Such a message might indicate an attempt to
6332 * perform request smuggling (Section 9.5) or response splitting
6333 * (Section 9.4) and ought to be handled as an error. A sender MUST
6334 * remove the received Content-Length field prior to forwarding such
6335 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006336 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006337 * 4. If a message is received without Transfer-Encoding and with
6338 * either multiple Content-Length header fields having differing
6339 * field-values or a single Content-Length header field having an
6340 * invalid value, then the message framing is invalid and the
6341 * recipient MUST treat it as an unrecoverable error. If this is a
6342 * request message, the server MUST respond with a 400 (Bad Request)
6343 * status code and then close the connection. If this is a response
6344 * message received by a proxy, the proxy MUST close the connection
6345 * to the server, discard the received response, and send a 502 (Bad
6346 * Gateway) response to the client. If this is a response message
6347 * received by a user agent, the user agent MUST close the
6348 * connection to the server and discard the received response.
6349 *
6350 * 5. If a valid Content-Length header field is present without
6351 * Transfer-Encoding, its decimal value defines the expected message
6352 * body length in octets. If the sender closes the connection or
6353 * the recipient times out before the indicated number of octets are
6354 * received, the recipient MUST consider the message to be
6355 * incomplete and close the connection.
6356 *
6357 * 6. If this is a request message and none of the above are true, then
6358 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006359 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006360 * 7. Otherwise, this is a response message without a declared message
6361 * body length, so the message body length is determined by the
6362 * number of octets received prior to the server closing the
6363 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006364 */
6365
6366 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006367 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006368 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006369 * FIXME: should we parse anyway and return an error on chunked encoding ?
6370 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006371 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6372 txn->status == 101)) {
6373 /* Either we've established an explicit tunnel, or we're
6374 * switching the protocol. In both cases, we're very unlikely
6375 * to understand the next protocols. We have to switch to tunnel
6376 * mode, so that we transfer the request and responses then let
6377 * this protocol pass unmodified. When we later implement specific
6378 * parsers for such protocols, we'll want to check the Upgrade
6379 * header which contains information about that protocol for
6380 * responses with status 101 (eg: see RFC2817 about TLS).
6381 */
6382 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6383 msg->flags |= HTTP_MSGF_XFER_LEN;
6384 goto end;
6385 }
6386
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006387 if (txn->meth == HTTP_METH_HEAD ||
6388 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006389 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006390 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006391 goto skip_content_length;
6392 }
6393
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006394 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006395 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006396 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006397 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006398 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6399 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006400 /* bad transfer-encoding (chunked followed by something else) */
6401 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006402 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006403 break;
6404 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006405 }
6406
Willy Tarreau1c913912015-04-30 10:57:51 +02006407 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006408 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006409 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006410 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6411 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6412 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006413 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006414 signed long long cl;
6415
Willy Tarreauad14f752011-09-02 20:33:27 +02006416 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006417 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006418 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006419 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006420
Willy Tarreauad14f752011-09-02 20:33:27 +02006421 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006422 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006423 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006424 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006425
Willy Tarreauad14f752011-09-02 20:33:27 +02006426 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006427 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006428 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006429 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006431 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006432 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006433 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006434 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006435
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006436 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006437 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006438 }
6439
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006440 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006441 /* Now we have to check if we need to modify the Connection header.
6442 * This is more difficult on the response than it is on the request,
6443 * because we can have two different HTTP versions and we don't know
6444 * how the client will interprete a response. For instance, let's say
6445 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6446 * HTTP/1.1 response without any header. Maybe it will bound itself to
6447 * HTTP/1.0 because it only knows about it, and will consider the lack
6448 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6449 * the lack of header as a keep-alive. Thus we will use two flags
6450 * indicating how a request MAY be understood by the client. In case
6451 * of multiple possibilities, we'll fix the header to be explicit. If
6452 * ambiguous cases such as both close and keepalive are seen, then we
6453 * will fall back to explicit close. Note that we won't take risks with
6454 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006455 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006456 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006457 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6458 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6459 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6460 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006461 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006462
Willy Tarreau70dffda2014-01-30 03:07:23 +01006463 /* this situation happens when combining pretend-keepalive with httpclose. */
6464 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006465 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006467 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6468
Willy Tarreau60466522010-01-18 19:08:45 +01006469 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006470 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006471 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6472 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006473
Willy Tarreau60466522010-01-18 19:08:45 +01006474 /* now adjust header transformations depending on current state */
6475 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6476 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6477 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006478 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006479 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006480 }
Willy Tarreau60466522010-01-18 19:08:45 +01006481 else { /* SCL / KAL */
6482 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006483 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006484 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006485 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006486
Willy Tarreau60466522010-01-18 19:08:45 +01006487 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006488 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006489
Willy Tarreau60466522010-01-18 19:08:45 +01006490 /* Some keep-alive responses are converted to Server-close if
6491 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006492 */
Willy Tarreau60466522010-01-18 19:08:45 +01006493 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6494 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006495 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006496 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006497 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006498 }
6499
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006500 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006501 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006502 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006503
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006504 /* end of job, return OK */
6505 rep->analysers &= ~an_bit;
6506 rep->analyse_exp = TICK_ETERNITY;
6507 channel_auto_close(rep);
6508 return 1;
6509
6510 abort_keep_alive:
6511 /* A keep-alive request to the server failed on a network error.
6512 * The client is required to retry. We need to close without returning
6513 * any other information so that the client retries.
6514 */
6515 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006516 rep->analysers &= AN_RES_FLT_END;
6517 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006518 channel_auto_close(rep);
6519 s->logs.logwait = 0;
6520 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006521 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006522 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006523 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006524 return 0;
6525}
6526
6527/* This function performs all the processing enabled for the current response.
6528 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006529 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006530 * other functions. It works like process_request (see indications above).
6531 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006532int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006533{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006534 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006535 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006536 struct http_msg *msg = &txn->rsp;
6537 struct proxy *cur_proxy;
6538 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006539 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006540
Willy Tarreau87b09662015-04-03 00:22:06 +02006541 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 +02006542 now_ms, __FUNCTION__,
6543 s,
6544 rep,
6545 rep->rex, rep->wex,
6546 rep->flags,
6547 rep->buf->i,
6548 rep->analysers);
6549
6550 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6551 return 0;
6552
Willy Tarreau70730dd2014-04-24 18:06:27 +02006553 /* The stats applet needs to adjust the Connection header but we don't
6554 * apply any filter there.
6555 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006556 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6557 rep->analysers &= ~an_bit;
6558 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006559 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006560 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006561
Willy Tarreau58975672014-04-24 21:13:57 +02006562 /*
6563 * We will have to evaluate the filters.
6564 * As opposed to version 1.2, now they will be evaluated in the
6565 * filters order and not in the header order. This means that
6566 * each filter has to be validated among all headers.
6567 *
6568 * Filters are tried with ->be first, then with ->fe if it is
6569 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006570 *
6571 * Maybe we are in resume condiion. In this case I choose the
6572 * "struct proxy" which contains the rule list matching the resume
6573 * pointer. If none of theses "struct proxy" match, I initialise
6574 * the process with the first one.
6575 *
6576 * In fact, I check only correspondance betwwen the current list
6577 * pointer and the ->fe rule list. If it doesn't match, I initialize
6578 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006579 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006580 if (s->current_rule_list == &sess->fe->http_res_rules)
6581 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006582 else
6583 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006584 while (1) {
6585 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006586
Willy Tarreau58975672014-04-24 21:13:57 +02006587 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006588 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006589 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006590
Willy Tarreau51d861a2015-05-22 17:30:48 +02006591 if (ret == HTTP_RULE_RES_BADREQ)
6592 goto return_srv_prx_502;
6593
6594 if (ret == HTTP_RULE_RES_DONE) {
6595 rep->analysers &= ~an_bit;
6596 rep->analyse_exp = TICK_ETERNITY;
6597 return 1;
6598 }
6599 }
6600
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006601 /* we need to be called again. */
6602 if (ret == HTTP_RULE_RES_YIELD) {
6603 channel_dont_close(rep);
6604 return 0;
6605 }
6606
Willy Tarreau58975672014-04-24 21:13:57 +02006607 /* try headers filters */
6608 if (rule_set->rsp_exp != NULL) {
6609 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6610 return_bad_resp:
6611 if (objt_server(s->target)) {
6612 objt_server(s->target)->counters.failed_resp++;
6613 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006614 }
Willy Tarreau58975672014-04-24 21:13:57 +02006615 s->be->be_counters.failed_resp++;
6616 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006617 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006618 txn->status = 502;
6619 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006620 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006621 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006622 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006623 if (!(s->flags & SF_ERR_MASK))
6624 s->flags |= SF_ERR_PRXCOND;
6625 if (!(s->flags & SF_FINST_MASK))
6626 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006627 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006628 }
Willy Tarreau58975672014-04-24 21:13:57 +02006629 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006630
Willy Tarreau58975672014-04-24 21:13:57 +02006631 /* has the response been denied ? */
6632 if (txn->flags & TX_SVDENY) {
6633 if (objt_server(s->target))
6634 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006635
Willy Tarreau58975672014-04-24 21:13:57 +02006636 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006637 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006638 if (sess->listener->counters)
6639 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006640
Willy Tarreau58975672014-04-24 21:13:57 +02006641 goto return_srv_prx_502;
6642 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006643
Willy Tarreau58975672014-04-24 21:13:57 +02006644 /* add response headers from the rule sets in the same order */
6645 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006646 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006647 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006648 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006649 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006650 ret = acl_pass(ret);
6651 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6652 ret = !ret;
6653 if (!ret)
6654 continue;
6655 }
6656 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6657 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006658 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006659
Willy Tarreau58975672014-04-24 21:13:57 +02006660 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006661 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006662 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006663 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006664 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006665
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006666 /* After this point, this anayzer can't return yield, so we can
6667 * remove the bit corresponding to this analyzer from the list.
6668 *
6669 * Note that the intermediate returns and goto found previously
6670 * reset the analyzers.
6671 */
6672 rep->analysers &= ~an_bit;
6673 rep->analyse_exp = TICK_ETERNITY;
6674
Willy Tarreau58975672014-04-24 21:13:57 +02006675 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006676 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006677 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006678
Willy Tarreau58975672014-04-24 21:13:57 +02006679 /*
6680 * Now check for a server cookie.
6681 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006682 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006683 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006684
Willy Tarreau58975672014-04-24 21:13:57 +02006685 /*
6686 * Check for cache-control or pragma headers if required.
6687 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006688 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 +02006689 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006690
Willy Tarreau58975672014-04-24 21:13:57 +02006691 /*
6692 * Add server cookie in the response if needed
6693 */
6694 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6695 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006696 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006697 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6698 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6699 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6700 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6701 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006702 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006703 /* the server is known, it's not the one the client requested, or the
6704 * cookie's last seen date needs to be refreshed. We have to
6705 * insert a set-cookie here, except if we want to insert only on POST
6706 * requests and this one isn't. Note that servers which don't have cookies
6707 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 */
Willy Tarreau58975672014-04-24 21:13:57 +02006709 if (!objt_server(s->target)->cookie) {
6710 chunk_printf(&trash,
6711 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6712 s->be->cookie_name);
6713 }
6714 else {
6715 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6718 /* emit last_date, which is mandatory */
6719 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6720 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6721 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 if (s->be->cookie_maxlife) {
6724 /* emit first_date, which is either the original one or
6725 * the current date.
6726 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006727 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006728 s30tob64(txn->cookie_first_date ?
6729 txn->cookie_first_date >> 2 :
6730 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006731 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006732 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006733 }
Willy Tarreau58975672014-04-24 21:13:57 +02006734 chunk_appendf(&trash, "; path=/");
6735 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006736
Willy Tarreau58975672014-04-24 21:13:57 +02006737 if (s->be->cookie_domain)
6738 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006739
Willy Tarreau58975672014-04-24 21:13:57 +02006740 if (s->be->ck_opts & PR_CK_HTTPONLY)
6741 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006742
Willy Tarreau58975672014-04-24 21:13:57 +02006743 if (s->be->ck_opts & PR_CK_SECURE)
6744 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006745
Willy Tarreau58975672014-04-24 21:13:57 +02006746 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6747 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006748
Willy Tarreau58975672014-04-24 21:13:57 +02006749 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006750 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006751 /* the server did not change, only the date was updated */
6752 txn->flags |= TX_SCK_UPDATED;
6753 else
6754 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006755
Willy Tarreau58975672014-04-24 21:13:57 +02006756 /* Here, we will tell an eventual cache on the client side that we don't
6757 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6758 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6759 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006760 */
Willy Tarreau58975672014-04-24 21:13:57 +02006761 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006762
Willy Tarreau58975672014-04-24 21:13:57 +02006763 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006764
Willy Tarreau58975672014-04-24 21:13:57 +02006765 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6766 "Cache-control: private", 22) < 0))
6767 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006768 }
Willy Tarreau58975672014-04-24 21:13:57 +02006769 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770
Willy Tarreau58975672014-04-24 21:13:57 +02006771 /*
6772 * Check if result will be cacheable with a cookie.
6773 * We'll block the response if security checks have caught
6774 * nasty things such as a cacheable cookie.
6775 */
6776 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6777 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6778 (s->be->options & PR_O_CHK_CACHE)) {
6779 /* we're in presence of a cacheable response containing
6780 * a set-cookie header. We'll block it as requested by
6781 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 */
Willy Tarreau58975672014-04-24 21:13:57 +02006783 if (objt_server(s->target))
6784 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006785
Willy Tarreau58975672014-04-24 21:13:57 +02006786 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006787 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006788 if (sess->listener->counters)
6789 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790
Willy Tarreau58975672014-04-24 21:13:57 +02006791 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6792 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6793 send_log(s->be, LOG_ALERT,
6794 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6795 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6796 goto return_srv_prx_502;
6797 }
Willy Tarreau03945942009-12-22 16:50:27 +01006798
Willy Tarreau70730dd2014-04-24 18:06:27 +02006799 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006800 /*
6801 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6802 * If an "Upgrade" token is found, the header is left untouched in order
6803 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006804 * if anything but "Upgrade" is present in the Connection header. We don't
6805 * want to touch any 101 response either since it's switching to another
6806 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006807 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006808 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006809 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006810 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006811 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6812 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006813
Willy Tarreau58975672014-04-24 21:13:57 +02006814 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6815 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6816 /* we want a keep-alive response here. Keep-alive header
6817 * required if either side is not 1.1.
6818 */
6819 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6820 want_flags |= TX_CON_KAL_SET;
6821 }
6822 else {
6823 /* we want a close response here. Close header required if
6824 * the server is 1.1, regardless of the client.
6825 */
6826 if (msg->flags & HTTP_MSGF_VER_11)
6827 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006828 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829
Willy Tarreau58975672014-04-24 21:13:57 +02006830 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6831 http_change_connection_header(txn, msg, want_flags);
6832 }
6833
6834 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006835 /* Always enter in the body analyzer */
6836 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6837 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838
Willy Tarreau58975672014-04-24 21:13:57 +02006839 /* if the user wants to log as soon as possible, without counting
6840 * bytes from the server, then this is the right moment. We have
6841 * to temporarily assign bytes_out to log what we currently have.
6842 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006843 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006844 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6845 s->logs.bytes_out = txn->rsp.eoh;
6846 s->do_log(s);
6847 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006848 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006849 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006850}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851
Willy Tarreaud98cf932009-12-27 22:54:55 +01006852/* This function is an analyser which forwards response body (including chunk
6853 * sizes if any). It is called as soon as we must forward, even if we forward
6854 * zero byte. The only situation where it must not be called is when we're in
6855 * tunnel mode and we want to forward till the close. It's used both to forward
6856 * remaining data and to resync after end of body. It expects the msg_state to
6857 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006858 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006859 *
6860 * It is capable of compressing response data both in content-length mode and
6861 * in chunked mode. The state machines follows different flows depending on
6862 * whether content-length and chunked modes are used, since there are no
6863 * trailers in content-length :
6864 *
6865 * chk-mode cl-mode
6866 * ,----- BODY -----.
6867 * / \
6868 * V size > 0 V chk-mode
6869 * .--> SIZE -------------> DATA -------------> CRLF
6870 * | | size == 0 | last byte |
6871 * | v final crlf v inspected |
6872 * | TRAILERS -----------> DONE |
6873 * | |
6874 * `----------------------------------------------'
6875 *
6876 * Compression only happens in the DATA state, and must be flushed in final
6877 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6878 * is performed at once on final states for all bytes parsed, or when leaving
6879 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006880 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006881int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006882{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006883 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006884 struct http_txn *txn = s->txn;
6885 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006886 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006887
Christopher Faulet814d2702017-03-30 11:33:44 +02006888 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6889 now_ms, __FUNCTION__,
6890 s,
6891 res,
6892 res->rex, res->wex,
6893 res->flags,
6894 res->buf->i,
6895 res->analysers);
6896
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006897 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6898 return 0;
6899
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006900 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006901 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006902 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006903 /* Output closed while we were sending data. We must abort and
6904 * wake the other side up.
6905 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006906 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006907 msg->msg_state = HTTP_MSG_ERROR;
6908 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006909 return 1;
6910 }
6911
Willy Tarreau4fe41902010-06-07 22:27:41 +02006912 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006913 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006914
Christopher Fauletd7c91962015-04-30 11:48:27 +02006915 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006916 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6917 ? HTTP_MSG_CHUNK_SIZE
6918 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006919 }
6920
Willy Tarreauefdf0942014-04-24 20:08:57 +02006921 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006922 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006923 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006924 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006925 }
6926
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006927 if (msg->msg_state < HTTP_MSG_DONE) {
6928 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6929 ? http_msg_forward_chunked_body(s, msg)
6930 : http_msg_forward_body(s, msg));
6931 if (!ret)
6932 goto missing_data_or_waiting;
6933 if (ret < 0)
6934 goto return_bad_res;
6935 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006936
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006937 /* other states, DONE...TUNNEL */
6938 /* for keep-alive we don't want to forward closes on DONE */
6939 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6940 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6941 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006942
Christopher Faulet894da4c2017-07-18 11:29:07 +02006943 http_resync_states(s);
6944 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006945 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6946 if (res->flags & CF_SHUTW) {
6947 /* response errors are most likely due to the
6948 * client aborting the transfer. */
6949 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006950 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006951 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006952 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006953 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006954 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006955 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006956 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006957 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006958
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006959 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006960 if (res->flags & CF_SHUTW)
6961 goto aborted_xfer;
6962
6963 /* stop waiting for data if the input is closed before the end. If the
6964 * client side was already closed, it means that the client has aborted,
6965 * so we don't want to count this as a server abort. Otherwise it's a
6966 * server abort.
6967 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006968 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006969 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006970 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006971 /* If we have some pending data, we continue the processing */
6972 if (!buffer_pending(res->buf)) {
6973 if (!(s->flags & SF_ERR_MASK))
6974 s->flags |= SF_ERR_SRVCL;
6975 s->be->be_counters.srv_aborts++;
6976 if (objt_server(s->target))
6977 objt_server(s->target)->counters.srv_aborts++;
6978 goto return_bad_res_stats_ok;
6979 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006980 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006981
Willy Tarreau40dba092010-03-04 18:14:51 +01006982 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006983 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006984 goto return_bad_res;
6985
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006986 /* When TE: chunked is used, we need to get there again to parse
6987 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006988 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6989 * or if there are filters registered on the stream, we don't want to
6990 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006991 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006992 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006993 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006994 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6995 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006996 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006997
Willy Tarreau5c620922011-05-11 19:56:11 +02006998 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006999 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007000 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007001 * modes are already handled by the stream sock layer. We must not do
7002 * this in content-length mode because it could present the MSG_MORE
7003 * flag with the last block of forwarded data, which would cause an
7004 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007005 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007006 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007007 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007008
Willy Tarreau87b09662015-04-03 00:22:06 +02007009 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007010 return 0;
7011
Willy Tarreau40dba092010-03-04 18:14:51 +01007012 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007013 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007014 if (objt_server(s->target))
7015 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007016
7017 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007018 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007019 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007020 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007021 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007022 res->analysers &= AN_RES_FLT_END;
7023 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007024 if (objt_server(s->target))
7025 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007026
Willy Tarreaue7dff022015-04-03 01:14:29 +02007027 if (!(s->flags & SF_ERR_MASK))
7028 s->flags |= SF_ERR_PRXCOND;
7029 if (!(s->flags & SF_FINST_MASK))
7030 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007031 return 0;
7032
7033 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007034 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007035 txn->rsp.msg_state = HTTP_MSG_ERROR;
7036 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007037 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007038 res->analysers &= AN_RES_FLT_END;
7039 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007040
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007041 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007042 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007043 if (objt_server(s->target))
7044 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007045
Willy Tarreaue7dff022015-04-03 01:14:29 +02007046 if (!(s->flags & SF_ERR_MASK))
7047 s->flags |= SF_ERR_CLICL;
7048 if (!(s->flags & SF_FINST_MASK))
7049 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007050 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007051}
7052
7053
7054static inline int
7055http_msg_forward_body(struct stream *s, struct http_msg *msg)
7056{
7057 struct channel *chn = msg->chn;
7058 int ret;
7059
7060 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7061
7062 if (msg->msg_state == HTTP_MSG_ENDING)
7063 goto ending;
7064
7065 /* Neither content-length, nor transfer-encoding was found, so we must
7066 * read the body until the server connection is closed. In that case, we
7067 * eat data as they come. Of course, this happens for response only. */
7068 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7069 unsigned long long len = (chn->buf->i - msg->next);
7070 msg->chunk_len += len;
7071 msg->body_len += len;
7072 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007073 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7074 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7075 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007076 msg->next += ret;
7077 msg->chunk_len -= ret;
7078 if (msg->chunk_len) {
7079 /* input empty or output full */
7080 if (chn->buf->i > msg->next)
7081 chn->flags |= CF_WAKE_WRITE;
7082 goto missing_data_or_waiting;
7083 }
7084
Christopher Faulet1486b0a2017-07-18 11:42:08 +02007085 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
7086 * always set for a request. */
7087 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7088 /* The server still sending data that should be filtered */
7089 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
7090 goto missing_data_or_waiting;
7091 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007092
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007093 msg->msg_state = HTTP_MSG_ENDING;
7094
7095 ending:
7096 /* we may have some pending data starting at res->buf->p such as a last
7097 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007098 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7099 /* default_ret */ msg->next,
7100 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007101 b_adv(chn->buf, ret);
7102 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007103 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7104 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007105 if (msg->next)
7106 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007107
Christopher Fauletda02e172015-12-04 09:25:05 +01007108 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7109 /* default_ret */ 1,
7110 /* on_error */ goto error,
7111 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007112 msg->msg_state = HTTP_MSG_DONE;
7113 return 1;
7114
7115 missing_data_or_waiting:
7116 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007117 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7118 /* default_ret */ msg->next,
7119 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007120 b_adv(chn->buf, ret);
7121 msg->next -= ret;
7122 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7123 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007124 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007125 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007126 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007127 return 0;
7128 error:
7129 return -1;
7130}
7131
7132static inline int
7133http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7134{
7135 struct channel *chn = msg->chn;
7136 int ret;
7137
7138 /* Here we have the guarantee to be in one of the following state:
7139 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7140 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7141
7142 switch_states:
7143 switch (msg->msg_state) {
7144 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007145 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7146 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7147 /* on_error */ goto error);
7148 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007149 msg->chunk_len -= ret;
7150 if (msg->chunk_len) {
7151 /* input empty or output full */
7152 if (chn->buf->i > msg->next)
7153 chn->flags |= CF_WAKE_WRITE;
7154 goto missing_data_or_waiting;
7155 }
7156
7157 /* nothing left to forward for this chunk*/
7158 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7159 /* fall through for HTTP_MSG_CHUNK_CRLF */
7160
7161 case HTTP_MSG_CHUNK_CRLF:
7162 /* we want the CRLF after the data */
7163 ret = http_skip_chunk_crlf(msg);
7164 if (ret == 0)
7165 goto missing_data_or_waiting;
7166 if (ret < 0)
7167 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007168 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007169 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7170 /* fall through for HTTP_MSG_CHUNK_SIZE */
7171
7172 case HTTP_MSG_CHUNK_SIZE:
7173 /* read the chunk size and assign it to ->chunk_len,
7174 * then set ->next to point to the body and switch to
7175 * DATA or TRAILERS state.
7176 */
7177 ret = http_parse_chunk_size(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 if (msg->chunk_len) {
7184 msg->msg_state = HTTP_MSG_DATA;
7185 goto switch_states;
7186 }
7187 msg->msg_state = HTTP_MSG_TRAILERS;
7188 /* fall through for HTTP_MSG_TRAILERS */
7189
7190 case HTTP_MSG_TRAILERS:
7191 ret = http_forward_trailers(msg);
7192 if (ret < 0)
7193 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007194 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7195 /* default_ret */ 1,
7196 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007197 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007198 if (!ret)
7199 goto missing_data_or_waiting;
7200 break;
7201
7202 case HTTP_MSG_ENDING:
7203 goto ending;
7204
7205 default:
7206 /* This should no happen in this function */
7207 goto error;
7208 }
7209
7210 msg->msg_state = HTTP_MSG_ENDING;
7211 ending:
7212 /* we may have some pending data starting at res->buf->p such as a last
7213 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007214 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007215 /* default_ret */ msg->next,
7216 /* on_error */ goto error);
7217 b_adv(chn->buf, ret);
7218 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007219 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7220 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007221 if (msg->next)
7222 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007223
Christopher Fauletda02e172015-12-04 09:25:05 +01007224 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007225 /* default_ret */ 1,
7226 /* on_error */ goto error,
7227 /* on_wait */ goto waiting);
7228 msg->msg_state = HTTP_MSG_DONE;
7229 return 1;
7230
7231 missing_data_or_waiting:
7232 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007233 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007234 /* default_ret */ msg->next,
7235 /* on_error */ goto error);
7236 b_adv(chn->buf, ret);
7237 msg->next -= ret;
7238 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7239 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007240 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007241 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007242 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007243 return 0;
7244
7245 chunk_parsing_error:
7246 if (msg->err_pos >= 0) {
7247 if (chn->flags & CF_ISRESP)
7248 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7249 msg->msg_state, strm_fe(s));
7250 else
7251 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7252 msg, msg->msg_state, s->be);
7253 }
7254 error:
7255 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007256}
7257
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259/* Iterate the same filter through all request headers.
7260 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007261 * Since it can manage the switch to another backend, it updates the per-proxy
7262 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007263 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007264int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007265{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007266 char *cur_ptr, *cur_end, *cur_next;
7267 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007268 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007269 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007270 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 last_hdr = 0;
7273
Willy Tarreau9b28e032012-10-12 23:49:43 +02007274 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 old_idx = 0;
7276
7277 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007278 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007279 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007280 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 (exp->action == ACT_ALLOW ||
7282 exp->action == ACT_DENY ||
7283 exp->action == ACT_TARPIT))
7284 return 0;
7285
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007286 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 if (!cur_idx)
7288 break;
7289
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007290 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 cur_ptr = cur_next;
7292 cur_end = cur_ptr + cur_hdr->len;
7293 cur_next = cur_end + cur_hdr->cr + 1;
7294
7295 /* Now we have one header between cur_ptr and cur_end,
7296 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007297 */
7298
Willy Tarreau15a53a42015-01-21 13:39:42 +01007299 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007300 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007301 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007302 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 last_hdr = 1;
7304 break;
7305
7306 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007307 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007309 break;
7310
7311 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007312 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 break;
7315
7316 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007317 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7318 if (trash.len < 0)
7319 return -1;
7320
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007321 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 /* FIXME: if the user adds a newline in the replacement, the
7323 * index will not be recalculated for now, and the new line
7324 * will not be counted as a new header.
7325 */
7326
7327 cur_end += delta;
7328 cur_next += delta;
7329 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007330 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 break;
7332
7333 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007334 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335 cur_next += delta;
7336
Willy Tarreaufa355d42009-11-29 18:12:29 +01007337 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007338 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7339 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 cur_hdr->len = 0;
7341 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007342 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007343 break;
7344
7345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 }
7347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348 /* keep the link from this header to next one in case of later
7349 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007350 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351 old_idx = cur_idx;
7352 }
7353 return 0;
7354}
7355
7356
7357/* Apply the filter to the request line.
7358 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7359 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 * Since it can manage the switch to another backend, it updates the per-proxy
7361 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007363int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 char *cur_ptr, *cur_end;
7366 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007367 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007368 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007369
Willy Tarreau3d300592007-03-18 18:34:41 +01007370 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007372 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 (exp->action == ACT_ALLOW ||
7374 exp->action == ACT_DENY ||
7375 exp->action == ACT_TARPIT))
7376 return 0;
7377 else if (exp->action == ACT_REMOVE)
7378 return 0;
7379
7380 done = 0;
7381
Willy Tarreau9b28e032012-10-12 23:49:43 +02007382 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007383 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384
7385 /* Now we have the request line between cur_ptr and cur_end */
7386
Willy Tarreau15a53a42015-01-21 13:39:42 +01007387 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007390 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 done = 1;
7392 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007394 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007395 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007396 done = 1;
7397 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007399 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007400 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007401 done = 1;
7402 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007405 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7406 if (trash.len < 0)
7407 return -1;
7408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007409 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007410 /* FIXME: if the user adds a newline in the replacement, the
7411 * index will not be recalculated for now, and the new line
7412 * will not be counted as a new header.
7413 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007414
Willy Tarreaufa355d42009-11-29 18:12:29 +01007415 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007417 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007418 HTTP_MSG_RQMETH,
7419 cur_ptr, cur_end + 1,
7420 NULL, NULL);
7421 if (unlikely(!cur_end))
7422 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 /* we have a full request and we know that we have either a CR
7425 * or an LF at <ptr>.
7426 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007427 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7428 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007429 /* there is no point trying this regex on headers */
7430 return 1;
7431 }
7432 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007433 return done;
7434}
Willy Tarreau97de6242006-12-27 17:18:38 +01007435
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436
Willy Tarreau58f10d72006-12-04 02:26:12 +01007437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007438/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007439 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441 * unparsable request. Since it can manage the switch to another backend, it
7442 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007443 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007444int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007445{
Willy Tarreau192252e2015-04-04 01:47:55 +02007446 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007447 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007448 struct hdr_exp *exp;
7449
7450 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007451 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 /*
7454 * The interleaving of transformations and verdicts
7455 * makes it difficult to decide to continue or stop
7456 * the evaluation.
7457 */
7458
Willy Tarreau6c123b12010-01-28 20:22:06 +01007459 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7460 break;
7461
Willy Tarreau3d300592007-03-18 18:34:41 +01007462 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007463 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007464 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007466
7467 /* if this filter had a condition, evaluate it now and skip to
7468 * next filter if the condition does not match.
7469 */
7470 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007471 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007472 ret = acl_pass(ret);
7473 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7474 ret = !ret;
7475
7476 if (!ret)
7477 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007478 }
7479
7480 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007481 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007482 if (unlikely(ret < 0))
7483 return -1;
7484
7485 if (likely(ret == 0)) {
7486 /* The filter did not match the request, it can be
7487 * iterated through all headers.
7488 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007489 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7490 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007492 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007493 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494}
7495
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007496
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007497/* Find the end of a cookie value contained between <s> and <e>. It works the
7498 * same way as with headers above except that the semi-colon also ends a token.
7499 * See RFC2965 for more information. Note that it requires a valid header to
7500 * return a valid result.
7501 */
7502char *find_cookie_value_end(char *s, const char *e)
7503{
7504 int quoted, qdpair;
7505
7506 quoted = qdpair = 0;
7507 for (; s < e; s++) {
7508 if (qdpair) qdpair = 0;
7509 else if (quoted) {
7510 if (*s == '\\') qdpair = 1;
7511 else if (*s == '"') quoted = 0;
7512 }
7513 else if (*s == '"') quoted = 1;
7514 else if (*s == ',' || *s == ';') return s;
7515 }
7516 return s;
7517}
7518
7519/* Delete a value in a header between delimiters <from> and <next> in buffer
7520 * <buf>. The number of characters displaced is returned, and the pointer to
7521 * the first delimiter is updated if required. The function tries as much as
7522 * possible to respect the following principles :
7523 * - replace <from> delimiter by the <next> one unless <from> points to a
7524 * colon, in which case <next> is simply removed
7525 * - set exactly one space character after the new first delimiter, unless
7526 * there are not enough characters in the block being moved to do so.
7527 * - remove unneeded spaces before the previous delimiter and after the new
7528 * one.
7529 *
7530 * It is the caller's responsibility to ensure that :
7531 * - <from> points to a valid delimiter or the colon ;
7532 * - <next> points to a valid delimiter or the final CR/LF ;
7533 * - there are non-space chars before <from> ;
7534 * - there is a CR/LF at or after <next>.
7535 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007536int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007537{
7538 char *prev = *from;
7539
7540 if (*prev == ':') {
7541 /* We're removing the first value, preserve the colon and add a
7542 * space if possible.
7543 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007544 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 next++;
7546 prev++;
7547 if (prev < next)
7548 *prev++ = ' ';
7549
Willy Tarreau2235b262016-11-05 15:50:20 +01007550 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551 next++;
7552 } else {
7553 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007554 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 prev--;
7556 *from = prev;
7557
7558 /* copy the delimiter and if possible a space if we're
7559 * not at the end of the line.
7560 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007561 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 *prev++ = *next++;
7563 if (prev + 1 < next)
7564 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007565 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 next++;
7567 }
7568 }
7569 return buffer_replace2(buf, prev, next, NULL, 0);
7570}
7571
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007572/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007573 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 * desirable to call it only when needed. This code is quite complex because
7575 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7576 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007577 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007578void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007580 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007581 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007583 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7585 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007587 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007588 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007589 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007591 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007593 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007595 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 hdr_beg = hdr_next;
7597 hdr_end = hdr_beg + cur_hdr->len;
7598 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007599
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 /* We have one full header between hdr_beg and hdr_end, and the
7601 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007602 * "Cookie:" headers.
7603 */
7604
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007606 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 old_idx = cur_idx;
7608 continue;
7609 }
7610
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007611 del_from = NULL; /* nothing to be deleted */
7612 preserve_hdr = 0; /* assume we may kill the whole header */
7613
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614 /* Now look for cookies. Conforming to RFC2109, we have to support
7615 * attributes whose name begin with a '$', and associate them with
7616 * the right cookie, if we want to delete this cookie.
7617 * So there are 3 cases for each cookie read :
7618 * 1) it's a special attribute, beginning with a '$' : ignore it.
7619 * 2) it's a server id cookie that we *MAY* want to delete : save
7620 * some pointers on it (last semi-colon, beginning of cookie...)
7621 * 3) it's an application cookie : we *MAY* have to delete a previous
7622 * "special" cookie.
7623 * At the end of loop, if a "special" cookie remains, we may have to
7624 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007625 * *MUST* delete it.
7626 *
7627 * Note: RFC2965 is unclear about the processing of spaces around
7628 * the equal sign in the ATTR=VALUE form. A careful inspection of
7629 * the RFC explicitly allows spaces before it, and not within the
7630 * tokens (attrs or values). An inspection of RFC2109 allows that
7631 * too but section 10.1.3 lets one think that spaces may be allowed
7632 * after the equal sign too, resulting in some (rare) buggy
7633 * implementations trying to do that. So let's do what servers do.
7634 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7635 * allowed quoted strings in values, with any possible character
7636 * after a backslash, including control chars and delimitors, which
7637 * causes parsing to become ambiguous. Browsers also allow spaces
7638 * within values even without quotes.
7639 *
7640 * We have to keep multiple pointers in order to support cookie
7641 * removal at the beginning, middle or end of header without
7642 * corrupting the header. All of these headers are valid :
7643 *
7644 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7645 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7646 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7647 * | | | | | | | | |
7648 * | | | | | | | | hdr_end <--+
7649 * | | | | | | | +--> next
7650 * | | | | | | +----> val_end
7651 * | | | | | +-----------> val_beg
7652 * | | | | +--------------> equal
7653 * | | | +----------------> att_end
7654 * | | +---------------------> att_beg
7655 * | +--------------------------> prev
7656 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007657 */
7658
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7660 /* Iterate through all cookies on this line */
7661
7662 /* find att_beg */
7663 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007664 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 att_beg++;
7666
7667 /* find att_end : this is the first character after the last non
7668 * space before the equal. It may be equal to hdr_end.
7669 */
7670 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007671
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 while (equal < hdr_end) {
7673 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007674 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007675 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 continue;
7677 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007678 }
7679
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7681 * is between <att_beg> and <equal>, both may be identical.
7682 */
7683
7684 /* look for end of cookie if there is an equal sign */
7685 if (equal < hdr_end && *equal == '=') {
7686 /* look for the beginning of the value */
7687 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007688 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 val_beg++;
7690
7691 /* find the end of the value, respecting quotes */
7692 next = find_cookie_value_end(val_beg, hdr_end);
7693
7694 /* make val_end point to the first white space or delimitor after the value */
7695 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007696 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 val_end--;
7698 } else {
7699 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007700 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* We have nothing to do with attributes beginning with '$'. However,
7703 * they will automatically be removed if a header before them is removed,
7704 * since they're supposed to be linked together.
7705 */
7706 if (*att_beg == '$')
7707 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 /* Ignore cookies with no equal sign */
7710 if (equal == next) {
7711 /* This is not our cookie, so we must preserve it. But if we already
7712 * scheduled another cookie for removal, we cannot remove the
7713 * complete header, but we can remove the previous block itself.
7714 */
7715 preserve_hdr = 1;
7716 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 val_end += delta;
7719 next += delta;
7720 hdr_end += delta;
7721 hdr_next += delta;
7722 cur_hdr->len += delta;
7723 http_msg_move_end(&txn->req, delta);
7724 prev = del_from;
7725 del_from = NULL;
7726 }
7727 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007728 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007729
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 /* if there are spaces around the equal sign, we need to
7731 * strip them otherwise we'll get trouble for cookie captures,
7732 * or even for rewrites. Since this happens extremely rarely,
7733 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007734 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7736 int stripped_before = 0;
7737 int stripped_after = 0;
7738
7739 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007740 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007741 equal += stripped_before;
7742 val_beg += stripped_before;
7743 }
7744
7745 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007746 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 val_beg += stripped_after;
7748 stripped_before += stripped_after;
7749 }
7750
7751 val_end += stripped_before;
7752 next += stripped_before;
7753 hdr_end += stripped_before;
7754 hdr_next += stripped_before;
7755 cur_hdr->len += stripped_before;
7756 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007757 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007759
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007760 /* First, let's see if we want to capture this cookie. We check
7761 * that we don't already have a client side cookie, because we
7762 * can only capture one. Also as an optimisation, we ignore
7763 * cookies shorter than the declared name.
7764 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007765 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7766 (val_end - att_beg >= sess->fe->capture_namelen) &&
7767 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 int log_len = val_end - att_beg;
7769
7770 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7771 Alert("HTTP logging : out of memory.\n");
7772 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007773 if (log_len > sess->fe->capture_len)
7774 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 memcpy(txn->cli_cookie, att_beg, log_len);
7776 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007777 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007779
Willy Tarreaubca99692010-10-06 19:25:55 +02007780 /* Persistence cookies in passive, rewrite or insert mode have the
7781 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007783 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 * For cookies in prefix mode, the form is :
7786 *
7787 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007789 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7790 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7791 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007793
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007794 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7795 * have the server ID between val_beg and delim, and the original cookie between
7796 * delim+1 and val_end. Otherwise, delim==val_end :
7797 *
7798 * Cookie: NAME=SRV; # in all but prefix modes
7799 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7800 * | || || | |+-> next
7801 * | || || | +--> val_end
7802 * | || || +---------> delim
7803 * | || |+------------> val_beg
7804 * | || +-------------> att_end = equal
7805 * | |+-----------------> att_beg
7806 * | +------------------> prev
7807 * +-------------------------> hdr_beg
7808 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007809
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007810 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007811 for (delim = val_beg; delim < val_end; delim++)
7812 if (*delim == COOKIE_DELIM)
7813 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007814 } else {
7815 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007816 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007817 /* Now check if the cookie contains a date field, which would
7818 * appear after a vertical bar ('|') just after the server name
7819 * and before the delimiter.
7820 */
7821 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7822 if (vbar1) {
7823 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007824 * right is the last seen date. It is a base64 encoded
7825 * 30-bit value representing the UNIX date since the
7826 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007827 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007828 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007829 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007830 if (val_end - vbar1 >= 5) {
7831 val = b64tos30(vbar1);
7832 if (val > 0)
7833 txn->cookie_last_date = val << 2;
7834 }
7835 /* look for a second vertical bar */
7836 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7837 if (vbar1 && (val_end - vbar1 > 5)) {
7838 val = b64tos30(vbar1 + 1);
7839 if (val > 0)
7840 txn->cookie_first_date = val << 2;
7841 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007842 }
7843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007844
Willy Tarreauf64d1412010-10-07 20:06:11 +02007845 /* if the cookie has an expiration date and the proxy wants to check
7846 * it, then we do that now. We first check if the cookie is too old,
7847 * then only if it has expired. We detect strict overflow because the
7848 * time resolution here is not great (4 seconds). Cookies with dates
7849 * in the future are ignored if their offset is beyond one day. This
7850 * allows an admin to fix timezone issues without expiring everyone
7851 * and at the same time avoids keeping unwanted side effects for too
7852 * long.
7853 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007854 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7855 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007856 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007857 txn->flags &= ~TX_CK_MASK;
7858 txn->flags |= TX_CK_OLD;
7859 delim = val_beg; // let's pretend we have not found the cookie
7860 txn->cookie_first_date = 0;
7861 txn->cookie_last_date = 0;
7862 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007863 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7864 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007865 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007866 txn->flags &= ~TX_CK_MASK;
7867 txn->flags |= TX_CK_EXPIRED;
7868 delim = val_beg; // let's pretend we have not found the cookie
7869 txn->cookie_first_date = 0;
7870 txn->cookie_last_date = 0;
7871 }
7872
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007873 /* Here, we'll look for the first running server which supports the cookie.
7874 * This allows to share a same cookie between several servers, for example
7875 * to dedicate backup servers to specific servers only.
7876 * However, to prevent clients from sticking to cookie-less backup server
7877 * when they have incidentely learned an empty cookie, we simply ignore
7878 * empty cookies and mark them as invalid.
7879 * The same behaviour is applied when persistence must be ignored.
7880 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007881 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007882 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007883
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 while (srv) {
7885 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7886 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007887 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007888 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007889 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007890 /* we found the server and we can use it */
7891 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007892 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007893 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007894 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007895 break;
7896 } else {
7897 /* we found a server, but it's down,
7898 * mark it as such and go on in case
7899 * another one is available.
7900 */
7901 txn->flags &= ~TX_CK_MASK;
7902 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007904 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007905 srv = srv->next;
7906 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007907
Willy Tarreauf64d1412010-10-07 20:06:11 +02007908 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007909 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007910 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007911 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007912 txn->flags |= TX_CK_UNUSED;
7913 else
7914 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007916
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007917 /* depending on the cookie mode, we may have to either :
7918 * - delete the complete cookie if we're in insert+indirect mode, so that
7919 * the server never sees it ;
7920 * - remove the server id from the cookie value, and tag the cookie as an
7921 * application cookie so that it does not get accidentely removed later,
7922 * if we're in cookie prefix mode
7923 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007924 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007925 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007926
Willy Tarreau9b28e032012-10-12 23:49:43 +02007927 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007928 val_end += delta;
7929 next += delta;
7930 hdr_end += delta;
7931 hdr_next += delta;
7932 cur_hdr->len += delta;
7933 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007935 del_from = NULL;
7936 preserve_hdr = 1; /* we want to keep this cookie */
7937 }
7938 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007939 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007940 del_from = prev;
7941 }
7942 } else {
7943 /* This is not our cookie, so we must preserve it. But if we already
7944 * scheduled another cookie for removal, we cannot remove the
7945 * complete header, but we can remove the previous block itself.
7946 */
7947 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007949 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007950 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007951 if (att_beg >= del_from)
7952 att_beg += delta;
7953 if (att_end >= del_from)
7954 att_end += delta;
7955 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007956 val_end += delta;
7957 next += delta;
7958 hdr_end += delta;
7959 hdr_next += delta;
7960 cur_hdr->len += delta;
7961 http_msg_move_end(&txn->req, delta);
7962 prev = del_from;
7963 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007964 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007965 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 /* continue with next cookie on this header line */
7968 att_beg = next;
7969 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007970
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007971 /* There are no more cookies on this line.
7972 * We may still have one (or several) marked for deletion at the
7973 * end of the line. We must do this now in two ways :
7974 * - if some cookies must be preserved, we only delete from the
7975 * mark to the end of line ;
7976 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007977 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007978 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007981 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007982 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007983 cur_hdr->len += delta;
7984 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007985 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007986
7987 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007988 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7989 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007990 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007991 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007992 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007993 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007994 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007995 }
7996
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007997 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007998 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007999 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008000}
8001
8002
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003/* Iterate the same filter through all response headers contained in <rtr>.
8004 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8005 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008006int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 char *cur_ptr, *cur_end, *cur_next;
8009 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008010 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008012 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013
8014 last_hdr = 0;
8015
Willy Tarreau9b28e032012-10-12 23:49:43 +02008016 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017 old_idx = 0;
8018
8019 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008020 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 (exp->action == ACT_ALLOW ||
8024 exp->action == ACT_DENY))
8025 return 0;
8026
8027 cur_idx = txn->hdr_idx.v[old_idx].next;
8028 if (!cur_idx)
8029 break;
8030
8031 cur_hdr = &txn->hdr_idx.v[cur_idx];
8032 cur_ptr = cur_next;
8033 cur_end = cur_ptr + cur_hdr->len;
8034 cur_next = cur_end + cur_hdr->cr + 1;
8035
8036 /* Now we have one header between cur_ptr and cur_end,
8037 * and the next header starts at cur_next.
8038 */
8039
Willy Tarreau15a53a42015-01-21 13:39:42 +01008040 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 switch (exp->action) {
8042 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008043 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 last_hdr = 1;
8045 break;
8046
8047 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008048 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 last_hdr = 1;
8050 break;
8051
8052 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008053 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8054 if (trash.len < 0)
8055 return -1;
8056
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008057 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058 /* FIXME: if the user adds a newline in the replacement, the
8059 * index will not be recalculated for now, and the new line
8060 * will not be counted as a new header.
8061 */
8062
8063 cur_end += delta;
8064 cur_next += delta;
8065 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008066 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 break;
8068
8069 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008070 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071 cur_next += delta;
8072
Willy Tarreaufa355d42009-11-29 18:12:29 +01008073 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8075 txn->hdr_idx.used--;
8076 cur_hdr->len = 0;
8077 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008078 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079 break;
8080
8081 }
8082 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083
8084 /* keep the link from this header to next one in case of later
8085 * removal of next header.
8086 */
8087 old_idx = cur_idx;
8088 }
8089 return 0;
8090}
8091
8092
8093/* Apply the filter to the status line in the response buffer <rtr>.
8094 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8095 * or -1 if a replacement resulted in an invalid status line.
8096 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008097int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099 char *cur_ptr, *cur_end;
8100 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008101 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008102 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103
8104
Willy Tarreau3d300592007-03-18 18:34:41 +01008105 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008107 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108 (exp->action == ACT_ALLOW ||
8109 exp->action == ACT_DENY))
8110 return 0;
8111 else if (exp->action == ACT_REMOVE)
8112 return 0;
8113
8114 done = 0;
8115
Willy Tarreau9b28e032012-10-12 23:49:43 +02008116 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008117 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118
8119 /* Now we have the status line between cur_ptr and cur_end */
8120
Willy Tarreau15a53a42015-01-21 13:39:42 +01008121 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122 switch (exp->action) {
8123 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008124 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125 done = 1;
8126 break;
8127
8128 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008129 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130 done = 1;
8131 break;
8132
8133 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008134 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8135 if (trash.len < 0)
8136 return -1;
8137
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008138 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 /* FIXME: if the user adds a newline in the replacement, the
8140 * index will not be recalculated for now, and the new line
8141 * will not be counted as a new header.
8142 */
8143
Willy Tarreaufa355d42009-11-29 18:12:29 +01008144 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008146 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008147 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148 cur_ptr, cur_end + 1,
8149 NULL, NULL);
8150 if (unlikely(!cur_end))
8151 return -1;
8152
8153 /* we have a full respnse and we know that we have either a CR
8154 * or an LF at <ptr>.
8155 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008156 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008157 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008158 /* there is no point trying this regex on headers */
8159 return 1;
8160 }
8161 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 return done;
8163}
8164
8165
8166
8167/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008168 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8170 * unparsable response.
8171 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008172int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008173{
Willy Tarreau192252e2015-04-04 01:47:55 +02008174 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008175 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008176 struct hdr_exp *exp;
8177
8178 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179 int ret;
8180
8181 /*
8182 * The interleaving of transformations and verdicts
8183 * makes it difficult to decide to continue or stop
8184 * the evaluation.
8185 */
8186
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008187 if (txn->flags & TX_SVDENY)
8188 break;
8189
Willy Tarreau3d300592007-03-18 18:34:41 +01008190 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8192 exp->action == ACT_PASS)) {
8193 exp = exp->next;
8194 continue;
8195 }
8196
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008197 /* if this filter had a condition, evaluate it now and skip to
8198 * next filter if the condition does not match.
8199 */
8200 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008201 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008202 ret = acl_pass(ret);
8203 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8204 ret = !ret;
8205 if (!ret)
8206 continue;
8207 }
8208
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008210 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 if (unlikely(ret < 0))
8212 return -1;
8213
8214 if (likely(ret == 0)) {
8215 /* The filter did not match the response, it can be
8216 * iterated through all headers.
8217 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008218 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8219 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221 }
8222 return 0;
8223}
8224
8225
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008227 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 * desirable to call it only when needed. This function is also used when we
8229 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008231void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008233 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008234 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008235 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008238 char *hdr_beg, *hdr_end, *hdr_next;
8239 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240
Willy Tarreaua15645d2007-03-18 16:22:39 +01008241 /* Iterate through the headers.
8242 * we start with the start line.
8243 */
8244 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008245 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246
8247 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8248 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008249 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008250
8251 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008252 hdr_beg = hdr_next;
8253 hdr_end = hdr_beg + cur_hdr->len;
8254 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255
Willy Tarreau24581ba2010-08-31 22:39:35 +02008256 /* We have one full header between hdr_beg and hdr_end, and the
8257 * next header starts at hdr_next. We're only interested in
8258 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259 */
8260
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 is_cookie2 = 0;
8262 prev = hdr_beg + 10;
8263 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008264 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008265 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8266 if (!val) {
8267 old_idx = cur_idx;
8268 continue;
8269 }
8270 is_cookie2 = 1;
8271 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008272 }
8273
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8275 * <prev> points to the colon.
8276 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008277 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008278
Willy Tarreau24581ba2010-08-31 22:39:35 +02008279 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8280 * check-cache is enabled) and we are not interested in checking
8281 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008282 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008283 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008284 return;
8285
Willy Tarreau24581ba2010-08-31 22:39:35 +02008286 /* OK so now we know we have to process this response cookie.
8287 * The format of the Set-Cookie header is slightly different
8288 * from the format of the Cookie header in that it does not
8289 * support the comma as a cookie delimiter (thus the header
8290 * cannot be folded) because the Expires attribute described in
8291 * the original Netscape's spec may contain an unquoted date
8292 * with a comma inside. We have to live with this because
8293 * many browsers don't support Max-Age and some browsers don't
8294 * support quoted strings. However the Set-Cookie2 header is
8295 * clean.
8296 *
8297 * We have to keep multiple pointers in order to support cookie
8298 * removal at the beginning, middle or end of header without
8299 * corrupting the header (in case of set-cookie2). A special
8300 * pointer, <scav> points to the beginning of the set-cookie-av
8301 * fields after the first semi-colon. The <next> pointer points
8302 * either to the end of line (set-cookie) or next unquoted comma
8303 * (set-cookie2). All of these headers are valid :
8304 *
8305 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8306 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8307 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8308 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8309 * | | | | | | | | | |
8310 * | | | | | | | | +-> next hdr_end <--+
8311 * | | | | | | | +------------> scav
8312 * | | | | | | +--------------> val_end
8313 * | | | | | +--------------------> val_beg
8314 * | | | | +----------------------> equal
8315 * | | | +------------------------> att_end
8316 * | | +----------------------------> att_beg
8317 * | +------------------------------> prev
8318 * +-----------------------------------------> hdr_beg
8319 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008320
Willy Tarreau24581ba2010-08-31 22:39:35 +02008321 for (; prev < hdr_end; prev = next) {
8322 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 /* find att_beg */
8325 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008326 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008327 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328
Willy Tarreau24581ba2010-08-31 22:39:35 +02008329 /* find att_end : this is the first character after the last non
8330 * space before the equal. It may be equal to hdr_end.
8331 */
8332 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 while (equal < hdr_end) {
8335 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8336 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008337 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008338 continue;
8339 att_end = equal;
8340 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8343 * is between <att_beg> and <equal>, both may be identical.
8344 */
8345
8346 /* look for end of cookie if there is an equal sign */
8347 if (equal < hdr_end && *equal == '=') {
8348 /* look for the beginning of the value */
8349 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008350 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008351 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008352
Willy Tarreau24581ba2010-08-31 22:39:35 +02008353 /* find the end of the value, respecting quotes */
8354 next = find_cookie_value_end(val_beg, hdr_end);
8355
8356 /* make val_end point to the first white space or delimitor after the value */
8357 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008358 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008359 val_end--;
8360 } else {
8361 /* <equal> points to next comma, semi-colon or EOL */
8362 val_beg = val_end = next = equal;
8363 }
8364
8365 if (next < hdr_end) {
8366 /* Set-Cookie2 supports multiple cookies, and <next> points to
8367 * a colon or semi-colon before the end. So skip all attr-value
8368 * pairs and look for the next comma. For Set-Cookie, since
8369 * commas are permitted in values, skip to the end.
8370 */
8371 if (is_cookie2)
8372 next = find_hdr_value_end(next, hdr_end);
8373 else
8374 next = hdr_end;
8375 }
8376
8377 /* Now everything is as on the diagram above */
8378
8379 /* Ignore cookies with no equal sign */
8380 if (equal == val_end)
8381 continue;
8382
8383 /* If there are spaces around the equal sign, we need to
8384 * strip them otherwise we'll get trouble for cookie captures,
8385 * or even for rewrites. Since this happens extremely rarely,
8386 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008387 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008388 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8389 int stripped_before = 0;
8390 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008391
Willy Tarreau24581ba2010-08-31 22:39:35 +02008392 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008393 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 equal += stripped_before;
8395 val_beg += stripped_before;
8396 }
8397
8398 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008399 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008400 val_beg += stripped_after;
8401 stripped_before += stripped_after;
8402 }
8403
8404 val_end += stripped_before;
8405 next += stripped_before;
8406 hdr_end += stripped_before;
8407 hdr_next += stripped_before;
8408 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008409 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008410 }
8411
8412 /* First, let's see if we want to capture this cookie. We check
8413 * that we don't already have a server side cookie, because we
8414 * can only capture one. Also as an optimisation, we ignore
8415 * cookies shorter than the declared name.
8416 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008417 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008418 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008419 (val_end - att_beg >= sess->fe->capture_namelen) &&
8420 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008421 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008422 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423 Alert("HTTP logging : out of memory.\n");
8424 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008425 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008426 if (log_len > sess->fe->capture_len)
8427 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008428 memcpy(txn->srv_cookie, att_beg, log_len);
8429 txn->srv_cookie[log_len] = 0;
8430 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008431 }
8432
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008433 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008434 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008435 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008436 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8437 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008438 /* assume passive cookie by default */
8439 txn->flags &= ~TX_SCK_MASK;
8440 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008441
8442 /* If the cookie is in insert mode on a known server, we'll delete
8443 * this occurrence because we'll insert another one later.
8444 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008445 * a direct access.
8446 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008447 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008448 /* The "preserve" flag was set, we don't want to touch the
8449 * server's cookie.
8450 */
8451 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008452 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008453 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008454 /* this cookie must be deleted */
8455 if (*prev == ':' && next == hdr_end) {
8456 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008457 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008458 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8459 txn->hdr_idx.used--;
8460 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008461 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008462 hdr_next += delta;
8463 http_msg_move_end(&txn->rsp, delta);
8464 /* note: while both invalid now, <next> and <hdr_end>
8465 * are still equal, so the for() will stop as expected.
8466 */
8467 } else {
8468 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008469 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008470 next = prev;
8471 hdr_end += delta;
8472 hdr_next += delta;
8473 cur_hdr->len += delta;
8474 http_msg_move_end(&txn->rsp, delta);
8475 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008476 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008477 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008478 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008480 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008481 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008482 * with this server since we know it.
8483 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008484 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008485 next += delta;
8486 hdr_end += delta;
8487 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008488 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008489 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008490
Willy Tarreauf1348312010-10-07 15:54:11 +02008491 txn->flags &= ~TX_SCK_MASK;
8492 txn->flags |= TX_SCK_REPLACED;
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_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008496 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008498 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
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 Tarreau827aee92011-03-10 16:55:02 +01008505 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008506 txn->flags &= ~TX_SCK_MASK;
8507 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008508 }
8509 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008510 /* that's done for this cookie, check the next one on the same
8511 * line when next != hdr_end (only if is_cookie2).
8512 */
8513 }
8514 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008516 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008517}
8518
8519
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008521 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008523void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008524{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008525 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526 char *p1, *p2;
8527
8528 char *cur_ptr, *cur_end, *cur_next;
8529 int cur_idx;
8530
Willy Tarreau5df51872007-11-25 16:20:08 +01008531 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532 return;
8533
8534 /* Iterate through the headers.
8535 * we start with the start line.
8536 */
8537 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008538 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008539
8540 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8541 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008542 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008543
8544 cur_hdr = &txn->hdr_idx.v[cur_idx];
8545 cur_ptr = cur_next;
8546 cur_end = cur_ptr + cur_hdr->len;
8547 cur_next = cur_end + cur_hdr->cr + 1;
8548
8549 /* We have one full header between cur_ptr and cur_end, and the
8550 * next header starts at cur_next. We're only interested in
8551 * "Cookie:" headers.
8552 */
8553
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008554 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8555 if (val) {
8556 if ((cur_end - (cur_ptr + val) >= 8) &&
8557 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8558 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8559 return;
8560 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008561 }
8562
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008563 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8564 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008565 continue;
8566
8567 /* OK, right now we know we have a cache-control header at cur_ptr */
8568
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008569 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008570
8571 if (p1 >= cur_end) /* no more info */
8572 continue;
8573
8574 /* p1 is at the beginning of the value */
8575 p2 = p1;
8576
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008577 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008578 p2++;
8579
8580 /* we have a complete value between p1 and p2 */
8581 if (p2 < cur_end && *p2 == '=') {
8582 /* we have something of the form no-cache="set-cookie" */
8583 if ((cur_end - p1 >= 21) &&
8584 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8585 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008586 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008587 continue;
8588 }
8589
8590 /* OK, so we know that either p2 points to the end of string or to a comma */
8591 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008592 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008593 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8594 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8595 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008596 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008597 return;
8598 }
8599
8600 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008601 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008602 continue;
8603 }
8604 }
8605}
8606
Willy Tarreau58f10d72006-12-04 02:26:12 +01008607
Willy Tarreaub2513902006-12-17 14:52:38 +01008608/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008609 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008610 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008611 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008612 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008613 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008614 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008615 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008616 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008617int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008618{
8619 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008620 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008621 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008622
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008623 if (!uri_auth)
8624 return 0;
8625
Cyril Bonté70be45d2010-10-12 00:14:35 +02008626 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008627 return 0;
8628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008629 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008630 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008631 return 0;
8632
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008633 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008634 return 0;
8635
Willy Tarreaub2513902006-12-17 14:52:38 +01008636 return 1;
8637}
8638
Willy Tarreau4076a152009-04-02 15:18:36 +02008639/*
8640 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008641 * By default it tries to report the error position as msg->err_pos. However if
8642 * this one is not set, it will then report msg->next, which is the last known
8643 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008644 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008645 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008646void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008647 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008648 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008649{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008650 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008651 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008652 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008653
Willy Tarreauf3764b72016-03-31 13:45:10 +02008654 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008655 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008656 len1 = MIN(len1, es->len);
8657 len2 = es->len - len1; /* remaining data if buffer wraps */
8658
Willy Tarreauf3764b72016-03-31 13:45:10 +02008659 if (!es->buf)
8660 es->buf = malloc(global.tune.bufsize);
8661
8662 if (es->buf) {
8663 memcpy(es->buf, chn->buf->p, len1);
8664 if (len2)
8665 memcpy(es->buf + len1, chn->buf->data, len2);
8666 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008667
Willy Tarreau4076a152009-04-02 15:18:36 +02008668 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008669 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008670 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008671 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008672
Willy Tarreau4076a152009-04-02 15:18:36 +02008673 es->when = date; // user-visible date
8674 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008675 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008676 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008677 if (objt_conn(sess->origin))
8678 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008679 else
8680 memset(&es->src, 0, sizeof(es->src));
8681
Willy Tarreau078272e2010-12-12 12:46:33 +01008682 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008683 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008684 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008685 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008686 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008687 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008688 es->b_out = chn->buf->o;
8689 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008690 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008691 es->m_clen = msg->chunk_len;
8692 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008693}
Willy Tarreaub2513902006-12-17 14:52:38 +01008694
Willy Tarreau294c4732011-12-16 21:35:50 +01008695/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8696 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8697 * performed over the whole headers. Otherwise it must contain a valid header
8698 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8699 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8700 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8701 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008702 * -1. The value fetch stops at commas, so this function is suited for use with
8703 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008705 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008706unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008707 struct hdr_idx *idx, int occ,
8708 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008709{
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 struct hdr_ctx local_ctx;
8711 char *ptr_hist[MAX_HDR_HISTORY];
8712 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008713 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008714 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008715
Willy Tarreau294c4732011-12-16 21:35:50 +01008716 if (!ctx) {
8717 local_ctx.idx = 0;
8718 ctx = &local_ctx;
8719 }
8720
Willy Tarreaubce70882009-09-07 11:51:47 +02008721 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008723 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008724 occ--;
8725 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008726 *vptr = ctx->line + ctx->val;
8727 *vlen = ctx->vlen;
8728 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008729 }
8730 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008731 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008732 }
8733
8734 /* negative occurrence, we scan all the list then walk back */
8735 if (-occ > MAX_HDR_HISTORY)
8736 return 0;
8737
Willy Tarreau294c4732011-12-16 21:35:50 +01008738 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008739 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008740 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8741 len_hist[hist_ptr] = ctx->vlen;
8742 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008743 hist_ptr = 0;
8744 found++;
8745 }
8746 if (-occ > found)
8747 return 0;
8748 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008749 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8750 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8751 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008752 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008753 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008754 if (hist_ptr >= MAX_HDR_HISTORY)
8755 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008756 *vptr = ptr_hist[hist_ptr];
8757 *vlen = len_hist[hist_ptr];
8758 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008759}
8760
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008761/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8762 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8763 * performed over the whole headers. Otherwise it must contain a valid header
8764 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8765 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8766 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8767 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8768 * -1. This function differs from http_get_hdr() in that it only returns full
8769 * line header values and does not stop at commas.
8770 * The return value is 0 if nothing was found, or non-zero otherwise.
8771 */
8772unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8773 struct hdr_idx *idx, int occ,
8774 struct hdr_ctx *ctx, char **vptr, int *vlen)
8775{
8776 struct hdr_ctx local_ctx;
8777 char *ptr_hist[MAX_HDR_HISTORY];
8778 int len_hist[MAX_HDR_HISTORY];
8779 unsigned int hist_ptr;
8780 int found;
8781
8782 if (!ctx) {
8783 local_ctx.idx = 0;
8784 ctx = &local_ctx;
8785 }
8786
8787 if (occ >= 0) {
8788 /* search from the beginning */
8789 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8790 occ--;
8791 if (occ <= 0) {
8792 *vptr = ctx->line + ctx->val;
8793 *vlen = ctx->vlen;
8794 return 1;
8795 }
8796 }
8797 return 0;
8798 }
8799
8800 /* negative occurrence, we scan all the list then walk back */
8801 if (-occ > MAX_HDR_HISTORY)
8802 return 0;
8803
8804 found = hist_ptr = 0;
8805 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8806 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8807 len_hist[hist_ptr] = ctx->vlen;
8808 if (++hist_ptr >= MAX_HDR_HISTORY)
8809 hist_ptr = 0;
8810 found++;
8811 }
8812 if (-occ > found)
8813 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008814
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008815 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008816 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8817 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8818 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008819 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008820 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008821 if (hist_ptr >= MAX_HDR_HISTORY)
8822 hist_ptr -= MAX_HDR_HISTORY;
8823 *vptr = ptr_hist[hist_ptr];
8824 *vlen = len_hist[hist_ptr];
8825 return 1;
8826}
8827
Willy Tarreaubaaee002006-06-26 02:48:02 +02008828/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008829 * Print a debug line with a header. Always stop at the first CR or LF char,
8830 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8831 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008832 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008833void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008834{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008835 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008836 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008837
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008838 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008839 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008840 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008841 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 +02008842
8843 for (max = 0; start + max < end; max++)
8844 if (start[max] == '\r' || start[max] == '\n')
8845 break;
8846
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008847 UBOUND(max, trash.size - trash.len - 3);
8848 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8849 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008850 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008851}
8852
Willy Tarreaueee5b512015-04-03 23:46:31 +02008853
8854/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8855 * The hdr_idx is allocated as well. In case of allocation failure, everything
8856 * allocated is freed and NULL is returned. Otherwise the new transaction is
8857 * assigned to the stream and returned.
8858 */
8859struct http_txn *http_alloc_txn(struct stream *s)
8860{
8861 struct http_txn *txn = s->txn;
8862
8863 if (txn)
8864 return txn;
8865
8866 txn = pool_alloc2(pool2_http_txn);
8867 if (!txn)
8868 return txn;
8869
8870 txn->hdr_idx.size = global.tune.max_http_hdr;
8871 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8872 if (!txn->hdr_idx.v) {
8873 pool_free2(pool2_http_txn, txn);
8874 return NULL;
8875 }
8876
8877 s->txn = txn;
8878 return txn;
8879}
8880
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008881void http_txn_reset_req(struct http_txn *txn)
8882{
8883 txn->req.flags = 0;
8884 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8885 txn->req.next = 0;
8886 txn->req.chunk_len = 0LL;
8887 txn->req.body_len = 0LL;
8888 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8889}
8890
8891void http_txn_reset_res(struct http_txn *txn)
8892{
8893 txn->rsp.flags = 0;
8894 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8895 txn->rsp.next = 0;
8896 txn->rsp.chunk_len = 0LL;
8897 txn->rsp.body_len = 0LL;
8898 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8899}
8900
Willy Tarreau0937bc42009-12-22 15:03:09 +01008901/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008902 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008903 * the required fields are properly allocated and that we only need to (re)init
8904 * them. This should be used before processing any new request.
8905 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008906void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008907{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008908 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008909 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008910
8911 txn->flags = 0;
8912 txn->status = -1;
8913
Willy Tarreauf64d1412010-10-07 20:06:11 +02008914 txn->cookie_first_date = 0;
8915 txn->cookie_last_date = 0;
8916
Willy Tarreaueee5b512015-04-03 23:46:31 +02008917 txn->srv_cookie = NULL;
8918 txn->cli_cookie = NULL;
8919 txn->uri = NULL;
8920
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008921 http_txn_reset_req(txn);
8922 http_txn_reset_res(txn);
8923
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008924 txn->req.chn = &s->req;
8925 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008926
8927 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008928
8929 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8930 if (fe->options2 & PR_O2_REQBUG_OK)
8931 txn->req.err_pos = -1; /* let buggy requests pass */
8932
Willy Tarreau0937bc42009-12-22 15:03:09 +01008933 if (txn->hdr_idx.v)
8934 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008935
8936 vars_init(&s->vars_txn, SCOPE_TXN);
8937 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008938}
8939
8940/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008941void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008942{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008943 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008944 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008945
8946 /* these ones will have been dynamically allocated */
8947 pool_free2(pool2_requri, txn->uri);
8948 pool_free2(pool2_capture, txn->cli_cookie);
8949 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008950 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008951
William Lallemanda73203e2012-03-12 12:48:57 +01008952 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008953 txn->uri = NULL;
8954 txn->srv_cookie = NULL;
8955 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008956
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008957 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008958 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008959 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008960 pool_free2(h->pool, s->req_cap[h->index]);
8961 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008962 }
8963
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008964 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008965 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008966 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008967 pool_free2(h->pool, s->res_cap[h->index]);
8968 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008969 }
8970
Willy Tarreau6204cd92016-03-10 16:33:04 +01008971 vars_prune(&s->vars_txn, s->sess, s);
8972 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008973}
8974
8975/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008976void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008977{
8978 http_end_txn(s);
8979 http_init_txn(s);
8980
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008981 /* reinitialise the current rule list pointer to NULL. We are sure that
8982 * any rulelist match the NULL pointer.
8983 */
8984 s->current_rule_list = NULL;
8985
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008986 s->be = strm_fe(s);
8987 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008988 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008989 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008990 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008991 /* re-init store persistence */
8992 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008993 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008994
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008996
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008997 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008998
Willy Tarreau739cfba2010-01-25 23:11:14 +01008999 /* We must trim any excess data from the response buffer, because we
9000 * may have blocked an invalid response from a server that we don't
9001 * want to accidentely forward once we disable the analysers, nor do
9002 * we want those data to come along with next response. A typical
9003 * example of such data would be from a buggy server responding to
9004 * a HEAD with some data, or sending more than the advertised
9005 * content-length.
9006 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009007 if (unlikely(s->res.buf->i))
9008 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009009
Christopher Fauletc0c672a2017-03-28 11:51:33 +02009010 /* Now we can realign the response buffer */
9011 buffer_realign(s->res.buf);
9012
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009013 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009014 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009015
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009016 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009017 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009018
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009019 s->req.rex = TICK_ETERNITY;
9020 s->req.wex = TICK_ETERNITY;
9021 s->req.analyse_exp = TICK_ETERNITY;
9022 s->res.rex = TICK_ETERNITY;
9023 s->res.wex = TICK_ETERNITY;
9024 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01009025 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009026}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009027
Sasha Pachev218f0642014-06-16 12:05:59 -06009028void free_http_res_rules(struct list *r)
9029{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009030 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009031
9032 list_for_each_entry_safe(pr, tr, r, list) {
9033 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009034 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009035 free(pr);
9036 }
9037}
9038
9039void free_http_req_rules(struct list *r)
9040{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009041 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042
9043 list_for_each_entry_safe(pr, tr, r, list) {
9044 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009045 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009046 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009047
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009048 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009049 free(pr);
9050 }
9051}
9052
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009053/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009054struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009055{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009056 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009057 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009058 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009059 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009060
Vincent Bernat02779b62016-04-03 13:48:43 +02009061 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009062 if (!rule) {
9063 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009064 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009065 }
9066
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009067 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009068 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009069 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009070 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009071 int code;
9072 int hc;
9073
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009074 if (!strcmp(args[0], "tarpit")) {
9075 rule->action = ACT_HTTP_REQ_TARPIT;
9076 rule->deny_status = HTTP_ERR_500;
9077 }
9078 else {
9079 rule->action = ACT_ACTION_DENY;
9080 rule->deny_status = HTTP_ERR_403;
9081 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009082 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009083 if (strcmp(args[cur_arg], "deny_status") == 0) {
9084 cur_arg++;
9085 if (!args[cur_arg]) {
9086 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9087 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9088 goto out_err;
9089 }
9090
9091 code = atol(args[cur_arg]);
9092 cur_arg++;
9093 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9094 if (http_err_codes[hc] == code) {
9095 rule->deny_status = hc;
9096 break;
9097 }
9098 }
9099
9100 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009101 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9102 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009103 }
9104 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009105 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009106 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009107 cur_arg = 1;
9108
9109 while(*args[cur_arg]) {
9110 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009111 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009112 cur_arg+=2;
9113 continue;
9114 } else
9115 break;
9116 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009117 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009118 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009119 cur_arg = 1;
9120
9121 if (!*args[cur_arg] ||
9122 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9123 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9124 file, linenum, args[0]);
9125 goto out_err;
9126 }
9127 rule->arg.nice = atoi(args[cur_arg]);
9128 if (rule->arg.nice < -1024)
9129 rule->arg.nice = -1024;
9130 else if (rule->arg.nice > 1024)
9131 rule->arg.nice = 1024;
9132 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009133 } else if (!strcmp(args[0], "set-tos")) {
9134#ifdef IP_TOS
9135 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009136 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009137 cur_arg = 1;
9138
9139 if (!*args[cur_arg] ||
9140 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9141 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9142 file, linenum, args[0]);
9143 goto out_err;
9144 }
9145
9146 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9147 if (err && *err != '\0') {
9148 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9149 file, linenum, err, args[0]);
9150 goto out_err;
9151 }
9152 cur_arg++;
9153#else
9154 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9155 goto out_err;
9156#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009157 } else if (!strcmp(args[0], "set-mark")) {
9158#ifdef SO_MARK
9159 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009160 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009161 cur_arg = 1;
9162
9163 if (!*args[cur_arg] ||
9164 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9165 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9166 file, linenum, args[0]);
9167 goto out_err;
9168 }
9169
9170 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9171 if (err && *err != '\0') {
9172 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9173 file, linenum, err, args[0]);
9174 goto out_err;
9175 }
9176 cur_arg++;
9177 global.last_checks |= LSTCHK_NETADM;
9178#else
9179 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9180 goto out_err;
9181#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009182 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009183 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009184 cur_arg = 1;
9185
9186 if (!*args[cur_arg] ||
9187 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9188 bad_log_level:
9189 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9190 file, linenum, args[0]);
9191 goto out_err;
9192 }
9193 if (strcmp(args[cur_arg], "silent") == 0)
9194 rule->arg.loglevel = -1;
9195 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9196 goto bad_log_level;
9197 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009198 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009199 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009200 cur_arg = 1;
9201
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009202 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9203 (*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 +01009204 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9205 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009206 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009207 }
9208
9209 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9210 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9211 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009212
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009213 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009214 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009215 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009216 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9217 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9218 file, linenum, args[0], error);
9219 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009220 goto out_err;
9221 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009222 free(proxy->conf.lfs_file);
9223 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9224 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009225 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009226 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009227 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009228 cur_arg = 1;
9229
9230 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009231 (*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 -06009232 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9233 file, linenum, args[0]);
9234 goto out_err;
9235 }
9236
9237 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9238 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9239 LIST_INIT(&rule->arg.hdr_add.fmt);
9240
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009241 error = NULL;
9242 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9243 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9244 args[cur_arg + 1], error);
9245 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009246 goto out_err;
9247 }
9248
9249 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009250 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009251 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 +01009252 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9253 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9254 file, linenum, args[0], error);
9255 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009256 goto out_err;
9257 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009258
9259 free(proxy->conf.lfs_file);
9260 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9261 proxy->conf.lfs_line = proxy->conf.args.line;
9262 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009263 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009264 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009265 cur_arg = 1;
9266
9267 if (!*args[cur_arg] ||
9268 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9269 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9270 file, linenum, args[0]);
9271 goto out_err;
9272 }
9273
9274 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9275 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9276
9277 proxy->conf.args.ctx = ARGC_HRQ;
9278 free(proxy->conf.lfs_file);
9279 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9280 proxy->conf.lfs_line = proxy->conf.args.line;
9281 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009282 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9283 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009284 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009285 struct sample_expr *expr;
9286 unsigned int where;
9287 char *err = NULL;
9288
9289 cur_arg = 1;
9290 proxy->conf.args.ctx = ARGC_TRK;
9291
9292 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9293 if (!expr) {
9294 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9295 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9296 free(err);
9297 goto out_err;
9298 }
9299
9300 where = 0;
9301 if (proxy->cap & PR_CAP_FE)
9302 where |= SMP_VAL_FE_HRQ_HDR;
9303 if (proxy->cap & PR_CAP_BE)
9304 where |= SMP_VAL_BE_HRQ_HDR;
9305
9306 if (!(expr->fetch->val & where)) {
9307 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9308 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9309 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9310 args[cur_arg-1], sample_src_names(expr->fetch->use));
9311 free(expr);
9312 goto out_err;
9313 }
9314
9315 if (strcmp(args[cur_arg], "table") == 0) {
9316 cur_arg++;
9317 if (!args[cur_arg]) {
9318 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9319 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9320 free(expr);
9321 goto out_err;
9322 }
9323 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009324 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009325 cur_arg++;
9326 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009327 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009328 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009329 } else if (strcmp(args[0], "redirect") == 0) {
9330 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009331 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009332
Willy Tarreaube4653b2015-05-28 15:26:58 +02009333 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009334 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9335 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9336 goto out_err;
9337 }
9338
9339 /* this redirect rule might already contain a parsed condition which
9340 * we'll pass to the http-request rule.
9341 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009342 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009343 rule->arg.redir = redir;
9344 rule->cond = redir->cond;
9345 redir->cond = NULL;
9346 cur_arg = 2;
9347 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009348 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9349 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009350 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009351 /*
9352 * '+ 8' for 'add-acl('
9353 * '- 9' for 'add-acl(' + trailing ')'
9354 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009355 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009356
9357 cur_arg = 1;
9358
9359 if (!*args[cur_arg] ||
9360 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9361 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9362 file, linenum, args[0]);
9363 goto out_err;
9364 }
9365
9366 LIST_INIT(&rule->arg.map.key);
9367 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009368 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009369 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009370 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9371 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9372 file, linenum, args[0], error);
9373 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009374 goto out_err;
9375 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009376 free(proxy->conf.lfs_file);
9377 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9378 proxy->conf.lfs_line = proxy->conf.args.line;
9379 cur_arg += 1;
9380 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9381 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009382 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009383 /*
9384 * '+ 8' for 'del-acl('
9385 * '- 9' for 'del-acl(' + trailing ')'
9386 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009387 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009388
9389 cur_arg = 1;
9390
9391 if (!*args[cur_arg] ||
9392 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9393 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9394 file, linenum, args[0]);
9395 goto out_err;
9396 }
9397
9398 LIST_INIT(&rule->arg.map.key);
9399 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009400 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009401 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009402 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9403 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9404 file, linenum, args[0], error);
9405 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009406 goto out_err;
9407 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009408 free(proxy->conf.lfs_file);
9409 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9410 proxy->conf.lfs_line = proxy->conf.args.line;
9411 cur_arg += 1;
9412 } else if (strncmp(args[0], "del-map", 7) == 0) {
9413 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009414 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009415 /*
9416 * '+ 8' for 'del-map('
9417 * '- 9' for 'del-map(' + trailing ')'
9418 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009419 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009420
9421 cur_arg = 1;
9422
9423 if (!*args[cur_arg] ||
9424 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9425 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9426 file, linenum, args[0]);
9427 goto out_err;
9428 }
9429
9430 LIST_INIT(&rule->arg.map.key);
9431 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009432 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009433 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009434 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9435 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9436 file, linenum, args[0], error);
9437 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009438 goto out_err;
9439 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009440 free(proxy->conf.lfs_file);
9441 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9442 proxy->conf.lfs_line = proxy->conf.args.line;
9443 cur_arg += 1;
9444 } else if (strncmp(args[0], "set-map", 7) == 0) {
9445 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009446 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009447 /*
9448 * '+ 8' for 'set-map('
9449 * '- 9' for 'set-map(' + trailing ')'
9450 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009451 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009452
9453 cur_arg = 1;
9454
9455 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9456 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9457 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9458 file, linenum, args[0]);
9459 goto out_err;
9460 }
9461
9462 LIST_INIT(&rule->arg.map.key);
9463 LIST_INIT(&rule->arg.map.value);
9464 proxy->conf.args.ctx = ARGC_HRQ;
9465
9466 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009467 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009468 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009469 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9470 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9471 file, linenum, args[0], error);
9472 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009473 goto out_err;
9474 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009475
9476 /* value 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 + 1], proxy, &rule->arg.map.value, 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' pattern: %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 free(proxy->conf.lfs_file);
9486 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9487 proxy->conf.lfs_line = proxy->conf.args.line;
9488
9489 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009490 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9491 char *errmsg = NULL;
9492 cur_arg = 1;
9493 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009494 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009495 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009496 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009497 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9498 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9499 free(errmsg);
9500 goto out_err;
9501 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009502 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009503 action_build_list(&http_req_keywords.list, &trash);
9504 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9505 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009506 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009507 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009508 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009509 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009510 }
9511
9512 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9513 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009514 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009515
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009516 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9517 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9518 file, linenum, args[0], errmsg);
9519 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009520 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009521 }
9522 rule->cond = cond;
9523 }
9524 else if (*args[cur_arg]) {
9525 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9526 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9527 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009528 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009529 }
9530
9531 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009532 out_err:
9533 free(rule);
9534 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009535}
9536
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009537/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009538struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009539{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009540 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009541 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009542 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009543 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009544
9545 rule = calloc(1, sizeof(*rule));
9546 if (!rule) {
9547 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9548 goto out_err;
9549 }
9550
9551 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009552 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009553 cur_arg = 1;
9554 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009555 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009556 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009557 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009558 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009559 cur_arg = 1;
9560
9561 if (!*args[cur_arg] ||
9562 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9563 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9564 file, linenum, args[0]);
9565 goto out_err;
9566 }
9567 rule->arg.nice = atoi(args[cur_arg]);
9568 if (rule->arg.nice < -1024)
9569 rule->arg.nice = -1024;
9570 else if (rule->arg.nice > 1024)
9571 rule->arg.nice = 1024;
9572 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009573 } else if (!strcmp(args[0], "set-tos")) {
9574#ifdef IP_TOS
9575 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009576 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009577 cur_arg = 1;
9578
9579 if (!*args[cur_arg] ||
9580 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9581 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9582 file, linenum, args[0]);
9583 goto out_err;
9584 }
9585
9586 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9587 if (err && *err != '\0') {
9588 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9589 file, linenum, err, args[0]);
9590 goto out_err;
9591 }
9592 cur_arg++;
9593#else
9594 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9595 goto out_err;
9596#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009597 } else if (!strcmp(args[0], "set-mark")) {
9598#ifdef SO_MARK
9599 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009600 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009601 cur_arg = 1;
9602
9603 if (!*args[cur_arg] ||
9604 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9605 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9606 file, linenum, args[0]);
9607 goto out_err;
9608 }
9609
9610 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9611 if (err && *err != '\0') {
9612 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9613 file, linenum, err, args[0]);
9614 goto out_err;
9615 }
9616 cur_arg++;
9617 global.last_checks |= LSTCHK_NETADM;
9618#else
9619 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9620 goto out_err;
9621#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009622 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009623 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009624 cur_arg = 1;
9625
9626 if (!*args[cur_arg] ||
9627 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9628 bad_log_level:
9629 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9630 file, linenum, args[0]);
9631 goto out_err;
9632 }
9633 if (strcmp(args[cur_arg], "silent") == 0)
9634 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009635 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009636 goto bad_log_level;
9637 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009638 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009639 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009640 cur_arg = 1;
9641
9642 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9643 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9644 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9645 file, linenum, args[0]);
9646 goto out_err;
9647 }
9648
9649 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9650 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9651 LIST_INIT(&rule->arg.hdr_add.fmt);
9652
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009653 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009654 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009655 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009656 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9657 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9658 file, linenum, args[0], error);
9659 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009660 goto out_err;
9661 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009662 free(proxy->conf.lfs_file);
9663 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9664 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009665 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009666 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009667 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009668 cur_arg = 1;
9669
9670 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009671 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9672 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009673 file, linenum, args[0]);
9674 goto out_err;
9675 }
9676
9677 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9678 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9679 LIST_INIT(&rule->arg.hdr_add.fmt);
9680
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009681 error = NULL;
9682 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9683 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9684 args[cur_arg + 1], error);
9685 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009686 goto out_err;
9687 }
9688
9689 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009690 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009691 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 +01009692 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9693 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9694 file, linenum, args[0], error);
9695 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009696 goto out_err;
9697 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009698
9699 free(proxy->conf.lfs_file);
9700 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9701 proxy->conf.lfs_line = proxy->conf.args.line;
9702 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009703 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009704 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009705 cur_arg = 1;
9706
9707 if (!*args[cur_arg] ||
9708 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9709 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9710 file, linenum, args[0]);
9711 goto out_err;
9712 }
9713
9714 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9715 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9716
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009717 proxy->conf.args.ctx = ARGC_HRS;
9718 free(proxy->conf.lfs_file);
9719 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9720 proxy->conf.lfs_line = proxy->conf.args.line;
9721 cur_arg += 1;
9722 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9723 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009724 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009725 /*
9726 * '+ 8' for 'add-acl('
9727 * '- 9' for 'add-acl(' + trailing ')'
9728 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009729 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009730
9731 cur_arg = 1;
9732
9733 if (!*args[cur_arg] ||
9734 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9735 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9736 file, linenum, args[0]);
9737 goto out_err;
9738 }
9739
9740 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009741 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009742 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009743 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009744 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9745 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9746 file, linenum, args[0], error);
9747 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009748 goto out_err;
9749 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009750 free(proxy->conf.lfs_file);
9751 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9752 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009753
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009754 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009755 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9756 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009757 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009758 /*
9759 * '+ 8' for 'del-acl('
9760 * '- 9' for 'del-acl(' + trailing ')'
9761 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009762 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763
9764 cur_arg = 1;
9765
9766 if (!*args[cur_arg] ||
9767 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9768 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9769 file, linenum, args[0]);
9770 goto out_err;
9771 }
9772
9773 LIST_INIT(&rule->arg.map.key);
9774 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009775 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009776 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009777 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9778 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9779 file, linenum, args[0], error);
9780 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009781 goto out_err;
9782 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009783 free(proxy->conf.lfs_file);
9784 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9785 proxy->conf.lfs_line = proxy->conf.args.line;
9786 cur_arg += 1;
9787 } else if (strncmp(args[0], "del-map", 7) == 0) {
9788 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009789 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009790 /*
9791 * '+ 8' for 'del-map('
9792 * '- 9' for 'del-map(' + trailing ')'
9793 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009794 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009795
9796 cur_arg = 1;
9797
9798 if (!*args[cur_arg] ||
9799 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9800 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9801 file, linenum, args[0]);
9802 goto out_err;
9803 }
9804
9805 LIST_INIT(&rule->arg.map.key);
9806 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009807 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009808 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009809 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9810 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9811 file, linenum, args[0], error);
9812 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009813 goto out_err;
9814 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009815 free(proxy->conf.lfs_file);
9816 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9817 proxy->conf.lfs_line = proxy->conf.args.line;
9818 cur_arg += 1;
9819 } else if (strncmp(args[0], "set-map", 7) == 0) {
9820 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009821 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009822 /*
9823 * '+ 8' for 'set-map('
9824 * '- 9' for 'set-map(' + trailing ')'
9825 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009826 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009827
9828 cur_arg = 1;
9829
9830 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9831 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9832 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9833 file, linenum, args[0]);
9834 goto out_err;
9835 }
9836
9837 LIST_INIT(&rule->arg.map.key);
9838 LIST_INIT(&rule->arg.map.value);
9839
9840 proxy->conf.args.ctx = ARGC_HRS;
9841
9842 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009843 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009844 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009845 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9846 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9847 file, linenum, args[0], error);
9848 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009849 goto out_err;
9850 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009851
9852 /* value 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 + 1], proxy, &rule->arg.map.value, 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' value: %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 free(proxy->conf.lfs_file);
9863 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9864 proxy->conf.lfs_line = proxy->conf.args.line;
9865
9866 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009867 } else if (strcmp(args[0], "redirect") == 0) {
9868 struct redirect_rule *redir;
9869 char *errmsg = NULL;
9870
9871 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9872 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9873 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9874 goto out_err;
9875 }
9876
9877 /* this redirect rule might already contain a parsed condition which
9878 * we'll pass to the http-request rule.
9879 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009880 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009881 rule->arg.redir = redir;
9882 rule->cond = redir->cond;
9883 redir->cond = NULL;
9884 cur_arg = 2;
9885 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009886 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9887 args[0][9] == '\0' && args[0][8] >= '0' &&
9888 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9889 struct sample_expr *expr;
9890 unsigned int where;
9891 char *err = NULL;
9892
9893 cur_arg = 1;
9894 proxy->conf.args.ctx = ARGC_TRK;
9895
9896 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9897 if (!expr) {
9898 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9899 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9900 free(err);
9901 goto out_err;
9902 }
9903
9904 where = 0;
9905 if (proxy->cap & PR_CAP_FE)
9906 where |= SMP_VAL_FE_HRS_HDR;
9907 if (proxy->cap & PR_CAP_BE)
9908 where |= SMP_VAL_BE_HRS_HDR;
9909
9910 if (!(expr->fetch->val & where)) {
9911 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9912 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9913 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9914 args[cur_arg-1], sample_src_names(expr->fetch->use));
9915 free(expr);
9916 goto out_err;
9917 }
9918
9919 if (strcmp(args[cur_arg], "table") == 0) {
9920 cur_arg++;
9921 if (!args[cur_arg]) {
9922 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9923 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9924 free(expr);
9925 goto out_err;
9926 }
9927 /* we copy the table name for now, it will be resolved later */
9928 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9929 cur_arg++;
9930 }
9931 rule->arg.trk_ctr.expr = expr;
9932 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009933 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9934 char *errmsg = NULL;
9935 cur_arg = 1;
9936 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009937 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009938 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009939 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009940 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9941 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9942 free(errmsg);
9943 goto out_err;
9944 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009945 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009946 action_build_list(&http_res_keywords.list, &trash);
9947 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9948 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009949 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009950 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009951 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009952 goto out_err;
9953 }
9954
9955 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9956 struct acl_cond *cond;
9957 char *errmsg = NULL;
9958
9959 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9960 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9961 file, linenum, args[0], errmsg);
9962 free(errmsg);
9963 goto out_err;
9964 }
9965 rule->cond = cond;
9966 }
9967 else if (*args[cur_arg]) {
9968 Alert("parsing [%s:%d]: 'http-response %s' expects"
9969 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9970 file, linenum, args[0], args[cur_arg]);
9971 goto out_err;
9972 }
9973
9974 return rule;
9975 out_err:
9976 free(rule);
9977 return NULL;
9978}
9979
Willy Tarreau4baae242012-12-27 12:00:31 +01009980/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009981 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009982 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9983 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009984 */
9985struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009986 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009987{
9988 struct redirect_rule *rule;
9989 int cur_arg;
9990 int type = REDIRECT_TYPE_NONE;
9991 int code = 302;
9992 const char *destination = NULL;
9993 const char *cookie = NULL;
9994 int cookie_set = 0;
9995 unsigned int flags = REDIRECT_FLAG_NONE;
9996 struct acl_cond *cond = NULL;
9997
9998 cur_arg = 0;
9999 while (*(args[cur_arg])) {
10000 if (strcmp(args[cur_arg], "location") == 0) {
10001 if (!*args[cur_arg + 1])
10002 goto missing_arg;
10003
10004 type = REDIRECT_TYPE_LOCATION;
10005 cur_arg++;
10006 destination = args[cur_arg];
10007 }
10008 else if (strcmp(args[cur_arg], "prefix") == 0) {
10009 if (!*args[cur_arg + 1])
10010 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010011 type = REDIRECT_TYPE_PREFIX;
10012 cur_arg++;
10013 destination = args[cur_arg];
10014 }
10015 else if (strcmp(args[cur_arg], "scheme") == 0) {
10016 if (!*args[cur_arg + 1])
10017 goto missing_arg;
10018
10019 type = REDIRECT_TYPE_SCHEME;
10020 cur_arg++;
10021 destination = args[cur_arg];
10022 }
10023 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10024 if (!*args[cur_arg + 1])
10025 goto missing_arg;
10026
10027 cur_arg++;
10028 cookie = args[cur_arg];
10029 cookie_set = 1;
10030 }
10031 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10032 if (!*args[cur_arg + 1])
10033 goto missing_arg;
10034
10035 cur_arg++;
10036 cookie = args[cur_arg];
10037 cookie_set = 0;
10038 }
10039 else if (strcmp(args[cur_arg], "code") == 0) {
10040 if (!*args[cur_arg + 1])
10041 goto missing_arg;
10042
10043 cur_arg++;
10044 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010045 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010046 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010047 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010048 args[cur_arg - 1], args[cur_arg]);
10049 return NULL;
10050 }
10051 }
10052 else if (!strcmp(args[cur_arg],"drop-query")) {
10053 flags |= REDIRECT_FLAG_DROP_QS;
10054 }
10055 else if (!strcmp(args[cur_arg],"append-slash")) {
10056 flags |= REDIRECT_FLAG_APPEND_SLASH;
10057 }
10058 else if (strcmp(args[cur_arg], "if") == 0 ||
10059 strcmp(args[cur_arg], "unless") == 0) {
10060 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10061 if (!cond) {
10062 memprintf(errmsg, "error in condition: %s", *errmsg);
10063 return NULL;
10064 }
10065 break;
10066 }
10067 else {
10068 memprintf(errmsg,
10069 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10070 args[cur_arg]);
10071 return NULL;
10072 }
10073 cur_arg++;
10074 }
10075
10076 if (type == REDIRECT_TYPE_NONE) {
10077 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10078 return NULL;
10079 }
10080
Willy Tarreaube4653b2015-05-28 15:26:58 +020010081 if (dir && type != REDIRECT_TYPE_LOCATION) {
10082 memprintf(errmsg, "response only supports redirect type 'location'");
10083 return NULL;
10084 }
10085
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010086 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010087 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010088 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010089
10090 if (!use_fmt) {
10091 /* old-style static redirect rule */
10092 rule->rdr_str = strdup(destination);
10093 rule->rdr_len = strlen(destination);
10094 }
10095 else {
10096 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010097
10098 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10099 * if prefix == "/", we don't want to add anything, otherwise it
10100 * makes it hard for the user to configure a self-redirection.
10101 */
Godbachd9722032014-12-18 15:44:58 +080010102 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010103 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010104 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10105 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 +010010106 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10107 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010108 return NULL;
10109 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010110 free(curproxy->conf.lfs_file);
10111 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10112 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010113 }
10114 }
10115
Willy Tarreau4baae242012-12-27 12:00:31 +010010116 if (cookie) {
10117 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10118 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10119 */
10120 rule->cookie_len = strlen(cookie);
10121 if (cookie_set) {
10122 rule->cookie_str = malloc(rule->cookie_len + 10);
10123 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10124 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10125 rule->cookie_len += 9;
10126 } else {
10127 rule->cookie_str = malloc(rule->cookie_len + 21);
10128 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10129 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10130 rule->cookie_len += 20;
10131 }
10132 }
10133 rule->type = type;
10134 rule->code = code;
10135 rule->flags = flags;
10136 LIST_INIT(&rule->list);
10137 return rule;
10138
10139 missing_arg:
10140 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10141 return NULL;
10142}
10143
Willy Tarreau8797c062007-05-07 00:55:35 +020010144/************************************************************************/
10145/* The code below is dedicated to ACL parsing and matching */
10146/************************************************************************/
10147
10148
Willy Tarreau14174bc2012-04-16 14:34:04 +020010149/* This function ensures that the prerequisites for an L7 fetch are ready,
10150 * which means that a request or response is ready. If some data is missing,
10151 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010152 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10153 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010154 *
10155 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010156 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10157 * decide whether or not an HTTP message is present ;
10158 * 0 if the requested data cannot be fetched or if it is certain that
10159 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010160 * 1 if an HTTP message is ready
10161 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010162int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010163 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010164{
Willy Tarreau192252e2015-04-04 01:47:55 +020010165 struct http_txn *txn;
10166 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010167
Willy Tarreaube508f12016-03-10 11:47:01 +010010168 /* Note: it is possible that <s> is NULL when called before stream
10169 * initialization (eg: tcp-request connection), so this function is the
10170 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010171 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010172 if (!s)
10173 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010174
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010175 if (!s->txn) {
10176 if (unlikely(!http_alloc_txn(s)))
10177 return 0; /* not enough memory */
10178 http_init_txn(s);
10179 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010180 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010181 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010182
10183 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010184 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010185
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010186 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010187 /* If the buffer does not leave enough free space at the end,
10188 * we must first realign it.
10189 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010190 if (s->req.buf->p > s->req.buf->data &&
10191 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10192 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010193
Willy Tarreau14174bc2012-04-16 14:34:04 +020010194 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010195 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010196 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010197
10198 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010199 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010200 http_msg_analyzer(msg, &txn->hdr_idx);
10201
10202 /* Still no valid request ? */
10203 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010204 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010205 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010206 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010207 }
10208 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010209 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010210 return 0;
10211 }
10212
10213 /* OK we just got a valid HTTP request. We have some minor
10214 * preparation to perform so that further checks can rely
10215 * on HTTP tests.
10216 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010217
10218 /* If the request was parsed but was too large, we must absolutely
10219 * return an error so that it is not processed. At the moment this
10220 * cannot happen, but if the parsers are to change in the future,
10221 * we want this check to be maintained.
10222 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010223 if (unlikely(s->req.buf->i + s->req.buf->p >
10224 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010225 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010226 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010227 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010228 return 1;
10229 }
10230
Willy Tarreau9b28e032012-10-12 23:49:43 +020010231 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010232 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010233 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010234
Willy Tarreau506d0502013-07-06 13:29:24 +020010235 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10236 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010237 }
10238
Willy Tarreau506d0502013-07-06 13:29:24 +020010239 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010240 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010241 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010242
10243 /* otherwise everything's ready for the request */
10244 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010245 else {
10246 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010247 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10248 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010249 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010250 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010251 }
10252
10253 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010254 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010255 return 1;
10256}
Willy Tarreau8797c062007-05-07 00:55:35 +020010257
Willy Tarreau8797c062007-05-07 00:55:35 +020010258/* 1. Check on METHOD
10259 * We use the pre-parsed method if it is known, and store its number as an
10260 * integer. If it is unknown, we use the pointer and the length.
10261 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010262static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010263{
10264 int len, meth;
10265
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010266 len = strlen(text);
10267 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010268
10269 pattern->val.i = meth;
10270 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010271 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010272 pattern->len = len;
10273 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010274 else {
10275 pattern->ptr.str = NULL;
10276 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010277 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010278 return 1;
10279}
10280
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010281/* This function fetches the method of current HTTP request and stores
10282 * it in the global pattern struct as a chunk. There are two possibilities :
10283 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10284 * in <len> and <ptr> is NULL ;
10285 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10286 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010287 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010288 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010289static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010290smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010291{
10292 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010293 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010294
Willy Tarreau24e32d82012-04-23 23:55:44 +020010295 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010296
Willy Tarreaube508f12016-03-10 11:47:01 +010010297 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010298 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010299 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010300 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010301 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010302 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10303 /* ensure the indexes are not affected */
10304 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010305 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010306 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10307 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010309 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010310 return 1;
10311}
10312
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010313/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010314static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010315{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010316 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010317 struct pattern_list *lst;
10318 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010319
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010320 list_for_each_entry(lst, &expr->patterns, list) {
10321 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010322
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010323 /* well-known method */
10324 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010326 return pattern;
10327 else
10328 continue;
10329 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010330
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010331 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010332 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010333 continue;
10334
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010335 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010336 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10337 (!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 +010010338 return pattern;
10339 }
10340 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010341}
10342
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010343static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010344smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010345{
Willy Tarreaube508f12016-03-10 11:47:01 +010010346 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010347 char *ptr;
10348 int len;
10349
Willy Tarreauc0239e02012-04-16 14:42:55 +020010350 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010351
Willy Tarreaube508f12016-03-10 11:47:01 +010010352 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010353 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010354 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010355
10356 while ((len-- > 0) && (*ptr++ != '/'));
10357 if (len <= 0)
10358 return 0;
10359
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010360 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010361 smp->data.u.str.str = ptr;
10362 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010363
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010364 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010365 return 1;
10366}
10367
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010368static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010369smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010370{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010371 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010372 char *ptr;
10373 int len;
10374
Willy Tarreauc0239e02012-04-16 14:42:55 +020010375 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010376
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010377 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010378 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10379 return 0;
10380
Willy Tarreau8797c062007-05-07 00:55:35 +020010381 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010382 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010383
10384 while ((len-- > 0) && (*ptr++ != '/'));
10385 if (len <= 0)
10386 return 0;
10387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010388 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010389 smp->data.u.str.str = ptr;
10390 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010391
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010392 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010393 return 1;
10394}
10395
10396/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010397static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010398smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010399{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010400 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010401 char *ptr;
10402 int len;
10403
Willy Tarreauc0239e02012-04-16 14:42:55 +020010404 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010405
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010406 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010407 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10408 return 0;
10409
Willy Tarreau8797c062007-05-07 00:55:35 +020010410 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010411 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010412
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010413 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010414 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010415 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010416 return 1;
10417}
10418
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010419static int
10420smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10421{
10422 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10423 return 0;
10424
10425 if (!smp->strm->unique_id) {
10426 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10427 return 0;
10428 smp->strm->unique_id[0] = '\0';
10429 }
10430 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10431 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10432
10433 smp->data.type = SMP_T_STR;
10434 smp->data.u.str.str = smp->strm->unique_id;
10435 smp->flags = SMP_F_CONST;
10436 return 1;
10437}
10438
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020010439/* Returns a string block containing all headers including the
10440 * empty line wich separes headers from the body. This is useful
10441 * form some headers analysis.
10442 */
10443static int
10444smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
10445{
10446 struct http_msg *msg;
10447 struct hdr_idx *idx;
10448 struct http_txn *txn;
10449
10450 CHECK_HTTP_MESSAGE_FIRST();
10451
10452 txn = smp->strm->txn;
10453 idx = &txn->hdr_idx;
10454 msg = &txn->req;
10455
10456 smp->data.type = SMP_T_STR;
10457 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
10458 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
10459 (msg->chn->buf->p[msg->eoh] == '\r');
10460
10461 return 1;
10462}
10463
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020010464/* Returns the header request in a length/value encoded format.
10465 * This is useful for exchanges with the SPOE.
10466 *
10467 * A "length value" is a multibyte code encoding numbers. It uses the
10468 * SPOE format. The encoding is the following:
10469 *
10470 * Each couple "header name" / "header value" is composed
10471 * like this:
10472 * "length value" "header name bytes"
10473 * "length value" "header value bytes"
10474 * When the last header is reached, the header name and the header
10475 * value are empty. Their length are 0
10476 */
10477static int
10478smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
10479{
10480 struct http_msg *msg;
10481 struct chunk *temp;
10482 struct hdr_idx *idx;
10483 const char *cur_ptr, *cur_next, *p;
10484 int old_idx, cur_idx;
10485 struct hdr_idx_elem *cur_hdr;
10486 const char *hn, *hv;
10487 int hnl, hvl;
10488 int ret;
10489 struct http_txn *txn;
10490 char *buf;
10491 char *end;
10492
10493 CHECK_HTTP_MESSAGE_FIRST();
10494
10495 temp = get_trash_chunk();
10496 buf = temp->str;
10497 end = temp->str + temp->size;
10498
10499 txn = smp->strm->txn;
10500 idx = &txn->hdr_idx;
10501 msg = &txn->req;
10502
10503 /* Build array of headers. */
10504 old_idx = 0;
10505 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
10506 while (1) {
10507 cur_idx = idx->v[old_idx].next;
10508 if (!cur_idx)
10509 break;
10510 old_idx = cur_idx;
10511
10512 cur_hdr = &idx->v[cur_idx];
10513 cur_ptr = cur_next;
10514 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
10515
10516 /* Now we have one full header at cur_ptr of len cur_hdr->len,
10517 * and the next header starts at cur_next. We'll check
10518 * this header in the list as well as against the default
10519 * rule.
10520 */
10521
10522 /* look for ': *'. */
10523 hn = cur_ptr;
10524 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
10525 if (p >= cur_ptr+cur_hdr->len)
10526 continue;
10527 hnl = p - hn;
10528 p++;
10529 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
10530 p++;
10531 if (p >= cur_ptr + cur_hdr->len)
10532 continue;
10533 hv = p;
10534 hvl = cur_ptr + cur_hdr->len-p;
10535
10536 /* encode the header name. */
10537 ret = encode_varint(hnl, &buf, end);
10538 if (ret == -1)
10539 return 0;
10540 if (buf + hnl > end)
10541 return 0;
10542 memcpy(buf, hn, hnl);
10543 buf += hnl;
10544
10545 /* encode and copy the value. */
10546 ret = encode_varint(hvl, &buf, end);
10547 if (ret == -1)
10548 return 0;
10549 if (buf + hvl > end)
10550 return 0;
10551 memcpy(buf, hv, hvl);
10552 buf += hvl;
10553 }
10554
10555 /* encode the end of the header list with empty
10556 * header name and header value.
10557 */
10558 ret = encode_varint(0, &buf, end);
10559 if (ret == -1)
10560 return 0;
10561 ret = encode_varint(0, &buf, end);
10562 if (ret == -1)
10563 return 0;
10564
10565 /* Initialise sample data which will be filled. */
10566 smp->data.type = SMP_T_BIN;
10567 smp->data.u.str.str = temp->str;
10568 smp->data.u.str.len = buf - temp->str;
10569 smp->data.u.str.size = temp->size;
10570
10571 return 1;
10572}
10573
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010574/* returns the longest available part of the body. This requires that the body
10575 * has been waited for using http-buffer-request.
10576 */
10577static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010578smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010579{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010580 struct http_msg *msg;
10581 unsigned long len;
10582 unsigned long block1;
10583 char *body;
10584 struct chunk *temp;
10585
10586 CHECK_HTTP_MESSAGE_FIRST();
10587
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010588 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010589 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010590 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010591 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010592
10593 len = http_body_bytes(msg);
10594 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10595
10596 block1 = len;
10597 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10598 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10599
10600 if (block1 == len) {
10601 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010602 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010603 smp->data.u.str.str = body;
10604 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010605 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10606 }
10607 else {
10608 /* buffer is wrapped, we need to defragment it */
10609 temp = get_trash_chunk();
10610 memcpy(temp->str, body, block1);
10611 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010612 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010613 smp->data.u.str.str = temp->str;
10614 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010615 smp->flags = SMP_F_VOL_TEST;
10616 }
10617 return 1;
10618}
10619
10620
10621/* returns the available length of the body. This requires that the body
10622 * has been waited for using http-buffer-request.
10623 */
10624static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010625smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010626{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010627 struct http_msg *msg;
10628
10629 CHECK_HTTP_MESSAGE_FIRST();
10630
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010631 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010632 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010633 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010634 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010635
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010636 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010637 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010638
10639 smp->flags = SMP_F_VOL_TEST;
10640 return 1;
10641}
10642
10643
10644/* returns the advertised length of the body, or the advertised size of the
10645 * chunks available in the buffer. This requires that the body has been waited
10646 * for using http-buffer-request.
10647 */
10648static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010649smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010650{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010651 struct http_msg *msg;
10652
10653 CHECK_HTTP_MESSAGE_FIRST();
10654
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010655 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010656 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010657 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010658 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010659
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010660 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010661 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010662
10663 smp->flags = SMP_F_VOL_TEST;
10664 return 1;
10665}
10666
10667
Willy Tarreau8797c062007-05-07 00:55:35 +020010668/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010669static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010670smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010671{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010672 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010673
Willy Tarreauc0239e02012-04-16 14:42:55 +020010674 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010675
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010676 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010677 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010678 smp->data.u.str.len = txn->req.sl.rq.u_l;
10679 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010680 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010681 return 1;
10682}
10683
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010684static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010685smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010686{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010687 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010688 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010689
Willy Tarreauc0239e02012-04-16 14:42:55 +020010690 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010691
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010692 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010693 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 +020010694 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010695 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010696
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010697 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010698 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010699 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010700 return 1;
10701}
10702
10703static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010704smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010705{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010706 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010707 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010708
Willy Tarreauc0239e02012-04-16 14:42:55 +020010709 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010710
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010711 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010712 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 +020010713 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10714 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010715
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010716 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010717 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010718 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010719 return 1;
10720}
10721
Willy Tarreau185b5c42012-04-26 15:11:51 +020010722/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10723 * Accepts an optional argument of type string containing the header field name,
10724 * and an optional argument of type signed or unsigned integer to request an
10725 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010726 * headers are considered from the first one. It does not stop on commas and
10727 * returns full lines instead (useful for User-Agent or Date for example).
10728 */
10729static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010730smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010731{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010732 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010733 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010734 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010735 int occ = 0;
10736 const char *name_str = NULL;
10737 int name_len = 0;
10738
10739 if (!ctx) {
10740 /* first call */
10741 ctx = &static_hdr_ctx;
10742 ctx->idx = 0;
10743 smp->ctx.a[0] = ctx;
10744 }
10745
10746 if (args) {
10747 if (args[0].type != ARGT_STR)
10748 return 0;
10749 name_str = args[0].data.str.str;
10750 name_len = args[0].data.str.len;
10751
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010752 if (args[1].type == ARGT_SINT)
10753 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010754 }
10755
10756 CHECK_HTTP_MESSAGE_FIRST();
10757
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010758 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010759 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 +020010760
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010761 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10762 /* search for header from the beginning */
10763 ctx->idx = 0;
10764
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010765 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010766 /* no explicit occurrence and single fetch => last header by default */
10767 occ = -1;
10768
10769 if (!occ)
10770 /* prepare to report multiple occurrences for ACL fetches */
10771 smp->flags |= SMP_F_NOT_LAST;
10772
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010773 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010774 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010775 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 +020010776 return 1;
10777
10778 smp->flags &= ~SMP_F_NOT_LAST;
10779 return 0;
10780}
10781
10782/* 6. Check on HTTP header count. The number of occurrences is returned.
10783 * Accepts exactly 1 argument of type string. It does not stop on commas and
10784 * returns full lines instead (useful for User-Agent or Date for example).
10785 */
10786static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010787smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010788{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010789 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010790 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010791 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010792 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010793 const char *name = NULL;
10794 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010795
Willy Tarreau601a4d12015-04-01 19:16:09 +020010796 if (args && args->type == ARGT_STR) {
10797 name = args->data.str.str;
10798 len = args->data.str.len;
10799 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010800
10801 CHECK_HTTP_MESSAGE_FIRST();
10802
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010803 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010804 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 +020010805
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010806 ctx.idx = 0;
10807 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010808 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010809 cnt++;
10810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010812 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010813 smp->flags = SMP_F_VOL_HDR;
10814 return 1;
10815}
10816
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010817static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010818smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010819{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010820 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010821 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010822 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010823 struct chunk *temp;
10824 char del = ',';
10825
10826 if (args && args->type == ARGT_STR)
10827 del = *args[0].data.str.str;
10828
10829 CHECK_HTTP_MESSAGE_FIRST();
10830
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010831 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010832 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 +020010833
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010834 temp = get_trash_chunk();
10835
10836 ctx.idx = 0;
10837 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10838 if (temp->len)
10839 temp->str[temp->len++] = del;
10840 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10841 temp->len += ctx.del;
10842 }
10843
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010844 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010845 smp->data.u.str.str = temp->str;
10846 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010847 smp->flags = SMP_F_VOL_HDR;
10848 return 1;
10849}
10850
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010851/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10852 * Accepts an optional argument of type string containing the header field name,
10853 * and an optional argument of type signed or unsigned integer to request an
10854 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010855 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010856 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010857static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010858smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010859{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010860 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010861 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010862 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010863 int occ = 0;
10864 const char *name_str = NULL;
10865 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010866
Willy Tarreaua890d072013-04-02 12:01:06 +020010867 if (!ctx) {
10868 /* first call */
10869 ctx = &static_hdr_ctx;
10870 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010871 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010872 }
10873
Willy Tarreau185b5c42012-04-26 15:11:51 +020010874 if (args) {
10875 if (args[0].type != ARGT_STR)
10876 return 0;
10877 name_str = args[0].data.str.str;
10878 name_len = args[0].data.str.len;
10879
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010880 if (args[1].type == ARGT_SINT)
10881 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010882 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010883
Willy Tarreaue333ec92012-04-16 16:26:40 +020010884 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010885
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010886 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010887 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 +020010888
Willy Tarreau185b5c42012-04-26 15:11:51 +020010889 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010890 /* search for header from the beginning */
10891 ctx->idx = 0;
10892
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010893 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010894 /* no explicit occurrence and single fetch => last header by default */
10895 occ = -1;
10896
10897 if (!occ)
10898 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010899 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010900
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010901 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010902 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010903 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 +020010904 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010905
Willy Tarreau37406352012-04-23 16:16:37 +020010906 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010907 return 0;
10908}
10909
Willy Tarreauc11416f2007-06-17 16:58:38 +020010910/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010911 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010912 */
10913static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010914smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010915{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010916 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010917 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010918 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010919 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010920 const char *name = NULL;
10921 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010922
Willy Tarreau601a4d12015-04-01 19:16:09 +020010923 if (args && args->type == ARGT_STR) {
10924 name = args->data.str.str;
10925 len = args->data.str.len;
10926 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010927
Willy Tarreaue333ec92012-04-16 16:26:40 +020010928 CHECK_HTTP_MESSAGE_FIRST();
10929
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010930 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010931 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 +020010932
Willy Tarreau33a7e692007-06-10 19:45:56 +020010933 ctx.idx = 0;
10934 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010935 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010936 cnt++;
10937
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010938 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010939 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010940 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010941 return 1;
10942}
10943
Willy Tarreau185b5c42012-04-26 15:11:51 +020010944/* Fetch an HTTP header's integer value. The integer value is returned. It
10945 * takes a mandatory argument of type string and an optional one of type int
10946 * to designate a specific occurrence. It returns an unsigned integer, which
10947 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010948 */
10949static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010950smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010951{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010952 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010953
Willy Tarreauf853c462012-04-23 18:53:56 +020010954 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010955 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010956 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010957 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010958
Willy Tarreaud53e2422012-04-16 17:21:11 +020010959 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010960}
10961
Cyril Bonté69fa9922012-10-25 00:01:06 +020010962/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10963 * and an optional one of type int to designate a specific occurrence.
10964 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010965 */
10966static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010967smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010968{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010969 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010970
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010971 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010972 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010973 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010974 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010975 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010976 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010977 if (smp->data.u.str.len < temp->size - 1) {
10978 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10979 temp->str[smp->data.u.str.len] = '\0';
10980 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010981 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010982 break;
10983 }
10984 }
10985 }
10986
Willy Tarreaud53e2422012-04-16 17:21:11 +020010987 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010988 if (!(smp->flags & SMP_F_NOT_LAST))
10989 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010990 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010991 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010992}
10993
Willy Tarreau737b0c12007-06-10 21:28:46 +020010994/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10995 * the first '/' after the possible hostname, and ends before the possible '?'.
10996 */
10997static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010998smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010999{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011000 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011001 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020011002
Willy Tarreauc0239e02012-04-16 14:42:55 +020011003 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020011004
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011005 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020011006 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010011007 ptr = http_get_path(txn);
11008 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020011009 return 0;
11010
11011 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011012 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011013 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011014
11015 while (ptr < end && *ptr != '?')
11016 ptr++;
11017
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011018 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011019 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011020 return 1;
11021}
11022
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011023/* This produces a concatenation of the first occurrence of the Host header
11024 * followed by the path component if it begins with a slash ('/'). This means
11025 * that '*' will not be added, resulting in exactly the first Host entry.
11026 * If no Host header is found, then the path is returned as-is. The returned
11027 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010011028 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011029 */
11030static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011031smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011032{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011033 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011034 char *ptr, *end, *beg;
11035 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011036 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011037
11038 CHECK_HTTP_MESSAGE_FIRST();
11039
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011040 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011041 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011042 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011043 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011044
11045 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011046 temp = get_trash_chunk();
11047 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011048 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011049 smp->data.u.str.str = temp->str;
11050 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011051
11052 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011053 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011054 beg = http_get_path(txn);
11055 if (!beg)
11056 beg = end;
11057
11058 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11059
11060 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011061 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
11062 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011063 }
11064
11065 smp->flags = SMP_F_VOL_1ST;
11066 return 1;
11067}
11068
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011069/* This produces a 32-bit hash of the concatenation of the first occurrence of
11070 * the Host header followed by the path component if it begins with a slash ('/').
11071 * This means that '*' will not be added, resulting in exactly the first Host
11072 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011073 * is hashed using the path hash followed by a full avalanche hash and provides a
11074 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011075 * high-traffic sites without having to store whole paths.
11076 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020011077int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011078smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011079{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011080 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011081 struct hdr_ctx ctx;
11082 unsigned int hash = 0;
11083 char *ptr, *beg, *end;
11084 int len;
11085
11086 CHECK_HTTP_MESSAGE_FIRST();
11087
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011088 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011089 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011090 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011091 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11092 ptr = ctx.line + ctx.val;
11093 len = ctx.vlen;
11094 while (len--)
11095 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11096 }
11097
11098 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011099 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011100 beg = http_get_path(txn);
11101 if (!beg)
11102 beg = end;
11103
11104 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11105
11106 if (beg < ptr && *beg == '/') {
11107 while (beg < ptr)
11108 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11109 }
11110 hash = full_hash(hash);
11111
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011112 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011113 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011114 smp->flags = SMP_F_VOL_1ST;
11115 return 1;
11116}
11117
Willy Tarreau4a550602012-12-09 14:53:32 +010011118/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011119 * path as returned by smp_fetch_base32(). The idea is to have per-source and
11120 * per-path counters. The result is a binary block from 8 to 20 bytes depending
11121 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010011122 * that in environments where IPv6 is insignificant, truncating the output to
11123 * 8 bytes would still work.
11124 */
11125static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011126smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011127{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011128 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011129 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011130
11131 if (!cli_conn)
11132 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011133
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011134 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011135 return 0;
11136
Willy Tarreau47ca5452012-12-23 20:22:19 +010011137 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011138 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011139 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011140
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011141 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011142 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011143 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011144 temp->len += 4;
11145 break;
11146 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011147 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011148 temp->len += 16;
11149 break;
11150 default:
11151 return 0;
11152 }
11153
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011154 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011155 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011156 return 1;
11157}
11158
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011159/* Extracts the query string, which comes after the question mark '?'. If no
11160 * question mark is found, nothing is returned. Otherwise it returns a sample
11161 * of type string carrying the whole query string.
11162 */
11163static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011164smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011165{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011166 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011167 char *ptr, *end;
11168
11169 CHECK_HTTP_MESSAGE_FIRST();
11170
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011171 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011172 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11173 end = ptr + txn->req.sl.rq.u_l;
11174
11175 /* look up the '?' */
11176 do {
11177 if (ptr == end)
11178 return 0;
11179 } while (*ptr++ != '?');
11180
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011181 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011182 smp->data.u.str.str = ptr;
11183 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011184 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11185 return 1;
11186}
11187
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011188static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011189smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011190{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011191 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11192 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11193 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011194
Willy Tarreau24e32d82012-04-23 23:55:44 +020011195 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011196
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011197 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011198 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011199 return 1;
11200}
11201
Willy Tarreau7f18e522010-10-22 20:04:13 +020011202/* return a valid test if the current request is the first one on the connection */
11203static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011204smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011205{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011206 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011207 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011208 return 1;
11209}
11210
Willy Tarreau34db1082012-04-19 17:16:54 +020011211/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011212static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011213smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011214{
11215
Willy Tarreau24e32d82012-04-23 23:55:44 +020011216 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011217 return 0;
11218
Willy Tarreauc0239e02012-04-16 14:42:55 +020011219 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011220
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011221 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011222 return 0;
11223
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011224 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011225 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011226 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011227 return 1;
11228}
Willy Tarreau8797c062007-05-07 00:55:35 +020011229
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011230/* Accepts exactly 1 argument of type userlist */
11231static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011232smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011233{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011234 if (!args || args->type != ARGT_USR)
11235 return 0;
11236
11237 CHECK_HTTP_MESSAGE_FIRST();
11238
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011239 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011240 return 0;
11241
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011242 /* if the user does not belong to the userlist or has a wrong password,
11243 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011244 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011245 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011246 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11247 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011248 return 0;
11249
11250 /* pat_match_auth() will need the user list */
11251 smp->ctx.a[0] = args->data.usr;
11252
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011253 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011254 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011255 smp->data.u.str.str = smp->strm->txn->auth.user;
11256 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011257
11258 return 1;
11259}
11260
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011261/* Try to find the next occurrence of a cookie name in a cookie header value.
11262 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11263 * the cookie value is returned into *value and *value_l, and the function
11264 * returns a pointer to the next pointer to search from if the value was found.
11265 * Otherwise if the cookie was not found, NULL is returned and neither value
11266 * nor value_l are touched. The input <hdr> string should first point to the
11267 * header's value, and the <hdr_end> pointer must point to the first character
11268 * not part of the value. <list> must be non-zero if value may represent a list
11269 * of values (cookie headers). This makes it faster to abort parsing when no
11270 * list is expected.
11271 */
David Carlier4686f792015-09-25 14:10:50 +010011272char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011273extract_cookie_value(char *hdr, const char *hdr_end,
11274 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011275 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276{
11277 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11278 char *next;
11279
11280 /* we search at least a cookie name followed by an equal, and more
11281 * generally something like this :
11282 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11283 */
11284 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11285 /* Iterate through all cookies on this line */
11286
Willy Tarreau2235b262016-11-05 15:50:20 +010011287 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011288 att_beg++;
11289
11290 /* find att_end : this is the first character after the last non
11291 * space before the equal. It may be equal to hdr_end.
11292 */
11293 equal = att_end = att_beg;
11294
11295 while (equal < hdr_end) {
11296 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11297 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011298 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011299 continue;
11300 att_end = equal;
11301 }
11302
11303 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11304 * is between <att_beg> and <equal>, both may be identical.
11305 */
11306
11307 /* look for end of cookie if there is an equal sign */
11308 if (equal < hdr_end && *equal == '=') {
11309 /* look for the beginning of the value */
11310 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011311 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011312 val_beg++;
11313
11314 /* find the end of the value, respecting quotes */
11315 next = find_cookie_value_end(val_beg, hdr_end);
11316
11317 /* make val_end point to the first white space or delimitor after the value */
11318 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011319 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011320 val_end--;
11321 } else {
11322 val_beg = val_end = next = equal;
11323 }
11324
11325 /* We have nothing to do with attributes beginning with '$'. However,
11326 * they will automatically be removed if a header before them is removed,
11327 * since they're supposed to be linked together.
11328 */
11329 if (*att_beg == '$')
11330 continue;
11331
11332 /* Ignore cookies with no equal sign */
11333 if (equal == next)
11334 continue;
11335
11336 /* Now we have the cookie name between att_beg and att_end, and
11337 * its value between val_beg and val_end.
11338 */
11339
11340 if (att_end - att_beg == cookie_name_l &&
11341 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11342 /* let's return this value and indicate where to go on from */
11343 *value = val_beg;
11344 *value_l = val_end - val_beg;
11345 return next + 1;
11346 }
11347
11348 /* Set-Cookie headers only have the name in the first attr=value part */
11349 if (!list)
11350 break;
11351 }
11352
11353 return NULL;
11354}
11355
William Lallemanda43ba4e2014-01-28 18:14:25 +010011356/* Fetch a captured HTTP request header. The index is the position of
11357 * the "capture" option in the configuration file
11358 */
11359static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011360smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011361{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011362 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011363 int idx;
11364
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011365 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011366 return 0;
11367
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011368 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011369
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011370 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 +010011371 return 0;
11372
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011373 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011374 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011375 smp->data.u.str.str = smp->strm->req_cap[idx];
11376 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011377
11378 return 1;
11379}
11380
11381/* Fetch a captured HTTP response header. The index is the position of
11382 * the "capture" option in the configuration file
11383 */
11384static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011385smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011386{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011387 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011388 int idx;
11389
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011390 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011391 return 0;
11392
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011393 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011394
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011395 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 +010011396 return 0;
11397
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011398 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011399 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011400 smp->data.u.str.str = smp->strm->res_cap[idx];
11401 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011402
11403 return 1;
11404}
11405
William Lallemand65ad6e12014-01-31 15:08:02 +010011406/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11407static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011408smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011409{
11410 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011411 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011412 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011413
Willy Tarreau15e91e12015-04-04 00:52:09 +020011414 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011415 return 0;
11416
William Lallemand96a77852014-02-05 00:30:02 +010011417 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011418
William Lallemand96a77852014-02-05 00:30:02 +010011419 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11420 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011421
William Lallemand96a77852014-02-05 00:30:02 +010011422 temp = get_trash_chunk();
11423 temp->str = txn->uri;
11424 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011425 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011426 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011427 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011428
11429 return 1;
11430
11431}
11432
11433/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11434static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011435smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011436{
11437 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011438 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011439 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011440
Willy Tarreau15e91e12015-04-04 00:52:09 +020011441 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011442 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011443
William Lallemand65ad6e12014-01-31 15:08:02 +010011444 ptr = txn->uri;
11445
11446 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11447 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011448
William Lallemand65ad6e12014-01-31 15:08:02 +010011449 if (!*ptr)
11450 return 0;
11451
11452 ptr++; /* skip the space */
11453
11454 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011455 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011456 if (!ptr)
11457 return 0;
11458 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11459 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011460
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011461 smp->data.u.str = *temp;
11462 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011463 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011464 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011465
11466 return 1;
11467}
11468
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011469/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11470 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11471 */
11472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011473smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011474{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011475 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011476
Willy Tarreau15e91e12015-04-04 00:52:09 +020011477 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011478 return 0;
11479
11480 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011481 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011482 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011483 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011484
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011485 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011486 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011487 smp->flags = SMP_F_CONST;
11488 return 1;
11489
11490}
11491
11492/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11493 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11494 */
11495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011496smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011497{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011498 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011499
Willy Tarreau15e91e12015-04-04 00:52:09 +020011500 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011501 return 0;
11502
11503 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011504 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011505 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011506 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011507
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011508 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011509 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011510 smp->flags = SMP_F_CONST;
11511 return 1;
11512
11513}
11514
William Lallemand65ad6e12014-01-31 15:08:02 +010011515
Willy Tarreaue333ec92012-04-16 16:26:40 +020011516/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011517 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011518 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011519 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011520 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011521 * Accepts exactly 1 argument of type string. If the input options indicate
11522 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011523 * The returned sample is of type CSTR. Can be used to parse cookies in other
11524 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011525 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011526int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011527{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011528 struct http_txn *txn;
11529 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011530 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011531 const struct http_msg *msg;
11532 const char *hdr_name;
11533 int hdr_name_len;
11534 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011535 int occ = 0;
11536 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011537
Willy Tarreau24e32d82012-04-23 23:55:44 +020011538 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011539 return 0;
11540
Willy Tarreaua890d072013-04-02 12:01:06 +020011541 if (!ctx) {
11542 /* first call */
11543 ctx = &static_hdr_ctx;
11544 ctx->idx = 0;
11545 smp->ctx.a[2] = ctx;
11546 }
11547
Willy Tarreaue333ec92012-04-16 16:26:40 +020011548 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011549
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011550 txn = smp->strm->txn;
11551 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011552
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011553 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011554 msg = &txn->req;
11555 hdr_name = "Cookie";
11556 hdr_name_len = 6;
11557 } else {
11558 msg = &txn->rsp;
11559 hdr_name = "Set-Cookie";
11560 hdr_name_len = 10;
11561 }
11562
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011563 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011564 /* no explicit occurrence and single fetch => last cookie by default */
11565 occ = -1;
11566
11567 /* OK so basically here, either we want only one value and it's the
11568 * last one, or we want to iterate over all of them and we fetch the
11569 * next one.
11570 */
11571
Willy Tarreau9b28e032012-10-12 23:49:43 +020011572 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011573 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011574 /* search for the header from the beginning, we must first initialize
11575 * the search parameters.
11576 */
Willy Tarreau37406352012-04-23 16:16:37 +020011577 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011578 ctx->idx = 0;
11579 }
11580
Willy Tarreau28376d62012-04-26 21:26:10 +020011581 smp->flags |= SMP_F_VOL_HDR;
11582
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011583 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011584 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11585 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011586 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11587 goto out;
11588
Willy Tarreau24e32d82012-04-23 23:55:44 +020011589 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011590 continue;
11591
Willy Tarreau37406352012-04-23 16:16:37 +020011592 smp->ctx.a[0] = ctx->line + ctx->val;
11593 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011594 }
11595
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011596 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011597 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011598 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011599 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011600 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011601 &smp->data.u.str.str,
11602 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011603 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011604 found = 1;
11605 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011606 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011607 smp->flags |= SMP_F_NOT_LAST;
11608 return 1;
11609 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011610 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011611 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011612 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011613 /* all cookie headers and values were scanned. If we're looking for the
11614 * last occurrence, we may return it now.
11615 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011616 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011617 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011618 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011619}
11620
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011621/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011622 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011623 * multiple cookies may be parsed on the same line. The returned sample is of
11624 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011625 */
11626static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011627smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011628{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011629 struct http_txn *txn;
11630 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011631 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011632 const struct http_msg *msg;
11633 const char *hdr_name;
11634 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011635 int cnt;
11636 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011637 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011638
Willy Tarreau24e32d82012-04-23 23:55:44 +020011639 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011640 return 0;
11641
Willy Tarreaue333ec92012-04-16 16:26:40 +020011642 CHECK_HTTP_MESSAGE_FIRST();
11643
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011644 txn = smp->strm->txn;
11645 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011646
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011647 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011648 msg = &txn->req;
11649 hdr_name = "Cookie";
11650 hdr_name_len = 6;
11651 } else {
11652 msg = &txn->rsp;
11653 hdr_name = "Set-Cookie";
11654 hdr_name_len = 10;
11655 }
11656
Willy Tarreau9b28e032012-10-12 23:49:43 +020011657 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011658 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011659 ctx.idx = 0;
11660 cnt = 0;
11661
11662 while (1) {
11663 /* Note: val_beg == NULL every time we need to fetch a new header */
11664 if (!val_beg) {
11665 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11666 break;
11667
Willy Tarreau24e32d82012-04-23 23:55:44 +020011668 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011669 continue;
11670
11671 val_beg = ctx.line + ctx.val;
11672 val_end = val_beg + ctx.vlen;
11673 }
11674
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011675 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011676 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011677 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011678 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011679 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011680 &smp->data.u.str.str,
11681 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011682 cnt++;
11683 }
11684 }
11685
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011686 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011687 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011688 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011689 return 1;
11690}
11691
Willy Tarreau51539362012-05-08 12:46:28 +020011692/* Fetch an cookie's integer value. The integer value is returned. It
11693 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11694 */
11695static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011696smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011697{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011698 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011699
11700 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011701 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011702 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011703 }
11704
11705 return ret;
11706}
11707
Willy Tarreau8797c062007-05-07 00:55:35 +020011708/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011709/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011710/************************************************************************/
11711
David Cournapeau16023ee2010-12-23 20:55:41 +090011712/*
11713 * Given a path string and its length, find the position of beginning of the
11714 * query string. Returns NULL if no query string is found in the path.
11715 *
11716 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11717 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011718 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011719 */
bedis4c75cca2012-10-05 08:38:24 +020011720static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011721{
11722 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011723
bedis4c75cca2012-10-05 08:38:24 +020011724 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011725 return p ? p + 1 : NULL;
11726}
11727
bedis4c75cca2012-10-05 08:38:24 +020011728static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011729{
bedis4c75cca2012-10-05 08:38:24 +020011730 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011731}
11732
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011733/* after increasing a pointer value, it can exceed the first buffer
11734 * size. This function transform the value of <ptr> according with
11735 * the expected position. <chunks> is an array of the one or two
11736 * avalaible chunks. The first value is the start of the first chunk,
11737 * the second value if the end+1 of the first chunks. The third value
11738 * is NULL or the start of the second chunk and the fourth value is
11739 * the end+1 of the second chunk. The function returns 1 if does a
11740 * wrap, else returns 0.
11741 */
11742static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11743{
11744 if (*ptr < chunks[1])
11745 return 0;
11746 if (!chunks[2])
11747 return 0;
11748 *ptr = chunks[2] + ( *ptr - chunks[1] );
11749 return 1;
11750}
11751
David Cournapeau16023ee2010-12-23 20:55:41 +090011752/*
11753 * Given a url parameter, find the starting position of the first occurence,
11754 * or NULL if the parameter is not found.
11755 *
11756 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11757 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011758 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011759 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011760 * or the second chunk. The caller must be check the position before using the
11761 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011762 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011763static const char *
11764find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011765 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011766 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011767{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011768 const char *pos, *last, *equal;
11769 const char **bufs = chunks;
11770 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011771
David Cournapeau16023ee2010-12-23 20:55:41 +090011772
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011773 pos = bufs[0];
11774 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011775 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011776 /* Check the equal. */
11777 equal = pos + url_param_name_l;
11778 if (fix_pointer_if_wrap(chunks, &equal)) {
11779 if (equal >= chunks[3])
11780 return NULL;
11781 } else {
11782 if (equal >= chunks[1])
11783 return NULL;
11784 }
11785 if (*equal == '=') {
11786 if (pos + url_param_name_l > last) {
11787 /* process wrap case, we detect a wrap. In this case, the
11788 * comparison is performed in two parts.
11789 */
11790
11791 /* This is the end, we dont have any other chunk. */
11792 if (bufs != chunks || !bufs[2])
11793 return NULL;
11794
11795 /* Compute the length of each part of the comparison. */
11796 l1 = last - pos;
11797 l2 = url_param_name_l - l1;
11798
11799 /* The second buffer is too short to contain the compared string. */
11800 if (bufs[2] + l2 > bufs[3])
11801 return NULL;
11802
11803 if (memcmp(pos, url_param_name, l1) == 0 &&
11804 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11805 return pos;
11806
11807 /* Perform wrapping and jump the string who fail the comparison. */
11808 bufs += 2;
11809 pos = bufs[0] + l2;
11810 last = bufs[1];
11811
11812 } else {
11813 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011814 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11815 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011816 pos += url_param_name_l + 1;
11817 if (fix_pointer_if_wrap(chunks, &pos))
11818 last = bufs[2];
11819 }
11820 }
11821
11822 while (1) {
11823 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011824 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011825 pos++;
11826 if (pos < last)
11827 break;
11828 /* process buffer wrapping. */
11829 if (bufs != chunks || !bufs[2])
11830 return NULL;
11831 bufs += 2;
11832 pos = bufs[0];
11833 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011834 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011835 pos++;
11836 }
11837 return NULL;
11838}
11839
11840/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011841 * Given a url parameter name and a query string, find the next value.
11842 * An empty url_param_name matches the first available parameter.
11843 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11844 * respectively provide a pointer to the value and its end.
11845 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011846 */
11847static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011848find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011849 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011850 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011851{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011852 const char *arg_start, *qs_end;
11853 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011854
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011855 arg_start = chunks[0];
11856 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011857 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011858 /* Looks for an argument name. */
11859 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011860 url_param_name, url_param_name_l,
11861 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011862 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011863 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011864 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011865 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011866 if (!arg_start)
11867 return 0;
11868
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011869 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011870 while (1) {
11871 /* looks for the first argument. */
11872 value_start = memchr(arg_start, '=', qs_end - arg_start);
11873 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011874 /* Check for wrapping. */
11875 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011876 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011877 chunks[2]) {
11878 arg_start = chunks[2];
11879 qs_end = chunks[3];
11880 continue;
11881 }
11882 return 0;
11883 }
11884 break;
11885 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011886 value_start++;
11887 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011888 else {
11889 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011890 value_start = arg_start + url_param_name_l + 1;
11891
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011892 /* Check for pointer wrapping. */
11893 if (fix_pointer_if_wrap(chunks, &value_start)) {
11894 /* Update the end pointer. */
11895 qs_end = chunks[3];
11896
11897 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011898 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011899 return 0;
11900 }
11901 }
11902
David Cournapeau16023ee2010-12-23 20:55:41 +090011903 value_end = value_start;
11904
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011905 while (1) {
11906 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11907 value_end++;
11908 if (value_end < qs_end)
11909 break;
11910 /* process buffer wrapping. */
11911 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011912 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011913 chunks[2]) {
11914 value_end = chunks[2];
11915 qs_end = chunks[3];
11916 continue;
11917 }
11918 break;
11919 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011920
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011921 *vstart = value_start;
11922 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011923 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011924}
11925
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011926/* This scans a URL-encoded query string. It takes an optionally wrapping
11927 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11928 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11929 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011930 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011931static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011932smp_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 +090011933{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011934 const char *vstart, *vend;
11935 struct chunk *temp;
11936 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011937
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011938 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011939 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011940 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011941 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011942 return 0;
11943
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011944 /* Create sample. If the value is contiguous, return the pointer as CONST,
11945 * if the value is wrapped, copy-it in a buffer.
11946 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011947 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011948 if (chunks[2] &&
11949 vstart >= chunks[0] && vstart <= chunks[1] &&
11950 vend >= chunks[2] && vend <= chunks[3]) {
11951 /* Wrapped case. */
11952 temp = get_trash_chunk();
11953 memcpy(temp->str, vstart, chunks[1] - vstart);
11954 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011955 smp->data.u.str.str = temp->str;
11956 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011957 } else {
11958 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011959 smp->data.u.str.str = (char *)vstart;
11960 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011961 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11962 }
11963
11964 /* Update context, check wrapping. */
11965 chunks[0] = vend;
11966 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11967 chunks[1] = chunks[3];
11968 chunks[2] = NULL;
11969 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011970
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011971 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011972 smp->flags |= SMP_F_NOT_LAST;
11973
David Cournapeau16023ee2010-12-23 20:55:41 +090011974 return 1;
11975}
11976
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011977/* This function iterates over each parameter of the query string. It uses
11978 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011979 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11980 * An optional parameter name is passed in args[0], otherwise any parameter is
11981 * considered. It supports an optional delimiter argument for the beginning of
11982 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011983 */
11984static int
11985smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11986{
11987 struct http_msg *msg;
11988 char delim = '?';
11989 const char *name;
11990 int name_len;
11991
Dragan Dosen26f77e52015-05-25 10:02:11 +020011992 if (!args ||
11993 (args[0].type && args[0].type != ARGT_STR) ||
11994 (args[1].type && args[1].type != ARGT_STR))
11995 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011996
Dragan Dosen26f77e52015-05-25 10:02:11 +020011997 name = "";
11998 name_len = 0;
11999 if (args->type == ARGT_STR) {
12000 name = args->data.str.str;
12001 name_len = args->data.str.len;
12002 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012003
Dragan Dosen26f77e52015-05-25 10:02:11 +020012004 if (args[1].type)
12005 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012006
Dragan Dosen26f77e52015-05-25 10:02:11 +020012007 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012008 CHECK_HTTP_MESSAGE_FIRST();
12009
12010 msg = &smp->strm->txn->req;
12011
12012 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
12013 msg->sl.rq.u_l, delim);
12014 if (!smp->ctx.a[0])
12015 return 0;
12016
12017 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012018
12019 /* Assume that the context is filled with NULL pointer
12020 * before the first call.
12021 * smp->ctx.a[2] = NULL;
12022 * smp->ctx.a[3] = NULL;
12023 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012024 }
12025
12026 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
12027}
12028
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012029/* This function iterates over each parameter of the body. This requires
12030 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012031 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
12032 * contigous part of the body, and optionally ctx->a[2..3] to reference the
12033 * optional second part if the body wraps at the end of the buffer. An optional
12034 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012035 */
12036static int
12037smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
12038{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012039 struct http_msg *msg;
12040 unsigned long len;
12041 unsigned long block1;
12042 char *body;
12043 const char *name;
12044 int name_len;
12045
12046 if (!args || (args[0].type && args[0].type != ARGT_STR))
12047 return 0;
12048
12049 name = "";
12050 name_len = 0;
12051 if (args[0].type == ARGT_STR) {
12052 name = args[0].data.str.str;
12053 name_len = args[0].data.str.len;
12054 }
12055
12056 if (!smp->ctx.a[0]) { // first call, find the query string
12057 CHECK_HTTP_MESSAGE_FIRST();
12058
12059 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010012060 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012061 else
Willy Tarreaube508f12016-03-10 11:47:01 +010012062 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012063
12064 len = http_body_bytes(msg);
12065 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
12066
12067 block1 = len;
12068 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
12069 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
12070
12071 if (block1 == len) {
12072 /* buffer is not wrapped (or empty) */
12073 smp->ctx.a[0] = body;
12074 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012075
12076 /* Assume that the context is filled with NULL pointer
12077 * before the first call.
12078 * smp->ctx.a[2] = NULL;
12079 * smp->ctx.a[3] = NULL;
12080 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012081 }
12082 else {
12083 /* buffer is wrapped, we need to defragment it */
12084 smp->ctx.a[0] = body;
12085 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012086 smp->ctx.a[2] = msg->chn->buf->data;
12087 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012088 }
12089 }
12090 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
12091}
12092
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012093/* Return the signed integer value for the specified url parameter (see url_param
12094 * above).
12095 */
12096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012097smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012098{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012099 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012100
12101 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012102 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012103 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012104 }
12105
12106 return ret;
12107}
12108
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012109/* This produces a 32-bit hash of the concatenation of the first occurrence of
12110 * the Host header followed by the path component if it begins with a slash ('/').
12111 * This means that '*' will not be added, resulting in exactly the first Host
12112 * entry. If no Host header is found, then the path is used. The resulting value
12113 * is hashed using the url hash followed by a full avalanche hash and provides a
12114 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
12115 * high-traffic sites without having to store whole paths.
12116 * this differs from the base32 functions in that it includes the url parameters
12117 * as well as the path
12118 */
12119static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012120smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012121{
Willy Tarreau15e91e12015-04-04 00:52:09 +020012122 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012123 struct hdr_ctx ctx;
12124 unsigned int hash = 0;
12125 char *ptr, *beg, *end;
12126 int len;
12127
12128 CHECK_HTTP_MESSAGE_FIRST();
12129
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012130 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012131 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012132 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012133 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12134 ptr = ctx.line + ctx.val;
12135 len = ctx.vlen;
12136 while (len--)
12137 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12138 }
12139
12140 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012141 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 +000012142 beg = http_get_path(txn);
12143 if (!beg)
12144 beg = end;
12145
12146 for (ptr = beg; ptr < end ; ptr++);
12147
12148 if (beg < ptr && *beg == '/') {
12149 while (beg < ptr)
12150 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12151 }
12152 hash = full_hash(hash);
12153
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012154 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012155 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012156 smp->flags = SMP_F_VOL_1ST;
12157 return 1;
12158}
12159
12160/* This concatenates the source address with the 32-bit hash of the Host and
12161 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12162 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12163 * on the source address length. The URL hash is stored before the address so
12164 * that in environments where IPv6 is insignificant, truncating the output to
12165 * 8 bytes would still work.
12166 */
12167static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012168smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012169{
12170 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012171 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012172
Dragan Dosendb5af612016-06-16 11:23:01 +020012173 if (!cli_conn)
12174 return 0;
12175
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012176 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012177 return 0;
12178
12179 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012180 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12181 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012182
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012183 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012184 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012185 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012186 temp->len += 4;
12187 break;
12188 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012189 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012190 temp->len += 16;
12191 break;
12192 default:
12193 return 0;
12194 }
12195
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012196 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012197 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012198 return 1;
12199}
12200
Willy Tarreau185b5c42012-04-26 15:11:51 +020012201/* This function is used to validate the arguments passed to any "hdr" fetch
12202 * keyword. These keywords support an optional positive or negative occurrence
12203 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12204 * is assumed that the types are already the correct ones. Returns 0 on error,
12205 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12206 * error message in case of error, that the caller is responsible for freeing.
12207 * The initial location must either be freeable or NULL.
12208 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012209int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012210{
12211 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012212 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012213 return 0;
12214 }
12215 return 1;
12216}
12217
Willy Tarreau276fae92013-07-25 14:36:01 +020012218/* takes an UINT value on input supposed to represent the time since EPOCH,
12219 * adds an optional offset found in args[0] and emits a string representing
12220 * the date in RFC-1123/5322 format.
12221 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012222static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012223{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012224 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012225 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12226 struct chunk *temp;
12227 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012228 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012229 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012230
12231 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012232 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012233 curr_date += args[0].data.sint;
12234
12235 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012236 if (!tm)
12237 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012238
12239 temp = get_trash_chunk();
12240 temp->len = snprintf(temp->str, temp->size - temp->len,
12241 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12242 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12243 tm->tm_hour, tm->tm_min, tm->tm_sec);
12244
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012245 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012246 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012247 return 1;
12248}
12249
Thierry FOURNIERad903512014-04-11 17:51:01 +020012250/* Match language range with language tag. RFC2616 14.4:
12251 *
12252 * A language-range matches a language-tag if it exactly equals
12253 * the tag, or if it exactly equals a prefix of the tag such
12254 * that the first tag character following the prefix is "-".
12255 *
12256 * Return 1 if the strings match, else return 0.
12257 */
12258static inline int language_range_match(const char *range, int range_len,
12259 const char *tag, int tag_len)
12260{
12261 const char *end = range + range_len;
12262 const char *tend = tag + tag_len;
12263 while (range < end) {
12264 if (*range == '-' && tag == tend)
12265 return 1;
12266 if (*range != *tag || tag == tend)
12267 return 0;
12268 range++;
12269 tag++;
12270 }
12271 /* Return true only if the last char of the tag is matched. */
12272 return tag == tend;
12273}
12274
12275/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012276static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012277{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012278 const char *al = smp->data.u.str.str;
12279 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012280 const char *token;
12281 int toklen;
12282 int qvalue;
12283 const char *str;
12284 const char *w;
12285 int best_q = 0;
12286
12287 /* Set the constant to the sample, because the output of the
12288 * function will be peek in the constant configuration string.
12289 */
12290 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012291 smp->data.u.str.size = 0;
12292 smp->data.u.str.str = "";
12293 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012294
12295 /* Parse the accept language */
12296 while (1) {
12297
12298 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012299 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012300 al++;
12301 if (al >= end)
12302 break;
12303
12304 /* Start of the fisrt word. */
12305 token = al;
12306
12307 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012308 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012309 al++;
12310 if (al == token)
12311 goto expect_comma;
12312
12313 /* Length of the token. */
12314 toklen = al - token;
12315 qvalue = 1000;
12316
12317 /* Check if the token exists in the list. If the token not exists,
12318 * jump to the next token.
12319 */
12320 str = args[0].data.str.str;
12321 w = str;
12322 while (1) {
12323 if (*str == ';' || *str == '\0') {
12324 if (language_range_match(token, toklen, w, str-w))
12325 goto look_for_q;
12326 if (*str == '\0')
12327 goto expect_comma;
12328 w = str + 1;
12329 }
12330 str++;
12331 }
12332 goto expect_comma;
12333
12334look_for_q:
12335
12336 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012337 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012338 al++;
12339 if (al >= end)
12340 goto process_value;
12341
12342 /* If ',' is found, process the result */
12343 if (*al == ',')
12344 goto process_value;
12345
12346 /* If the character is different from ';', look
12347 * for the end of the header part in best effort.
12348 */
12349 if (*al != ';')
12350 goto expect_comma;
12351
12352 /* Assumes that the char is ';', now expect "q=". */
12353 al++;
12354
12355 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012356 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012357 al++;
12358 if (al >= end)
12359 goto process_value;
12360
12361 /* Expect 'q'. If no 'q', continue in best effort */
12362 if (*al != 'q')
12363 goto process_value;
12364 al++;
12365
12366 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012367 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012368 al++;
12369 if (al >= end)
12370 goto process_value;
12371
12372 /* Expect '='. If no '=', continue in best effort */
12373 if (*al != '=')
12374 goto process_value;
12375 al++;
12376
12377 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012378 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012379 al++;
12380 if (al >= end)
12381 goto process_value;
12382
12383 /* Parse the q value. */
12384 qvalue = parse_qvalue(al, &al);
12385
12386process_value:
12387
12388 /* If the new q value is the best q value, then store the associated
12389 * language in the response. If qvalue is the biggest value (1000),
12390 * break the process.
12391 */
12392 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012393 smp->data.u.str.str = (char *)w;
12394 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012395 if (qvalue >= 1000)
12396 break;
12397 best_q = qvalue;
12398 }
12399
12400expect_comma:
12401
12402 /* Expect comma or end. If the end is detected, quit the loop. */
12403 while (al < end && *al != ',')
12404 al++;
12405 if (al >= end)
12406 break;
12407
12408 /* Comma is found, jump it and restart the analyzer. */
12409 al++;
12410 }
12411
12412 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012413 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12414 smp->data.u.str.str = args[1].data.str.str;
12415 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012416 }
12417
12418 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012419 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012420}
12421
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012422/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012423static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012424{
12425 /* If the constant flag is set or if not size is avalaible at
12426 * the end of the buffer, copy the string in other buffer
12427 * before decoding.
12428 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012429 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012430 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012431 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12432 smp->data.u.str.str = str->str;
12433 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012434 smp->flags &= ~SMP_F_CONST;
12435 }
12436
12437 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012438 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12439 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012440 return 1;
12441}
12442
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012443static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12444{
12445 struct proxy *fe = strm_fe(smp->strm);
12446 int idx, i;
12447 struct cap_hdr *hdr;
12448 int len;
12449
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012450 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012451 return 0;
12452
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012453 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012454
12455 /* Check the availibity of the capture id. */
12456 if (idx > fe->nb_req_cap - 1)
12457 return 0;
12458
12459 /* Look for the original configuration. */
12460 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12461 hdr != NULL && i != idx ;
12462 i--, hdr = hdr->next);
12463 if (!hdr)
12464 return 0;
12465
12466 /* check for the memory allocation */
12467 if (smp->strm->req_cap[hdr->index] == NULL)
12468 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12469 if (smp->strm->req_cap[hdr->index] == NULL)
12470 return 0;
12471
12472 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012473 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012474 if (len > hdr->len)
12475 len = hdr->len;
12476
12477 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012478 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012479 smp->strm->req_cap[idx][len] = '\0';
12480
12481 return 1;
12482}
12483
12484static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12485{
12486 struct proxy *fe = strm_fe(smp->strm);
12487 int idx, i;
12488 struct cap_hdr *hdr;
12489 int len;
12490
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012491 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012492 return 0;
12493
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012494 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012495
12496 /* Check the availibity of the capture id. */
12497 if (idx > fe->nb_rsp_cap - 1)
12498 return 0;
12499
12500 /* Look for the original configuration. */
12501 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12502 hdr != NULL && i != idx ;
12503 i--, hdr = hdr->next);
12504 if (!hdr)
12505 return 0;
12506
12507 /* check for the memory allocation */
12508 if (smp->strm->res_cap[hdr->index] == NULL)
12509 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12510 if (smp->strm->res_cap[hdr->index] == NULL)
12511 return 0;
12512
12513 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012514 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012515 if (len > hdr->len)
12516 len = hdr->len;
12517
12518 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012519 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012520 smp->strm->res_cap[idx][len] = '\0';
12521
12522 return 1;
12523}
12524
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012525/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012526 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012527 * the relevant part of the request line accordingly. Then it updates various
12528 * pointers to the next elements which were moved, and the total buffer length.
12529 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012530 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12531 * error, though this can be revisited when this code is finally exploited.
12532 *
12533 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12534 * query string and 3 to replace uri.
12535 *
12536 * In query string case, the mark question '?' must be set at the start of the
12537 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012538 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012539int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012540 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012541{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012542 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012543 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012544 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012545 int delta;
12546
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012547 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012548 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012549 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012550 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12551
12552 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012553 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012554 txn->req.sl.rq.m_l += delta;
12555 txn->req.sl.rq.u += delta;
12556 txn->req.sl.rq.v += delta;
12557 break;
12558
12559 case 1: // path
12560 cur_ptr = http_get_path(txn);
12561 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012562 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012563
12564 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012565 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 +010012566 cur_end++;
12567
12568 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012569 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012570 txn->req.sl.rq.u_l += delta;
12571 txn->req.sl.rq.v += delta;
12572 break;
12573
12574 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012575 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012576 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012577 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12578 while (cur_ptr < cur_end && *cur_ptr != '?')
12579 cur_ptr++;
12580
12581 /* skip the question mark or indicate that we must insert it
12582 * (but only if the format string is not empty then).
12583 */
12584 if (cur_ptr < cur_end)
12585 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012586 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012587 offset = 0;
12588
12589 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012590 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012591 txn->req.sl.rq.u_l += delta;
12592 txn->req.sl.rq.v += delta;
12593 break;
12594
12595 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012596 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012597 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12598
12599 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012600 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012601 txn->req.sl.rq.u_l += delta;
12602 txn->req.sl.rq.v += delta;
12603 break;
12604
12605 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012606 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012607 }
12608
12609 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012610 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012611 txn->req.sl.rq.l += delta;
12612 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012613 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012614 return 0;
12615}
12616
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012617/* This function replace the HTTP status code and the associated message. The
12618 * variable <status> contains the new status code. This function never fails.
12619 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012620void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012621{
12622 struct http_txn *txn = s->txn;
12623 char *cur_ptr, *cur_end;
12624 int delta;
12625 char *res;
12626 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012627 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012628 int msg_len;
12629
12630 chunk_reset(&trash);
12631
12632 res = ultoa_o(status, trash.str, trash.size);
12633 c_l = res - trash.str;
12634
12635 trash.str[c_l] = ' ';
12636 trash.len = c_l + 1;
12637
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012638 /* Do we have a custom reason format string? */
12639 if (msg == NULL)
12640 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012641 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012642 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12643 trash.len += msg_len;
12644
12645 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12646 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12647
12648 /* commit changes and adjust message */
12649 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12650
12651 /* adjust res line offsets and lengths */
12652 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12653 txn->rsp.sl.st.c_l = c_l;
12654 txn->rsp.sl.st.r_l = msg_len;
12655
12656 delta = trash.len - (cur_end - cur_ptr);
12657 txn->rsp.sl.st.l += delta;
12658 txn->hdr_idx.v[0].len += delta;
12659 http_msg_move_end(&txn->rsp, delta);
12660}
12661
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012662/* This function executes one of the set-{method,path,query,uri} actions. It
12663 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012664 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012665 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012666 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12667 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012668 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012669enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012670 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012671{
12672 chunk_reset(&trash);
12673
12674 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012675 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012676 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012677 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012678
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012679 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012680 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012681}
12682
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012683/* This function is just a compliant action wrapper for "set-status". */
12684enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012685 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012686{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012687 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012688 return ACT_RET_CONT;
12689}
12690
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012691/* parse an http-request action among :
12692 * set-method
12693 * set-path
12694 * set-query
12695 * set-uri
12696 *
12697 * All of them accept a single argument of type string representing a log-format.
12698 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12699 * 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 +020012700 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012701 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012702enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12703 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012704{
12705 int cur_arg = *orig_arg;
12706
Thierry FOURNIER42148732015-09-02 17:17:33 +020012707 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012708
12709 switch (args[0][4]) {
12710 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012711 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012712 rule->action_ptr = http_action_set_req_line;
12713 break;
12714 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012715 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012716 rule->action_ptr = http_action_set_req_line;
12717 break;
12718 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012719 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012720 rule->action_ptr = http_action_set_req_line;
12721 break;
12722 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012723 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012724 rule->action_ptr = http_action_set_req_line;
12725 break;
12726 default:
12727 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012728 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012729 }
12730
12731 if (!*args[cur_arg] ||
12732 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12733 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012734 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012735 }
12736
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012737 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012738 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012739 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012740 (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 +010012741 return ACT_RET_PRS_ERR;
12742 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012743
12744 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012745 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012746}
12747
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012748/* parse set-status action:
12749 * This action accepts a single argument of type int representing
12750 * an http status code. It returns ACT_RET_PRS_OK on success,
12751 * ACT_RET_PRS_ERR on error.
12752 */
12753enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12754 struct act_rule *rule, char **err)
12755{
12756 char *error;
12757
Thierry FOURNIER42148732015-09-02 17:17:33 +020012758 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012759 rule->action_ptr = action_http_set_status;
12760
12761 /* Check if an argument is available */
12762 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012763 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012764 return ACT_RET_PRS_ERR;
12765 }
12766
12767 /* convert status code as integer */
12768 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12769 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12770 memprintf(err, "expects an integer status code between 100 and 999");
12771 return ACT_RET_PRS_ERR;
12772 }
12773
12774 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012775
12776 /* set custom reason string */
12777 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12778 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12779 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12780 (*orig_arg)++;
12781 rule->arg.status.reason = strdup(args[*orig_arg]);
12782 (*orig_arg)++;
12783 }
12784
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012785 return ACT_RET_PRS_OK;
12786}
12787
Willy Tarreaua9083d02015-05-08 15:27:59 +020012788/* This function executes the "capture" action. It executes a fetch expression,
12789 * turns the result into a string and puts it in a capture slot. It always
12790 * returns 1. If an error occurs the action is cancelled, but the rule
12791 * processing continues.
12792 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012793enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012794 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012795{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012796 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012797 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012798 char **cap = s->req_cap;
12799 int len;
12800
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012801 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 +020012802 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012803 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012804
12805 if (cap[h->index] == NULL)
12806 cap[h->index] = pool_alloc2(h->pool);
12807
12808 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012809 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012810
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012811 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012812 if (len > h->len)
12813 len = h->len;
12814
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012815 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012816 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012817 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012818}
12819
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012820/* This function executes the "capture" action and store the result in a
12821 * capture slot if exists. It executes a fetch expression, turns the result
12822 * into a string and puts it in a capture slot. It always returns 1. If an
12823 * error occurs the action is cancelled, but the rule processing continues.
12824 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012825enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012826 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012827{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012828 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012829 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012830 char **cap = s->req_cap;
12831 struct proxy *fe = strm_fe(s);
12832 int len;
12833 int i;
12834
12835 /* Look for the original configuration. */
12836 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012837 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012838 i--, h = h->next);
12839 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012840 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012841
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012842 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 +020012843 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012844 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012845
12846 if (cap[h->index] == NULL)
12847 cap[h->index] = pool_alloc2(h->pool);
12848
12849 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012850 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012851
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012852 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012853 if (len > h->len)
12854 len = h->len;
12855
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012856 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012857 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012858 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012859}
12860
Willy Tarreaua9083d02015-05-08 15:27:59 +020012861/* parse an "http-request capture" action. It takes a single argument which is
12862 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012863 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012864 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012865 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012866enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12867 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012868{
12869 struct sample_expr *expr;
12870 struct cap_hdr *hdr;
12871 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012872 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012873
12874 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12875 if (strcmp(args[cur_arg], "if") == 0 ||
12876 strcmp(args[cur_arg], "unless") == 0)
12877 break;
12878
12879 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012880 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012881 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012882 }
12883
Willy Tarreaua9083d02015-05-08 15:27:59 +020012884 cur_arg = *orig_arg;
12885 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12886 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012887 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012888
12889 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12890 memprintf(err,
12891 "fetch method '%s' extracts information from '%s', none of which is available here",
12892 args[cur_arg-1], sample_src_names(expr->fetch->use));
12893 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012894 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012895 }
12896
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012897 if (!args[cur_arg] || !*args[cur_arg]) {
12898 memprintf(err, "expects 'len or 'id'");
12899 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012900 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012901 }
12902
Willy Tarreaua9083d02015-05-08 15:27:59 +020012903 if (strcmp(args[cur_arg], "len") == 0) {
12904 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012905
12906 if (!(px->cap & PR_CAP_FE)) {
12907 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012908 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012909 }
12910
12911 proxy->conf.args.ctx = ARGC_CAP;
12912
Willy Tarreaua9083d02015-05-08 15:27:59 +020012913 if (!args[cur_arg]) {
12914 memprintf(err, "missing length value");
12915 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012916 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012917 }
12918 /* we copy the table name for now, it will be resolved later */
12919 len = atoi(args[cur_arg]);
12920 if (len <= 0) {
12921 memprintf(err, "length must be > 0");
12922 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012923 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012924 }
12925 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012926
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012927 if (!len) {
12928 memprintf(err, "a positive 'len' argument is mandatory");
12929 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012930 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012931 }
12932
Vincent Bernat02779b62016-04-03 13:48:43 +020012933 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012934 hdr->next = px->req_cap;
12935 hdr->name = NULL; /* not a header capture */
12936 hdr->namelen = 0;
12937 hdr->len = len;
12938 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12939 hdr->index = px->nb_req_cap++;
12940
12941 px->req_cap = hdr;
12942 px->to_log |= LW_REQHDR;
12943
Thierry FOURNIER42148732015-09-02 17:17:33 +020012944 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012945 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012946 rule->arg.cap.expr = expr;
12947 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012948 }
12949
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012950 else if (strcmp(args[cur_arg], "id") == 0) {
12951 int id;
12952 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012953
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012954 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012955
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012956 if (!args[cur_arg]) {
12957 memprintf(err, "missing id value");
12958 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012959 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012960 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012961
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012962 id = strtol(args[cur_arg], &error, 10);
12963 if (*error != '\0') {
12964 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12965 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012966 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012967 }
12968 cur_arg++;
12969
12970 proxy->conf.args.ctx = ARGC_CAP;
12971
Thierry FOURNIER42148732015-09-02 17:17:33 +020012972 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012973 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012974 rule->arg.capid.expr = expr;
12975 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012976 }
12977
12978 else {
12979 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12980 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012981 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012982 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012983
12984 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012985 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012986}
12987
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012988/* This function executes the "capture" action and store the result in a
12989 * capture slot if exists. It executes a fetch expression, turns the result
12990 * into a string and puts it in a capture slot. It always returns 1. If an
12991 * error occurs the action is cancelled, but the rule processing continues.
12992 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012993enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012994 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012995{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012996 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012997 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012998 char **cap = s->res_cap;
12999 struct proxy *fe = strm_fe(s);
13000 int len;
13001 int i;
13002
13003 /* Look for the original configuration. */
13004 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013005 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013006 i--, h = h->next);
13007 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013008 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013009
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013010 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 +020013011 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013012 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013013
13014 if (cap[h->index] == NULL)
13015 cap[h->index] = pool_alloc2(h->pool);
13016
13017 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013018 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013019
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013020 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013021 if (len > h->len)
13022 len = h->len;
13023
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013024 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013025 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013026 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013027}
13028
13029/* parse an "http-response capture" action. It takes a single argument which is
13030 * a sample fetch expression. It stores the expression into arg->act.p[0] and
13031 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013032 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013033 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013034enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
13035 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013036{
13037 struct sample_expr *expr;
13038 int cur_arg;
13039 int id;
13040 char *error;
13041
13042 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
13043 if (strcmp(args[cur_arg], "if") == 0 ||
13044 strcmp(args[cur_arg], "unless") == 0)
13045 break;
13046
13047 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013048 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013049 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013050 }
13051
13052 cur_arg = *orig_arg;
13053 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
13054 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013055 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013056
13057 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
13058 memprintf(err,
13059 "fetch method '%s' extracts information from '%s', none of which is available here",
13060 args[cur_arg-1], sample_src_names(expr->fetch->use));
13061 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013062 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013063 }
13064
13065 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013066 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013067 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013068 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013069 }
13070
13071 if (strcmp(args[cur_arg], "id") != 0) {
13072 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
13073 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013074 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013075 }
13076
13077 cur_arg++;
13078
13079 if (!args[cur_arg]) {
13080 memprintf(err, "missing id value");
13081 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013082 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013083 }
13084
13085 id = strtol(args[cur_arg], &error, 10);
13086 if (*error != '\0') {
13087 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
13088 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013089 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013090 }
13091 cur_arg++;
13092
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013093 proxy->conf.args.ctx = ARGC_CAP;
13094
Thierry FOURNIER42148732015-09-02 17:17:33 +020013095 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013096 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013097 rule->arg.capid.expr = expr;
13098 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013099
13100 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013101 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013102}
13103
William Lallemand73025dd2014-04-24 14:38:37 +020013104/*
13105 * Return the struct http_req_action_kw associated to a keyword.
13106 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013107struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013108{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013109 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013110}
13111
13112/*
13113 * Return the struct http_res_action_kw associated to a keyword.
13114 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013115struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013116{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013117 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013118}
13119
Willy Tarreau12207b32016-11-22 19:48:51 +010013120
13121/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
13122 * now.
13123 */
13124static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
13125{
13126 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13127 return 1;
13128
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013129 if (*args[2]) {
13130 struct proxy *px;
13131
13132 px = proxy_find_by_name(args[2], 0, 0);
13133 if (px)
13134 appctx->ctx.errors.iid = px->uuid;
13135 else
13136 appctx->ctx.errors.iid = atoi(args[2]);
13137
13138 if (!appctx->ctx.errors.iid) {
13139 appctx->ctx.cli.msg = "No such proxy.\n";
13140 appctx->st0 = CLI_ST_PRINT;
13141 return 1;
13142 }
13143 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013144 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013145 appctx->ctx.errors.iid = -1; // dump all proxies
13146
Willy Tarreau35069f82016-11-25 09:16:37 +010013147 appctx->ctx.errors.flag = 0;
13148 if (strcmp(args[3], "request") == 0)
13149 appctx->ctx.errors.flag |= 4; // ignore response
13150 else if (strcmp(args[3], "response") == 0)
13151 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013152 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013153 return 0;
13154}
13155
13156/* This function dumps all captured errors onto the stream interface's
13157 * read buffer. It returns 0 if the output buffer is full and it needs
13158 * to be called again, otherwise non-zero.
13159 */
13160static int cli_io_handler_show_errors(struct appctx *appctx)
13161{
13162 struct stream_interface *si = appctx->owner;
13163 extern const char *monthname[12];
13164
13165 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13166 return 1;
13167
13168 chunk_reset(&trash);
13169
13170 if (!appctx->ctx.errors.px) {
13171 /* the function had not been called yet, let's prepare the
13172 * buffer for a response.
13173 */
13174 struct tm tm;
13175
13176 get_localtime(date.tv_sec, &tm);
13177 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13178 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13179 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13180 error_snapshot_id);
13181
13182 if (bi_putchk(si_ic(si), &trash) == -1) {
13183 /* Socket buffer full. Let's try again later from the same point */
13184 si_applet_cant_put(si);
13185 return 0;
13186 }
13187
13188 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013189 appctx->ctx.errors.bol = 0;
13190 appctx->ctx.errors.ptr = -1;
13191 }
13192
13193 /* we have two inner loops here, one for the proxy, the other one for
13194 * the buffer.
13195 */
13196 while (appctx->ctx.errors.px) {
13197 struct error_snapshot *es;
13198
Willy Tarreau35069f82016-11-25 09:16:37 +010013199 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013200 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013201 if (appctx->ctx.errors.flag & 2) // skip req
13202 goto next;
13203 }
13204 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013205 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013206 if (appctx->ctx.errors.flag & 4) // skip resp
13207 goto next;
13208 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013209
13210 if (!es->when.tv_sec)
13211 goto next;
13212
13213 if (appctx->ctx.errors.iid >= 0 &&
13214 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13215 es->oe->uuid != appctx->ctx.errors.iid)
13216 goto next;
13217
13218 if (appctx->ctx.errors.ptr < 0) {
13219 /* just print headers now */
13220
13221 char pn[INET6_ADDRSTRLEN];
13222 struct tm tm;
13223 int port;
13224
13225 get_localtime(es->when.tv_sec, &tm);
13226 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13227 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13228 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13229
13230 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13231 case AF_INET:
13232 case AF_INET6:
13233 port = get_host_port(&es->src);
13234 break;
13235 default:
13236 port = 0;
13237 }
13238
Willy Tarreau35069f82016-11-25 09:16:37 +010013239 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013240 case 0:
13241 chunk_appendf(&trash,
13242 " frontend %s (#%d): invalid request\n"
13243 " backend %s (#%d)",
13244 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13245 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13246 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13247 break;
13248 case 1:
13249 chunk_appendf(&trash,
13250 " backend %s (#%d): invalid response\n"
13251 " frontend %s (#%d)",
13252 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13253 es->oe->id, es->oe->uuid);
13254 break;
13255 }
13256
13257 chunk_appendf(&trash,
13258 ", server %s (#%d), event #%u\n"
13259 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013260 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013261 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13262 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13263 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13264 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13265 es->ev_id,
13266 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013267 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013268 es->m_clen, es->m_blen,
13269 es->b_flags, es->b_out, es->b_tot,
13270 es->len, es->b_wrap, es->pos);
13271
13272 if (bi_putchk(si_ic(si), &trash) == -1) {
13273 /* Socket buffer full. Let's try again later from the same point */
13274 si_applet_cant_put(si);
13275 return 0;
13276 }
13277 appctx->ctx.errors.ptr = 0;
13278 appctx->ctx.errors.sid = es->sid;
13279 }
13280
13281 if (appctx->ctx.errors.sid != es->sid) {
13282 /* the snapshot changed while we were dumping it */
13283 chunk_appendf(&trash,
13284 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13285 if (bi_putchk(si_ic(si), &trash) == -1) {
13286 si_applet_cant_put(si);
13287 return 0;
13288 }
13289 goto next;
13290 }
13291
13292 /* OK, ptr >= 0, so we have to dump the current line */
13293 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13294 int newptr;
13295 int newline;
13296
13297 newline = appctx->ctx.errors.bol;
13298 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13299 if (newptr == appctx->ctx.errors.ptr)
13300 return 0;
13301
13302 if (bi_putchk(si_ic(si), &trash) == -1) {
13303 /* Socket buffer full. Let's try again later from the same point */
13304 si_applet_cant_put(si);
13305 return 0;
13306 }
13307 appctx->ctx.errors.ptr = newptr;
13308 appctx->ctx.errors.bol = newline;
13309 };
13310 next:
13311 appctx->ctx.errors.bol = 0;
13312 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013313 appctx->ctx.errors.flag ^= 1;
13314 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013315 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013316 }
13317
13318 /* dump complete */
13319 return 1;
13320}
13321
13322/* register cli keywords */
13323static struct cli_kw_list cli_kws = {{ },{
13324 { { "show", "errors", NULL },
13325 "show errors : report last request and response errors for each proxy",
13326 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13327 },
13328 {{},}
13329}};
13330
Willy Tarreau4a568972010-05-12 08:08:50 +020013331/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013332/* All supported ACL keywords must be declared here. */
13333/************************************************************************/
13334
13335/* Note: must not be declared <const> as its list will be overwritten.
13336 * Please take care of keeping this list alphabetically sorted.
13337 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013338static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013339 { "base", "base", PAT_MATCH_STR },
13340 { "base_beg", "base", PAT_MATCH_BEG },
13341 { "base_dir", "base", PAT_MATCH_DIR },
13342 { "base_dom", "base", PAT_MATCH_DOM },
13343 { "base_end", "base", PAT_MATCH_END },
13344 { "base_len", "base", PAT_MATCH_LEN },
13345 { "base_reg", "base", PAT_MATCH_REG },
13346 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013347
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013348 { "cook", "req.cook", PAT_MATCH_STR },
13349 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13350 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13351 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13352 { "cook_end", "req.cook", PAT_MATCH_END },
13353 { "cook_len", "req.cook", PAT_MATCH_LEN },
13354 { "cook_reg", "req.cook", PAT_MATCH_REG },
13355 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013356
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013357 { "hdr", "req.hdr", PAT_MATCH_STR },
13358 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13359 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13360 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13361 { "hdr_end", "req.hdr", PAT_MATCH_END },
13362 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13363 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13364 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013365
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013366 /* these two declarations uses strings with list storage (in place
13367 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13368 * and delete functions are relative to the list management. The parse
13369 * and match method are related to the corresponding fetch methods. This
13370 * is very particular ACL declaration mode.
13371 */
13372 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13373 { "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 +020013374
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013375 { "path", "path", PAT_MATCH_STR },
13376 { "path_beg", "path", PAT_MATCH_BEG },
13377 { "path_dir", "path", PAT_MATCH_DIR },
13378 { "path_dom", "path", PAT_MATCH_DOM },
13379 { "path_end", "path", PAT_MATCH_END },
13380 { "path_len", "path", PAT_MATCH_LEN },
13381 { "path_reg", "path", PAT_MATCH_REG },
13382 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013383
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013384 { "req_ver", "req.ver", PAT_MATCH_STR },
13385 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013386
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013387 { "scook", "res.cook", PAT_MATCH_STR },
13388 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13389 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13390 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13391 { "scook_end", "res.cook", PAT_MATCH_END },
13392 { "scook_len", "res.cook", PAT_MATCH_LEN },
13393 { "scook_reg", "res.cook", PAT_MATCH_REG },
13394 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013395
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013396 { "shdr", "res.hdr", PAT_MATCH_STR },
13397 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13398 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13399 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13400 { "shdr_end", "res.hdr", PAT_MATCH_END },
13401 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13402 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13403 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013404
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013405 { "url", "url", PAT_MATCH_STR },
13406 { "url_beg", "url", PAT_MATCH_BEG },
13407 { "url_dir", "url", PAT_MATCH_DIR },
13408 { "url_dom", "url", PAT_MATCH_DOM },
13409 { "url_end", "url", PAT_MATCH_END },
13410 { "url_len", "url", PAT_MATCH_LEN },
13411 { "url_reg", "url", PAT_MATCH_REG },
13412 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013413
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013414 { "urlp", "urlp", PAT_MATCH_STR },
13415 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13416 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13417 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13418 { "urlp_end", "urlp", PAT_MATCH_END },
13419 { "urlp_len", "urlp", PAT_MATCH_LEN },
13420 { "urlp_reg", "urlp", PAT_MATCH_REG },
13421 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013422
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013423 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013424}};
13425
13426/************************************************************************/
13427/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013428/************************************************************************/
13429/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013430static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013431 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013432 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013433 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13434
Willy Tarreau87b09662015-04-03 00:22:06 +020013435 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013436 { "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 +020013437
13438 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013439 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13440 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13441 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013442
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013443 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13444 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013445
Willy Tarreau409bcde2013-01-08 00:31:00 +010013446 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13447 * are only here to match the ACL's name, are request-only and are used
13448 * for ACL compatibility only.
13449 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013450 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13451 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013452 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13453 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013454
13455 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13456 * only here to match the ACL's name, are request-only and are used for
13457 * ACL compatibility only.
13458 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013459 { "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 +020013460 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013461 { "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 +020013462 { "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 +010013463
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013464 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013465 { "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 +010013466 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013467 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013468 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013469 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013470
13471 /* HTTP protocol on the request path */
13472 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013473 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013474
13475 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013476 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13477 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013478
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013479 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013480 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13481 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013482 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013483
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020013484 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020013485 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13486
Willy Tarreau18ed2562013-01-14 15:56:36 +010013487 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013488 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13489 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013490
Willy Tarreau18ed2562013-01-14 15:56:36 +010013491 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013492 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013493 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13494 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013495
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013496 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013497 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013498 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013499 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013500 { "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 +010013501 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013502 { "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 +010013503
13504 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013505 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013506 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13507 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013508
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013509 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013510 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013511 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013512 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013513 { "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 +010013514 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013515 { "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 +010013516
Willy Tarreau409bcde2013-01-08 00:31:00 +010013517 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013518 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013519 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13520 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013521 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013522
13523 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013524 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013525 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013526 { "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 +020013527 { "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 +010013528
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013529 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013530 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013531 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013532 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013533 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013534 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013535 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013536 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13537 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013538 { "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 +010013539 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013540}};
13541
Willy Tarreau8797c062007-05-07 00:55:35 +020013542
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013543/************************************************************************/
13544/* All supported converter keywords must be declared here. */
13545/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013546/* Note: must not be declared <const> as its list will be overwritten */
13547static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013548 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013549 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013550 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13551 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013552 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013553 { NULL, NULL, 0, 0, 0 },
13554}};
13555
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013556
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013557/************************************************************************/
13558/* All supported http-request action keywords must be declared here. */
13559/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013560struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013561 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013562 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013563 { "set-method", parse_set_req_line },
13564 { "set-path", parse_set_req_line },
13565 { "set-query", parse_set_req_line },
13566 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013567 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013568 }
13569};
13570
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013571struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013572 .kw = {
13573 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013574 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013575 { NULL, NULL }
13576 }
13577};
13578
Willy Tarreau8797c062007-05-07 00:55:35 +020013579__attribute__((constructor))
13580static void __http_protocol_init(void)
13581{
13582 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013583 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013584 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013585 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013586 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013587 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013588}
13589
13590
Willy Tarreau58f10d72006-12-04 02:26:12 +010013591/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013592 * Local variables:
13593 * c-indent-level: 8
13594 * c-basic-offset: 8
13595 * End:
13596 */