blob: 4386282c4656e7d1d1c78b03a81ff183989a1933 [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
Willy Tarreau610ecce2010-01-04 21:15:02 +01005316 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5317 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
5360 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5361 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005362 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005363 txn->req.msg_state = HTTP_MSG_TUNNEL;
5364 goto wait_other_side;
5365 }
5366
5367 /* When we get here, it means that both the request and the
5368 * response have finished receiving. Depending on the connection
5369 * mode, we'll have to wait for the last bytes to leave in either
5370 * direction, and sometimes for a close to be effective.
5371 */
5372
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005373 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5374 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005375 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5376 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005377 }
5378 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5379 /* Option forceclose is set, or either side wants to close,
5380 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005381 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005382 * once both states are CLOSED.
5383 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005384 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5385 channel_shutr_now(chn);
5386 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005388 }
5389 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005390 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5391 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005392 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005393 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5394 channel_auto_read(chn);
5395 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005396 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 }
5398
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005399 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005401 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005402
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005403 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404 txn->req.msg_state = HTTP_MSG_CLOSING;
5405 goto http_msg_closing;
5406 }
5407 else {
5408 txn->req.msg_state = HTTP_MSG_CLOSED;
5409 goto http_msg_closed;
5410 }
5411 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005412 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005413 }
5414
5415 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5416 http_msg_closing:
5417 /* nothing else to forward, just waiting for the output buffer
5418 * to be empty and for the shutw_now to take effect.
5419 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005420 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 txn->req.msg_state = HTTP_MSG_CLOSED;
5422 goto http_msg_closed;
5423 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005424 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005425 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 txn->req.msg_state = HTTP_MSG_ERROR;
5427 goto wait_other_side;
5428 }
5429 }
5430
5431 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5432 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005433 /* see above in MSG_DONE why we only do this in these states */
5434 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5435 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5436 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005437 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005438 goto wait_other_side;
5439 }
5440
5441 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005442 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443}
5444
5445
5446/* This function updates the response state machine according to the request
5447 * state machine and buffer flags. It returns 1 if it changes anything (flag
5448 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5449 * it is only used to find when a request/response couple is complete. Both
5450 * this function and its equivalent should loop until both return zero. It
5451 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5452 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005453int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005454{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005455 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005456 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005457 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005458 unsigned int old_state = txn->rsp.msg_state;
5459
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5461 return 0;
5462
5463 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5464 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005465 * still monitor the server connection for a possible close
5466 * while the request is being uploaded, so we don't disable
5467 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005468 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005469 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005470
5471 if (txn->req.msg_state == HTTP_MSG_ERROR)
5472 goto wait_other_side;
5473
5474 if (txn->req.msg_state < HTTP_MSG_DONE) {
5475 /* The client seems to still be sending data, probably
5476 * because we got an error response during an upload.
5477 * We have the choice of either breaking the connection
5478 * or letting it pass through. Let's do the later.
5479 */
5480 goto wait_other_side;
5481 }
5482
5483 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5484 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005485 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005486 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005487 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 goto wait_other_side;
5489 }
5490
5491 /* When we get here, it means that both the request and the
5492 * response have finished receiving. Depending on the connection
5493 * mode, we'll have to wait for the last bytes to leave in either
5494 * direction, and sometimes for a close to be effective.
5495 */
5496
5497 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5498 /* Server-close mode : shut read and wait for the request
5499 * side to close its output buffer. The caller will detect
5500 * when we're in DONE and the other is in CLOSED and will
5501 * catch that for the final cleanup.
5502 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005503 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5504 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005505 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005506 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5507 /* Option forceclose is set, or either side wants to close,
5508 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005509 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005510 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005511 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005512 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5513 channel_shutr_now(chn);
5514 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005515 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005516 }
5517 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005518 /* The last possible modes are keep-alive and tunnel. Tunnel will
5519 * need to forward remaining data. Keep-alive will need to monitor
5520 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005522 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005523 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005524 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5525 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 }
5527
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005528 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005530 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5532 goto http_msg_closing;
5533 }
5534 else {
5535 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5536 goto http_msg_closed;
5537 }
5538 }
5539 goto wait_other_side;
5540 }
5541
5542 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5543 http_msg_closing:
5544 /* nothing else to forward, just waiting for the output buffer
5545 * to be empty and for the shutw_now to take effect.
5546 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005547 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5549 goto http_msg_closed;
5550 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005551 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005552 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005553 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005554 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005555 if (objt_server(s->target))
5556 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005557 goto wait_other_side;
5558 }
5559 }
5560
5561 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5562 http_msg_closed:
5563 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005564 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005565 channel_auto_close(chn);
5566 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005567 goto wait_other_side;
5568 }
5569
5570 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005571 /* We force the response to leave immediately if we're waiting for the
5572 * other side, since there is no pending shutdown to push it out.
5573 */
5574 if (!channel_is_empty(chn))
5575 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005576 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577}
5578
5579
5580/* Resync the request and response state machines. Return 1 if either state
5581 * changes.
5582 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005583int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005584{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005585 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005586 int old_req_state = txn->req.msg_state;
5587 int old_res_state = txn->rsp.msg_state;
5588
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 http_sync_req_state(s);
5590 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005591 if (!http_sync_res_state(s))
5592 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005593 if (!http_sync_req_state(s))
5594 break;
5595 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005596
Christopher Faulet814d2702017-03-30 11:33:44 +02005597 DPRINTF(stderr,"[%u] %s: stream=%p old=%d,%d cur=%d,%d\n",
5598 now_ms, __FUNCTION__,
5599 s,
5600 old_req_state, old_res_state,
5601 txn->req.msg_state, txn->rsp.msg_state);
5602
5603
Willy Tarreau610ecce2010-01-04 21:15:02 +01005604 /* OK, both state machines agree on a compatible state.
5605 * There are a few cases we're interested in :
5606 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5607 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5608 * directions, so let's simply disable both analysers.
5609 * - HTTP_MSG_CLOSED on the response only means we must abort the
5610 * request.
5611 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5612 * with server-close mode means we've completed one request and we
5613 * must re-initialize the server connection.
5614 */
5615
5616 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5617 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5618 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5619 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005620 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005621 channel_auto_close(&s->req);
5622 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005623 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005624 channel_auto_close(&s->res);
5625 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005626 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005627 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005628 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005629 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005630 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005631 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005632 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005633 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005634 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005635 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005636 channel_auto_close(&s->res);
5637 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005638 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005639 channel_abort(&s->req);
5640 channel_auto_close(&s->req);
5641 channel_auto_read(&s->req);
5642 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005643 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005644 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5645 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005646 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005647 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5648 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5649 /* server-close/keep-alive: terminate this transaction,
5650 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005651 * a fresh-new transaction, but only once we're sure there's
5652 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02005653 * another request. They must not hold any pending output data
5654 * and the response buffer must realigned
5655 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01005656 */
Christopher Fauleta94fda32017-07-06 15:51:35 +02005657 if (s->req.buf->o) {
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005658 s->req.flags |= CF_WAKE_WRITE;
Christopher Fauleta94fda32017-07-06 15:51:35 +02005659 return 0;
5660 }
5661 else if (s->res.buf->o) {
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005662 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta94fda32017-07-06 15:51:35 +02005663 return 0;
Christopher Faulete6006242017-03-10 11:52:44 +01005664 }
Christopher Fauleta94fda32017-07-06 15:51:35 +02005665 s->req.analysers = AN_REQ_FLT_END;
5666 s->res.analysers = AN_RES_FLT_END;
5667 txn->flags |= TX_WAIT_CLEANUP;
5668 return 1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005669 }
5670
Willy Tarreau610ecce2010-01-04 21:15:02 +01005671 return txn->req.msg_state != old_req_state ||
5672 txn->rsp.msg_state != old_res_state;
5673}
5674
Willy Tarreaud98cf932009-12-27 22:54:55 +01005675/* This function is an analyser which forwards request body (including chunk
5676 * sizes if any). It is called as soon as we must forward, even if we forward
5677 * zero byte. The only situation where it must not be called is when we're in
5678 * tunnel mode and we want to forward till the close. It's used both to forward
5679 * remaining data and to resync after end of body. It expects the msg_state to
5680 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005681 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005682 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005683 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005684 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005685int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005686{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005687 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005688 struct http_txn *txn = s->txn;
5689 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005690 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005691
Christopher Faulet814d2702017-03-30 11:33:44 +02005692 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5693 now_ms, __FUNCTION__,
5694 s,
5695 req,
5696 req->rex, req->wex,
5697 req->flags,
5698 req->buf->i,
5699 req->analysers);
5700
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005701 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5702 return 0;
5703
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005704 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005705 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005706 /* Output closed while we were sending data. We must abort and
5707 * wake the other side up.
5708 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005709 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005710 msg->msg_state = HTTP_MSG_ERROR;
5711 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005712 return 1;
5713 }
5714
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 /* Note that we don't have to send 100-continue back because we don't
5716 * need the data to complete our job, and it's up to the server to
5717 * decide whether to return 100, 417 or anything else in return of
5718 * an "Expect: 100-continue" header.
5719 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005720 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005721 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5722 ? HTTP_MSG_CHUNK_SIZE
5723 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005724
5725 /* TODO/filters: when http-buffer-request option is set or if a
5726 * rule on url_param exists, the first chunk size could be
5727 * already parsed. In that case, msg->next is after the chunk
5728 * size (including the CRLF after the size). So this case should
5729 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005730 }
5731
Willy Tarreau7ba23542014-04-17 21:50:00 +02005732 /* Some post-connect processing might want us to refrain from starting to
5733 * forward data. Currently, the only reason for this is "balance url_param"
5734 * whichs need to parse/process the request after we've enabled forwarding.
5735 */
5736 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005737 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005738 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005739 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005740 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005741 }
5742 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5743 }
5744
Willy Tarreau80a92c02014-03-12 10:41:13 +01005745 /* in most states, we should abort in case of early close */
5746 channel_auto_close(req);
5747
Willy Tarreauefdf0942014-04-24 20:08:57 +02005748 if (req->to_forward) {
5749 /* We can't process the buffer's contents yet */
5750 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005751 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005752 }
5753
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005754 if (msg->msg_state < HTTP_MSG_DONE) {
5755 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5756 ? http_msg_forward_chunked_body(s, msg)
5757 : http_msg_forward_body(s, msg));
5758 if (!ret)
5759 goto missing_data_or_waiting;
5760 if (ret < 0)
5761 goto return_bad_req;
5762 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005763
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005764 /* other states, DONE...TUNNEL */
5765 /* we don't want to forward closes on DONE except in tunnel mode. */
5766 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5767 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005768
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005769 if (http_resync_states(s)) {
5770 /* some state changes occurred, maybe the analyser
5771 * was disabled too. */
5772 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 Fauletdbe34eb2015-12-02 10:01:17 +01006943 if (http_resync_states(s)) {
6944 /* some state changes occurred, maybe the analyser was disabled
6945 * too. */
6946 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6947 if (res->flags & CF_SHUTW) {
6948 /* response errors are most likely due to the
6949 * client aborting the transfer. */
6950 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006951 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006952 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006953 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006954 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006955 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006956 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006957 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006958 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006959
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006960 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006961 if (res->flags & CF_SHUTW)
6962 goto aborted_xfer;
6963
6964 /* stop waiting for data if the input is closed before the end. If the
6965 * client side was already closed, it means that the client has aborted,
6966 * so we don't want to count this as a server abort. Otherwise it's a
6967 * server abort.
6968 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006969 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006970 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006971 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006972 /* If we have some pending data, we continue the processing */
6973 if (!buffer_pending(res->buf)) {
6974 if (!(s->flags & SF_ERR_MASK))
6975 s->flags |= SF_ERR_SRVCL;
6976 s->be->be_counters.srv_aborts++;
6977 if (objt_server(s->target))
6978 objt_server(s->target)->counters.srv_aborts++;
6979 goto return_bad_res_stats_ok;
6980 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006981 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006982
Willy Tarreau40dba092010-03-04 18:14:51 +01006983 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006984 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006985 goto return_bad_res;
6986
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006987 /* When TE: chunked is used, we need to get there again to parse
6988 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006989 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6990 * or if there are filters registered on the stream, we don't want to
6991 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006992 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006993 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006994 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006995 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6996 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006997 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006998
Willy Tarreau5c620922011-05-11 19:56:11 +02006999 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007000 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007001 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007002 * modes are already handled by the stream sock layer. We must not do
7003 * this in content-length mode because it could present the MSG_MORE
7004 * flag with the last block of forwarded data, which would cause an
7005 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007006 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007007 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007008 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007009
Christopher Faulet69744d92017-03-30 10:54:35 +02007010 /* If there is neither content-length, nor transfer-encoding header
7011 * _AND_ there is no data filtering, we can safely forward all data
7012 * indefinitely. */
7013 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_DATA_FILTERS(s, res)) {
7014 buffer_flush(res->buf);
7015 channel_forward_forever(res);
7016 }
7017
Willy Tarreau87b09662015-04-03 00:22:06 +02007018 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007019 return 0;
7020
Willy Tarreau40dba092010-03-04 18:14:51 +01007021 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007022 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007023 if (objt_server(s->target))
7024 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007025
7026 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007027 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007028 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007029 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007030 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007031 res->analysers &= AN_RES_FLT_END;
7032 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 +01007033 if (objt_server(s->target))
7034 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007035
Willy Tarreaue7dff022015-04-03 01:14:29 +02007036 if (!(s->flags & SF_ERR_MASK))
7037 s->flags |= SF_ERR_PRXCOND;
7038 if (!(s->flags & SF_FINST_MASK))
7039 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007040 return 0;
7041
7042 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007043 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007044 txn->rsp.msg_state = HTTP_MSG_ERROR;
7045 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007046 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007047 res->analysers &= AN_RES_FLT_END;
7048 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 +01007049
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007050 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007051 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007052 if (objt_server(s->target))
7053 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007054
Willy Tarreaue7dff022015-04-03 01:14:29 +02007055 if (!(s->flags & SF_ERR_MASK))
7056 s->flags |= SF_ERR_CLICL;
7057 if (!(s->flags & SF_FINST_MASK))
7058 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007059 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007060}
7061
7062
7063static inline int
7064http_msg_forward_body(struct stream *s, struct http_msg *msg)
7065{
7066 struct channel *chn = msg->chn;
7067 int ret;
7068
7069 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7070
7071 if (msg->msg_state == HTTP_MSG_ENDING)
7072 goto ending;
7073
7074 /* Neither content-length, nor transfer-encoding was found, so we must
7075 * read the body until the server connection is closed. In that case, we
7076 * eat data as they come. Of course, this happens for response only. */
7077 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7078 unsigned long long len = (chn->buf->i - msg->next);
7079 msg->chunk_len += len;
7080 msg->body_len += len;
7081 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007082 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7083 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7084 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007085 msg->next += ret;
7086 msg->chunk_len -= ret;
7087 if (msg->chunk_len) {
7088 /* input empty or output full */
7089 if (chn->buf->i > msg->next)
7090 chn->flags |= CF_WAKE_WRITE;
7091 goto missing_data_or_waiting;
7092 }
7093
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007094 /* The server still sending data that should be filtered */
7095 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007096 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007097
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007098 msg->msg_state = HTTP_MSG_ENDING;
7099
7100 ending:
7101 /* we may have some pending data starting at res->buf->p such as a last
7102 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007103 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7104 /* default_ret */ msg->next,
7105 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007106 b_adv(chn->buf, ret);
7107 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007108 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7109 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007110 if (msg->next)
7111 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007112
Christopher Fauletda02e172015-12-04 09:25:05 +01007113 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7114 /* default_ret */ 1,
7115 /* on_error */ goto error,
7116 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007117 msg->msg_state = HTTP_MSG_DONE;
7118 return 1;
7119
7120 missing_data_or_waiting:
7121 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007122 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7123 /* default_ret */ msg->next,
7124 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007125 b_adv(chn->buf, ret);
7126 msg->next -= ret;
7127 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7128 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007129 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007130 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007131 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007132 return 0;
7133 error:
7134 return -1;
7135}
7136
7137static inline int
7138http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7139{
7140 struct channel *chn = msg->chn;
7141 int ret;
7142
7143 /* Here we have the guarantee to be in one of the following state:
7144 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7145 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7146
7147 switch_states:
7148 switch (msg->msg_state) {
7149 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007150 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7151 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7152 /* on_error */ goto error);
7153 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007154 msg->chunk_len -= ret;
7155 if (msg->chunk_len) {
7156 /* input empty or output full */
7157 if (chn->buf->i > msg->next)
7158 chn->flags |= CF_WAKE_WRITE;
7159 goto missing_data_or_waiting;
7160 }
7161
7162 /* nothing left to forward for this chunk*/
7163 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7164 /* fall through for HTTP_MSG_CHUNK_CRLF */
7165
7166 case HTTP_MSG_CHUNK_CRLF:
7167 /* we want the CRLF after the data */
7168 ret = http_skip_chunk_crlf(msg);
7169 if (ret == 0)
7170 goto missing_data_or_waiting;
7171 if (ret < 0)
7172 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007173 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007174 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7175 /* fall through for HTTP_MSG_CHUNK_SIZE */
7176
7177 case HTTP_MSG_CHUNK_SIZE:
7178 /* read the chunk size and assign it to ->chunk_len,
7179 * then set ->next to point to the body and switch to
7180 * DATA or TRAILERS state.
7181 */
7182 ret = http_parse_chunk_size(msg);
7183 if (ret == 0)
7184 goto missing_data_or_waiting;
7185 if (ret < 0)
7186 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007187 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007188 if (msg->chunk_len) {
7189 msg->msg_state = HTTP_MSG_DATA;
7190 goto switch_states;
7191 }
7192 msg->msg_state = HTTP_MSG_TRAILERS;
7193 /* fall through for HTTP_MSG_TRAILERS */
7194
7195 case HTTP_MSG_TRAILERS:
7196 ret = http_forward_trailers(msg);
7197 if (ret < 0)
7198 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007199 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7200 /* default_ret */ 1,
7201 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007202 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007203 if (!ret)
7204 goto missing_data_or_waiting;
7205 break;
7206
7207 case HTTP_MSG_ENDING:
7208 goto ending;
7209
7210 default:
7211 /* This should no happen in this function */
7212 goto error;
7213 }
7214
7215 msg->msg_state = HTTP_MSG_ENDING;
7216 ending:
7217 /* we may have some pending data starting at res->buf->p such as a last
7218 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007219 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007220 /* default_ret */ msg->next,
7221 /* on_error */ goto error);
7222 b_adv(chn->buf, ret);
7223 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007224 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7225 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007226 if (msg->next)
7227 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007228
Christopher Fauletda02e172015-12-04 09:25:05 +01007229 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007230 /* default_ret */ 1,
7231 /* on_error */ goto error,
7232 /* on_wait */ goto waiting);
7233 msg->msg_state = HTTP_MSG_DONE;
7234 return 1;
7235
7236 missing_data_or_waiting:
7237 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007238 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007239 /* default_ret */ msg->next,
7240 /* on_error */ goto error);
7241 b_adv(chn->buf, ret);
7242 msg->next -= ret;
7243 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7244 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007245 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007246 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007247 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007248 return 0;
7249
7250 chunk_parsing_error:
7251 if (msg->err_pos >= 0) {
7252 if (chn->flags & CF_ISRESP)
7253 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7254 msg->msg_state, strm_fe(s));
7255 else
7256 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7257 msg, msg->msg_state, s->be);
7258 }
7259 error:
7260 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007261}
7262
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264/* Iterate the same filter through all request headers.
7265 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007266 * Since it can manage the switch to another backend, it updates the per-proxy
7267 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007269int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007270{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 char *cur_ptr, *cur_end, *cur_next;
7272 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007273 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007275 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 last_hdr = 0;
7278
Willy Tarreau9b28e032012-10-12 23:49:43 +02007279 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 old_idx = 0;
7281
7282 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007285 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 (exp->action == ACT_ALLOW ||
7287 exp->action == ACT_DENY ||
7288 exp->action == ACT_TARPIT))
7289 return 0;
7290
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007291 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 if (!cur_idx)
7293 break;
7294
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007295 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 cur_ptr = cur_next;
7297 cur_end = cur_ptr + cur_hdr->len;
7298 cur_next = cur_end + cur_hdr->cr + 1;
7299
7300 /* Now we have one header between cur_ptr and cur_end,
7301 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302 */
7303
Willy Tarreau15a53a42015-01-21 13:39:42 +01007304 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007307 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 last_hdr = 1;
7309 break;
7310
7311 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007312 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 break;
7315
7316 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007317 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 break;
7320
7321 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007322 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7323 if (trash.len < 0)
7324 return -1;
7325
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007326 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 /* FIXME: if the user adds a newline in the replacement, the
7328 * index will not be recalculated for now, and the new line
7329 * will not be counted as a new header.
7330 */
7331
7332 cur_end += delta;
7333 cur_next += delta;
7334 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007335 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 break;
7337
7338 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007339 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 cur_next += delta;
7341
Willy Tarreaufa355d42009-11-29 18:12:29 +01007342 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007343 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7344 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 cur_hdr->len = 0;
7346 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007347 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348 break;
7349
7350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007351 }
7352
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353 /* keep the link from this header to next one in case of later
7354 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007355 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007356 old_idx = cur_idx;
7357 }
7358 return 0;
7359}
7360
7361
7362/* Apply the filter to the request line.
7363 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7364 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365 * Since it can manage the switch to another backend, it updates the per-proxy
7366 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007368int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007369{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 char *cur_ptr, *cur_end;
7371 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007372 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007373 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007374
Willy Tarreau3d300592007-03-18 18:34:41 +01007375 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007377 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 (exp->action == ACT_ALLOW ||
7379 exp->action == ACT_DENY ||
7380 exp->action == ACT_TARPIT))
7381 return 0;
7382 else if (exp->action == ACT_REMOVE)
7383 return 0;
7384
7385 done = 0;
7386
Willy Tarreau9b28e032012-10-12 23:49:43 +02007387 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007388 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389
7390 /* Now we have the request line between cur_ptr and cur_end */
7391
Willy Tarreau15a53a42015-01-21 13:39:42 +01007392 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007394 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007395 txn->flags |= TX_CLALLOW;
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_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007400 txn->flags |= TX_CLDENY;
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_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007405 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 done = 1;
7407 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007410 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7411 if (trash.len < 0)
7412 return -1;
7413
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007414 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415 /* FIXME: if the user adds a newline in the replacement, the
7416 * index will not be recalculated for now, and the new line
7417 * will not be counted as a new header.
7418 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007419
Willy Tarreaufa355d42009-11-29 18:12:29 +01007420 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007421 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007422 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007423 HTTP_MSG_RQMETH,
7424 cur_ptr, cur_end + 1,
7425 NULL, NULL);
7426 if (unlikely(!cur_end))
7427 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007429 /* we have a full request and we know that we have either a CR
7430 * or an LF at <ptr>.
7431 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007432 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7433 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 /* there is no point trying this regex on headers */
7435 return 1;
7436 }
7437 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007438 return done;
7439}
Willy Tarreau97de6242006-12-27 17:18:38 +01007440
Willy Tarreau58f10d72006-12-04 02:26:12 +01007441
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007443/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007444 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007445 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446 * unparsable request. Since it can manage the switch to another backend, it
7447 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007448 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007449int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007450{
Willy Tarreau192252e2015-04-04 01:47:55 +02007451 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007452 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007453 struct hdr_exp *exp;
7454
7455 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007456 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007458 /*
7459 * The interleaving of transformations and verdicts
7460 * makes it difficult to decide to continue or stop
7461 * the evaluation.
7462 */
7463
Willy Tarreau6c123b12010-01-28 20:22:06 +01007464 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7465 break;
7466
Willy Tarreau3d300592007-03-18 18:34:41 +01007467 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007468 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007469 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007470 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007471
7472 /* if this filter had a condition, evaluate it now and skip to
7473 * next filter if the condition does not match.
7474 */
7475 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007476 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007477 ret = acl_pass(ret);
7478 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7479 ret = !ret;
7480
7481 if (!ret)
7482 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007483 }
7484
7485 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007486 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007487 if (unlikely(ret < 0))
7488 return -1;
7489
7490 if (likely(ret == 0)) {
7491 /* The filter did not match the request, it can be
7492 * iterated through all headers.
7493 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007494 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7495 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007496 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007498 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007499}
7500
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007501
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007502/* Find the end of a cookie value contained between <s> and <e>. It works the
7503 * same way as with headers above except that the semi-colon also ends a token.
7504 * See RFC2965 for more information. Note that it requires a valid header to
7505 * return a valid result.
7506 */
7507char *find_cookie_value_end(char *s, const char *e)
7508{
7509 int quoted, qdpair;
7510
7511 quoted = qdpair = 0;
7512 for (; s < e; s++) {
7513 if (qdpair) qdpair = 0;
7514 else if (quoted) {
7515 if (*s == '\\') qdpair = 1;
7516 else if (*s == '"') quoted = 0;
7517 }
7518 else if (*s == '"') quoted = 1;
7519 else if (*s == ',' || *s == ';') return s;
7520 }
7521 return s;
7522}
7523
7524/* Delete a value in a header between delimiters <from> and <next> in buffer
7525 * <buf>. The number of characters displaced is returned, and the pointer to
7526 * the first delimiter is updated if required. The function tries as much as
7527 * possible to respect the following principles :
7528 * - replace <from> delimiter by the <next> one unless <from> points to a
7529 * colon, in which case <next> is simply removed
7530 * - set exactly one space character after the new first delimiter, unless
7531 * there are not enough characters in the block being moved to do so.
7532 * - remove unneeded spaces before the previous delimiter and after the new
7533 * one.
7534 *
7535 * It is the caller's responsibility to ensure that :
7536 * - <from> points to a valid delimiter or the colon ;
7537 * - <next> points to a valid delimiter or the final CR/LF ;
7538 * - there are non-space chars before <from> ;
7539 * - there is a CR/LF at or after <next>.
7540 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007541int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007542{
7543 char *prev = *from;
7544
7545 if (*prev == ':') {
7546 /* We're removing the first value, preserve the colon and add a
7547 * space if possible.
7548 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007549 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007550 next++;
7551 prev++;
7552 if (prev < next)
7553 *prev++ = ' ';
7554
Willy Tarreau2235b262016-11-05 15:50:20 +01007555 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007556 next++;
7557 } else {
7558 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007559 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 prev--;
7561 *from = prev;
7562
7563 /* copy the delimiter and if possible a space if we're
7564 * not at the end of the line.
7565 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007566 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 *prev++ = *next++;
7568 if (prev + 1 < next)
7569 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007570 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 next++;
7572 }
7573 }
7574 return buffer_replace2(buf, prev, next, NULL, 0);
7575}
7576
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007577/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007578 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007579 * desirable to call it only when needed. This code is quite complex because
7580 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7581 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007583void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007585 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007586 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007587 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007588 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7590 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007593 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007594 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007596 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007597 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007598 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007599
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007600 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 hdr_beg = hdr_next;
7602 hdr_end = hdr_beg + cur_hdr->len;
7603 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 /* We have one full header between hdr_beg and hdr_end, and the
7606 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 * "Cookie:" headers.
7608 */
7609
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007610 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007611 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007612 old_idx = cur_idx;
7613 continue;
7614 }
7615
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007616 del_from = NULL; /* nothing to be deleted */
7617 preserve_hdr = 0; /* assume we may kill the whole header */
7618
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619 /* Now look for cookies. Conforming to RFC2109, we have to support
7620 * attributes whose name begin with a '$', and associate them with
7621 * the right cookie, if we want to delete this cookie.
7622 * So there are 3 cases for each cookie read :
7623 * 1) it's a special attribute, beginning with a '$' : ignore it.
7624 * 2) it's a server id cookie that we *MAY* want to delete : save
7625 * some pointers on it (last semi-colon, beginning of cookie...)
7626 * 3) it's an application cookie : we *MAY* have to delete a previous
7627 * "special" cookie.
7628 * At the end of loop, if a "special" cookie remains, we may have to
7629 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 * *MUST* delete it.
7631 *
7632 * Note: RFC2965 is unclear about the processing of spaces around
7633 * the equal sign in the ATTR=VALUE form. A careful inspection of
7634 * the RFC explicitly allows spaces before it, and not within the
7635 * tokens (attrs or values). An inspection of RFC2109 allows that
7636 * too but section 10.1.3 lets one think that spaces may be allowed
7637 * after the equal sign too, resulting in some (rare) buggy
7638 * implementations trying to do that. So let's do what servers do.
7639 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7640 * allowed quoted strings in values, with any possible character
7641 * after a backslash, including control chars and delimitors, which
7642 * causes parsing to become ambiguous. Browsers also allow spaces
7643 * within values even without quotes.
7644 *
7645 * We have to keep multiple pointers in order to support cookie
7646 * removal at the beginning, middle or end of header without
7647 * corrupting the header. All of these headers are valid :
7648 *
7649 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7650 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7651 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7652 * | | | | | | | | |
7653 * | | | | | | | | hdr_end <--+
7654 * | | | | | | | +--> next
7655 * | | | | | | +----> val_end
7656 * | | | | | +-----------> val_beg
7657 * | | | | +--------------> equal
7658 * | | | +----------------> att_end
7659 * | | +---------------------> att_beg
7660 * | +--------------------------> prev
7661 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662 */
7663
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7665 /* Iterate through all cookies on this line */
7666
7667 /* find att_beg */
7668 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007669 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 att_beg++;
7671
7672 /* find att_end : this is the first character after the last non
7673 * space before the equal. It may be equal to hdr_end.
7674 */
7675 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007676
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 while (equal < hdr_end) {
7678 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007680 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 continue;
7682 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007683 }
7684
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7686 * is between <att_beg> and <equal>, both may be identical.
7687 */
7688
7689 /* look for end of cookie if there is an equal sign */
7690 if (equal < hdr_end && *equal == '=') {
7691 /* look for the beginning of the value */
7692 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007693 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 val_beg++;
7695
7696 /* find the end of the value, respecting quotes */
7697 next = find_cookie_value_end(val_beg, hdr_end);
7698
7699 /* make val_end point to the first white space or delimitor after the value */
7700 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007701 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 val_end--;
7703 } else {
7704 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007705 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 /* We have nothing to do with attributes beginning with '$'. However,
7708 * they will automatically be removed if a header before them is removed,
7709 * since they're supposed to be linked together.
7710 */
7711 if (*att_beg == '$')
7712 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007713
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 /* Ignore cookies with no equal sign */
7715 if (equal == next) {
7716 /* This is not our cookie, so we must preserve it. But if we already
7717 * scheduled another cookie for removal, we cannot remove the
7718 * complete header, but we can remove the previous block itself.
7719 */
7720 preserve_hdr = 1;
7721 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007722 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 val_end += delta;
7724 next += delta;
7725 hdr_end += delta;
7726 hdr_next += delta;
7727 cur_hdr->len += delta;
7728 http_msg_move_end(&txn->req, delta);
7729 prev = del_from;
7730 del_from = NULL;
7731 }
7732 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007733 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 /* if there are spaces around the equal sign, we need to
7736 * strip them otherwise we'll get trouble for cookie captures,
7737 * or even for rewrites. Since this happens extremely rarely,
7738 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007739 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007740 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7741 int stripped_before = 0;
7742 int stripped_after = 0;
7743
7744 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007745 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 equal += stripped_before;
7747 val_beg += stripped_before;
7748 }
7749
7750 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007751 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007752 val_beg += stripped_after;
7753 stripped_before += stripped_after;
7754 }
7755
7756 val_end += stripped_before;
7757 next += stripped_before;
7758 hdr_end += stripped_before;
7759 hdr_next += stripped_before;
7760 cur_hdr->len += stripped_before;
7761 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 /* First, let's see if we want to capture this cookie. We check
7766 * that we don't already have a client side cookie, because we
7767 * can only capture one. Also as an optimisation, we ignore
7768 * cookies shorter than the declared name.
7769 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007770 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7771 (val_end - att_beg >= sess->fe->capture_namelen) &&
7772 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 int log_len = val_end - att_beg;
7774
7775 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7776 Alert("HTTP logging : out of memory.\n");
7777 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007778 if (log_len > sess->fe->capture_len)
7779 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 memcpy(txn->cli_cookie, att_beg, log_len);
7781 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007782 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007784
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 /* Persistence cookies in passive, rewrite or insert mode have the
7786 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007788 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007789 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007790 * For cookies in prefix mode, the form is :
7791 *
7792 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007794 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7795 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7796 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007797 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7800 * have the server ID between val_beg and delim, and the original cookie between
7801 * delim+1 and val_end. Otherwise, delim==val_end :
7802 *
7803 * Cookie: NAME=SRV; # in all but prefix modes
7804 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7805 * | || || | |+-> next
7806 * | || || | +--> val_end
7807 * | || || +---------> delim
7808 * | || |+------------> val_beg
7809 * | || +-------------> att_end = equal
7810 * | |+-----------------> att_beg
7811 * | +------------------> prev
7812 * +-------------------------> hdr_beg
7813 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007814
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007815 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007816 for (delim = val_beg; delim < val_end; delim++)
7817 if (*delim == COOKIE_DELIM)
7818 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007819 } else {
7820 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007821 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007822 /* Now check if the cookie contains a date field, which would
7823 * appear after a vertical bar ('|') just after the server name
7824 * and before the delimiter.
7825 */
7826 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7827 if (vbar1) {
7828 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007829 * right is the last seen date. It is a base64 encoded
7830 * 30-bit value representing the UNIX date since the
7831 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007832 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007833 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007834 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007835 if (val_end - vbar1 >= 5) {
7836 val = b64tos30(vbar1);
7837 if (val > 0)
7838 txn->cookie_last_date = val << 2;
7839 }
7840 /* look for a second vertical bar */
7841 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7842 if (vbar1 && (val_end - vbar1 > 5)) {
7843 val = b64tos30(vbar1 + 1);
7844 if (val > 0)
7845 txn->cookie_first_date = val << 2;
7846 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007847 }
7848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007849
Willy Tarreauf64d1412010-10-07 20:06:11 +02007850 /* if the cookie has an expiration date and the proxy wants to check
7851 * it, then we do that now. We first check if the cookie is too old,
7852 * then only if it has expired. We detect strict overflow because the
7853 * time resolution here is not great (4 seconds). Cookies with dates
7854 * in the future are ignored if their offset is beyond one day. This
7855 * allows an admin to fix timezone issues without expiring everyone
7856 * and at the same time avoids keeping unwanted side effects for too
7857 * long.
7858 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007859 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7860 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007861 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007862 txn->flags &= ~TX_CK_MASK;
7863 txn->flags |= TX_CK_OLD;
7864 delim = val_beg; // let's pretend we have not found the cookie
7865 txn->cookie_first_date = 0;
7866 txn->cookie_last_date = 0;
7867 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007868 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7869 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007870 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007871 txn->flags &= ~TX_CK_MASK;
7872 txn->flags |= TX_CK_EXPIRED;
7873 delim = val_beg; // let's pretend we have not found the cookie
7874 txn->cookie_first_date = 0;
7875 txn->cookie_last_date = 0;
7876 }
7877
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007878 /* Here, we'll look for the first running server which supports the cookie.
7879 * This allows to share a same cookie between several servers, for example
7880 * to dedicate backup servers to specific servers only.
7881 * However, to prevent clients from sticking to cookie-less backup server
7882 * when they have incidentely learned an empty cookie, we simply ignore
7883 * empty cookies and mark them as invalid.
7884 * The same behaviour is applied when persistence must be ignored.
7885 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007886 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007887 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007889 while (srv) {
7890 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7891 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007892 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007893 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007894 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007895 /* we found the server and we can use it */
7896 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007897 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007898 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007899 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 break;
7901 } else {
7902 /* we found a server, but it's down,
7903 * mark it as such and go on in case
7904 * another one is available.
7905 */
7906 txn->flags &= ~TX_CK_MASK;
7907 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007910 srv = srv->next;
7911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912
Willy Tarreauf64d1412010-10-07 20:06:11 +02007913 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007914 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007916 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007917 txn->flags |= TX_CK_UNUSED;
7918 else
7919 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007922 /* depending on the cookie mode, we may have to either :
7923 * - delete the complete cookie if we're in insert+indirect mode, so that
7924 * the server never sees it ;
7925 * - remove the server id from the cookie value, and tag the cookie as an
7926 * application cookie so that it does not get accidentely removed later,
7927 * if we're in cookie prefix mode
7928 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007929 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007931
Willy Tarreau9b28e032012-10-12 23:49:43 +02007932 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007933 val_end += delta;
7934 next += delta;
7935 hdr_end += delta;
7936 hdr_next += delta;
7937 cur_hdr->len += delta;
7938 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007939
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007940 del_from = NULL;
7941 preserve_hdr = 1; /* we want to keep this cookie */
7942 }
7943 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007944 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007945 del_from = prev;
7946 }
7947 } else {
7948 /* This is not our cookie, so we must preserve it. But if we already
7949 * scheduled another cookie for removal, we cannot remove the
7950 * complete header, but we can remove the previous block itself.
7951 */
7952 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007954 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007955 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007956 if (att_beg >= del_from)
7957 att_beg += delta;
7958 if (att_end >= del_from)
7959 att_end += delta;
7960 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007961 val_end += delta;
7962 next += delta;
7963 hdr_end += delta;
7964 hdr_next += delta;
7965 cur_hdr->len += delta;
7966 http_msg_move_end(&txn->req, delta);
7967 prev = del_from;
7968 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007972 /* continue with next cookie on this header line */
7973 att_beg = next;
7974 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007976 /* There are no more cookies on this line.
7977 * We may still have one (or several) marked for deletion at the
7978 * end of the line. We must do this now in two ways :
7979 * - if some cookies must be preserved, we only delete from the
7980 * mark to the end of line ;
7981 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007983 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007984 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007985 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007986 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007987 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988 cur_hdr->len += delta;
7989 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007990 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007991
7992 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007993 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7994 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007995 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007996 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007998 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007999 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008000 }
8001
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008002 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01008003 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008004 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008005}
8006
8007
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008/* Iterate the same filter through all response headers contained in <rtr>.
8009 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8010 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008011int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 char *cur_ptr, *cur_end, *cur_next;
8014 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008015 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008017 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018
8019 last_hdr = 0;
8020
Willy Tarreau9b28e032012-10-12 23:49:43 +02008021 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 old_idx = 0;
8023
8024 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008025 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008027 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 (exp->action == ACT_ALLOW ||
8029 exp->action == ACT_DENY))
8030 return 0;
8031
8032 cur_idx = txn->hdr_idx.v[old_idx].next;
8033 if (!cur_idx)
8034 break;
8035
8036 cur_hdr = &txn->hdr_idx.v[cur_idx];
8037 cur_ptr = cur_next;
8038 cur_end = cur_ptr + cur_hdr->len;
8039 cur_next = cur_end + cur_hdr->cr + 1;
8040
8041 /* Now we have one header between cur_ptr and cur_end,
8042 * and the next header starts at cur_next.
8043 */
8044
Willy Tarreau15a53a42015-01-21 13:39:42 +01008045 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 switch (exp->action) {
8047 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008048 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 last_hdr = 1;
8050 break;
8051
8052 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008053 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054 last_hdr = 1;
8055 break;
8056
8057 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008058 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8059 if (trash.len < 0)
8060 return -1;
8061
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008062 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 /* FIXME: if the user adds a newline in the replacement, the
8064 * index will not be recalculated for now, and the new line
8065 * will not be counted as a new header.
8066 */
8067
8068 cur_end += delta;
8069 cur_next += delta;
8070 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008071 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072 break;
8073
8074 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008075 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 cur_next += delta;
8077
Willy Tarreaufa355d42009-11-29 18:12:29 +01008078 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8080 txn->hdr_idx.used--;
8081 cur_hdr->len = 0;
8082 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008083 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008084 break;
8085
8086 }
8087 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088
8089 /* keep the link from this header to next one in case of later
8090 * removal of next header.
8091 */
8092 old_idx = cur_idx;
8093 }
8094 return 0;
8095}
8096
8097
8098/* Apply the filter to the status line in the response buffer <rtr>.
8099 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8100 * or -1 if a replacement resulted in an invalid status line.
8101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008102int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104 char *cur_ptr, *cur_end;
8105 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008106 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008107 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108
8109
Willy Tarreau3d300592007-03-18 18:34:41 +01008110 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008112 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 (exp->action == ACT_ALLOW ||
8114 exp->action == ACT_DENY))
8115 return 0;
8116 else if (exp->action == ACT_REMOVE)
8117 return 0;
8118
8119 done = 0;
8120
Willy Tarreau9b28e032012-10-12 23:49:43 +02008121 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008122 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123
8124 /* Now we have the status line between cur_ptr and cur_end */
8125
Willy Tarreau15a53a42015-01-21 13:39:42 +01008126 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008127 switch (exp->action) {
8128 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008129 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130 done = 1;
8131 break;
8132
8133 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008134 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 done = 1;
8136 break;
8137
8138 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008139 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8140 if (trash.len < 0)
8141 return -1;
8142
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008143 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 /* FIXME: if the user adds a newline in the replacement, the
8145 * index will not be recalculated for now, and the new line
8146 * will not be counted as a new header.
8147 */
8148
Willy Tarreaufa355d42009-11-29 18:12:29 +01008149 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008151 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008152 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153 cur_ptr, cur_end + 1,
8154 NULL, NULL);
8155 if (unlikely(!cur_end))
8156 return -1;
8157
8158 /* we have a full respnse and we know that we have either a CR
8159 * or an LF at <ptr>.
8160 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008161 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008162 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163 /* there is no point trying this regex on headers */
8164 return 1;
8165 }
8166 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167 return done;
8168}
8169
8170
8171
8172/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008173 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008174 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8175 * unparsable response.
8176 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008177int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008178{
Willy Tarreau192252e2015-04-04 01:47:55 +02008179 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008180 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008181 struct hdr_exp *exp;
8182
8183 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184 int ret;
8185
8186 /*
8187 * The interleaving of transformations and verdicts
8188 * makes it difficult to decide to continue or stop
8189 * the evaluation.
8190 */
8191
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008192 if (txn->flags & TX_SVDENY)
8193 break;
8194
Willy Tarreau3d300592007-03-18 18:34:41 +01008195 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008196 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8197 exp->action == ACT_PASS)) {
8198 exp = exp->next;
8199 continue;
8200 }
8201
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008202 /* if this filter had a condition, evaluate it now and skip to
8203 * next filter if the condition does not match.
8204 */
8205 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008206 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008207 ret = acl_pass(ret);
8208 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8209 ret = !ret;
8210 if (!ret)
8211 continue;
8212 }
8213
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008215 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 if (unlikely(ret < 0))
8217 return -1;
8218
8219 if (likely(ret == 0)) {
8220 /* The filter did not match the response, it can be
8221 * iterated through all headers.
8222 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008223 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8224 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 }
8227 return 0;
8228}
8229
8230
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008232 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 * desirable to call it only when needed. This function is also used when we
8234 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008235 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008236void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008238 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008239 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008240 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008241 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008243 char *hdr_beg, *hdr_end, *hdr_next;
8244 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246 /* Iterate through the headers.
8247 * we start with the start line.
8248 */
8249 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008250 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251
8252 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8253 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008254 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255
8256 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 hdr_beg = hdr_next;
8258 hdr_end = hdr_beg + cur_hdr->len;
8259 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 /* We have one full header between hdr_beg and hdr_end, and the
8262 * next header starts at hdr_next. We're only interested in
8263 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264 */
8265
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 is_cookie2 = 0;
8267 prev = hdr_beg + 10;
8268 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008269 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008270 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8271 if (!val) {
8272 old_idx = cur_idx;
8273 continue;
8274 }
8275 is_cookie2 = 1;
8276 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277 }
8278
Willy Tarreau24581ba2010-08-31 22:39:35 +02008279 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8280 * <prev> points to the colon.
8281 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008282 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283
Willy Tarreau24581ba2010-08-31 22:39:35 +02008284 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8285 * check-cache is enabled) and we are not interested in checking
8286 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008287 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008288 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008289 return;
8290
Willy Tarreau24581ba2010-08-31 22:39:35 +02008291 /* OK so now we know we have to process this response cookie.
8292 * The format of the Set-Cookie header is slightly different
8293 * from the format of the Cookie header in that it does not
8294 * support the comma as a cookie delimiter (thus the header
8295 * cannot be folded) because the Expires attribute described in
8296 * the original Netscape's spec may contain an unquoted date
8297 * with a comma inside. We have to live with this because
8298 * many browsers don't support Max-Age and some browsers don't
8299 * support quoted strings. However the Set-Cookie2 header is
8300 * clean.
8301 *
8302 * We have to keep multiple pointers in order to support cookie
8303 * removal at the beginning, middle or end of header without
8304 * corrupting the header (in case of set-cookie2). A special
8305 * pointer, <scav> points to the beginning of the set-cookie-av
8306 * fields after the first semi-colon. The <next> pointer points
8307 * either to the end of line (set-cookie) or next unquoted comma
8308 * (set-cookie2). All of these headers are valid :
8309 *
8310 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8311 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8312 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8313 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8314 * | | | | | | | | | |
8315 * | | | | | | | | +-> next hdr_end <--+
8316 * | | | | | | | +------------> scav
8317 * | | | | | | +--------------> val_end
8318 * | | | | | +--------------------> val_beg
8319 * | | | | +----------------------> equal
8320 * | | | +------------------------> att_end
8321 * | | +----------------------------> att_beg
8322 * | +------------------------------> prev
8323 * +-----------------------------------------> hdr_beg
8324 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 for (; prev < hdr_end; prev = next) {
8327 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328
Willy Tarreau24581ba2010-08-31 22:39:35 +02008329 /* find att_beg */
8330 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008331 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008332 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 /* find att_end : this is the first character after the last non
8335 * space before the equal. It may be equal to hdr_end.
8336 */
8337 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008338
Willy Tarreau24581ba2010-08-31 22:39:35 +02008339 while (equal < hdr_end) {
8340 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8341 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008342 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008343 continue;
8344 att_end = equal;
8345 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008346
Willy Tarreau24581ba2010-08-31 22:39:35 +02008347 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8348 * is between <att_beg> and <equal>, both may be identical.
8349 */
8350
8351 /* look for end of cookie if there is an equal sign */
8352 if (equal < hdr_end && *equal == '=') {
8353 /* look for the beginning of the value */
8354 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008355 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008356 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008357
Willy Tarreau24581ba2010-08-31 22:39:35 +02008358 /* find the end of the value, respecting quotes */
8359 next = find_cookie_value_end(val_beg, hdr_end);
8360
8361 /* make val_end point to the first white space or delimitor after the value */
8362 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008363 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008364 val_end--;
8365 } else {
8366 /* <equal> points to next comma, semi-colon or EOL */
8367 val_beg = val_end = next = equal;
8368 }
8369
8370 if (next < hdr_end) {
8371 /* Set-Cookie2 supports multiple cookies, and <next> points to
8372 * a colon or semi-colon before the end. So skip all attr-value
8373 * pairs and look for the next comma. For Set-Cookie, since
8374 * commas are permitted in values, skip to the end.
8375 */
8376 if (is_cookie2)
8377 next = find_hdr_value_end(next, hdr_end);
8378 else
8379 next = hdr_end;
8380 }
8381
8382 /* Now everything is as on the diagram above */
8383
8384 /* Ignore cookies with no equal sign */
8385 if (equal == val_end)
8386 continue;
8387
8388 /* If there are spaces around the equal sign, we need to
8389 * strip them otherwise we'll get trouble for cookie captures,
8390 * or even for rewrites. Since this happens extremely rarely,
8391 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008393 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8394 int stripped_before = 0;
8395 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008396
Willy Tarreau24581ba2010-08-31 22:39:35 +02008397 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008398 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008399 equal += stripped_before;
8400 val_beg += stripped_before;
8401 }
8402
8403 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008404 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008405 val_beg += stripped_after;
8406 stripped_before += stripped_after;
8407 }
8408
8409 val_end += stripped_before;
8410 next += stripped_before;
8411 hdr_end += stripped_before;
8412 hdr_next += stripped_before;
8413 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008414 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008415 }
8416
8417 /* First, let's see if we want to capture this cookie. We check
8418 * that we don't already have a server side cookie, because we
8419 * can only capture one. Also as an optimisation, we ignore
8420 * cookies shorter than the declared name.
8421 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008422 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008423 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008424 (val_end - att_beg >= sess->fe->capture_namelen) &&
8425 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008426 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008427 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008428 Alert("HTTP logging : out of memory.\n");
8429 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008430 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008431 if (log_len > sess->fe->capture_len)
8432 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008433 memcpy(txn->srv_cookie, att_beg, log_len);
8434 txn->srv_cookie[log_len] = 0;
8435 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 }
8437
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008438 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008439 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008440 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008441 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8442 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008443 /* assume passive cookie by default */
8444 txn->flags &= ~TX_SCK_MASK;
8445 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446
8447 /* If the cookie is in insert mode on a known server, we'll delete
8448 * this occurrence because we'll insert another one later.
8449 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008450 * a direct access.
8451 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008452 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008453 /* The "preserve" flag was set, we don't want to touch the
8454 * server's cookie.
8455 */
8456 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008457 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008458 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008459 /* this cookie must be deleted */
8460 if (*prev == ':' && next == hdr_end) {
8461 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008462 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008463 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8464 txn->hdr_idx.used--;
8465 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008466 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008467 hdr_next += delta;
8468 http_msg_move_end(&txn->rsp, delta);
8469 /* note: while both invalid now, <next> and <hdr_end>
8470 * are still equal, so the for() will stop as expected.
8471 */
8472 } else {
8473 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008474 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008475 next = prev;
8476 hdr_end += delta;
8477 hdr_next += delta;
8478 cur_hdr->len += delta;
8479 http_msg_move_end(&txn->rsp, delta);
8480 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008481 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008482 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008483 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008485 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008486 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 * with this server since we know it.
8488 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008489 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008490 next += delta;
8491 hdr_end += delta;
8492 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008494 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495
Willy Tarreauf1348312010-10-07 15:54:11 +02008496 txn->flags &= ~TX_SCK_MASK;
8497 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008499 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008501 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008502 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008503 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008504 next += delta;
8505 hdr_end += delta;
8506 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008508 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509
Willy Tarreau827aee92011-03-10 16:55:02 +01008510 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008511 txn->flags &= ~TX_SCK_MASK;
8512 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008513 }
8514 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008515 /* that's done for this cookie, check the next one on the same
8516 * line when next != hdr_end (only if is_cookie2).
8517 */
8518 }
8519 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008521 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522}
8523
8524
Willy Tarreaua15645d2007-03-18 16:22:39 +01008525/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008526 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008527 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008528void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008529{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008530 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531 char *p1, *p2;
8532
8533 char *cur_ptr, *cur_end, *cur_next;
8534 int cur_idx;
8535
Willy Tarreau5df51872007-11-25 16:20:08 +01008536 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008537 return;
8538
8539 /* Iterate through the headers.
8540 * we start with the start line.
8541 */
8542 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008543 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008544
8545 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8546 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008547 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008548
8549 cur_hdr = &txn->hdr_idx.v[cur_idx];
8550 cur_ptr = cur_next;
8551 cur_end = cur_ptr + cur_hdr->len;
8552 cur_next = cur_end + cur_hdr->cr + 1;
8553
8554 /* We have one full header between cur_ptr and cur_end, and the
8555 * next header starts at cur_next. We're only interested in
8556 * "Cookie:" headers.
8557 */
8558
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008559 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8560 if (val) {
8561 if ((cur_end - (cur_ptr + val) >= 8) &&
8562 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8563 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8564 return;
8565 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008566 }
8567
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008568 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8569 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008570 continue;
8571
8572 /* OK, right now we know we have a cache-control header at cur_ptr */
8573
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008574 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008575
8576 if (p1 >= cur_end) /* no more info */
8577 continue;
8578
8579 /* p1 is at the beginning of the value */
8580 p2 = p1;
8581
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008582 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008583 p2++;
8584
8585 /* we have a complete value between p1 and p2 */
8586 if (p2 < cur_end && *p2 == '=') {
8587 /* we have something of the form no-cache="set-cookie" */
8588 if ((cur_end - p1 >= 21) &&
8589 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8590 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008591 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008592 continue;
8593 }
8594
8595 /* OK, so we know that either p2 points to the end of string or to a comma */
8596 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008597 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008598 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8599 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8600 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008601 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008602 return;
8603 }
8604
8605 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008606 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008607 continue;
8608 }
8609 }
8610}
8611
Willy Tarreau58f10d72006-12-04 02:26:12 +01008612
Willy Tarreaub2513902006-12-17 14:52:38 +01008613/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008614 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008615 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008616 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008617 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008618 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008619 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008620 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008621 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008622int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008623{
8624 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008625 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008626 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008627
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008628 if (!uri_auth)
8629 return 0;
8630
Cyril Bonté70be45d2010-10-12 00:14:35 +02008631 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008632 return 0;
8633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008634 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008635 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008636 return 0;
8637
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008638 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008639 return 0;
8640
Willy Tarreaub2513902006-12-17 14:52:38 +01008641 return 1;
8642}
8643
Willy Tarreau4076a152009-04-02 15:18:36 +02008644/*
8645 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008646 * By default it tries to report the error position as msg->err_pos. However if
8647 * this one is not set, it will then report msg->next, which is the last known
8648 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008649 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008650 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008651void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008652 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008653 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008654{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008655 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008656 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008657 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008658
Willy Tarreauf3764b72016-03-31 13:45:10 +02008659 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008660 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008661 len1 = MIN(len1, es->len);
8662 len2 = es->len - len1; /* remaining data if buffer wraps */
8663
Willy Tarreauf3764b72016-03-31 13:45:10 +02008664 if (!es->buf)
8665 es->buf = malloc(global.tune.bufsize);
8666
8667 if (es->buf) {
8668 memcpy(es->buf, chn->buf->p, len1);
8669 if (len2)
8670 memcpy(es->buf + len1, chn->buf->data, len2);
8671 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008672
Willy Tarreau4076a152009-04-02 15:18:36 +02008673 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008674 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008675 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008676 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008677
Willy Tarreau4076a152009-04-02 15:18:36 +02008678 es->when = date; // user-visible date
8679 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008680 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008681 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008682 if (objt_conn(sess->origin))
8683 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008684 else
8685 memset(&es->src, 0, sizeof(es->src));
8686
Willy Tarreau078272e2010-12-12 12:46:33 +01008687 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008688 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008689 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008690 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008691 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008692 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008693 es->b_out = chn->buf->o;
8694 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008695 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008696 es->m_clen = msg->chunk_len;
8697 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008698}
Willy Tarreaub2513902006-12-17 14:52:38 +01008699
Willy Tarreau294c4732011-12-16 21:35:50 +01008700/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8701 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8702 * performed over the whole headers. Otherwise it must contain a valid header
8703 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8704 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8705 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8706 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008707 * -1. The value fetch stops at commas, so this function is suited for use with
8708 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008710 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008711unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008712 struct hdr_idx *idx, int occ,
8713 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008714{
Willy Tarreau294c4732011-12-16 21:35:50 +01008715 struct hdr_ctx local_ctx;
8716 char *ptr_hist[MAX_HDR_HISTORY];
8717 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008718 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008719 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008720
Willy Tarreau294c4732011-12-16 21:35:50 +01008721 if (!ctx) {
8722 local_ctx.idx = 0;
8723 ctx = &local_ctx;
8724 }
8725
Willy Tarreaubce70882009-09-07 11:51:47 +02008726 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008727 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008728 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008729 occ--;
8730 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008731 *vptr = ctx->line + ctx->val;
8732 *vlen = ctx->vlen;
8733 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008734 }
8735 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008736 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008737 }
8738
8739 /* negative occurrence, we scan all the list then walk back */
8740 if (-occ > MAX_HDR_HISTORY)
8741 return 0;
8742
Willy Tarreau294c4732011-12-16 21:35:50 +01008743 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008744 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008745 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8746 len_hist[hist_ptr] = ctx->vlen;
8747 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008748 hist_ptr = 0;
8749 found++;
8750 }
8751 if (-occ > found)
8752 return 0;
8753 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008754 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8755 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8756 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008757 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008758 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008759 if (hist_ptr >= MAX_HDR_HISTORY)
8760 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008761 *vptr = ptr_hist[hist_ptr];
8762 *vlen = len_hist[hist_ptr];
8763 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008764}
8765
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008766/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8767 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8768 * performed over the whole headers. Otherwise it must contain a valid header
8769 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8770 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8771 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8772 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8773 * -1. This function differs from http_get_hdr() in that it only returns full
8774 * line header values and does not stop at commas.
8775 * The return value is 0 if nothing was found, or non-zero otherwise.
8776 */
8777unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8778 struct hdr_idx *idx, int occ,
8779 struct hdr_ctx *ctx, char **vptr, int *vlen)
8780{
8781 struct hdr_ctx local_ctx;
8782 char *ptr_hist[MAX_HDR_HISTORY];
8783 int len_hist[MAX_HDR_HISTORY];
8784 unsigned int hist_ptr;
8785 int found;
8786
8787 if (!ctx) {
8788 local_ctx.idx = 0;
8789 ctx = &local_ctx;
8790 }
8791
8792 if (occ >= 0) {
8793 /* search from the beginning */
8794 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8795 occ--;
8796 if (occ <= 0) {
8797 *vptr = ctx->line + ctx->val;
8798 *vlen = ctx->vlen;
8799 return 1;
8800 }
8801 }
8802 return 0;
8803 }
8804
8805 /* negative occurrence, we scan all the list then walk back */
8806 if (-occ > MAX_HDR_HISTORY)
8807 return 0;
8808
8809 found = hist_ptr = 0;
8810 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8811 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8812 len_hist[hist_ptr] = ctx->vlen;
8813 if (++hist_ptr >= MAX_HDR_HISTORY)
8814 hist_ptr = 0;
8815 found++;
8816 }
8817 if (-occ > found)
8818 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008819
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008820 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008821 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8822 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8823 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008824 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008825 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008826 if (hist_ptr >= MAX_HDR_HISTORY)
8827 hist_ptr -= MAX_HDR_HISTORY;
8828 *vptr = ptr_hist[hist_ptr];
8829 *vlen = len_hist[hist_ptr];
8830 return 1;
8831}
8832
Willy Tarreaubaaee002006-06-26 02:48:02 +02008833/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008834 * Print a debug line with a header. Always stop at the first CR or LF char,
8835 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8836 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008837 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008838void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008839{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008840 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008841 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008842
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008843 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008844 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008845 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008846 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 +02008847
8848 for (max = 0; start + max < end; max++)
8849 if (start[max] == '\r' || start[max] == '\n')
8850 break;
8851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008852 UBOUND(max, trash.size - trash.len - 3);
8853 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8854 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008855 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008856}
8857
Willy Tarreaueee5b512015-04-03 23:46:31 +02008858
8859/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8860 * The hdr_idx is allocated as well. In case of allocation failure, everything
8861 * allocated is freed and NULL is returned. Otherwise the new transaction is
8862 * assigned to the stream and returned.
8863 */
8864struct http_txn *http_alloc_txn(struct stream *s)
8865{
8866 struct http_txn *txn = s->txn;
8867
8868 if (txn)
8869 return txn;
8870
8871 txn = pool_alloc2(pool2_http_txn);
8872 if (!txn)
8873 return txn;
8874
8875 txn->hdr_idx.size = global.tune.max_http_hdr;
8876 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8877 if (!txn->hdr_idx.v) {
8878 pool_free2(pool2_http_txn, txn);
8879 return NULL;
8880 }
8881
8882 s->txn = txn;
8883 return txn;
8884}
8885
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008886void http_txn_reset_req(struct http_txn *txn)
8887{
8888 txn->req.flags = 0;
8889 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8890 txn->req.next = 0;
8891 txn->req.chunk_len = 0LL;
8892 txn->req.body_len = 0LL;
8893 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8894}
8895
8896void http_txn_reset_res(struct http_txn *txn)
8897{
8898 txn->rsp.flags = 0;
8899 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8900 txn->rsp.next = 0;
8901 txn->rsp.chunk_len = 0LL;
8902 txn->rsp.body_len = 0LL;
8903 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8904}
8905
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008907 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008908 * the required fields are properly allocated and that we only need to (re)init
8909 * them. This should be used before processing any new request.
8910 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008911void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008912{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008913 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008914 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915
8916 txn->flags = 0;
8917 txn->status = -1;
8918
Willy Tarreauf64d1412010-10-07 20:06:11 +02008919 txn->cookie_first_date = 0;
8920 txn->cookie_last_date = 0;
8921
Willy Tarreaueee5b512015-04-03 23:46:31 +02008922 txn->srv_cookie = NULL;
8923 txn->cli_cookie = NULL;
8924 txn->uri = NULL;
8925
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008926 http_txn_reset_req(txn);
8927 http_txn_reset_res(txn);
8928
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008929 txn->req.chn = &s->req;
8930 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008931
8932 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008933
8934 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8935 if (fe->options2 & PR_O2_REQBUG_OK)
8936 txn->req.err_pos = -1; /* let buggy requests pass */
8937
Willy Tarreau0937bc42009-12-22 15:03:09 +01008938 if (txn->hdr_idx.v)
8939 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008940
8941 vars_init(&s->vars_txn, SCOPE_TXN);
8942 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008943}
8944
8945/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008946void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008948 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008949 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008950
8951 /* these ones will have been dynamically allocated */
8952 pool_free2(pool2_requri, txn->uri);
8953 pool_free2(pool2_capture, txn->cli_cookie);
8954 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008955 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008956
William Lallemanda73203e2012-03-12 12:48:57 +01008957 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008958 txn->uri = NULL;
8959 txn->srv_cookie = NULL;
8960 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008961
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008962 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008963 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008964 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008965 pool_free2(h->pool, s->req_cap[h->index]);
8966 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008967 }
8968
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008969 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008970 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008971 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008972 pool_free2(h->pool, s->res_cap[h->index]);
8973 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008974 }
8975
Willy Tarreau6204cd92016-03-10 16:33:04 +01008976 vars_prune(&s->vars_txn, s->sess, s);
8977 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008978}
8979
8980/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008981void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008982{
8983 http_end_txn(s);
8984 http_init_txn(s);
8985
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008986 /* reinitialise the current rule list pointer to NULL. We are sure that
8987 * any rulelist match the NULL pointer.
8988 */
8989 s->current_rule_list = NULL;
8990
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008991 s->be = strm_fe(s);
8992 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008993 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008994 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008995 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008996 /* re-init store persistence */
8997 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008998 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008999
Willy Tarreau0937bc42009-12-22 15:03:09 +01009000 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009001
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009002 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009003
Willy Tarreau739cfba2010-01-25 23:11:14 +01009004 /* We must trim any excess data from the response buffer, because we
9005 * may have blocked an invalid response from a server that we don't
9006 * want to accidentely forward once we disable the analysers, nor do
9007 * we want those data to come along with next response. A typical
9008 * example of such data would be from a buggy server responding to
9009 * a HEAD with some data, or sending more than the advertised
9010 * content-length.
9011 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009012 if (unlikely(s->res.buf->i))
9013 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009014
Christopher Fauletc0c672a2017-03-28 11:51:33 +02009015 /* Now we can realign the response buffer */
9016 buffer_realign(s->res.buf);
9017
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009018 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009019 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009020
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009021 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009022 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009023
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009024 s->req.rex = TICK_ETERNITY;
9025 s->req.wex = TICK_ETERNITY;
9026 s->req.analyse_exp = TICK_ETERNITY;
9027 s->res.rex = TICK_ETERNITY;
9028 s->res.wex = TICK_ETERNITY;
9029 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01009030 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009031}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009032
Sasha Pachev218f0642014-06-16 12:05:59 -06009033void free_http_res_rules(struct list *r)
9034{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009035 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009036
9037 list_for_each_entry_safe(pr, tr, r, list) {
9038 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009039 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009040 free(pr);
9041 }
9042}
9043
9044void free_http_req_rules(struct list *r)
9045{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009046 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009047
9048 list_for_each_entry_safe(pr, tr, r, list) {
9049 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009050 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009051 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009052
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009053 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009054 free(pr);
9055 }
9056}
9057
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009058/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009059struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009060{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009061 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009062 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009063 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009064 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009065
Vincent Bernat02779b62016-04-03 13:48:43 +02009066 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009067 if (!rule) {
9068 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009069 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009070 }
9071
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009072 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009073 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009074 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009075 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009076 int code;
9077 int hc;
9078
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009079 if (!strcmp(args[0], "tarpit")) {
9080 rule->action = ACT_HTTP_REQ_TARPIT;
9081 rule->deny_status = HTTP_ERR_500;
9082 }
9083 else {
9084 rule->action = ACT_ACTION_DENY;
9085 rule->deny_status = HTTP_ERR_403;
9086 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009087 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009088 if (strcmp(args[cur_arg], "deny_status") == 0) {
9089 cur_arg++;
9090 if (!args[cur_arg]) {
9091 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9092 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9093 goto out_err;
9094 }
9095
9096 code = atol(args[cur_arg]);
9097 cur_arg++;
9098 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9099 if (http_err_codes[hc] == code) {
9100 rule->deny_status = hc;
9101 break;
9102 }
9103 }
9104
9105 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009106 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9107 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009108 }
9109 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009110 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009111 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009112 cur_arg = 1;
9113
9114 while(*args[cur_arg]) {
9115 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009116 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009117 cur_arg+=2;
9118 continue;
9119 } else
9120 break;
9121 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009122 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009123 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009124 cur_arg = 1;
9125
9126 if (!*args[cur_arg] ||
9127 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9128 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9129 file, linenum, args[0]);
9130 goto out_err;
9131 }
9132 rule->arg.nice = atoi(args[cur_arg]);
9133 if (rule->arg.nice < -1024)
9134 rule->arg.nice = -1024;
9135 else if (rule->arg.nice > 1024)
9136 rule->arg.nice = 1024;
9137 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009138 } else if (!strcmp(args[0], "set-tos")) {
9139#ifdef IP_TOS
9140 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009141 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009142 cur_arg = 1;
9143
9144 if (!*args[cur_arg] ||
9145 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9146 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9147 file, linenum, args[0]);
9148 goto out_err;
9149 }
9150
9151 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9152 if (err && *err != '\0') {
9153 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9154 file, linenum, err, args[0]);
9155 goto out_err;
9156 }
9157 cur_arg++;
9158#else
9159 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9160 goto out_err;
9161#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009162 } else if (!strcmp(args[0], "set-mark")) {
9163#ifdef SO_MARK
9164 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009165 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009166 cur_arg = 1;
9167
9168 if (!*args[cur_arg] ||
9169 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9170 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9171 file, linenum, args[0]);
9172 goto out_err;
9173 }
9174
9175 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9176 if (err && *err != '\0') {
9177 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9178 file, linenum, err, args[0]);
9179 goto out_err;
9180 }
9181 cur_arg++;
9182 global.last_checks |= LSTCHK_NETADM;
9183#else
9184 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9185 goto out_err;
9186#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009187 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009188 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009189 cur_arg = 1;
9190
9191 if (!*args[cur_arg] ||
9192 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9193 bad_log_level:
9194 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9195 file, linenum, args[0]);
9196 goto out_err;
9197 }
9198 if (strcmp(args[cur_arg], "silent") == 0)
9199 rule->arg.loglevel = -1;
9200 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9201 goto bad_log_level;
9202 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009203 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009204 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009205 cur_arg = 1;
9206
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009207 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9208 (*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 +01009209 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9210 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009211 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009212 }
9213
9214 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9215 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9216 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009217
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009218 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009219 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009220 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 +01009221 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9222 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9223 file, linenum, args[0], error);
9224 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009225 goto out_err;
9226 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009227 free(proxy->conf.lfs_file);
9228 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9229 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009230 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009231 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009232 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009233 cur_arg = 1;
9234
9235 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009236 (*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 -06009237 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9238 file, linenum, args[0]);
9239 goto out_err;
9240 }
9241
9242 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9243 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9244 LIST_INIT(&rule->arg.hdr_add.fmt);
9245
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009246 error = NULL;
9247 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9248 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9249 args[cur_arg + 1], error);
9250 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009251 goto out_err;
9252 }
9253
9254 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009255 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009256 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 +01009257 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9258 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9259 file, linenum, args[0], error);
9260 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009261 goto out_err;
9262 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009263
9264 free(proxy->conf.lfs_file);
9265 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9266 proxy->conf.lfs_line = proxy->conf.args.line;
9267 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009268 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009269 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009270 cur_arg = 1;
9271
9272 if (!*args[cur_arg] ||
9273 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9274 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9275 file, linenum, args[0]);
9276 goto out_err;
9277 }
9278
9279 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9280 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9281
9282 proxy->conf.args.ctx = ARGC_HRQ;
9283 free(proxy->conf.lfs_file);
9284 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9285 proxy->conf.lfs_line = proxy->conf.args.line;
9286 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009287 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9288 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009289 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009290 struct sample_expr *expr;
9291 unsigned int where;
9292 char *err = NULL;
9293
9294 cur_arg = 1;
9295 proxy->conf.args.ctx = ARGC_TRK;
9296
9297 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9298 if (!expr) {
9299 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9300 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9301 free(err);
9302 goto out_err;
9303 }
9304
9305 where = 0;
9306 if (proxy->cap & PR_CAP_FE)
9307 where |= SMP_VAL_FE_HRQ_HDR;
9308 if (proxy->cap & PR_CAP_BE)
9309 where |= SMP_VAL_BE_HRQ_HDR;
9310
9311 if (!(expr->fetch->val & where)) {
9312 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9313 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9314 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9315 args[cur_arg-1], sample_src_names(expr->fetch->use));
9316 free(expr);
9317 goto out_err;
9318 }
9319
9320 if (strcmp(args[cur_arg], "table") == 0) {
9321 cur_arg++;
9322 if (!args[cur_arg]) {
9323 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9324 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9325 free(expr);
9326 goto out_err;
9327 }
9328 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009329 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009330 cur_arg++;
9331 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009332 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009333 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009334 } else if (strcmp(args[0], "redirect") == 0) {
9335 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009336 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009337
Willy Tarreaube4653b2015-05-28 15:26:58 +02009338 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009339 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9340 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9341 goto out_err;
9342 }
9343
9344 /* this redirect rule might already contain a parsed condition which
9345 * we'll pass to the http-request rule.
9346 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009347 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009348 rule->arg.redir = redir;
9349 rule->cond = redir->cond;
9350 redir->cond = NULL;
9351 cur_arg = 2;
9352 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009353 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9354 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009355 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009356 /*
9357 * '+ 8' for 'add-acl('
9358 * '- 9' for 'add-acl(' + trailing ')'
9359 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009360 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009361
9362 cur_arg = 1;
9363
9364 if (!*args[cur_arg] ||
9365 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9366 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9367 file, linenum, args[0]);
9368 goto out_err;
9369 }
9370
9371 LIST_INIT(&rule->arg.map.key);
9372 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009373 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009374 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009375 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9376 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9377 file, linenum, args[0], error);
9378 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009379 goto out_err;
9380 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009381 free(proxy->conf.lfs_file);
9382 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9383 proxy->conf.lfs_line = proxy->conf.args.line;
9384 cur_arg += 1;
9385 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9386 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009387 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009388 /*
9389 * '+ 8' for 'del-acl('
9390 * '- 9' for 'del-acl(' + trailing ')'
9391 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009392 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009393
9394 cur_arg = 1;
9395
9396 if (!*args[cur_arg] ||
9397 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9398 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9399 file, linenum, args[0]);
9400 goto out_err;
9401 }
9402
9403 LIST_INIT(&rule->arg.map.key);
9404 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009405 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009406 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009407 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9408 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9409 file, linenum, args[0], error);
9410 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009411 goto out_err;
9412 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009413 free(proxy->conf.lfs_file);
9414 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9415 proxy->conf.lfs_line = proxy->conf.args.line;
9416 cur_arg += 1;
9417 } else if (strncmp(args[0], "del-map", 7) == 0) {
9418 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009419 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009420 /*
9421 * '+ 8' for 'del-map('
9422 * '- 9' for 'del-map(' + trailing ')'
9423 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009424 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009425
9426 cur_arg = 1;
9427
9428 if (!*args[cur_arg] ||
9429 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9430 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9431 file, linenum, args[0]);
9432 goto out_err;
9433 }
9434
9435 LIST_INIT(&rule->arg.map.key);
9436 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009437 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009438 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009439 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9440 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9441 file, linenum, args[0], error);
9442 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009443 goto out_err;
9444 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009445 free(proxy->conf.lfs_file);
9446 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9447 proxy->conf.lfs_line = proxy->conf.args.line;
9448 cur_arg += 1;
9449 } else if (strncmp(args[0], "set-map", 7) == 0) {
9450 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009451 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009452 /*
9453 * '+ 8' for 'set-map('
9454 * '- 9' for 'set-map(' + trailing ')'
9455 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009456 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009457
9458 cur_arg = 1;
9459
9460 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9461 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9462 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9463 file, linenum, args[0]);
9464 goto out_err;
9465 }
9466
9467 LIST_INIT(&rule->arg.map.key);
9468 LIST_INIT(&rule->arg.map.value);
9469 proxy->conf.args.ctx = ARGC_HRQ;
9470
9471 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009472 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009473 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009474 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9475 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9476 file, linenum, args[0], error);
9477 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009478 goto out_err;
9479 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009480
9481 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009482 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009483 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 +01009484 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9485 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9486 file, linenum, args[0], error);
9487 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009488 goto out_err;
9489 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009490 free(proxy->conf.lfs_file);
9491 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9492 proxy->conf.lfs_line = proxy->conf.args.line;
9493
9494 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009495 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9496 char *errmsg = NULL;
9497 cur_arg = 1;
9498 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009499 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009500 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009501 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009502 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9503 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9504 free(errmsg);
9505 goto out_err;
9506 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009507 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009508 action_build_list(&http_req_keywords.list, &trash);
9509 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9510 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009511 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009512 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009513 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009514 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009515 }
9516
9517 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9518 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009519 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009520
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009521 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9522 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9523 file, linenum, args[0], errmsg);
9524 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009525 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009526 }
9527 rule->cond = cond;
9528 }
9529 else if (*args[cur_arg]) {
9530 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9531 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9532 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009533 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009534 }
9535
9536 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009537 out_err:
9538 free(rule);
9539 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009540}
9541
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009542/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009543struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009544{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009545 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009546 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009547 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009548 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009549
9550 rule = calloc(1, sizeof(*rule));
9551 if (!rule) {
9552 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9553 goto out_err;
9554 }
9555
9556 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009557 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009558 cur_arg = 1;
9559 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009560 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009561 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009562 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009563 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009564 cur_arg = 1;
9565
9566 if (!*args[cur_arg] ||
9567 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9568 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9569 file, linenum, args[0]);
9570 goto out_err;
9571 }
9572 rule->arg.nice = atoi(args[cur_arg]);
9573 if (rule->arg.nice < -1024)
9574 rule->arg.nice = -1024;
9575 else if (rule->arg.nice > 1024)
9576 rule->arg.nice = 1024;
9577 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009578 } else if (!strcmp(args[0], "set-tos")) {
9579#ifdef IP_TOS
9580 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009581 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009582 cur_arg = 1;
9583
9584 if (!*args[cur_arg] ||
9585 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9586 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9587 file, linenum, args[0]);
9588 goto out_err;
9589 }
9590
9591 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9592 if (err && *err != '\0') {
9593 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9594 file, linenum, err, args[0]);
9595 goto out_err;
9596 }
9597 cur_arg++;
9598#else
9599 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9600 goto out_err;
9601#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009602 } else if (!strcmp(args[0], "set-mark")) {
9603#ifdef SO_MARK
9604 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009605 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009606 cur_arg = 1;
9607
9608 if (!*args[cur_arg] ||
9609 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9610 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9611 file, linenum, args[0]);
9612 goto out_err;
9613 }
9614
9615 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9616 if (err && *err != '\0') {
9617 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9618 file, linenum, err, args[0]);
9619 goto out_err;
9620 }
9621 cur_arg++;
9622 global.last_checks |= LSTCHK_NETADM;
9623#else
9624 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9625 goto out_err;
9626#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009627 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009628 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009629 cur_arg = 1;
9630
9631 if (!*args[cur_arg] ||
9632 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9633 bad_log_level:
9634 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9635 file, linenum, args[0]);
9636 goto out_err;
9637 }
9638 if (strcmp(args[cur_arg], "silent") == 0)
9639 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009640 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009641 goto bad_log_level;
9642 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009643 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009644 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009645 cur_arg = 1;
9646
9647 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9648 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9649 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9650 file, linenum, args[0]);
9651 goto out_err;
9652 }
9653
9654 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9655 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9656 LIST_INIT(&rule->arg.hdr_add.fmt);
9657
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009658 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009659 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009660 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 +01009661 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9662 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9663 file, linenum, args[0], error);
9664 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009665 goto out_err;
9666 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009667 free(proxy->conf.lfs_file);
9668 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9669 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009670 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009671 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009672 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009673 cur_arg = 1;
9674
9675 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009676 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9677 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009678 file, linenum, args[0]);
9679 goto out_err;
9680 }
9681
9682 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9683 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9684 LIST_INIT(&rule->arg.hdr_add.fmt);
9685
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009686 error = NULL;
9687 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9688 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9689 args[cur_arg + 1], error);
9690 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009691 goto out_err;
9692 }
9693
9694 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009695 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009696 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 +01009697 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9698 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9699 file, linenum, args[0], error);
9700 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009701 goto out_err;
9702 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009703
9704 free(proxy->conf.lfs_file);
9705 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9706 proxy->conf.lfs_line = proxy->conf.args.line;
9707 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009708 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009709 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009710 cur_arg = 1;
9711
9712 if (!*args[cur_arg] ||
9713 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9714 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9715 file, linenum, args[0]);
9716 goto out_err;
9717 }
9718
9719 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9720 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9721
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009722 proxy->conf.args.ctx = ARGC_HRS;
9723 free(proxy->conf.lfs_file);
9724 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9725 proxy->conf.lfs_line = proxy->conf.args.line;
9726 cur_arg += 1;
9727 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9728 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009729 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009730 /*
9731 * '+ 8' for 'add-acl('
9732 * '- 9' for 'add-acl(' + trailing ')'
9733 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009734 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009735
9736 cur_arg = 1;
9737
9738 if (!*args[cur_arg] ||
9739 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9740 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9741 file, linenum, args[0]);
9742 goto out_err;
9743 }
9744
9745 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009746 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009747 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009748 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009749 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9750 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9751 file, linenum, args[0], error);
9752 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009753 goto out_err;
9754 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009755 free(proxy->conf.lfs_file);
9756 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9757 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009758
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009759 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009760 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9761 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009762 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763 /*
9764 * '+ 8' for 'del-acl('
9765 * '- 9' for 'del-acl(' + trailing ')'
9766 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009767 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009768
9769 cur_arg = 1;
9770
9771 if (!*args[cur_arg] ||
9772 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9773 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9774 file, linenum, args[0]);
9775 goto out_err;
9776 }
9777
9778 LIST_INIT(&rule->arg.map.key);
9779 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009780 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009781 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009782 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9783 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9784 file, linenum, args[0], error);
9785 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009786 goto out_err;
9787 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009788 free(proxy->conf.lfs_file);
9789 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9790 proxy->conf.lfs_line = proxy->conf.args.line;
9791 cur_arg += 1;
9792 } else if (strncmp(args[0], "del-map", 7) == 0) {
9793 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009794 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009795 /*
9796 * '+ 8' for 'del-map('
9797 * '- 9' for 'del-map(' + trailing ')'
9798 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009799 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009800
9801 cur_arg = 1;
9802
9803 if (!*args[cur_arg] ||
9804 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9805 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9806 file, linenum, args[0]);
9807 goto out_err;
9808 }
9809
9810 LIST_INIT(&rule->arg.map.key);
9811 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009812 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009813 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009814 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9815 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9816 file, linenum, args[0], error);
9817 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009818 goto out_err;
9819 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009820 free(proxy->conf.lfs_file);
9821 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9822 proxy->conf.lfs_line = proxy->conf.args.line;
9823 cur_arg += 1;
9824 } else if (strncmp(args[0], "set-map", 7) == 0) {
9825 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009826 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009827 /*
9828 * '+ 8' for 'set-map('
9829 * '- 9' for 'set-map(' + trailing ')'
9830 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009831 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009832
9833 cur_arg = 1;
9834
9835 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9836 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9837 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9838 file, linenum, args[0]);
9839 goto out_err;
9840 }
9841
9842 LIST_INIT(&rule->arg.map.key);
9843 LIST_INIT(&rule->arg.map.value);
9844
9845 proxy->conf.args.ctx = ARGC_HRS;
9846
9847 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009848 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009849 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009850 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9851 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9852 file, linenum, args[0], error);
9853 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009854 goto out_err;
9855 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009856
9857 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009858 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009859 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 +01009860 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9861 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9862 file, linenum, args[0], error);
9863 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009864 goto out_err;
9865 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009866
9867 free(proxy->conf.lfs_file);
9868 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9869 proxy->conf.lfs_line = proxy->conf.args.line;
9870
9871 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009872 } else if (strcmp(args[0], "redirect") == 0) {
9873 struct redirect_rule *redir;
9874 char *errmsg = NULL;
9875
9876 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9877 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9878 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9879 goto out_err;
9880 }
9881
9882 /* this redirect rule might already contain a parsed condition which
9883 * we'll pass to the http-request rule.
9884 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009885 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009886 rule->arg.redir = redir;
9887 rule->cond = redir->cond;
9888 redir->cond = NULL;
9889 cur_arg = 2;
9890 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009891 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9892 args[0][9] == '\0' && args[0][8] >= '0' &&
9893 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9894 struct sample_expr *expr;
9895 unsigned int where;
9896 char *err = NULL;
9897
9898 cur_arg = 1;
9899 proxy->conf.args.ctx = ARGC_TRK;
9900
9901 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9902 if (!expr) {
9903 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9904 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9905 free(err);
9906 goto out_err;
9907 }
9908
9909 where = 0;
9910 if (proxy->cap & PR_CAP_FE)
9911 where |= SMP_VAL_FE_HRS_HDR;
9912 if (proxy->cap & PR_CAP_BE)
9913 where |= SMP_VAL_BE_HRS_HDR;
9914
9915 if (!(expr->fetch->val & where)) {
9916 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9917 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9918 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9919 args[cur_arg-1], sample_src_names(expr->fetch->use));
9920 free(expr);
9921 goto out_err;
9922 }
9923
9924 if (strcmp(args[cur_arg], "table") == 0) {
9925 cur_arg++;
9926 if (!args[cur_arg]) {
9927 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9928 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9929 free(expr);
9930 goto out_err;
9931 }
9932 /* we copy the table name for now, it will be resolved later */
9933 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9934 cur_arg++;
9935 }
9936 rule->arg.trk_ctr.expr = expr;
9937 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009938 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9939 char *errmsg = NULL;
9940 cur_arg = 1;
9941 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009942 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009943 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009944 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009945 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9946 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9947 free(errmsg);
9948 goto out_err;
9949 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009950 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009951 action_build_list(&http_res_keywords.list, &trash);
9952 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9953 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009954 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009955 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009956 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009957 goto out_err;
9958 }
9959
9960 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9961 struct acl_cond *cond;
9962 char *errmsg = NULL;
9963
9964 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9965 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9966 file, linenum, args[0], errmsg);
9967 free(errmsg);
9968 goto out_err;
9969 }
9970 rule->cond = cond;
9971 }
9972 else if (*args[cur_arg]) {
9973 Alert("parsing [%s:%d]: 'http-response %s' expects"
9974 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9975 file, linenum, args[0], args[cur_arg]);
9976 goto out_err;
9977 }
9978
9979 return rule;
9980 out_err:
9981 free(rule);
9982 return NULL;
9983}
9984
Willy Tarreau4baae242012-12-27 12:00:31 +01009985/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009986 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009987 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9988 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009989 */
9990struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009991 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009992{
9993 struct redirect_rule *rule;
9994 int cur_arg;
9995 int type = REDIRECT_TYPE_NONE;
9996 int code = 302;
9997 const char *destination = NULL;
9998 const char *cookie = NULL;
9999 int cookie_set = 0;
10000 unsigned int flags = REDIRECT_FLAG_NONE;
10001 struct acl_cond *cond = NULL;
10002
10003 cur_arg = 0;
10004 while (*(args[cur_arg])) {
10005 if (strcmp(args[cur_arg], "location") == 0) {
10006 if (!*args[cur_arg + 1])
10007 goto missing_arg;
10008
10009 type = REDIRECT_TYPE_LOCATION;
10010 cur_arg++;
10011 destination = args[cur_arg];
10012 }
10013 else if (strcmp(args[cur_arg], "prefix") == 0) {
10014 if (!*args[cur_arg + 1])
10015 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010016 type = REDIRECT_TYPE_PREFIX;
10017 cur_arg++;
10018 destination = args[cur_arg];
10019 }
10020 else if (strcmp(args[cur_arg], "scheme") == 0) {
10021 if (!*args[cur_arg + 1])
10022 goto missing_arg;
10023
10024 type = REDIRECT_TYPE_SCHEME;
10025 cur_arg++;
10026 destination = args[cur_arg];
10027 }
10028 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10029 if (!*args[cur_arg + 1])
10030 goto missing_arg;
10031
10032 cur_arg++;
10033 cookie = args[cur_arg];
10034 cookie_set = 1;
10035 }
10036 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10037 if (!*args[cur_arg + 1])
10038 goto missing_arg;
10039
10040 cur_arg++;
10041 cookie = args[cur_arg];
10042 cookie_set = 0;
10043 }
10044 else if (strcmp(args[cur_arg], "code") == 0) {
10045 if (!*args[cur_arg + 1])
10046 goto missing_arg;
10047
10048 cur_arg++;
10049 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010050 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010051 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010052 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010053 args[cur_arg - 1], args[cur_arg]);
10054 return NULL;
10055 }
10056 }
10057 else if (!strcmp(args[cur_arg],"drop-query")) {
10058 flags |= REDIRECT_FLAG_DROP_QS;
10059 }
10060 else if (!strcmp(args[cur_arg],"append-slash")) {
10061 flags |= REDIRECT_FLAG_APPEND_SLASH;
10062 }
10063 else if (strcmp(args[cur_arg], "if") == 0 ||
10064 strcmp(args[cur_arg], "unless") == 0) {
10065 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10066 if (!cond) {
10067 memprintf(errmsg, "error in condition: %s", *errmsg);
10068 return NULL;
10069 }
10070 break;
10071 }
10072 else {
10073 memprintf(errmsg,
10074 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10075 args[cur_arg]);
10076 return NULL;
10077 }
10078 cur_arg++;
10079 }
10080
10081 if (type == REDIRECT_TYPE_NONE) {
10082 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10083 return NULL;
10084 }
10085
Willy Tarreaube4653b2015-05-28 15:26:58 +020010086 if (dir && type != REDIRECT_TYPE_LOCATION) {
10087 memprintf(errmsg, "response only supports redirect type 'location'");
10088 return NULL;
10089 }
10090
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010091 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010092 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010093 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010094
10095 if (!use_fmt) {
10096 /* old-style static redirect rule */
10097 rule->rdr_str = strdup(destination);
10098 rule->rdr_len = strlen(destination);
10099 }
10100 else {
10101 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010102
10103 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10104 * if prefix == "/", we don't want to add anything, otherwise it
10105 * makes it hard for the user to configure a self-redirection.
10106 */
Godbachd9722032014-12-18 15:44:58 +080010107 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010108 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010109 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10110 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 +010010111 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10112 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010113 return NULL;
10114 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010115 free(curproxy->conf.lfs_file);
10116 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10117 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010118 }
10119 }
10120
Willy Tarreau4baae242012-12-27 12:00:31 +010010121 if (cookie) {
10122 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10123 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10124 */
10125 rule->cookie_len = strlen(cookie);
10126 if (cookie_set) {
10127 rule->cookie_str = malloc(rule->cookie_len + 10);
10128 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10129 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10130 rule->cookie_len += 9;
10131 } else {
10132 rule->cookie_str = malloc(rule->cookie_len + 21);
10133 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10134 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10135 rule->cookie_len += 20;
10136 }
10137 }
10138 rule->type = type;
10139 rule->code = code;
10140 rule->flags = flags;
10141 LIST_INIT(&rule->list);
10142 return rule;
10143
10144 missing_arg:
10145 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10146 return NULL;
10147}
10148
Willy Tarreau8797c062007-05-07 00:55:35 +020010149/************************************************************************/
10150/* The code below is dedicated to ACL parsing and matching */
10151/************************************************************************/
10152
10153
Willy Tarreau14174bc2012-04-16 14:34:04 +020010154/* This function ensures that the prerequisites for an L7 fetch are ready,
10155 * which means that a request or response is ready. If some data is missing,
10156 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010157 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10158 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010159 *
10160 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010161 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10162 * decide whether or not an HTTP message is present ;
10163 * 0 if the requested data cannot be fetched or if it is certain that
10164 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010165 * 1 if an HTTP message is ready
10166 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010167int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010168 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010169{
Willy Tarreau192252e2015-04-04 01:47:55 +020010170 struct http_txn *txn;
10171 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010172
Willy Tarreaube508f12016-03-10 11:47:01 +010010173 /* Note: it is possible that <s> is NULL when called before stream
10174 * initialization (eg: tcp-request connection), so this function is the
10175 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010176 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010177 if (!s)
10178 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010179
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010180 if (!s->txn) {
10181 if (unlikely(!http_alloc_txn(s)))
10182 return 0; /* not enough memory */
10183 http_init_txn(s);
10184 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010185 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010186 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010187
10188 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010189 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010190
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010191 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010192 /* If the buffer does not leave enough free space at the end,
10193 * we must first realign it.
10194 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010195 if (s->req.buf->p > s->req.buf->data &&
10196 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10197 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010198
Willy Tarreau14174bc2012-04-16 14:34:04 +020010199 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010200 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010201 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010202
10203 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010204 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010205 http_msg_analyzer(msg, &txn->hdr_idx);
10206
10207 /* Still no valid request ? */
10208 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010209 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010210 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010211 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010212 }
10213 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010214 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010215 return 0;
10216 }
10217
10218 /* OK we just got a valid HTTP request. We have some minor
10219 * preparation to perform so that further checks can rely
10220 * on HTTP tests.
10221 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010222
10223 /* If the request was parsed but was too large, we must absolutely
10224 * return an error so that it is not processed. At the moment this
10225 * cannot happen, but if the parsers are to change in the future,
10226 * we want this check to be maintained.
10227 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010228 if (unlikely(s->req.buf->i + s->req.buf->p >
10229 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010230 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010231 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010232 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010233 return 1;
10234 }
10235
Willy Tarreau9b28e032012-10-12 23:49:43 +020010236 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010237 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010238 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010239
Willy Tarreau506d0502013-07-06 13:29:24 +020010240 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10241 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010242 }
10243
Willy Tarreau506d0502013-07-06 13:29:24 +020010244 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010245 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010246 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010247
10248 /* otherwise everything's ready for the request */
10249 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010250 else {
10251 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010252 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10253 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010254 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010255 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010256 }
10257
10258 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010259 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010260 return 1;
10261}
Willy Tarreau8797c062007-05-07 00:55:35 +020010262
Willy Tarreau8797c062007-05-07 00:55:35 +020010263/* 1. Check on METHOD
10264 * We use the pre-parsed method if it is known, and store its number as an
10265 * integer. If it is unknown, we use the pointer and the length.
10266 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010267static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010268{
10269 int len, meth;
10270
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010271 len = strlen(text);
10272 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010273
10274 pattern->val.i = meth;
10275 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010276 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010277 pattern->len = len;
10278 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010279 else {
10280 pattern->ptr.str = NULL;
10281 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010282 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010283 return 1;
10284}
10285
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010286/* This function fetches the method of current HTTP request and stores
10287 * it in the global pattern struct as a chunk. There are two possibilities :
10288 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10289 * in <len> and <ptr> is NULL ;
10290 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10291 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010292 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010293 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010295smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010296{
10297 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010298 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299
Willy Tarreau24e32d82012-04-23 23:55:44 +020010300 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010301
Willy Tarreaube508f12016-03-10 11:47:01 +010010302 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010303 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010304 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010305 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010306 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010307 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10308 /* ensure the indexes are not affected */
10309 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010310 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010311 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10312 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010313 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010314 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010315 return 1;
10316}
10317
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010318/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010319static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010320{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010321 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010322 struct pattern_list *lst;
10323 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010324
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010325 list_for_each_entry(lst, &expr->patterns, list) {
10326 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010327
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010328 /* well-known method */
10329 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010330 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010331 return pattern;
10332 else
10333 continue;
10334 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010335
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010336 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010337 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010338 continue;
10339
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010340 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010341 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10342 (!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 +010010343 return pattern;
10344 }
10345 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010346}
10347
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010348static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010349smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010350{
Willy Tarreaube508f12016-03-10 11:47:01 +010010351 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010352 char *ptr;
10353 int len;
10354
Willy Tarreauc0239e02012-04-16 14:42:55 +020010355 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010356
Willy Tarreaube508f12016-03-10 11:47:01 +010010357 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010358 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010359 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010360
10361 while ((len-- > 0) && (*ptr++ != '/'));
10362 if (len <= 0)
10363 return 0;
10364
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010365 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010366 smp->data.u.str.str = ptr;
10367 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010368
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010369 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010370 return 1;
10371}
10372
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010373static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010374smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010375{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010376 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010377 char *ptr;
10378 int len;
10379
Willy Tarreauc0239e02012-04-16 14:42:55 +020010380 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010381
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010382 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010383 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10384 return 0;
10385
Willy Tarreau8797c062007-05-07 00:55:35 +020010386 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010387 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010388
10389 while ((len-- > 0) && (*ptr++ != '/'));
10390 if (len <= 0)
10391 return 0;
10392
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010393 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010394 smp->data.u.str.str = ptr;
10395 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010396
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010397 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010398 return 1;
10399}
10400
10401/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010402static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010403smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010404{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010405 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010406 char *ptr;
10407 int len;
10408
Willy Tarreauc0239e02012-04-16 14:42:55 +020010409 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010410
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010411 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010412 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10413 return 0;
10414
Willy Tarreau8797c062007-05-07 00:55:35 +020010415 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010416 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010417
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010418 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010419 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010420 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010421 return 1;
10422}
10423
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010424static int
10425smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10426{
10427 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10428 return 0;
10429
10430 if (!smp->strm->unique_id) {
10431 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10432 return 0;
10433 smp->strm->unique_id[0] = '\0';
10434 }
10435 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10436 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10437
10438 smp->data.type = SMP_T_STR;
10439 smp->data.u.str.str = smp->strm->unique_id;
10440 smp->flags = SMP_F_CONST;
10441 return 1;
10442}
10443
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020010444/* Returns a string block containing all headers including the
10445 * empty line wich separes headers from the body. This is useful
10446 * form some headers analysis.
10447 */
10448static int
10449smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
10450{
10451 struct http_msg *msg;
10452 struct hdr_idx *idx;
10453 struct http_txn *txn;
10454
10455 CHECK_HTTP_MESSAGE_FIRST();
10456
10457 txn = smp->strm->txn;
10458 idx = &txn->hdr_idx;
10459 msg = &txn->req;
10460
10461 smp->data.type = SMP_T_STR;
10462 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
10463 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
10464 (msg->chn->buf->p[msg->eoh] == '\r');
10465
10466 return 1;
10467}
10468
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020010469/* Returns the header request in a length/value encoded format.
10470 * This is useful for exchanges with the SPOE.
10471 *
10472 * A "length value" is a multibyte code encoding numbers. It uses the
10473 * SPOE format. The encoding is the following:
10474 *
10475 * Each couple "header name" / "header value" is composed
10476 * like this:
10477 * "length value" "header name bytes"
10478 * "length value" "header value bytes"
10479 * When the last header is reached, the header name and the header
10480 * value are empty. Their length are 0
10481 */
10482static int
10483smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
10484{
10485 struct http_msg *msg;
10486 struct chunk *temp;
10487 struct hdr_idx *idx;
10488 const char *cur_ptr, *cur_next, *p;
10489 int old_idx, cur_idx;
10490 struct hdr_idx_elem *cur_hdr;
10491 const char *hn, *hv;
10492 int hnl, hvl;
10493 int ret;
10494 struct http_txn *txn;
10495 char *buf;
10496 char *end;
10497
10498 CHECK_HTTP_MESSAGE_FIRST();
10499
10500 temp = get_trash_chunk();
10501 buf = temp->str;
10502 end = temp->str + temp->size;
10503
10504 txn = smp->strm->txn;
10505 idx = &txn->hdr_idx;
10506 msg = &txn->req;
10507
10508 /* Build array of headers. */
10509 old_idx = 0;
10510 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
10511 while (1) {
10512 cur_idx = idx->v[old_idx].next;
10513 if (!cur_idx)
10514 break;
10515 old_idx = cur_idx;
10516
10517 cur_hdr = &idx->v[cur_idx];
10518 cur_ptr = cur_next;
10519 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
10520
10521 /* Now we have one full header at cur_ptr of len cur_hdr->len,
10522 * and the next header starts at cur_next. We'll check
10523 * this header in the list as well as against the default
10524 * rule.
10525 */
10526
10527 /* look for ': *'. */
10528 hn = cur_ptr;
10529 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
10530 if (p >= cur_ptr+cur_hdr->len)
10531 continue;
10532 hnl = p - hn;
10533 p++;
10534 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
10535 p++;
10536 if (p >= cur_ptr + cur_hdr->len)
10537 continue;
10538 hv = p;
10539 hvl = cur_ptr + cur_hdr->len-p;
10540
10541 /* encode the header name. */
10542 ret = encode_varint(hnl, &buf, end);
10543 if (ret == -1)
10544 return 0;
10545 if (buf + hnl > end)
10546 return 0;
10547 memcpy(buf, hn, hnl);
10548 buf += hnl;
10549
10550 /* encode and copy the value. */
10551 ret = encode_varint(hvl, &buf, end);
10552 if (ret == -1)
10553 return 0;
10554 if (buf + hvl > end)
10555 return 0;
10556 memcpy(buf, hv, hvl);
10557 buf += hvl;
10558 }
10559
10560 /* encode the end of the header list with empty
10561 * header name and header value.
10562 */
10563 ret = encode_varint(0, &buf, end);
10564 if (ret == -1)
10565 return 0;
10566 ret = encode_varint(0, &buf, end);
10567 if (ret == -1)
10568 return 0;
10569
10570 /* Initialise sample data which will be filled. */
10571 smp->data.type = SMP_T_BIN;
10572 smp->data.u.str.str = temp->str;
10573 smp->data.u.str.len = buf - temp->str;
10574 smp->data.u.str.size = temp->size;
10575
10576 return 1;
10577}
10578
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010579/* returns the longest available part of the body. This requires that the body
10580 * has been waited for using http-buffer-request.
10581 */
10582static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010583smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010584{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010585 struct http_msg *msg;
10586 unsigned long len;
10587 unsigned long block1;
10588 char *body;
10589 struct chunk *temp;
10590
10591 CHECK_HTTP_MESSAGE_FIRST();
10592
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010593 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010594 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010595 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010596 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010597
10598 len = http_body_bytes(msg);
10599 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10600
10601 block1 = len;
10602 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10603 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10604
10605 if (block1 == len) {
10606 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010607 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010608 smp->data.u.str.str = body;
10609 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010610 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10611 }
10612 else {
10613 /* buffer is wrapped, we need to defragment it */
10614 temp = get_trash_chunk();
10615 memcpy(temp->str, body, block1);
10616 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010617 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010618 smp->data.u.str.str = temp->str;
10619 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010620 smp->flags = SMP_F_VOL_TEST;
10621 }
10622 return 1;
10623}
10624
10625
10626/* returns the available length of the body. This requires that the body
10627 * has been waited for using http-buffer-request.
10628 */
10629static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010630smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010631{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010632 struct http_msg *msg;
10633
10634 CHECK_HTTP_MESSAGE_FIRST();
10635
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010636 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010637 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010638 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010639 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010640
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010641 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010642 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010643
10644 smp->flags = SMP_F_VOL_TEST;
10645 return 1;
10646}
10647
10648
10649/* returns the advertised length of the body, or the advertised size of the
10650 * chunks available in the buffer. This requires that the body has been waited
10651 * for using http-buffer-request.
10652 */
10653static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010654smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010655{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010656 struct http_msg *msg;
10657
10658 CHECK_HTTP_MESSAGE_FIRST();
10659
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010660 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010661 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010662 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010663 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010664
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010665 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010666 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010667
10668 smp->flags = SMP_F_VOL_TEST;
10669 return 1;
10670}
10671
10672
Willy Tarreau8797c062007-05-07 00:55:35 +020010673/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010674static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010675smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010676{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010677 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010678
Willy Tarreauc0239e02012-04-16 14:42:55 +020010679 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010680
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010681 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010682 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010683 smp->data.u.str.len = txn->req.sl.rq.u_l;
10684 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010685 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010686 return 1;
10687}
10688
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010689static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010690smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010691{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010692 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010693 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010694
Willy Tarreauc0239e02012-04-16 14:42:55 +020010695 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010696
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010697 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010698 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 +020010699 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010700 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010701
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010702 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010703 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010704 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010705 return 1;
10706}
10707
10708static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010709smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010710{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010711 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010712 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010713
Willy Tarreauc0239e02012-04-16 14:42:55 +020010714 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010715
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010716 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010717 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 +020010718 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10719 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010720
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010721 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010722 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010723 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010724 return 1;
10725}
10726
Willy Tarreau185b5c42012-04-26 15:11:51 +020010727/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10728 * Accepts an optional argument of type string containing the header field name,
10729 * and an optional argument of type signed or unsigned integer to request an
10730 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010731 * headers are considered from the first one. It does not stop on commas and
10732 * returns full lines instead (useful for User-Agent or Date for example).
10733 */
10734static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010735smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010736{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010737 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010738 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010739 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010740 int occ = 0;
10741 const char *name_str = NULL;
10742 int name_len = 0;
10743
10744 if (!ctx) {
10745 /* first call */
10746 ctx = &static_hdr_ctx;
10747 ctx->idx = 0;
10748 smp->ctx.a[0] = ctx;
10749 }
10750
10751 if (args) {
10752 if (args[0].type != ARGT_STR)
10753 return 0;
10754 name_str = args[0].data.str.str;
10755 name_len = args[0].data.str.len;
10756
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010757 if (args[1].type == ARGT_SINT)
10758 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010759 }
10760
10761 CHECK_HTTP_MESSAGE_FIRST();
10762
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010763 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010764 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 +020010765
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010766 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10767 /* search for header from the beginning */
10768 ctx->idx = 0;
10769
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010770 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010771 /* no explicit occurrence and single fetch => last header by default */
10772 occ = -1;
10773
10774 if (!occ)
10775 /* prepare to report multiple occurrences for ACL fetches */
10776 smp->flags |= SMP_F_NOT_LAST;
10777
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010778 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010779 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010780 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 +020010781 return 1;
10782
10783 smp->flags &= ~SMP_F_NOT_LAST;
10784 return 0;
10785}
10786
10787/* 6. Check on HTTP header count. The number of occurrences is returned.
10788 * Accepts exactly 1 argument of type string. It does not stop on commas and
10789 * returns full lines instead (useful for User-Agent or Date for example).
10790 */
10791static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010792smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010793{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010794 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010795 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010796 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010797 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010798 const char *name = NULL;
10799 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010800
Willy Tarreau601a4d12015-04-01 19:16:09 +020010801 if (args && args->type == ARGT_STR) {
10802 name = args->data.str.str;
10803 len = args->data.str.len;
10804 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010805
10806 CHECK_HTTP_MESSAGE_FIRST();
10807
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010808 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010809 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 +020010810
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010811 ctx.idx = 0;
10812 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010813 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010814 cnt++;
10815
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010816 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010817 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010818 smp->flags = SMP_F_VOL_HDR;
10819 return 1;
10820}
10821
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010823smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010824{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010825 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010826 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010827 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010828 struct chunk *temp;
10829 char del = ',';
10830
10831 if (args && args->type == ARGT_STR)
10832 del = *args[0].data.str.str;
10833
10834 CHECK_HTTP_MESSAGE_FIRST();
10835
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010836 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010837 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 +020010838
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010839 temp = get_trash_chunk();
10840
10841 ctx.idx = 0;
10842 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10843 if (temp->len)
10844 temp->str[temp->len++] = del;
10845 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10846 temp->len += ctx.del;
10847 }
10848
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010849 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010850 smp->data.u.str.str = temp->str;
10851 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010852 smp->flags = SMP_F_VOL_HDR;
10853 return 1;
10854}
10855
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010856/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10857 * Accepts an optional argument of type string containing the header field name,
10858 * and an optional argument of type signed or unsigned integer to request an
10859 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010860 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010861 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010862static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010863smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010864{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010865 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010866 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010867 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010868 int occ = 0;
10869 const char *name_str = NULL;
10870 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010871
Willy Tarreaua890d072013-04-02 12:01:06 +020010872 if (!ctx) {
10873 /* first call */
10874 ctx = &static_hdr_ctx;
10875 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010876 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010877 }
10878
Willy Tarreau185b5c42012-04-26 15:11:51 +020010879 if (args) {
10880 if (args[0].type != ARGT_STR)
10881 return 0;
10882 name_str = args[0].data.str.str;
10883 name_len = args[0].data.str.len;
10884
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010885 if (args[1].type == ARGT_SINT)
10886 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010887 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010888
Willy Tarreaue333ec92012-04-16 16:26:40 +020010889 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010890
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010891 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010892 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 +020010893
Willy Tarreau185b5c42012-04-26 15:11:51 +020010894 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010895 /* search for header from the beginning */
10896 ctx->idx = 0;
10897
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010898 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010899 /* no explicit occurrence and single fetch => last header by default */
10900 occ = -1;
10901
10902 if (!occ)
10903 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010904 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010905
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010906 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010907 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010908 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 +020010909 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010910
Willy Tarreau37406352012-04-23 16:16:37 +020010911 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010912 return 0;
10913}
10914
Willy Tarreauc11416f2007-06-17 16:58:38 +020010915/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010916 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010917 */
10918static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010919smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010920{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010921 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010922 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010923 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010924 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010925 const char *name = NULL;
10926 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010927
Willy Tarreau601a4d12015-04-01 19:16:09 +020010928 if (args && args->type == ARGT_STR) {
10929 name = args->data.str.str;
10930 len = args->data.str.len;
10931 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010932
Willy Tarreaue333ec92012-04-16 16:26:40 +020010933 CHECK_HTTP_MESSAGE_FIRST();
10934
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010935 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010936 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 +020010937
Willy Tarreau33a7e692007-06-10 19:45:56 +020010938 ctx.idx = 0;
10939 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010940 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010941 cnt++;
10942
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010943 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010944 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010945 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010946 return 1;
10947}
10948
Willy Tarreau185b5c42012-04-26 15:11:51 +020010949/* Fetch an HTTP header's integer value. The integer value is returned. It
10950 * takes a mandatory argument of type string and an optional one of type int
10951 * to designate a specific occurrence. It returns an unsigned integer, which
10952 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010953 */
10954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010955smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010956{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010957 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010958
Willy Tarreauf853c462012-04-23 18:53:56 +020010959 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010960 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010961 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010962 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010963
Willy Tarreaud53e2422012-04-16 17:21:11 +020010964 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010965}
10966
Cyril Bonté69fa9922012-10-25 00:01:06 +020010967/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10968 * and an optional one of type int to designate a specific occurrence.
10969 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010970 */
10971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010972smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010973{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010974 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010975
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010976 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010977 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010978 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010979 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010980 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010981 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010982 if (smp->data.u.str.len < temp->size - 1) {
10983 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10984 temp->str[smp->data.u.str.len] = '\0';
10985 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010986 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010987 break;
10988 }
10989 }
10990 }
10991
Willy Tarreaud53e2422012-04-16 17:21:11 +020010992 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010993 if (!(smp->flags & SMP_F_NOT_LAST))
10994 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010995 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010996 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010997}
10998
Willy Tarreau737b0c12007-06-10 21:28:46 +020010999/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
11000 * the first '/' after the possible hostname, and ends before the possible '?'.
11001 */
11002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011003smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020011004{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011005 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011006 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020011007
Willy Tarreauc0239e02012-04-16 14:42:55 +020011008 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020011009
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011010 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020011011 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010011012 ptr = http_get_path(txn);
11013 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020011014 return 0;
11015
11016 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011017 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011018 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011019
11020 while (ptr < end && *ptr != '?')
11021 ptr++;
11022
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011023 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011024 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011025 return 1;
11026}
11027
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011028/* This produces a concatenation of the first occurrence of the Host header
11029 * followed by the path component if it begins with a slash ('/'). This means
11030 * that '*' will not be added, resulting in exactly the first Host entry.
11031 * If no Host header is found, then the path is returned as-is. The returned
11032 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010011033 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011034 */
11035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011036smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011037{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011038 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011039 char *ptr, *end, *beg;
11040 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011041 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011042
11043 CHECK_HTTP_MESSAGE_FIRST();
11044
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011045 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011046 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011047 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011048 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011049
11050 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011051 temp = get_trash_chunk();
11052 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011053 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011054 smp->data.u.str.str = temp->str;
11055 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011056
11057 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011058 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011059 beg = http_get_path(txn);
11060 if (!beg)
11061 beg = end;
11062
11063 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11064
11065 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011066 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
11067 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011068 }
11069
11070 smp->flags = SMP_F_VOL_1ST;
11071 return 1;
11072}
11073
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011074/* This produces a 32-bit hash of the concatenation of the first occurrence of
11075 * the Host header followed by the path component if it begins with a slash ('/').
11076 * This means that '*' will not be added, resulting in exactly the first Host
11077 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011078 * is hashed using the path hash followed by a full avalanche hash and provides a
11079 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011080 * high-traffic sites without having to store whole paths.
11081 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020011082int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011083smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011084{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011085 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011086 struct hdr_ctx ctx;
11087 unsigned int hash = 0;
11088 char *ptr, *beg, *end;
11089 int len;
11090
11091 CHECK_HTTP_MESSAGE_FIRST();
11092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011093 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011094 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011095 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011096 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11097 ptr = ctx.line + ctx.val;
11098 len = ctx.vlen;
11099 while (len--)
11100 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11101 }
11102
11103 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011104 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011105 beg = http_get_path(txn);
11106 if (!beg)
11107 beg = end;
11108
11109 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11110
11111 if (beg < ptr && *beg == '/') {
11112 while (beg < ptr)
11113 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11114 }
11115 hash = full_hash(hash);
11116
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011117 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011118 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011119 smp->flags = SMP_F_VOL_1ST;
11120 return 1;
11121}
11122
Willy Tarreau4a550602012-12-09 14:53:32 +010011123/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011124 * path as returned by smp_fetch_base32(). The idea is to have per-source and
11125 * per-path counters. The result is a binary block from 8 to 20 bytes depending
11126 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010011127 * that in environments where IPv6 is insignificant, truncating the output to
11128 * 8 bytes would still work.
11129 */
11130static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011131smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011132{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011133 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011134 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011135
11136 if (!cli_conn)
11137 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011138
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011139 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011140 return 0;
11141
Willy Tarreau47ca5452012-12-23 20:22:19 +010011142 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011143 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011144 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011145
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011146 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011147 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011148 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011149 temp->len += 4;
11150 break;
11151 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011152 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011153 temp->len += 16;
11154 break;
11155 default:
11156 return 0;
11157 }
11158
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011159 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011160 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011161 return 1;
11162}
11163
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011164/* Extracts the query string, which comes after the question mark '?'. If no
11165 * question mark is found, nothing is returned. Otherwise it returns a sample
11166 * of type string carrying the whole query string.
11167 */
11168static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011169smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011170{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011171 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011172 char *ptr, *end;
11173
11174 CHECK_HTTP_MESSAGE_FIRST();
11175
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011176 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011177 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11178 end = ptr + txn->req.sl.rq.u_l;
11179
11180 /* look up the '?' */
11181 do {
11182 if (ptr == end)
11183 return 0;
11184 } while (*ptr++ != '?');
11185
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011186 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011187 smp->data.u.str.str = ptr;
11188 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011189 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11190 return 1;
11191}
11192
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011193static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011194smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011195{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011196 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11197 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11198 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011199
Willy Tarreau24e32d82012-04-23 23:55:44 +020011200 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011201
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011202 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011203 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011204 return 1;
11205}
11206
Willy Tarreau7f18e522010-10-22 20:04:13 +020011207/* return a valid test if the current request is the first one on the connection */
11208static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011209smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011210{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011211 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011212 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011213 return 1;
11214}
11215
Willy Tarreau34db1082012-04-19 17:16:54 +020011216/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011217static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011218smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011219{
11220
Willy Tarreau24e32d82012-04-23 23:55:44 +020011221 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011222 return 0;
11223
Willy Tarreauc0239e02012-04-16 14:42:55 +020011224 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011225
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011226 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011227 return 0;
11228
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011229 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011230 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011231 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011232 return 1;
11233}
Willy Tarreau8797c062007-05-07 00:55:35 +020011234
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011235/* Accepts exactly 1 argument of type userlist */
11236static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011237smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011238{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011239 if (!args || args->type != ARGT_USR)
11240 return 0;
11241
11242 CHECK_HTTP_MESSAGE_FIRST();
11243
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011244 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011245 return 0;
11246
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011247 /* if the user does not belong to the userlist or has a wrong password,
11248 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011249 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011250 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011251 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11252 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011253 return 0;
11254
11255 /* pat_match_auth() will need the user list */
11256 smp->ctx.a[0] = args->data.usr;
11257
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011258 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011259 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011260 smp->data.u.str.str = smp->strm->txn->auth.user;
11261 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011262
11263 return 1;
11264}
11265
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011266/* Try to find the next occurrence of a cookie name in a cookie header value.
11267 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11268 * the cookie value is returned into *value and *value_l, and the function
11269 * returns a pointer to the next pointer to search from if the value was found.
11270 * Otherwise if the cookie was not found, NULL is returned and neither value
11271 * nor value_l are touched. The input <hdr> string should first point to the
11272 * header's value, and the <hdr_end> pointer must point to the first character
11273 * not part of the value. <list> must be non-zero if value may represent a list
11274 * of values (cookie headers). This makes it faster to abort parsing when no
11275 * list is expected.
11276 */
David Carlier4686f792015-09-25 14:10:50 +010011277char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011278extract_cookie_value(char *hdr, const char *hdr_end,
11279 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011280 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011281{
11282 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11283 char *next;
11284
11285 /* we search at least a cookie name followed by an equal, and more
11286 * generally something like this :
11287 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11288 */
11289 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11290 /* Iterate through all cookies on this line */
11291
Willy Tarreau2235b262016-11-05 15:50:20 +010011292 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011293 att_beg++;
11294
11295 /* find att_end : this is the first character after the last non
11296 * space before the equal. It may be equal to hdr_end.
11297 */
11298 equal = att_end = att_beg;
11299
11300 while (equal < hdr_end) {
11301 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11302 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011303 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011304 continue;
11305 att_end = equal;
11306 }
11307
11308 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11309 * is between <att_beg> and <equal>, both may be identical.
11310 */
11311
11312 /* look for end of cookie if there is an equal sign */
11313 if (equal < hdr_end && *equal == '=') {
11314 /* look for the beginning of the value */
11315 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011316 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011317 val_beg++;
11318
11319 /* find the end of the value, respecting quotes */
11320 next = find_cookie_value_end(val_beg, hdr_end);
11321
11322 /* make val_end point to the first white space or delimitor after the value */
11323 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011324 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011325 val_end--;
11326 } else {
11327 val_beg = val_end = next = equal;
11328 }
11329
11330 /* We have nothing to do with attributes beginning with '$'. However,
11331 * they will automatically be removed if a header before them is removed,
11332 * since they're supposed to be linked together.
11333 */
11334 if (*att_beg == '$')
11335 continue;
11336
11337 /* Ignore cookies with no equal sign */
11338 if (equal == next)
11339 continue;
11340
11341 /* Now we have the cookie name between att_beg and att_end, and
11342 * its value between val_beg and val_end.
11343 */
11344
11345 if (att_end - att_beg == cookie_name_l &&
11346 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11347 /* let's return this value and indicate where to go on from */
11348 *value = val_beg;
11349 *value_l = val_end - val_beg;
11350 return next + 1;
11351 }
11352
11353 /* Set-Cookie headers only have the name in the first attr=value part */
11354 if (!list)
11355 break;
11356 }
11357
11358 return NULL;
11359}
11360
William Lallemanda43ba4e2014-01-28 18:14:25 +010011361/* Fetch a captured HTTP request header. The index is the position of
11362 * the "capture" option in the configuration file
11363 */
11364static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011365smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011366{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011367 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011368 int idx;
11369
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011370 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011371 return 0;
11372
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011373 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011374
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011375 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 +010011376 return 0;
11377
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011378 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011379 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011380 smp->data.u.str.str = smp->strm->req_cap[idx];
11381 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011382
11383 return 1;
11384}
11385
11386/* Fetch a captured HTTP response header. The index is the position of
11387 * the "capture" option in the configuration file
11388 */
11389static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011390smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011391{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011392 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011393 int idx;
11394
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011395 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011396 return 0;
11397
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011398 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011399
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011400 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 +010011401 return 0;
11402
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011403 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011404 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011405 smp->data.u.str.str = smp->strm->res_cap[idx];
11406 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011407
11408 return 1;
11409}
11410
William Lallemand65ad6e12014-01-31 15:08:02 +010011411/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11412static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011413smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011414{
11415 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011416 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011417 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011418
Willy Tarreau15e91e12015-04-04 00:52:09 +020011419 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011420 return 0;
11421
William Lallemand96a77852014-02-05 00:30:02 +010011422 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011423
William Lallemand96a77852014-02-05 00:30:02 +010011424 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11425 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011426
William Lallemand96a77852014-02-05 00:30:02 +010011427 temp = get_trash_chunk();
11428 temp->str = txn->uri;
11429 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011430 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011431 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011432 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011433
11434 return 1;
11435
11436}
11437
11438/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11439static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011440smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011441{
11442 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011443 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011444 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011445
Willy Tarreau15e91e12015-04-04 00:52:09 +020011446 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011447 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011448
William Lallemand65ad6e12014-01-31 15:08:02 +010011449 ptr = txn->uri;
11450
11451 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11452 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011453
William Lallemand65ad6e12014-01-31 15:08:02 +010011454 if (!*ptr)
11455 return 0;
11456
11457 ptr++; /* skip the space */
11458
11459 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011460 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011461 if (!ptr)
11462 return 0;
11463 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11464 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011465
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011466 smp->data.u.str = *temp;
11467 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011468 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011469 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011470
11471 return 1;
11472}
11473
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011474/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11475 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11476 */
11477static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011478smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011479{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011480 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011481
Willy Tarreau15e91e12015-04-04 00:52:09 +020011482 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011483 return 0;
11484
11485 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011486 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011487 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011488 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011489
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011490 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011491 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011492 smp->flags = SMP_F_CONST;
11493 return 1;
11494
11495}
11496
11497/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11498 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11499 */
11500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011501smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011502{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011503 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011504
Willy Tarreau15e91e12015-04-04 00:52:09 +020011505 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011506 return 0;
11507
11508 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011509 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011510 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011511 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011512
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011513 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011514 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011515 smp->flags = SMP_F_CONST;
11516 return 1;
11517
11518}
11519
William Lallemand65ad6e12014-01-31 15:08:02 +010011520
Willy Tarreaue333ec92012-04-16 16:26:40 +020011521/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011522 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011523 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011524 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011525 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011526 * Accepts exactly 1 argument of type string. If the input options indicate
11527 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011528 * The returned sample is of type CSTR. Can be used to parse cookies in other
11529 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011530 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011531int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011532{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011533 struct http_txn *txn;
11534 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011535 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011536 const struct http_msg *msg;
11537 const char *hdr_name;
11538 int hdr_name_len;
11539 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011540 int occ = 0;
11541 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011542
Willy Tarreau24e32d82012-04-23 23:55:44 +020011543 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011544 return 0;
11545
Willy Tarreaua890d072013-04-02 12:01:06 +020011546 if (!ctx) {
11547 /* first call */
11548 ctx = &static_hdr_ctx;
11549 ctx->idx = 0;
11550 smp->ctx.a[2] = ctx;
11551 }
11552
Willy Tarreaue333ec92012-04-16 16:26:40 +020011553 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011554
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011555 txn = smp->strm->txn;
11556 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011557
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011558 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011559 msg = &txn->req;
11560 hdr_name = "Cookie";
11561 hdr_name_len = 6;
11562 } else {
11563 msg = &txn->rsp;
11564 hdr_name = "Set-Cookie";
11565 hdr_name_len = 10;
11566 }
11567
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011568 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011569 /* no explicit occurrence and single fetch => last cookie by default */
11570 occ = -1;
11571
11572 /* OK so basically here, either we want only one value and it's the
11573 * last one, or we want to iterate over all of them and we fetch the
11574 * next one.
11575 */
11576
Willy Tarreau9b28e032012-10-12 23:49:43 +020011577 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011578 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011579 /* search for the header from the beginning, we must first initialize
11580 * the search parameters.
11581 */
Willy Tarreau37406352012-04-23 16:16:37 +020011582 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011583 ctx->idx = 0;
11584 }
11585
Willy Tarreau28376d62012-04-26 21:26:10 +020011586 smp->flags |= SMP_F_VOL_HDR;
11587
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011588 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011589 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11590 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011591 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11592 goto out;
11593
Willy Tarreau24e32d82012-04-23 23:55:44 +020011594 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011595 continue;
11596
Willy Tarreau37406352012-04-23 16:16:37 +020011597 smp->ctx.a[0] = ctx->line + ctx->val;
11598 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011599 }
11600
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011601 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011602 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011603 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011604 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011605 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011606 &smp->data.u.str.str,
11607 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011608 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011609 found = 1;
11610 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011611 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011612 smp->flags |= SMP_F_NOT_LAST;
11613 return 1;
11614 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011615 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011616 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011617 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011618 /* all cookie headers and values were scanned. If we're looking for the
11619 * last occurrence, we may return it now.
11620 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011621 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011622 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011623 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011624}
11625
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011626/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011627 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011628 * multiple cookies may be parsed on the same line. The returned sample is of
11629 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011630 */
11631static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011632smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011633{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011634 struct http_txn *txn;
11635 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011636 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011637 const struct http_msg *msg;
11638 const char *hdr_name;
11639 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011640 int cnt;
11641 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011642 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011643
Willy Tarreau24e32d82012-04-23 23:55:44 +020011644 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011645 return 0;
11646
Willy Tarreaue333ec92012-04-16 16:26:40 +020011647 CHECK_HTTP_MESSAGE_FIRST();
11648
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011649 txn = smp->strm->txn;
11650 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011651
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011652 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011653 msg = &txn->req;
11654 hdr_name = "Cookie";
11655 hdr_name_len = 6;
11656 } else {
11657 msg = &txn->rsp;
11658 hdr_name = "Set-Cookie";
11659 hdr_name_len = 10;
11660 }
11661
Willy Tarreau9b28e032012-10-12 23:49:43 +020011662 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011663 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011664 ctx.idx = 0;
11665 cnt = 0;
11666
11667 while (1) {
11668 /* Note: val_beg == NULL every time we need to fetch a new header */
11669 if (!val_beg) {
11670 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11671 break;
11672
Willy Tarreau24e32d82012-04-23 23:55:44 +020011673 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011674 continue;
11675
11676 val_beg = ctx.line + ctx.val;
11677 val_end = val_beg + ctx.vlen;
11678 }
11679
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011680 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011681 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011682 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011683 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011684 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011685 &smp->data.u.str.str,
11686 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011687 cnt++;
11688 }
11689 }
11690
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011691 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011692 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011693 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011694 return 1;
11695}
11696
Willy Tarreau51539362012-05-08 12:46:28 +020011697/* Fetch an cookie's integer value. The integer value is returned. It
11698 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11699 */
11700static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011701smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011702{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011703 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011704
11705 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011706 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011707 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011708 }
11709
11710 return ret;
11711}
11712
Willy Tarreau8797c062007-05-07 00:55:35 +020011713/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011714/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011715/************************************************************************/
11716
David Cournapeau16023ee2010-12-23 20:55:41 +090011717/*
11718 * Given a path string and its length, find the position of beginning of the
11719 * query string. Returns NULL if no query string is found in the path.
11720 *
11721 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11722 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011723 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011724 */
bedis4c75cca2012-10-05 08:38:24 +020011725static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011726{
11727 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011728
bedis4c75cca2012-10-05 08:38:24 +020011729 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011730 return p ? p + 1 : NULL;
11731}
11732
bedis4c75cca2012-10-05 08:38:24 +020011733static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011734{
bedis4c75cca2012-10-05 08:38:24 +020011735 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011736}
11737
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011738/* after increasing a pointer value, it can exceed the first buffer
11739 * size. This function transform the value of <ptr> according with
11740 * the expected position. <chunks> is an array of the one or two
11741 * avalaible chunks. The first value is the start of the first chunk,
11742 * the second value if the end+1 of the first chunks. The third value
11743 * is NULL or the start of the second chunk and the fourth value is
11744 * the end+1 of the second chunk. The function returns 1 if does a
11745 * wrap, else returns 0.
11746 */
11747static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11748{
11749 if (*ptr < chunks[1])
11750 return 0;
11751 if (!chunks[2])
11752 return 0;
11753 *ptr = chunks[2] + ( *ptr - chunks[1] );
11754 return 1;
11755}
11756
David Cournapeau16023ee2010-12-23 20:55:41 +090011757/*
11758 * Given a url parameter, find the starting position of the first occurence,
11759 * or NULL if the parameter is not found.
11760 *
11761 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11762 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011763 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011764 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011765 * or the second chunk. The caller must be check the position before using the
11766 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011767 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011768static const char *
11769find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011770 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011771 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011772{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011773 const char *pos, *last, *equal;
11774 const char **bufs = chunks;
11775 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011776
David Cournapeau16023ee2010-12-23 20:55:41 +090011777
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011778 pos = bufs[0];
11779 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011780 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011781 /* Check the equal. */
11782 equal = pos + url_param_name_l;
11783 if (fix_pointer_if_wrap(chunks, &equal)) {
11784 if (equal >= chunks[3])
11785 return NULL;
11786 } else {
11787 if (equal >= chunks[1])
11788 return NULL;
11789 }
11790 if (*equal == '=') {
11791 if (pos + url_param_name_l > last) {
11792 /* process wrap case, we detect a wrap. In this case, the
11793 * comparison is performed in two parts.
11794 */
11795
11796 /* This is the end, we dont have any other chunk. */
11797 if (bufs != chunks || !bufs[2])
11798 return NULL;
11799
11800 /* Compute the length of each part of the comparison. */
11801 l1 = last - pos;
11802 l2 = url_param_name_l - l1;
11803
11804 /* The second buffer is too short to contain the compared string. */
11805 if (bufs[2] + l2 > bufs[3])
11806 return NULL;
11807
11808 if (memcmp(pos, url_param_name, l1) == 0 &&
11809 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11810 return pos;
11811
11812 /* Perform wrapping and jump the string who fail the comparison. */
11813 bufs += 2;
11814 pos = bufs[0] + l2;
11815 last = bufs[1];
11816
11817 } else {
11818 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011819 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11820 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011821 pos += url_param_name_l + 1;
11822 if (fix_pointer_if_wrap(chunks, &pos))
11823 last = bufs[2];
11824 }
11825 }
11826
11827 while (1) {
11828 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011829 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011830 pos++;
11831 if (pos < last)
11832 break;
11833 /* process buffer wrapping. */
11834 if (bufs != chunks || !bufs[2])
11835 return NULL;
11836 bufs += 2;
11837 pos = bufs[0];
11838 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011839 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011840 pos++;
11841 }
11842 return NULL;
11843}
11844
11845/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011846 * Given a url parameter name and a query string, find the next value.
11847 * An empty url_param_name matches the first available parameter.
11848 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11849 * respectively provide a pointer to the value and its end.
11850 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011851 */
11852static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011853find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011854 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011855 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011856{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011857 const char *arg_start, *qs_end;
11858 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011859
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011860 arg_start = chunks[0];
11861 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011862 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011863 /* Looks for an argument name. */
11864 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011865 url_param_name, url_param_name_l,
11866 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011867 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011868 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011869 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011870 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011871 if (!arg_start)
11872 return 0;
11873
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011874 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011875 while (1) {
11876 /* looks for the first argument. */
11877 value_start = memchr(arg_start, '=', qs_end - arg_start);
11878 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011879 /* Check for wrapping. */
11880 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011881 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011882 chunks[2]) {
11883 arg_start = chunks[2];
11884 qs_end = chunks[3];
11885 continue;
11886 }
11887 return 0;
11888 }
11889 break;
11890 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011891 value_start++;
11892 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011893 else {
11894 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011895 value_start = arg_start + url_param_name_l + 1;
11896
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011897 /* Check for pointer wrapping. */
11898 if (fix_pointer_if_wrap(chunks, &value_start)) {
11899 /* Update the end pointer. */
11900 qs_end = chunks[3];
11901
11902 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011903 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011904 return 0;
11905 }
11906 }
11907
David Cournapeau16023ee2010-12-23 20:55:41 +090011908 value_end = value_start;
11909
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011910 while (1) {
11911 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11912 value_end++;
11913 if (value_end < qs_end)
11914 break;
11915 /* process buffer wrapping. */
11916 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011917 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011918 chunks[2]) {
11919 value_end = chunks[2];
11920 qs_end = chunks[3];
11921 continue;
11922 }
11923 break;
11924 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011925
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011926 *vstart = value_start;
11927 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011928 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011929}
11930
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011931/* This scans a URL-encoded query string. It takes an optionally wrapping
11932 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11933 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11934 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011935 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011936static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011937smp_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 +090011938{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011939 const char *vstart, *vend;
11940 struct chunk *temp;
11941 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011942
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011943 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011944 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011945 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011946 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011947 return 0;
11948
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011949 /* Create sample. If the value is contiguous, return the pointer as CONST,
11950 * if the value is wrapped, copy-it in a buffer.
11951 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011952 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011953 if (chunks[2] &&
11954 vstart >= chunks[0] && vstart <= chunks[1] &&
11955 vend >= chunks[2] && vend <= chunks[3]) {
11956 /* Wrapped case. */
11957 temp = get_trash_chunk();
11958 memcpy(temp->str, vstart, chunks[1] - vstart);
11959 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011960 smp->data.u.str.str = temp->str;
11961 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011962 } else {
11963 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011964 smp->data.u.str.str = (char *)vstart;
11965 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011966 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11967 }
11968
11969 /* Update context, check wrapping. */
11970 chunks[0] = vend;
11971 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11972 chunks[1] = chunks[3];
11973 chunks[2] = NULL;
11974 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011975
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011976 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011977 smp->flags |= SMP_F_NOT_LAST;
11978
David Cournapeau16023ee2010-12-23 20:55:41 +090011979 return 1;
11980}
11981
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011982/* This function iterates over each parameter of the query string. It uses
11983 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011984 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11985 * An optional parameter name is passed in args[0], otherwise any parameter is
11986 * considered. It supports an optional delimiter argument for the beginning of
11987 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011988 */
11989static int
11990smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11991{
11992 struct http_msg *msg;
11993 char delim = '?';
11994 const char *name;
11995 int name_len;
11996
Dragan Dosen26f77e52015-05-25 10:02:11 +020011997 if (!args ||
11998 (args[0].type && args[0].type != ARGT_STR) ||
11999 (args[1].type && args[1].type != ARGT_STR))
12000 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012001
Dragan Dosen26f77e52015-05-25 10:02:11 +020012002 name = "";
12003 name_len = 0;
12004 if (args->type == ARGT_STR) {
12005 name = args->data.str.str;
12006 name_len = args->data.str.len;
12007 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012008
Dragan Dosen26f77e52015-05-25 10:02:11 +020012009 if (args[1].type)
12010 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012011
Dragan Dosen26f77e52015-05-25 10:02:11 +020012012 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012013 CHECK_HTTP_MESSAGE_FIRST();
12014
12015 msg = &smp->strm->txn->req;
12016
12017 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
12018 msg->sl.rq.u_l, delim);
12019 if (!smp->ctx.a[0])
12020 return 0;
12021
12022 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012023
12024 /* Assume that the context is filled with NULL pointer
12025 * before the first call.
12026 * smp->ctx.a[2] = NULL;
12027 * smp->ctx.a[3] = NULL;
12028 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012029 }
12030
12031 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
12032}
12033
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012034/* This function iterates over each parameter of the body. This requires
12035 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012036 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
12037 * contigous part of the body, and optionally ctx->a[2..3] to reference the
12038 * optional second part if the body wraps at the end of the buffer. An optional
12039 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012040 */
12041static int
12042smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
12043{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012044 struct http_msg *msg;
12045 unsigned long len;
12046 unsigned long block1;
12047 char *body;
12048 const char *name;
12049 int name_len;
12050
12051 if (!args || (args[0].type && args[0].type != ARGT_STR))
12052 return 0;
12053
12054 name = "";
12055 name_len = 0;
12056 if (args[0].type == ARGT_STR) {
12057 name = args[0].data.str.str;
12058 name_len = args[0].data.str.len;
12059 }
12060
12061 if (!smp->ctx.a[0]) { // first call, find the query string
12062 CHECK_HTTP_MESSAGE_FIRST();
12063
12064 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010012065 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012066 else
Willy Tarreaube508f12016-03-10 11:47:01 +010012067 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012068
12069 len = http_body_bytes(msg);
12070 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
12071
12072 block1 = len;
12073 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
12074 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
12075
12076 if (block1 == len) {
12077 /* buffer is not wrapped (or empty) */
12078 smp->ctx.a[0] = body;
12079 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012080
12081 /* Assume that the context is filled with NULL pointer
12082 * before the first call.
12083 * smp->ctx.a[2] = NULL;
12084 * smp->ctx.a[3] = NULL;
12085 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012086 }
12087 else {
12088 /* buffer is wrapped, we need to defragment it */
12089 smp->ctx.a[0] = body;
12090 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012091 smp->ctx.a[2] = msg->chn->buf->data;
12092 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012093 }
12094 }
12095 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
12096}
12097
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012098/* Return the signed integer value for the specified url parameter (see url_param
12099 * above).
12100 */
12101static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012102smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012103{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012104 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012105
12106 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012107 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012108 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012109 }
12110
12111 return ret;
12112}
12113
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012114/* This produces a 32-bit hash of the concatenation of the first occurrence of
12115 * the Host header followed by the path component if it begins with a slash ('/').
12116 * This means that '*' will not be added, resulting in exactly the first Host
12117 * entry. If no Host header is found, then the path is used. The resulting value
12118 * is hashed using the url hash followed by a full avalanche hash and provides a
12119 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
12120 * high-traffic sites without having to store whole paths.
12121 * this differs from the base32 functions in that it includes the url parameters
12122 * as well as the path
12123 */
12124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012125smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012126{
Willy Tarreau15e91e12015-04-04 00:52:09 +020012127 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012128 struct hdr_ctx ctx;
12129 unsigned int hash = 0;
12130 char *ptr, *beg, *end;
12131 int len;
12132
12133 CHECK_HTTP_MESSAGE_FIRST();
12134
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012135 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012136 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012137 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012138 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12139 ptr = ctx.line + ctx.val;
12140 len = ctx.vlen;
12141 while (len--)
12142 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12143 }
12144
12145 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012146 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 +000012147 beg = http_get_path(txn);
12148 if (!beg)
12149 beg = end;
12150
12151 for (ptr = beg; ptr < end ; ptr++);
12152
12153 if (beg < ptr && *beg == '/') {
12154 while (beg < ptr)
12155 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12156 }
12157 hash = full_hash(hash);
12158
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012159 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012160 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012161 smp->flags = SMP_F_VOL_1ST;
12162 return 1;
12163}
12164
12165/* This concatenates the source address with the 32-bit hash of the Host and
12166 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12167 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12168 * on the source address length. The URL hash is stored before the address so
12169 * that in environments where IPv6 is insignificant, truncating the output to
12170 * 8 bytes would still work.
12171 */
12172static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012173smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012174{
12175 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012176 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012177
Dragan Dosendb5af612016-06-16 11:23:01 +020012178 if (!cli_conn)
12179 return 0;
12180
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012181 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012182 return 0;
12183
12184 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012185 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12186 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012187
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012188 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012189 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012190 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012191 temp->len += 4;
12192 break;
12193 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012194 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012195 temp->len += 16;
12196 break;
12197 default:
12198 return 0;
12199 }
12200
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012201 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012202 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012203 return 1;
12204}
12205
Willy Tarreau185b5c42012-04-26 15:11:51 +020012206/* This function is used to validate the arguments passed to any "hdr" fetch
12207 * keyword. These keywords support an optional positive or negative occurrence
12208 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12209 * is assumed that the types are already the correct ones. Returns 0 on error,
12210 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12211 * error message in case of error, that the caller is responsible for freeing.
12212 * The initial location must either be freeable or NULL.
12213 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012214int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012215{
12216 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012217 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012218 return 0;
12219 }
12220 return 1;
12221}
12222
Willy Tarreau276fae92013-07-25 14:36:01 +020012223/* takes an UINT value on input supposed to represent the time since EPOCH,
12224 * adds an optional offset found in args[0] and emits a string representing
12225 * the date in RFC-1123/5322 format.
12226 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012227static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012228{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012229 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012230 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12231 struct chunk *temp;
12232 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012233 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012234 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012235
12236 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012237 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012238 curr_date += args[0].data.sint;
12239
12240 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012241 if (!tm)
12242 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012243
12244 temp = get_trash_chunk();
12245 temp->len = snprintf(temp->str, temp->size - temp->len,
12246 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12247 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12248 tm->tm_hour, tm->tm_min, tm->tm_sec);
12249
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012250 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012251 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012252 return 1;
12253}
12254
Thierry FOURNIERad903512014-04-11 17:51:01 +020012255/* Match language range with language tag. RFC2616 14.4:
12256 *
12257 * A language-range matches a language-tag if it exactly equals
12258 * the tag, or if it exactly equals a prefix of the tag such
12259 * that the first tag character following the prefix is "-".
12260 *
12261 * Return 1 if the strings match, else return 0.
12262 */
12263static inline int language_range_match(const char *range, int range_len,
12264 const char *tag, int tag_len)
12265{
12266 const char *end = range + range_len;
12267 const char *tend = tag + tag_len;
12268 while (range < end) {
12269 if (*range == '-' && tag == tend)
12270 return 1;
12271 if (*range != *tag || tag == tend)
12272 return 0;
12273 range++;
12274 tag++;
12275 }
12276 /* Return true only if the last char of the tag is matched. */
12277 return tag == tend;
12278}
12279
12280/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012281static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012282{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012283 const char *al = smp->data.u.str.str;
12284 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012285 const char *token;
12286 int toklen;
12287 int qvalue;
12288 const char *str;
12289 const char *w;
12290 int best_q = 0;
12291
12292 /* Set the constant to the sample, because the output of the
12293 * function will be peek in the constant configuration string.
12294 */
12295 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012296 smp->data.u.str.size = 0;
12297 smp->data.u.str.str = "";
12298 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012299
12300 /* Parse the accept language */
12301 while (1) {
12302
12303 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012304 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012305 al++;
12306 if (al >= end)
12307 break;
12308
12309 /* Start of the fisrt word. */
12310 token = al;
12311
12312 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012313 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012314 al++;
12315 if (al == token)
12316 goto expect_comma;
12317
12318 /* Length of the token. */
12319 toklen = al - token;
12320 qvalue = 1000;
12321
12322 /* Check if the token exists in the list. If the token not exists,
12323 * jump to the next token.
12324 */
12325 str = args[0].data.str.str;
12326 w = str;
12327 while (1) {
12328 if (*str == ';' || *str == '\0') {
12329 if (language_range_match(token, toklen, w, str-w))
12330 goto look_for_q;
12331 if (*str == '\0')
12332 goto expect_comma;
12333 w = str + 1;
12334 }
12335 str++;
12336 }
12337 goto expect_comma;
12338
12339look_for_q:
12340
12341 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012342 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012343 al++;
12344 if (al >= end)
12345 goto process_value;
12346
12347 /* If ',' is found, process the result */
12348 if (*al == ',')
12349 goto process_value;
12350
12351 /* If the character is different from ';', look
12352 * for the end of the header part in best effort.
12353 */
12354 if (*al != ';')
12355 goto expect_comma;
12356
12357 /* Assumes that the char is ';', now expect "q=". */
12358 al++;
12359
12360 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012361 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012362 al++;
12363 if (al >= end)
12364 goto process_value;
12365
12366 /* Expect 'q'. If no 'q', continue in best effort */
12367 if (*al != 'q')
12368 goto process_value;
12369 al++;
12370
12371 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012372 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012373 al++;
12374 if (al >= end)
12375 goto process_value;
12376
12377 /* Expect '='. If no '=', continue in best effort */
12378 if (*al != '=')
12379 goto process_value;
12380 al++;
12381
12382 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012383 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012384 al++;
12385 if (al >= end)
12386 goto process_value;
12387
12388 /* Parse the q value. */
12389 qvalue = parse_qvalue(al, &al);
12390
12391process_value:
12392
12393 /* If the new q value is the best q value, then store the associated
12394 * language in the response. If qvalue is the biggest value (1000),
12395 * break the process.
12396 */
12397 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012398 smp->data.u.str.str = (char *)w;
12399 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012400 if (qvalue >= 1000)
12401 break;
12402 best_q = qvalue;
12403 }
12404
12405expect_comma:
12406
12407 /* Expect comma or end. If the end is detected, quit the loop. */
12408 while (al < end && *al != ',')
12409 al++;
12410 if (al >= end)
12411 break;
12412
12413 /* Comma is found, jump it and restart the analyzer. */
12414 al++;
12415 }
12416
12417 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012418 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12419 smp->data.u.str.str = args[1].data.str.str;
12420 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012421 }
12422
12423 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012424 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012425}
12426
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012427/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012428static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012429{
12430 /* If the constant flag is set or if not size is avalaible at
12431 * the end of the buffer, copy the string in other buffer
12432 * before decoding.
12433 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012434 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012435 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012436 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12437 smp->data.u.str.str = str->str;
12438 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012439 smp->flags &= ~SMP_F_CONST;
12440 }
12441
12442 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012443 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12444 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012445 return 1;
12446}
12447
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012448static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12449{
12450 struct proxy *fe = strm_fe(smp->strm);
12451 int idx, i;
12452 struct cap_hdr *hdr;
12453 int len;
12454
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012455 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012456 return 0;
12457
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012458 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012459
12460 /* Check the availibity of the capture id. */
12461 if (idx > fe->nb_req_cap - 1)
12462 return 0;
12463
12464 /* Look for the original configuration. */
12465 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12466 hdr != NULL && i != idx ;
12467 i--, hdr = hdr->next);
12468 if (!hdr)
12469 return 0;
12470
12471 /* check for the memory allocation */
12472 if (smp->strm->req_cap[hdr->index] == NULL)
12473 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12474 if (smp->strm->req_cap[hdr->index] == NULL)
12475 return 0;
12476
12477 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012478 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012479 if (len > hdr->len)
12480 len = hdr->len;
12481
12482 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012483 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012484 smp->strm->req_cap[idx][len] = '\0';
12485
12486 return 1;
12487}
12488
12489static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12490{
12491 struct proxy *fe = strm_fe(smp->strm);
12492 int idx, i;
12493 struct cap_hdr *hdr;
12494 int len;
12495
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012496 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012497 return 0;
12498
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012499 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012500
12501 /* Check the availibity of the capture id. */
12502 if (idx > fe->nb_rsp_cap - 1)
12503 return 0;
12504
12505 /* Look for the original configuration. */
12506 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12507 hdr != NULL && i != idx ;
12508 i--, hdr = hdr->next);
12509 if (!hdr)
12510 return 0;
12511
12512 /* check for the memory allocation */
12513 if (smp->strm->res_cap[hdr->index] == NULL)
12514 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12515 if (smp->strm->res_cap[hdr->index] == NULL)
12516 return 0;
12517
12518 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012519 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012520 if (len > hdr->len)
12521 len = hdr->len;
12522
12523 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012524 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012525 smp->strm->res_cap[idx][len] = '\0';
12526
12527 return 1;
12528}
12529
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012530/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012531 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012532 * the relevant part of the request line accordingly. Then it updates various
12533 * pointers to the next elements which were moved, and the total buffer length.
12534 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012535 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12536 * error, though this can be revisited when this code is finally exploited.
12537 *
12538 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12539 * query string and 3 to replace uri.
12540 *
12541 * In query string case, the mark question '?' must be set at the start of the
12542 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012543 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012544int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012545 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012546{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012547 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012548 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012549 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012550 int delta;
12551
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012552 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012553 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012554 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012555 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12556
12557 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012558 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012559 txn->req.sl.rq.m_l += delta;
12560 txn->req.sl.rq.u += delta;
12561 txn->req.sl.rq.v += delta;
12562 break;
12563
12564 case 1: // path
12565 cur_ptr = http_get_path(txn);
12566 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012567 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012568
12569 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012570 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 +010012571 cur_end++;
12572
12573 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012574 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012575 txn->req.sl.rq.u_l += delta;
12576 txn->req.sl.rq.v += delta;
12577 break;
12578
12579 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012580 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012581 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012582 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12583 while (cur_ptr < cur_end && *cur_ptr != '?')
12584 cur_ptr++;
12585
12586 /* skip the question mark or indicate that we must insert it
12587 * (but only if the format string is not empty then).
12588 */
12589 if (cur_ptr < cur_end)
12590 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012591 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012592 offset = 0;
12593
12594 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012595 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012596 txn->req.sl.rq.u_l += delta;
12597 txn->req.sl.rq.v += delta;
12598 break;
12599
12600 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012601 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012602 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12603
12604 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012605 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012606 txn->req.sl.rq.u_l += delta;
12607 txn->req.sl.rq.v += delta;
12608 break;
12609
12610 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012611 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012612 }
12613
12614 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012615 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012616 txn->req.sl.rq.l += delta;
12617 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012618 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012619 return 0;
12620}
12621
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012622/* This function replace the HTTP status code and the associated message. The
12623 * variable <status> contains the new status code. This function never fails.
12624 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012625void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012626{
12627 struct http_txn *txn = s->txn;
12628 char *cur_ptr, *cur_end;
12629 int delta;
12630 char *res;
12631 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012632 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012633 int msg_len;
12634
12635 chunk_reset(&trash);
12636
12637 res = ultoa_o(status, trash.str, trash.size);
12638 c_l = res - trash.str;
12639
12640 trash.str[c_l] = ' ';
12641 trash.len = c_l + 1;
12642
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012643 /* Do we have a custom reason format string? */
12644 if (msg == NULL)
12645 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012646 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012647 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12648 trash.len += msg_len;
12649
12650 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12651 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12652
12653 /* commit changes and adjust message */
12654 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12655
12656 /* adjust res line offsets and lengths */
12657 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12658 txn->rsp.sl.st.c_l = c_l;
12659 txn->rsp.sl.st.r_l = msg_len;
12660
12661 delta = trash.len - (cur_end - cur_ptr);
12662 txn->rsp.sl.st.l += delta;
12663 txn->hdr_idx.v[0].len += delta;
12664 http_msg_move_end(&txn->rsp, delta);
12665}
12666
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012667/* This function executes one of the set-{method,path,query,uri} actions. It
12668 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012669 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012670 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012671 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12672 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012673 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012674enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012675 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012676{
12677 chunk_reset(&trash);
12678
12679 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012680 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012681 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012682 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012683
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012684 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012685 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012686}
12687
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012688/* This function is just a compliant action wrapper for "set-status". */
12689enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012690 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012691{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012692 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012693 return ACT_RET_CONT;
12694}
12695
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012696/* parse an http-request action among :
12697 * set-method
12698 * set-path
12699 * set-query
12700 * set-uri
12701 *
12702 * All of them accept a single argument of type string representing a log-format.
12703 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12704 * 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 +020012705 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012706 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012707enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12708 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012709{
12710 int cur_arg = *orig_arg;
12711
Thierry FOURNIER42148732015-09-02 17:17:33 +020012712 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012713
12714 switch (args[0][4]) {
12715 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012716 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012717 rule->action_ptr = http_action_set_req_line;
12718 break;
12719 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012720 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012721 rule->action_ptr = http_action_set_req_line;
12722 break;
12723 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012724 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012725 rule->action_ptr = http_action_set_req_line;
12726 break;
12727 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012728 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012729 rule->action_ptr = http_action_set_req_line;
12730 break;
12731 default:
12732 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012733 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012734 }
12735
12736 if (!*args[cur_arg] ||
12737 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12738 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012739 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012740 }
12741
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012742 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012743 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012744 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012745 (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 +010012746 return ACT_RET_PRS_ERR;
12747 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012748
12749 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012750 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012751}
12752
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012753/* parse set-status action:
12754 * This action accepts a single argument of type int representing
12755 * an http status code. It returns ACT_RET_PRS_OK on success,
12756 * ACT_RET_PRS_ERR on error.
12757 */
12758enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12759 struct act_rule *rule, char **err)
12760{
12761 char *error;
12762
Thierry FOURNIER42148732015-09-02 17:17:33 +020012763 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012764 rule->action_ptr = action_http_set_status;
12765
12766 /* Check if an argument is available */
12767 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012768 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012769 return ACT_RET_PRS_ERR;
12770 }
12771
12772 /* convert status code as integer */
12773 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12774 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12775 memprintf(err, "expects an integer status code between 100 and 999");
12776 return ACT_RET_PRS_ERR;
12777 }
12778
12779 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012780
12781 /* set custom reason string */
12782 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12783 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12784 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12785 (*orig_arg)++;
12786 rule->arg.status.reason = strdup(args[*orig_arg]);
12787 (*orig_arg)++;
12788 }
12789
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012790 return ACT_RET_PRS_OK;
12791}
12792
Willy Tarreaua9083d02015-05-08 15:27:59 +020012793/* This function executes the "capture" action. It executes a fetch expression,
12794 * turns the result into a string and puts it in a capture slot. It always
12795 * returns 1. If an error occurs the action is cancelled, but the rule
12796 * processing continues.
12797 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012798enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012799 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012800{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012801 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012802 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012803 char **cap = s->req_cap;
12804 int len;
12805
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012806 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 +020012807 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012808 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012809
12810 if (cap[h->index] == NULL)
12811 cap[h->index] = pool_alloc2(h->pool);
12812
12813 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012814 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012815
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012816 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012817 if (len > h->len)
12818 len = h->len;
12819
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012820 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012821 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012822 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012823}
12824
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012825/* This function executes the "capture" action and store the result in a
12826 * capture slot if exists. It executes a fetch expression, turns the result
12827 * into a string and puts it in a capture slot. It always returns 1. If an
12828 * error occurs the action is cancelled, but the rule processing continues.
12829 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012830enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012831 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012832{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012833 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012834 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012835 char **cap = s->req_cap;
12836 struct proxy *fe = strm_fe(s);
12837 int len;
12838 int i;
12839
12840 /* Look for the original configuration. */
12841 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012842 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012843 i--, h = h->next);
12844 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012845 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012846
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012847 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 +020012848 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012849 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012850
12851 if (cap[h->index] == NULL)
12852 cap[h->index] = pool_alloc2(h->pool);
12853
12854 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012855 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012856
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012857 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012858 if (len > h->len)
12859 len = h->len;
12860
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012861 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012862 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012863 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012864}
12865
Willy Tarreaua9083d02015-05-08 15:27:59 +020012866/* parse an "http-request capture" action. It takes a single argument which is
12867 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012868 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012869 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012870 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012871enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12872 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012873{
12874 struct sample_expr *expr;
12875 struct cap_hdr *hdr;
12876 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012877 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012878
12879 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12880 if (strcmp(args[cur_arg], "if") == 0 ||
12881 strcmp(args[cur_arg], "unless") == 0)
12882 break;
12883
12884 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012885 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012886 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012887 }
12888
Willy Tarreaua9083d02015-05-08 15:27:59 +020012889 cur_arg = *orig_arg;
12890 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12891 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012892 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012893
12894 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12895 memprintf(err,
12896 "fetch method '%s' extracts information from '%s', none of which is available here",
12897 args[cur_arg-1], sample_src_names(expr->fetch->use));
12898 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012899 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012900 }
12901
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012902 if (!args[cur_arg] || !*args[cur_arg]) {
12903 memprintf(err, "expects 'len or 'id'");
12904 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012905 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012906 }
12907
Willy Tarreaua9083d02015-05-08 15:27:59 +020012908 if (strcmp(args[cur_arg], "len") == 0) {
12909 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012910
12911 if (!(px->cap & PR_CAP_FE)) {
12912 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012913 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012914 }
12915
12916 proxy->conf.args.ctx = ARGC_CAP;
12917
Willy Tarreaua9083d02015-05-08 15:27:59 +020012918 if (!args[cur_arg]) {
12919 memprintf(err, "missing length value");
12920 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012921 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012922 }
12923 /* we copy the table name for now, it will be resolved later */
12924 len = atoi(args[cur_arg]);
12925 if (len <= 0) {
12926 memprintf(err, "length must be > 0");
12927 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012928 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012929 }
12930 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012931
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012932 if (!len) {
12933 memprintf(err, "a positive 'len' argument is mandatory");
12934 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012935 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012936 }
12937
Vincent Bernat02779b62016-04-03 13:48:43 +020012938 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012939 hdr->next = px->req_cap;
12940 hdr->name = NULL; /* not a header capture */
12941 hdr->namelen = 0;
12942 hdr->len = len;
12943 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12944 hdr->index = px->nb_req_cap++;
12945
12946 px->req_cap = hdr;
12947 px->to_log |= LW_REQHDR;
12948
Thierry FOURNIER42148732015-09-02 17:17:33 +020012949 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012950 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012951 rule->arg.cap.expr = expr;
12952 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012953 }
12954
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012955 else if (strcmp(args[cur_arg], "id") == 0) {
12956 int id;
12957 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012958
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012959 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012960
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012961 if (!args[cur_arg]) {
12962 memprintf(err, "missing id value");
12963 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012964 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012965 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012966
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012967 id = strtol(args[cur_arg], &error, 10);
12968 if (*error != '\0') {
12969 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12970 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012971 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012972 }
12973 cur_arg++;
12974
12975 proxy->conf.args.ctx = ARGC_CAP;
12976
Thierry FOURNIER42148732015-09-02 17:17:33 +020012977 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012978 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012979 rule->arg.capid.expr = expr;
12980 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012981 }
12982
12983 else {
12984 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12985 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012986 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012987 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012988
12989 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012990 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012991}
12992
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012993/* This function executes the "capture" action and store the result in a
12994 * capture slot if exists. It executes a fetch expression, turns the result
12995 * into a string and puts it in a capture slot. It always returns 1. If an
12996 * error occurs the action is cancelled, but the rule processing continues.
12997 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012998enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012999 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013000{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013001 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013002 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013003 char **cap = s->res_cap;
13004 struct proxy *fe = strm_fe(s);
13005 int len;
13006 int i;
13007
13008 /* Look for the original configuration. */
13009 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013010 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013011 i--, h = h->next);
13012 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013013 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013014
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013015 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 +020013016 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013017 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013018
13019 if (cap[h->index] == NULL)
13020 cap[h->index] = pool_alloc2(h->pool);
13021
13022 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013023 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013024
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013025 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013026 if (len > h->len)
13027 len = h->len;
13028
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013029 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013030 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013031 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013032}
13033
13034/* parse an "http-response capture" action. It takes a single argument which is
13035 * a sample fetch expression. It stores the expression into arg->act.p[0] and
13036 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013037 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013038 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013039enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
13040 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013041{
13042 struct sample_expr *expr;
13043 int cur_arg;
13044 int id;
13045 char *error;
13046
13047 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
13048 if (strcmp(args[cur_arg], "if") == 0 ||
13049 strcmp(args[cur_arg], "unless") == 0)
13050 break;
13051
13052 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013053 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013054 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013055 }
13056
13057 cur_arg = *orig_arg;
13058 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
13059 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013060 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013061
13062 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
13063 memprintf(err,
13064 "fetch method '%s' extracts information from '%s', none of which is available here",
13065 args[cur_arg-1], sample_src_names(expr->fetch->use));
13066 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013067 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013068 }
13069
13070 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013071 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013072 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013073 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013074 }
13075
13076 if (strcmp(args[cur_arg], "id") != 0) {
13077 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
13078 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013079 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013080 }
13081
13082 cur_arg++;
13083
13084 if (!args[cur_arg]) {
13085 memprintf(err, "missing id value");
13086 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013087 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013088 }
13089
13090 id = strtol(args[cur_arg], &error, 10);
13091 if (*error != '\0') {
13092 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
13093 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013094 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013095 }
13096 cur_arg++;
13097
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013098 proxy->conf.args.ctx = ARGC_CAP;
13099
Thierry FOURNIER42148732015-09-02 17:17:33 +020013100 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013101 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013102 rule->arg.capid.expr = expr;
13103 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013104
13105 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013106 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013107}
13108
William Lallemand73025dd2014-04-24 14:38:37 +020013109/*
13110 * Return the struct http_req_action_kw associated to a keyword.
13111 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013112struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013113{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013114 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013115}
13116
13117/*
13118 * Return the struct http_res_action_kw associated to a keyword.
13119 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013120struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013121{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013122 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013123}
13124
Willy Tarreau12207b32016-11-22 19:48:51 +010013125
13126/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
13127 * now.
13128 */
13129static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
13130{
13131 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13132 return 1;
13133
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013134 if (*args[2]) {
13135 struct proxy *px;
13136
13137 px = proxy_find_by_name(args[2], 0, 0);
13138 if (px)
13139 appctx->ctx.errors.iid = px->uuid;
13140 else
13141 appctx->ctx.errors.iid = atoi(args[2]);
13142
13143 if (!appctx->ctx.errors.iid) {
13144 appctx->ctx.cli.msg = "No such proxy.\n";
13145 appctx->st0 = CLI_ST_PRINT;
13146 return 1;
13147 }
13148 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013149 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013150 appctx->ctx.errors.iid = -1; // dump all proxies
13151
Willy Tarreau35069f82016-11-25 09:16:37 +010013152 appctx->ctx.errors.flag = 0;
13153 if (strcmp(args[3], "request") == 0)
13154 appctx->ctx.errors.flag |= 4; // ignore response
13155 else if (strcmp(args[3], "response") == 0)
13156 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013157 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013158 return 0;
13159}
13160
13161/* This function dumps all captured errors onto the stream interface's
13162 * read buffer. It returns 0 if the output buffer is full and it needs
13163 * to be called again, otherwise non-zero.
13164 */
13165static int cli_io_handler_show_errors(struct appctx *appctx)
13166{
13167 struct stream_interface *si = appctx->owner;
13168 extern const char *monthname[12];
13169
13170 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13171 return 1;
13172
13173 chunk_reset(&trash);
13174
13175 if (!appctx->ctx.errors.px) {
13176 /* the function had not been called yet, let's prepare the
13177 * buffer for a response.
13178 */
13179 struct tm tm;
13180
13181 get_localtime(date.tv_sec, &tm);
13182 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13183 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13184 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13185 error_snapshot_id);
13186
13187 if (bi_putchk(si_ic(si), &trash) == -1) {
13188 /* Socket buffer full. Let's try again later from the same point */
13189 si_applet_cant_put(si);
13190 return 0;
13191 }
13192
13193 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013194 appctx->ctx.errors.bol = 0;
13195 appctx->ctx.errors.ptr = -1;
13196 }
13197
13198 /* we have two inner loops here, one for the proxy, the other one for
13199 * the buffer.
13200 */
13201 while (appctx->ctx.errors.px) {
13202 struct error_snapshot *es;
13203
Willy Tarreau35069f82016-11-25 09:16:37 +010013204 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013205 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013206 if (appctx->ctx.errors.flag & 2) // skip req
13207 goto next;
13208 }
13209 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013210 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013211 if (appctx->ctx.errors.flag & 4) // skip resp
13212 goto next;
13213 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013214
13215 if (!es->when.tv_sec)
13216 goto next;
13217
13218 if (appctx->ctx.errors.iid >= 0 &&
13219 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13220 es->oe->uuid != appctx->ctx.errors.iid)
13221 goto next;
13222
13223 if (appctx->ctx.errors.ptr < 0) {
13224 /* just print headers now */
13225
13226 char pn[INET6_ADDRSTRLEN];
13227 struct tm tm;
13228 int port;
13229
13230 get_localtime(es->when.tv_sec, &tm);
13231 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13232 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13233 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13234
13235 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13236 case AF_INET:
13237 case AF_INET6:
13238 port = get_host_port(&es->src);
13239 break;
13240 default:
13241 port = 0;
13242 }
13243
Willy Tarreau35069f82016-11-25 09:16:37 +010013244 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013245 case 0:
13246 chunk_appendf(&trash,
13247 " frontend %s (#%d): invalid request\n"
13248 " backend %s (#%d)",
13249 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13250 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13251 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13252 break;
13253 case 1:
13254 chunk_appendf(&trash,
13255 " backend %s (#%d): invalid response\n"
13256 " frontend %s (#%d)",
13257 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13258 es->oe->id, es->oe->uuid);
13259 break;
13260 }
13261
13262 chunk_appendf(&trash,
13263 ", server %s (#%d), event #%u\n"
13264 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013265 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013266 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13267 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13268 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13269 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13270 es->ev_id,
13271 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013272 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013273 es->m_clen, es->m_blen,
13274 es->b_flags, es->b_out, es->b_tot,
13275 es->len, es->b_wrap, es->pos);
13276
13277 if (bi_putchk(si_ic(si), &trash) == -1) {
13278 /* Socket buffer full. Let's try again later from the same point */
13279 si_applet_cant_put(si);
13280 return 0;
13281 }
13282 appctx->ctx.errors.ptr = 0;
13283 appctx->ctx.errors.sid = es->sid;
13284 }
13285
13286 if (appctx->ctx.errors.sid != es->sid) {
13287 /* the snapshot changed while we were dumping it */
13288 chunk_appendf(&trash,
13289 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13290 if (bi_putchk(si_ic(si), &trash) == -1) {
13291 si_applet_cant_put(si);
13292 return 0;
13293 }
13294 goto next;
13295 }
13296
13297 /* OK, ptr >= 0, so we have to dump the current line */
13298 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13299 int newptr;
13300 int newline;
13301
13302 newline = appctx->ctx.errors.bol;
13303 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13304 if (newptr == appctx->ctx.errors.ptr)
13305 return 0;
13306
13307 if (bi_putchk(si_ic(si), &trash) == -1) {
13308 /* Socket buffer full. Let's try again later from the same point */
13309 si_applet_cant_put(si);
13310 return 0;
13311 }
13312 appctx->ctx.errors.ptr = newptr;
13313 appctx->ctx.errors.bol = newline;
13314 };
13315 next:
13316 appctx->ctx.errors.bol = 0;
13317 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013318 appctx->ctx.errors.flag ^= 1;
13319 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013320 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013321 }
13322
13323 /* dump complete */
13324 return 1;
13325}
13326
13327/* register cli keywords */
13328static struct cli_kw_list cli_kws = {{ },{
13329 { { "show", "errors", NULL },
13330 "show errors : report last request and response errors for each proxy",
13331 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13332 },
13333 {{},}
13334}};
13335
Willy Tarreau4a568972010-05-12 08:08:50 +020013336/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013337/* All supported ACL keywords must be declared here. */
13338/************************************************************************/
13339
13340/* Note: must not be declared <const> as its list will be overwritten.
13341 * Please take care of keeping this list alphabetically sorted.
13342 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013343static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013344 { "base", "base", PAT_MATCH_STR },
13345 { "base_beg", "base", PAT_MATCH_BEG },
13346 { "base_dir", "base", PAT_MATCH_DIR },
13347 { "base_dom", "base", PAT_MATCH_DOM },
13348 { "base_end", "base", PAT_MATCH_END },
13349 { "base_len", "base", PAT_MATCH_LEN },
13350 { "base_reg", "base", PAT_MATCH_REG },
13351 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013352
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013353 { "cook", "req.cook", PAT_MATCH_STR },
13354 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13355 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13356 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13357 { "cook_end", "req.cook", PAT_MATCH_END },
13358 { "cook_len", "req.cook", PAT_MATCH_LEN },
13359 { "cook_reg", "req.cook", PAT_MATCH_REG },
13360 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013361
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013362 { "hdr", "req.hdr", PAT_MATCH_STR },
13363 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13364 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13365 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13366 { "hdr_end", "req.hdr", PAT_MATCH_END },
13367 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13368 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13369 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013370
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013371 /* these two declarations uses strings with list storage (in place
13372 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13373 * and delete functions are relative to the list management. The parse
13374 * and match method are related to the corresponding fetch methods. This
13375 * is very particular ACL declaration mode.
13376 */
13377 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13378 { "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 +020013379
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013380 { "path", "path", PAT_MATCH_STR },
13381 { "path_beg", "path", PAT_MATCH_BEG },
13382 { "path_dir", "path", PAT_MATCH_DIR },
13383 { "path_dom", "path", PAT_MATCH_DOM },
13384 { "path_end", "path", PAT_MATCH_END },
13385 { "path_len", "path", PAT_MATCH_LEN },
13386 { "path_reg", "path", PAT_MATCH_REG },
13387 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013388
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013389 { "req_ver", "req.ver", PAT_MATCH_STR },
13390 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013391
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013392 { "scook", "res.cook", PAT_MATCH_STR },
13393 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13394 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13395 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13396 { "scook_end", "res.cook", PAT_MATCH_END },
13397 { "scook_len", "res.cook", PAT_MATCH_LEN },
13398 { "scook_reg", "res.cook", PAT_MATCH_REG },
13399 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013400
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013401 { "shdr", "res.hdr", PAT_MATCH_STR },
13402 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13403 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13404 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13405 { "shdr_end", "res.hdr", PAT_MATCH_END },
13406 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13407 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13408 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013409
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013410 { "url", "url", PAT_MATCH_STR },
13411 { "url_beg", "url", PAT_MATCH_BEG },
13412 { "url_dir", "url", PAT_MATCH_DIR },
13413 { "url_dom", "url", PAT_MATCH_DOM },
13414 { "url_end", "url", PAT_MATCH_END },
13415 { "url_len", "url", PAT_MATCH_LEN },
13416 { "url_reg", "url", PAT_MATCH_REG },
13417 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013418
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013419 { "urlp", "urlp", PAT_MATCH_STR },
13420 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13421 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13422 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13423 { "urlp_end", "urlp", PAT_MATCH_END },
13424 { "urlp_len", "urlp", PAT_MATCH_LEN },
13425 { "urlp_reg", "urlp", PAT_MATCH_REG },
13426 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013427
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013428 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013429}};
13430
13431/************************************************************************/
13432/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013433/************************************************************************/
13434/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013435static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013436 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013437 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013438 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13439
Willy Tarreau87b09662015-04-03 00:22:06 +020013440 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013441 { "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 +020013442
13443 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013444 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13445 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13446 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013447
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013448 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13449 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013450
Willy Tarreau409bcde2013-01-08 00:31:00 +010013451 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13452 * are only here to match the ACL's name, are request-only and are used
13453 * for ACL compatibility only.
13454 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013455 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13456 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013457 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13458 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013459
13460 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13461 * only here to match the ACL's name, are request-only and are used for
13462 * ACL compatibility only.
13463 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013464 { "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 +020013465 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013466 { "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 +020013467 { "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 +010013468
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013469 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013470 { "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 +010013471 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013472 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013473 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013474 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013475
13476 /* HTTP protocol on the request path */
13477 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013478 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013479
13480 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013481 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13482 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013483
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013484 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013485 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13486 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013487 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013488
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020013489 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020013490 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13491
Willy Tarreau18ed2562013-01-14 15:56:36 +010013492 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013493 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13494 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013495
Willy Tarreau18ed2562013-01-14 15:56:36 +010013496 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013497 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013498 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13499 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013500
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013501 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013502 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013503 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013504 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013505 { "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 +010013506 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013507 { "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 +010013508
13509 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013510 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013511 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13512 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013513
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013514 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013515 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013516 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013517 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013518 { "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 +010013519 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013520 { "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 +010013521
Willy Tarreau409bcde2013-01-08 00:31:00 +010013522 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013523 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013524 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13525 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013526 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013527
13528 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013529 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013530 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013531 { "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 +020013532 { "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 +010013533
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013534 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013535 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013536 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013537 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013538 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013539 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013540 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013541 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13542 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013543 { "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 +010013544 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013545}};
13546
Willy Tarreau8797c062007-05-07 00:55:35 +020013547
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013548/************************************************************************/
13549/* All supported converter keywords must be declared here. */
13550/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013551/* Note: must not be declared <const> as its list will be overwritten */
13552static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013553 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013554 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013555 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13556 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013557 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013558 { NULL, NULL, 0, 0, 0 },
13559}};
13560
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013561
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013562/************************************************************************/
13563/* All supported http-request action keywords must be declared here. */
13564/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013565struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013566 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013567 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013568 { "set-method", parse_set_req_line },
13569 { "set-path", parse_set_req_line },
13570 { "set-query", parse_set_req_line },
13571 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013572 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013573 }
13574};
13575
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013576struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013577 .kw = {
13578 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013579 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013580 { NULL, NULL }
13581 }
13582};
13583
Willy Tarreau8797c062007-05-07 00:55:35 +020013584__attribute__((constructor))
13585static void __http_protocol_init(void)
13586{
13587 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013588 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013589 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013590 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013591 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013592 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013593}
13594
13595
Willy Tarreau58f10d72006-12-04 02:26:12 +010013596/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013597 * Local variables:
13598 * c-indent-level: 8
13599 * c-basic-offset: 8
13600 * End:
13601 */