blob: e0ed2dab67421fecf2e9d0462fe03760975e8e39 [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 /*
6241 * 2: check for cacheability.
6242 */
6243
6244 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006245 case 100:
6246 /*
6247 * We may be facing a 100-continue response, in which case this
6248 * is not the right response, and we're waiting for the next one.
6249 * Let's allow this response to go to the client and wait for the
6250 * next one.
6251 */
6252 hdr_idx_init(&txn->hdr_idx);
6253 msg->next -= channel_forward(rep, msg->next);
6254 msg->msg_state = HTTP_MSG_RPBEFORE;
6255 txn->status = 0;
6256 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006257 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006258 goto next_one;
6259
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006260 case 200:
6261 case 203:
6262 case 206:
6263 case 300:
6264 case 301:
6265 case 410:
6266 /* RFC2616 @13.4:
6267 * "A response received with a status code of
6268 * 200, 203, 206, 300, 301 or 410 MAY be stored
6269 * by a cache (...) unless a cache-control
6270 * directive prohibits caching."
6271 *
6272 * RFC2616 @9.5: POST method :
6273 * "Responses to this method are not cacheable,
6274 * unless the response includes appropriate
6275 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006276 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006277 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006278 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006279 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6280 break;
6281 default:
6282 break;
6283 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006284
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006285 /*
6286 * 3: we may need to capture headers
6287 */
6288 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006289 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006290 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006291 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006292
Willy Tarreau557f1992015-05-01 10:05:17 +02006293 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6294 * by RFC7230#3.3.3 :
6295 *
6296 * The length of a message body is determined by one of the following
6297 * (in order of precedence):
6298 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006299 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6300 * the connection will become a tunnel immediately after the empty
6301 * line that concludes the header fields. A client MUST ignore
6302 * any Content-Length or Transfer-Encoding header fields received
6303 * in such a message. Any 101 response (Switching Protocols) is
6304 * managed in the same manner.
6305 *
6306 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006307 * (Informational), 204 (No Content), or 304 (Not Modified) status
6308 * code is always terminated by the first empty line after the
6309 * header fields, regardless of the header fields present in the
6310 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006311 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006312 * 3. If a Transfer-Encoding header field is present and the chunked
6313 * transfer coding (Section 4.1) is the final encoding, the message
6314 * body length is determined by reading and decoding the chunked
6315 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006316 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006317 * If a Transfer-Encoding header field is present in a response and
6318 * the chunked transfer coding is not the final encoding, the
6319 * message body length is determined by reading the connection until
6320 * it is closed by the server. If a Transfer-Encoding header field
6321 * is present in a request and the chunked transfer coding is not
6322 * the final encoding, the message body length cannot be determined
6323 * reliably; the server MUST respond with the 400 (Bad Request)
6324 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006325 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006326 * If a message is received with both a Transfer-Encoding and a
6327 * Content-Length header field, the Transfer-Encoding overrides the
6328 * Content-Length. Such a message might indicate an attempt to
6329 * perform request smuggling (Section 9.5) or response splitting
6330 * (Section 9.4) and ought to be handled as an error. A sender MUST
6331 * remove the received Content-Length field prior to forwarding such
6332 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006333 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006334 * 4. If a message is received without Transfer-Encoding and with
6335 * either multiple Content-Length header fields having differing
6336 * field-values or a single Content-Length header field having an
6337 * invalid value, then the message framing is invalid and the
6338 * recipient MUST treat it as an unrecoverable error. If this is a
6339 * request message, the server MUST respond with a 400 (Bad Request)
6340 * status code and then close the connection. If this is a response
6341 * message received by a proxy, the proxy MUST close the connection
6342 * to the server, discard the received response, and send a 502 (Bad
6343 * Gateway) response to the client. If this is a response message
6344 * received by a user agent, the user agent MUST close the
6345 * connection to the server and discard the received response.
6346 *
6347 * 5. If a valid Content-Length header field is present without
6348 * Transfer-Encoding, its decimal value defines the expected message
6349 * body length in octets. If the sender closes the connection or
6350 * the recipient times out before the indicated number of octets are
6351 * received, the recipient MUST consider the message to be
6352 * incomplete and close the connection.
6353 *
6354 * 6. If this is a request message and none of the above are true, then
6355 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006356 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006357 * 7. Otherwise, this is a response message without a declared message
6358 * body length, so the message body length is determined by the
6359 * number of octets received prior to the server closing the
6360 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006361 */
6362
6363 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006364 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006365 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006366 * FIXME: should we parse anyway and return an error on chunked encoding ?
6367 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006368 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6369 txn->status == 101)) {
6370 /* Either we've established an explicit tunnel, or we're
6371 * switching the protocol. In both cases, we're very unlikely
6372 * to understand the next protocols. We have to switch to tunnel
6373 * mode, so that we transfer the request and responses then let
6374 * this protocol pass unmodified. When we later implement specific
6375 * parsers for such protocols, we'll want to check the Upgrade
6376 * header which contains information about that protocol for
6377 * responses with status 101 (eg: see RFC2817 about TLS).
6378 */
6379 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6380 msg->flags |= HTTP_MSGF_XFER_LEN;
6381 goto end;
6382 }
6383
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006384 if (txn->meth == HTTP_METH_HEAD ||
6385 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006386 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006387 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006388 goto skip_content_length;
6389 }
6390
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006391 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006392 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006393 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006394 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006395 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6396 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006397 /* bad transfer-encoding (chunked followed by something else) */
6398 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006399 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006400 break;
6401 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006402 }
6403
Willy Tarreau1c913912015-04-30 10:57:51 +02006404 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006405 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006406 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006407 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6408 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6409 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006410 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006411 signed long long cl;
6412
Willy Tarreauad14f752011-09-02 20:33:27 +02006413 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006414 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006415 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006416 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006417
Willy Tarreauad14f752011-09-02 20:33:27 +02006418 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006419 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006420 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006421 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006422
Willy Tarreauad14f752011-09-02 20:33:27 +02006423 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006424 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006425 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006426 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006427
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006428 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006429 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006431 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006432
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006433 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006434 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006435 }
6436
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006437 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006438 /* Now we have to check if we need to modify the Connection header.
6439 * This is more difficult on the response than it is on the request,
6440 * because we can have two different HTTP versions and we don't know
6441 * how the client will interprete a response. For instance, let's say
6442 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6443 * HTTP/1.1 response without any header. Maybe it will bound itself to
6444 * HTTP/1.0 because it only knows about it, and will consider the lack
6445 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6446 * the lack of header as a keep-alive. Thus we will use two flags
6447 * indicating how a request MAY be understood by the client. In case
6448 * of multiple possibilities, we'll fix the header to be explicit. If
6449 * ambiguous cases such as both close and keepalive are seen, then we
6450 * will fall back to explicit close. Note that we won't take risks with
6451 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006452 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006453 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006454 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6455 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6456 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6457 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006458 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006459
Willy Tarreau70dffda2014-01-30 03:07:23 +01006460 /* this situation happens when combining pretend-keepalive with httpclose. */
6461 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006462 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006463 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006464 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6465
Willy Tarreau60466522010-01-18 19:08:45 +01006466 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006467 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006468 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6469 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006470
Willy Tarreau60466522010-01-18 19:08:45 +01006471 /* now adjust header transformations depending on current state */
6472 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6473 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6474 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006475 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006476 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006477 }
Willy Tarreau60466522010-01-18 19:08:45 +01006478 else { /* SCL / KAL */
6479 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006480 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006481 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006482 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006483
Willy Tarreau60466522010-01-18 19:08:45 +01006484 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006485 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006486
Willy Tarreau60466522010-01-18 19:08:45 +01006487 /* Some keep-alive responses are converted to Server-close if
6488 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006489 */
Willy Tarreau60466522010-01-18 19:08:45 +01006490 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6491 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006492 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006493 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006494 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006495 }
6496
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006497 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006498 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006499 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006500
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006501 /* end of job, return OK */
6502 rep->analysers &= ~an_bit;
6503 rep->analyse_exp = TICK_ETERNITY;
6504 channel_auto_close(rep);
6505 return 1;
6506
6507 abort_keep_alive:
6508 /* A keep-alive request to the server failed on a network error.
6509 * The client is required to retry. We need to close without returning
6510 * any other information so that the client retries.
6511 */
6512 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006513 rep->analysers &= AN_RES_FLT_END;
6514 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006515 channel_auto_close(rep);
6516 s->logs.logwait = 0;
6517 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006518 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006519 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006520 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006521 return 0;
6522}
6523
6524/* This function performs all the processing enabled for the current response.
6525 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006526 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006527 * other functions. It works like process_request (see indications above).
6528 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006529int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006530{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006531 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006532 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006533 struct http_msg *msg = &txn->rsp;
6534 struct proxy *cur_proxy;
6535 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006536 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006537
Willy Tarreau87b09662015-04-03 00:22:06 +02006538 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 +02006539 now_ms, __FUNCTION__,
6540 s,
6541 rep,
6542 rep->rex, rep->wex,
6543 rep->flags,
6544 rep->buf->i,
6545 rep->analysers);
6546
6547 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6548 return 0;
6549
Willy Tarreau70730dd2014-04-24 18:06:27 +02006550 /* The stats applet needs to adjust the Connection header but we don't
6551 * apply any filter there.
6552 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006553 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6554 rep->analysers &= ~an_bit;
6555 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006556 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006557 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006558
Willy Tarreau58975672014-04-24 21:13:57 +02006559 /*
6560 * We will have to evaluate the filters.
6561 * As opposed to version 1.2, now they will be evaluated in the
6562 * filters order and not in the header order. This means that
6563 * each filter has to be validated among all headers.
6564 *
6565 * Filters are tried with ->be first, then with ->fe if it is
6566 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006567 *
6568 * Maybe we are in resume condiion. In this case I choose the
6569 * "struct proxy" which contains the rule list matching the resume
6570 * pointer. If none of theses "struct proxy" match, I initialise
6571 * the process with the first one.
6572 *
6573 * In fact, I check only correspondance betwwen the current list
6574 * pointer and the ->fe rule list. If it doesn't match, I initialize
6575 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006576 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006577 if (s->current_rule_list == &sess->fe->http_res_rules)
6578 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006579 else
6580 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006581 while (1) {
6582 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006583
Willy Tarreau58975672014-04-24 21:13:57 +02006584 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006585 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006586 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006587
Willy Tarreau51d861a2015-05-22 17:30:48 +02006588 if (ret == HTTP_RULE_RES_BADREQ)
6589 goto return_srv_prx_502;
6590
6591 if (ret == HTTP_RULE_RES_DONE) {
6592 rep->analysers &= ~an_bit;
6593 rep->analyse_exp = TICK_ETERNITY;
6594 return 1;
6595 }
6596 }
6597
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006598 /* we need to be called again. */
6599 if (ret == HTTP_RULE_RES_YIELD) {
6600 channel_dont_close(rep);
6601 return 0;
6602 }
6603
Willy Tarreau58975672014-04-24 21:13:57 +02006604 /* try headers filters */
6605 if (rule_set->rsp_exp != NULL) {
6606 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6607 return_bad_resp:
6608 if (objt_server(s->target)) {
6609 objt_server(s->target)->counters.failed_resp++;
6610 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006611 }
Willy Tarreau58975672014-04-24 21:13:57 +02006612 s->be->be_counters.failed_resp++;
6613 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006614 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006615 txn->status = 502;
6616 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006617 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006618 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006619 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006620 if (!(s->flags & SF_ERR_MASK))
6621 s->flags |= SF_ERR_PRXCOND;
6622 if (!(s->flags & SF_FINST_MASK))
6623 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006624 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006625 }
Willy Tarreau58975672014-04-24 21:13:57 +02006626 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 /* has the response been denied ? */
6629 if (txn->flags & TX_SVDENY) {
6630 if (objt_server(s->target))
6631 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006632
Willy Tarreau58975672014-04-24 21:13:57 +02006633 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006634 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006635 if (sess->listener->counters)
6636 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006637
Willy Tarreau58975672014-04-24 21:13:57 +02006638 goto return_srv_prx_502;
6639 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006640
Willy Tarreau58975672014-04-24 21:13:57 +02006641 /* add response headers from the rule sets in the same order */
6642 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006643 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006644 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006645 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006646 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006647 ret = acl_pass(ret);
6648 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6649 ret = !ret;
6650 if (!ret)
6651 continue;
6652 }
6653 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6654 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006655 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006656
Willy Tarreau58975672014-04-24 21:13:57 +02006657 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006658 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006659 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006660 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006661 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006662
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006663 /* After this point, this anayzer can't return yield, so we can
6664 * remove the bit corresponding to this analyzer from the list.
6665 *
6666 * Note that the intermediate returns and goto found previously
6667 * reset the analyzers.
6668 */
6669 rep->analysers &= ~an_bit;
6670 rep->analyse_exp = TICK_ETERNITY;
6671
Willy Tarreau58975672014-04-24 21:13:57 +02006672 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006673 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006674 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006675
Willy Tarreau58975672014-04-24 21:13:57 +02006676 /*
6677 * Now check for a server cookie.
6678 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006679 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006680 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006681
Willy Tarreau58975672014-04-24 21:13:57 +02006682 /*
6683 * Check for cache-control or pragma headers if required.
6684 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006685 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 +02006686 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 /*
6689 * Add server cookie in the response if needed
6690 */
6691 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6692 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006693 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006694 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6695 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6696 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6697 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6698 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006699 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006700 /* the server is known, it's not the one the client requested, or the
6701 * cookie's last seen date needs to be refreshed. We have to
6702 * insert a set-cookie here, except if we want to insert only on POST
6703 * requests and this one isn't. Note that servers which don't have cookies
6704 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 */
Willy Tarreau58975672014-04-24 21:13:57 +02006706 if (!objt_server(s->target)->cookie) {
6707 chunk_printf(&trash,
6708 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6709 s->be->cookie_name);
6710 }
6711 else {
6712 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6715 /* emit last_date, which is mandatory */
6716 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6717 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6718 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006719
Willy Tarreau58975672014-04-24 21:13:57 +02006720 if (s->be->cookie_maxlife) {
6721 /* emit first_date, which is either the original one or
6722 * the current date.
6723 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006724 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006725 s30tob64(txn->cookie_first_date ?
6726 txn->cookie_first_date >> 2 :
6727 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006728 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006729 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006730 }
Willy Tarreau58975672014-04-24 21:13:57 +02006731 chunk_appendf(&trash, "; path=/");
6732 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006733
Willy Tarreau58975672014-04-24 21:13:57 +02006734 if (s->be->cookie_domain)
6735 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006736
Willy Tarreau58975672014-04-24 21:13:57 +02006737 if (s->be->ck_opts & PR_CK_HTTPONLY)
6738 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006739
Willy Tarreau58975672014-04-24 21:13:57 +02006740 if (s->be->ck_opts & PR_CK_SECURE)
6741 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006742
Willy Tarreau58975672014-04-24 21:13:57 +02006743 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6744 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006745
Willy Tarreau58975672014-04-24 21:13:57 +02006746 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006747 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006748 /* the server did not change, only the date was updated */
6749 txn->flags |= TX_SCK_UPDATED;
6750 else
6751 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006752
Willy Tarreau58975672014-04-24 21:13:57 +02006753 /* Here, we will tell an eventual cache on the client side that we don't
6754 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6755 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6756 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006757 */
Willy Tarreau58975672014-04-24 21:13:57 +02006758 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006759
Willy Tarreau58975672014-04-24 21:13:57 +02006760 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006761
Willy Tarreau58975672014-04-24 21:13:57 +02006762 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6763 "Cache-control: private", 22) < 0))
6764 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006765 }
Willy Tarreau58975672014-04-24 21:13:57 +02006766 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767
Willy Tarreau58975672014-04-24 21:13:57 +02006768 /*
6769 * Check if result will be cacheable with a cookie.
6770 * We'll block the response if security checks have caught
6771 * nasty things such as a cacheable cookie.
6772 */
6773 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6774 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6775 (s->be->options & PR_O_CHK_CACHE)) {
6776 /* we're in presence of a cacheable response containing
6777 * a set-cookie header. We'll block it as requested by
6778 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006779 */
Willy Tarreau58975672014-04-24 21:13:57 +02006780 if (objt_server(s->target))
6781 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006782
Willy Tarreau58975672014-04-24 21:13:57 +02006783 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006784 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006785 if (sess->listener->counters)
6786 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6789 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6790 send_log(s->be, LOG_ALERT,
6791 "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 goto return_srv_prx_502;
6794 }
Willy Tarreau03945942009-12-22 16:50:27 +01006795
Willy Tarreau70730dd2014-04-24 18:06:27 +02006796 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006797 /*
6798 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6799 * If an "Upgrade" token is found, the header is left untouched in order
6800 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006801 * if anything but "Upgrade" is present in the Connection header. We don't
6802 * want to touch any 101 response either since it's switching to another
6803 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006804 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006805 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006806 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006807 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006808 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6809 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006810
Willy Tarreau58975672014-04-24 21:13:57 +02006811 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6812 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6813 /* we want a keep-alive response here. Keep-alive header
6814 * required if either side is not 1.1.
6815 */
6816 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6817 want_flags |= TX_CON_KAL_SET;
6818 }
6819 else {
6820 /* we want a close response here. Close header required if
6821 * the server is 1.1, regardless of the client.
6822 */
6823 if (msg->flags & HTTP_MSGF_VER_11)
6824 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006825 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826
Willy Tarreau58975672014-04-24 21:13:57 +02006827 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6828 http_change_connection_header(txn, msg, want_flags);
6829 }
6830
6831 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006832 /* Always enter in the body analyzer */
6833 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6834 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835
Willy Tarreau58975672014-04-24 21:13:57 +02006836 /* if the user wants to log as soon as possible, without counting
6837 * bytes from the server, then this is the right moment. We have
6838 * to temporarily assign bytes_out to log what we currently have.
6839 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006840 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006841 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6842 s->logs.bytes_out = txn->rsp.eoh;
6843 s->do_log(s);
6844 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006845 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006846 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006847}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848
Willy Tarreaud98cf932009-12-27 22:54:55 +01006849/* This function is an analyser which forwards response body (including chunk
6850 * sizes if any). It is called as soon as we must forward, even if we forward
6851 * zero byte. The only situation where it must not be called is when we're in
6852 * tunnel mode and we want to forward till the close. It's used both to forward
6853 * remaining data and to resync after end of body. It expects the msg_state to
6854 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006855 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006856 *
6857 * It is capable of compressing response data both in content-length mode and
6858 * in chunked mode. The state machines follows different flows depending on
6859 * whether content-length and chunked modes are used, since there are no
6860 * trailers in content-length :
6861 *
6862 * chk-mode cl-mode
6863 * ,----- BODY -----.
6864 * / \
6865 * V size > 0 V chk-mode
6866 * .--> SIZE -------------> DATA -------------> CRLF
6867 * | | size == 0 | last byte |
6868 * | v final crlf v inspected |
6869 * | TRAILERS -----------> DONE |
6870 * | |
6871 * `----------------------------------------------'
6872 *
6873 * Compression only happens in the DATA state, and must be flushed in final
6874 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6875 * is performed at once on final states for all bytes parsed, or when leaving
6876 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006877 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006878int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006879{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006880 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006881 struct http_txn *txn = s->txn;
6882 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006883 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006884
Christopher Faulet814d2702017-03-30 11:33:44 +02006885 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6886 now_ms, __FUNCTION__,
6887 s,
6888 res,
6889 res->rex, res->wex,
6890 res->flags,
6891 res->buf->i,
6892 res->analysers);
6893
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006894 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6895 return 0;
6896
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006897 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006898 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006899 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006900 /* Output closed while we were sending data. We must abort and
6901 * wake the other side up.
6902 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006903 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006904 msg->msg_state = HTTP_MSG_ERROR;
6905 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006906 return 1;
6907 }
6908
Willy Tarreau4fe41902010-06-07 22:27:41 +02006909 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006910 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006911
Christopher Fauletd7c91962015-04-30 11:48:27 +02006912 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006913 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6914 ? HTTP_MSG_CHUNK_SIZE
6915 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006916 }
6917
Willy Tarreauefdf0942014-04-24 20:08:57 +02006918 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006919 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006920 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006921 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006922 }
6923
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006924 if (msg->msg_state < HTTP_MSG_DONE) {
6925 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6926 ? http_msg_forward_chunked_body(s, msg)
6927 : http_msg_forward_body(s, msg));
6928 if (!ret)
6929 goto missing_data_or_waiting;
6930 if (ret < 0)
6931 goto return_bad_res;
6932 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006933
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006934 /* other states, DONE...TUNNEL */
6935 /* for keep-alive we don't want to forward closes on DONE */
6936 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6937 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6938 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006939
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006940 if (http_resync_states(s)) {
6941 /* some state changes occurred, maybe the analyser was disabled
6942 * too. */
6943 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6944 if (res->flags & CF_SHUTW) {
6945 /* response errors are most likely due to the
6946 * client aborting the transfer. */
6947 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006948 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006949 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006950 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006951 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006952 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006953 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006954 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006955 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006956
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006957 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006958 if (res->flags & CF_SHUTW)
6959 goto aborted_xfer;
6960
6961 /* stop waiting for data if the input is closed before the end. If the
6962 * client side was already closed, it means that the client has aborted,
6963 * so we don't want to count this as a server abort. Otherwise it's a
6964 * server abort.
6965 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006966 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006967 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006968 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006969 /* If we have some pending data, we continue the processing */
6970 if (!buffer_pending(res->buf)) {
6971 if (!(s->flags & SF_ERR_MASK))
6972 s->flags |= SF_ERR_SRVCL;
6973 s->be->be_counters.srv_aborts++;
6974 if (objt_server(s->target))
6975 objt_server(s->target)->counters.srv_aborts++;
6976 goto return_bad_res_stats_ok;
6977 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006978 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006979
Willy Tarreau40dba092010-03-04 18:14:51 +01006980 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006981 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006982 goto return_bad_res;
6983
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006984 /* When TE: chunked is used, we need to get there again to parse
6985 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006986 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6987 * or if there are filters registered on the stream, we don't want to
6988 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006989 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006990 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006991 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006992 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6993 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006994 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006995
Willy Tarreau5c620922011-05-11 19:56:11 +02006996 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006997 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006998 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006999 * modes are already handled by the stream sock layer. We must not do
7000 * this in content-length mode because it could present the MSG_MORE
7001 * flag with the last block of forwarded data, which would cause an
7002 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007003 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007004 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007005 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007006
Christopher Faulet69744d92017-03-30 10:54:35 +02007007 /* If there is neither content-length, nor transfer-encoding header
7008 * _AND_ there is no data filtering, we can safely forward all data
7009 * indefinitely. */
7010 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_DATA_FILTERS(s, res)) {
7011 buffer_flush(res->buf);
7012 channel_forward_forever(res);
7013 }
7014
Willy Tarreau87b09662015-04-03 00:22:06 +02007015 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007016 return 0;
7017
Willy Tarreau40dba092010-03-04 18:14:51 +01007018 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007019 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007020 if (objt_server(s->target))
7021 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007022
7023 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007024 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007025 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007026 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007027 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007028 res->analysers &= AN_RES_FLT_END;
7029 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 +01007030 if (objt_server(s->target))
7031 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007032
Willy Tarreaue7dff022015-04-03 01:14:29 +02007033 if (!(s->flags & SF_ERR_MASK))
7034 s->flags |= SF_ERR_PRXCOND;
7035 if (!(s->flags & SF_FINST_MASK))
7036 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007037 return 0;
7038
7039 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007040 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007041 txn->rsp.msg_state = HTTP_MSG_ERROR;
7042 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007043 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007044 res->analysers &= AN_RES_FLT_END;
7045 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 +01007046
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007047 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007048 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007049 if (objt_server(s->target))
7050 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007051
Willy Tarreaue7dff022015-04-03 01:14:29 +02007052 if (!(s->flags & SF_ERR_MASK))
7053 s->flags |= SF_ERR_CLICL;
7054 if (!(s->flags & SF_FINST_MASK))
7055 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007056 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007057}
7058
7059
7060static inline int
7061http_msg_forward_body(struct stream *s, struct http_msg *msg)
7062{
7063 struct channel *chn = msg->chn;
7064 int ret;
7065
7066 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7067
7068 if (msg->msg_state == HTTP_MSG_ENDING)
7069 goto ending;
7070
7071 /* Neither content-length, nor transfer-encoding was found, so we must
7072 * read the body until the server connection is closed. In that case, we
7073 * eat data as they come. Of course, this happens for response only. */
7074 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7075 unsigned long long len = (chn->buf->i - msg->next);
7076 msg->chunk_len += len;
7077 msg->body_len += len;
7078 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007079 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7080 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7081 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007082 msg->next += ret;
7083 msg->chunk_len -= ret;
7084 if (msg->chunk_len) {
7085 /* input empty or output full */
7086 if (chn->buf->i > msg->next)
7087 chn->flags |= CF_WAKE_WRITE;
7088 goto missing_data_or_waiting;
7089 }
7090
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007091 /* The server still sending data that should be filtered */
7092 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007093 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007094
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007095 msg->msg_state = HTTP_MSG_ENDING;
7096
7097 ending:
7098 /* we may have some pending data starting at res->buf->p such as a last
7099 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007100 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7101 /* default_ret */ msg->next,
7102 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007103 b_adv(chn->buf, ret);
7104 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007105 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7106 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007107 if (msg->next)
7108 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007109
Christopher Fauletda02e172015-12-04 09:25:05 +01007110 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7111 /* default_ret */ 1,
7112 /* on_error */ goto error,
7113 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007114 msg->msg_state = HTTP_MSG_DONE;
7115 return 1;
7116
7117 missing_data_or_waiting:
7118 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007119 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7120 /* default_ret */ msg->next,
7121 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007122 b_adv(chn->buf, ret);
7123 msg->next -= ret;
7124 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7125 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007126 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007127 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007128 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007129 return 0;
7130 error:
7131 return -1;
7132}
7133
7134static inline int
7135http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7136{
7137 struct channel *chn = msg->chn;
7138 int ret;
7139
7140 /* Here we have the guarantee to be in one of the following state:
7141 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7142 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7143
7144 switch_states:
7145 switch (msg->msg_state) {
7146 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007147 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7148 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7149 /* on_error */ goto error);
7150 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007151 msg->chunk_len -= ret;
7152 if (msg->chunk_len) {
7153 /* input empty or output full */
7154 if (chn->buf->i > msg->next)
7155 chn->flags |= CF_WAKE_WRITE;
7156 goto missing_data_or_waiting;
7157 }
7158
7159 /* nothing left to forward for this chunk*/
7160 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7161 /* fall through for HTTP_MSG_CHUNK_CRLF */
7162
7163 case HTTP_MSG_CHUNK_CRLF:
7164 /* we want the CRLF after the data */
7165 ret = http_skip_chunk_crlf(msg);
7166 if (ret == 0)
7167 goto missing_data_or_waiting;
7168 if (ret < 0)
7169 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007170 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007171 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7172 /* fall through for HTTP_MSG_CHUNK_SIZE */
7173
7174 case HTTP_MSG_CHUNK_SIZE:
7175 /* read the chunk size and assign it to ->chunk_len,
7176 * then set ->next to point to the body and switch to
7177 * DATA or TRAILERS state.
7178 */
7179 ret = http_parse_chunk_size(msg);
7180 if (ret == 0)
7181 goto missing_data_or_waiting;
7182 if (ret < 0)
7183 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007184 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007185 if (msg->chunk_len) {
7186 msg->msg_state = HTTP_MSG_DATA;
7187 goto switch_states;
7188 }
7189 msg->msg_state = HTTP_MSG_TRAILERS;
7190 /* fall through for HTTP_MSG_TRAILERS */
7191
7192 case HTTP_MSG_TRAILERS:
7193 ret = http_forward_trailers(msg);
7194 if (ret < 0)
7195 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007196 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7197 /* default_ret */ 1,
7198 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007199 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007200 if (!ret)
7201 goto missing_data_or_waiting;
7202 break;
7203
7204 case HTTP_MSG_ENDING:
7205 goto ending;
7206
7207 default:
7208 /* This should no happen in this function */
7209 goto error;
7210 }
7211
7212 msg->msg_state = HTTP_MSG_ENDING;
7213 ending:
7214 /* we may have some pending data starting at res->buf->p such as a last
7215 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007216 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007217 /* default_ret */ msg->next,
7218 /* on_error */ goto error);
7219 b_adv(chn->buf, ret);
7220 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007221 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7222 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007223 if (msg->next)
7224 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007225
Christopher Fauletda02e172015-12-04 09:25:05 +01007226 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007227 /* default_ret */ 1,
7228 /* on_error */ goto error,
7229 /* on_wait */ goto waiting);
7230 msg->msg_state = HTTP_MSG_DONE;
7231 return 1;
7232
7233 missing_data_or_waiting:
7234 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007235 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007236 /* default_ret */ msg->next,
7237 /* on_error */ goto error);
7238 b_adv(chn->buf, ret);
7239 msg->next -= ret;
7240 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7241 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007242 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007243 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007244 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007245 return 0;
7246
7247 chunk_parsing_error:
7248 if (msg->err_pos >= 0) {
7249 if (chn->flags & CF_ISRESP)
7250 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7251 msg->msg_state, strm_fe(s));
7252 else
7253 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7254 msg, msg->msg_state, s->be);
7255 }
7256 error:
7257 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007258}
7259
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261/* Iterate the same filter through all request headers.
7262 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 * Since it can manage the switch to another backend, it updates the per-proxy
7264 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007265 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007266int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 char *cur_ptr, *cur_end, *cur_next;
7269 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007270 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007272 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 last_hdr = 0;
7275
Willy Tarreau9b28e032012-10-12 23:49:43 +02007276 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 old_idx = 0;
7278
7279 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007280 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007282 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 (exp->action == ACT_ALLOW ||
7284 exp->action == ACT_DENY ||
7285 exp->action == ACT_TARPIT))
7286 return 0;
7287
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007288 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 if (!cur_idx)
7290 break;
7291
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007292 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 cur_ptr = cur_next;
7294 cur_end = cur_ptr + cur_hdr->len;
7295 cur_next = cur_end + cur_hdr->cr + 1;
7296
7297 /* Now we have one header between cur_ptr and cur_end,
7298 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007299 */
7300
Willy Tarreau15a53a42015-01-21 13:39:42 +01007301 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007304 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 last_hdr = 1;
7306 break;
7307
7308 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007309 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007310 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 break;
7312
7313 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007314 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 break;
7317
7318 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007319 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7320 if (trash.len < 0)
7321 return -1;
7322
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007323 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 /* FIXME: if the user adds a newline in the replacement, the
7325 * index will not be recalculated for now, and the new line
7326 * will not be counted as a new header.
7327 */
7328
7329 cur_end += delta;
7330 cur_next += delta;
7331 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007332 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 break;
7334
7335 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007336 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 cur_next += delta;
7338
Willy Tarreaufa355d42009-11-29 18:12:29 +01007339 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007340 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7341 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 cur_hdr->len = 0;
7343 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007344 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 break;
7346
7347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007348 }
7349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 /* keep the link from this header to next one in case of later
7351 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007352 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353 old_idx = cur_idx;
7354 }
7355 return 0;
7356}
7357
7358
7359/* Apply the filter to the request line.
7360 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7361 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 * Since it can manage the switch to another backend, it updates the per-proxy
7363 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007365int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 char *cur_ptr, *cur_end;
7368 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007369 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007370 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007371
Willy Tarreau3d300592007-03-18 18:34:41 +01007372 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007374 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 (exp->action == ACT_ALLOW ||
7376 exp->action == ACT_DENY ||
7377 exp->action == ACT_TARPIT))
7378 return 0;
7379 else if (exp->action == ACT_REMOVE)
7380 return 0;
7381
7382 done = 0;
7383
Willy Tarreau9b28e032012-10-12 23:49:43 +02007384 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007385 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007386
7387 /* Now we have the request line between cur_ptr and cur_end */
7388
Willy Tarreau15a53a42015-01-21 13:39:42 +01007389 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007392 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 done = 1;
7394 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007396 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007397 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007398 done = 1;
7399 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007401 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007402 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 done = 1;
7404 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007407 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7408 if (trash.len < 0)
7409 return -1;
7410
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007411 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 /* FIXME: if the user adds a newline in the replacement, the
7413 * index will not be recalculated for now, and the new line
7414 * will not be counted as a new header.
7415 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007416
Willy Tarreaufa355d42009-11-29 18:12:29 +01007417 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007418 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007419 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420 HTTP_MSG_RQMETH,
7421 cur_ptr, cur_end + 1,
7422 NULL, NULL);
7423 if (unlikely(!cur_end))
7424 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426 /* we have a full request and we know that we have either a CR
7427 * or an LF at <ptr>.
7428 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007429 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7430 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007431 /* there is no point trying this regex on headers */
7432 return 1;
7433 }
7434 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007435 return done;
7436}
Willy Tarreau97de6242006-12-27 17:18:38 +01007437
Willy Tarreau58f10d72006-12-04 02:26:12 +01007438
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007441 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443 * unparsable request. Since it can manage the switch to another backend, it
7444 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007445 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007446int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007447{
Willy Tarreau192252e2015-04-04 01:47:55 +02007448 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007449 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007450 struct hdr_exp *exp;
7451
7452 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007455 /*
7456 * The interleaving of transformations and verdicts
7457 * makes it difficult to decide to continue or stop
7458 * the evaluation.
7459 */
7460
Willy Tarreau6c123b12010-01-28 20:22:06 +01007461 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7462 break;
7463
Willy Tarreau3d300592007-03-18 18:34:41 +01007464 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007466 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007467 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007468
7469 /* if this filter had a condition, evaluate it now and skip to
7470 * next filter if the condition does not match.
7471 */
7472 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007473 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007474 ret = acl_pass(ret);
7475 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7476 ret = !ret;
7477
7478 if (!ret)
7479 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007480 }
7481
7482 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007483 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007484 if (unlikely(ret < 0))
7485 return -1;
7486
7487 if (likely(ret == 0)) {
7488 /* The filter did not match the request, it can be
7489 * iterated through all headers.
7490 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007491 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7492 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007493 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007495 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007496}
7497
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007498
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007499/* Find the end of a cookie value contained between <s> and <e>. It works the
7500 * same way as with headers above except that the semi-colon also ends a token.
7501 * See RFC2965 for more information. Note that it requires a valid header to
7502 * return a valid result.
7503 */
7504char *find_cookie_value_end(char *s, const char *e)
7505{
7506 int quoted, qdpair;
7507
7508 quoted = qdpair = 0;
7509 for (; s < e; s++) {
7510 if (qdpair) qdpair = 0;
7511 else if (quoted) {
7512 if (*s == '\\') qdpair = 1;
7513 else if (*s == '"') quoted = 0;
7514 }
7515 else if (*s == '"') quoted = 1;
7516 else if (*s == ',' || *s == ';') return s;
7517 }
7518 return s;
7519}
7520
7521/* Delete a value in a header between delimiters <from> and <next> in buffer
7522 * <buf>. The number of characters displaced is returned, and the pointer to
7523 * the first delimiter is updated if required. The function tries as much as
7524 * possible to respect the following principles :
7525 * - replace <from> delimiter by the <next> one unless <from> points to a
7526 * colon, in which case <next> is simply removed
7527 * - set exactly one space character after the new first delimiter, unless
7528 * there are not enough characters in the block being moved to do so.
7529 * - remove unneeded spaces before the previous delimiter and after the new
7530 * one.
7531 *
7532 * It is the caller's responsibility to ensure that :
7533 * - <from> points to a valid delimiter or the colon ;
7534 * - <next> points to a valid delimiter or the final CR/LF ;
7535 * - there are non-space chars before <from> ;
7536 * - there is a CR/LF at or after <next>.
7537 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007538int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539{
7540 char *prev = *from;
7541
7542 if (*prev == ':') {
7543 /* We're removing the first value, preserve the colon and add a
7544 * space if possible.
7545 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007546 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 next++;
7548 prev++;
7549 if (prev < next)
7550 *prev++ = ' ';
7551
Willy Tarreau2235b262016-11-05 15:50:20 +01007552 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 next++;
7554 } else {
7555 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007556 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 prev--;
7558 *from = prev;
7559
7560 /* copy the delimiter and if possible a space if we're
7561 * not at the end of the line.
7562 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007563 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 *prev++ = *next++;
7565 if (prev + 1 < next)
7566 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007567 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 next++;
7569 }
7570 }
7571 return buffer_replace2(buf, prev, next, NULL, 0);
7572}
7573
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007574/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007575 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 * desirable to call it only when needed. This code is quite complex because
7577 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7578 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007580void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007582 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007583 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007585 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7587 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007590 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007591 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007593 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007595 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007597 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 hdr_beg = hdr_next;
7599 hdr_end = hdr_beg + cur_hdr->len;
7600 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 /* We have one full header between hdr_beg and hdr_end, and the
7603 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604 * "Cookie:" headers.
7605 */
7606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007608 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007609 old_idx = cur_idx;
7610 continue;
7611 }
7612
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 del_from = NULL; /* nothing to be deleted */
7614 preserve_hdr = 0; /* assume we may kill the whole header */
7615
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 /* Now look for cookies. Conforming to RFC2109, we have to support
7617 * attributes whose name begin with a '$', and associate them with
7618 * the right cookie, if we want to delete this cookie.
7619 * So there are 3 cases for each cookie read :
7620 * 1) it's a special attribute, beginning with a '$' : ignore it.
7621 * 2) it's a server id cookie that we *MAY* want to delete : save
7622 * some pointers on it (last semi-colon, beginning of cookie...)
7623 * 3) it's an application cookie : we *MAY* have to delete a previous
7624 * "special" cookie.
7625 * At the end of loop, if a "special" cookie remains, we may have to
7626 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007627 * *MUST* delete it.
7628 *
7629 * Note: RFC2965 is unclear about the processing of spaces around
7630 * the equal sign in the ATTR=VALUE form. A careful inspection of
7631 * the RFC explicitly allows spaces before it, and not within the
7632 * tokens (attrs or values). An inspection of RFC2109 allows that
7633 * too but section 10.1.3 lets one think that spaces may be allowed
7634 * after the equal sign too, resulting in some (rare) buggy
7635 * implementations trying to do that. So let's do what servers do.
7636 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7637 * allowed quoted strings in values, with any possible character
7638 * after a backslash, including control chars and delimitors, which
7639 * causes parsing to become ambiguous. Browsers also allow spaces
7640 * within values even without quotes.
7641 *
7642 * We have to keep multiple pointers in order to support cookie
7643 * removal at the beginning, middle or end of header without
7644 * corrupting the header. All of these headers are valid :
7645 *
7646 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7647 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7648 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7649 * | | | | | | | | |
7650 * | | | | | | | | hdr_end <--+
7651 * | | | | | | | +--> next
7652 * | | | | | | +----> val_end
7653 * | | | | | +-----------> val_beg
7654 * | | | | +--------------> equal
7655 * | | | +----------------> att_end
7656 * | | +---------------------> att_beg
7657 * | +--------------------------> prev
7658 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007659 */
7660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7662 /* Iterate through all cookies on this line */
7663
7664 /* find att_beg */
7665 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007666 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007667 att_beg++;
7668
7669 /* find att_end : this is the first character after the last non
7670 * space before the equal. It may be equal to hdr_end.
7671 */
7672 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 while (equal < hdr_end) {
7675 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007676 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007677 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007678 continue;
7679 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680 }
7681
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7683 * is between <att_beg> and <equal>, both may be identical.
7684 */
7685
7686 /* look for end of cookie if there is an equal sign */
7687 if (equal < hdr_end && *equal == '=') {
7688 /* look for the beginning of the value */
7689 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007690 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 val_beg++;
7692
7693 /* find the end of the value, respecting quotes */
7694 next = find_cookie_value_end(val_beg, hdr_end);
7695
7696 /* make val_end point to the first white space or delimitor after the value */
7697 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007698 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 val_end--;
7700 } else {
7701 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007704 /* We have nothing to do with attributes beginning with '$'. However,
7705 * they will automatically be removed if a header before them is removed,
7706 * since they're supposed to be linked together.
7707 */
7708 if (*att_beg == '$')
7709 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 /* Ignore cookies with no equal sign */
7712 if (equal == next) {
7713 /* This is not our cookie, so we must preserve it. But if we already
7714 * scheduled another cookie for removal, we cannot remove the
7715 * complete header, but we can remove the previous block itself.
7716 */
7717 preserve_hdr = 1;
7718 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007719 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 val_end += delta;
7721 next += delta;
7722 hdr_end += delta;
7723 hdr_next += delta;
7724 cur_hdr->len += delta;
7725 http_msg_move_end(&txn->req, delta);
7726 prev = del_from;
7727 del_from = NULL;
7728 }
7729 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 /* if there are spaces around the equal sign, we need to
7733 * strip them otherwise we'll get trouble for cookie captures,
7734 * or even for rewrites. Since this happens extremely rarely,
7735 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7738 int stripped_before = 0;
7739 int stripped_after = 0;
7740
7741 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007742 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007743 equal += stripped_before;
7744 val_beg += stripped_before;
7745 }
7746
7747 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007748 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 val_beg += stripped_after;
7750 stripped_before += stripped_after;
7751 }
7752
7753 val_end += stripped_before;
7754 next += stripped_before;
7755 hdr_end += stripped_before;
7756 hdr_next += stripped_before;
7757 cur_hdr->len += stripped_before;
7758 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007759 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007760 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007761
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 /* First, let's see if we want to capture this cookie. We check
7763 * that we don't already have a client side cookie, because we
7764 * can only capture one. Also as an optimisation, we ignore
7765 * cookies shorter than the declared name.
7766 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007767 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7768 (val_end - att_beg >= sess->fe->capture_namelen) &&
7769 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007770 int log_len = val_end - att_beg;
7771
7772 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7773 Alert("HTTP logging : out of memory.\n");
7774 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007775 if (log_len > sess->fe->capture_len)
7776 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 memcpy(txn->cli_cookie, att_beg, log_len);
7778 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007779 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781
Willy Tarreaubca99692010-10-06 19:25:55 +02007782 /* Persistence cookies in passive, rewrite or insert mode have the
7783 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007787 * For cookies in prefix mode, the form is :
7788 *
7789 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007791 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7792 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7793 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007794 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007795
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007796 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7797 * have the server ID between val_beg and delim, and the original cookie between
7798 * delim+1 and val_end. Otherwise, delim==val_end :
7799 *
7800 * Cookie: NAME=SRV; # in all but prefix modes
7801 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7802 * | || || | |+-> next
7803 * | || || | +--> val_end
7804 * | || || +---------> delim
7805 * | || |+------------> val_beg
7806 * | || +-------------> att_end = equal
7807 * | |+-----------------> att_beg
7808 * | +------------------> prev
7809 * +-------------------------> hdr_beg
7810 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007812 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 for (delim = val_beg; delim < val_end; delim++)
7814 if (*delim == COOKIE_DELIM)
7815 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007816 } else {
7817 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007818 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007819 /* Now check if the cookie contains a date field, which would
7820 * appear after a vertical bar ('|') just after the server name
7821 * and before the delimiter.
7822 */
7823 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7824 if (vbar1) {
7825 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007826 * right is the last seen date. It is a base64 encoded
7827 * 30-bit value representing the UNIX date since the
7828 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007829 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007830 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007831 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007832 if (val_end - vbar1 >= 5) {
7833 val = b64tos30(vbar1);
7834 if (val > 0)
7835 txn->cookie_last_date = val << 2;
7836 }
7837 /* look for a second vertical bar */
7838 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7839 if (vbar1 && (val_end - vbar1 > 5)) {
7840 val = b64tos30(vbar1 + 1);
7841 if (val > 0)
7842 txn->cookie_first_date = val << 2;
7843 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007844 }
7845 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846
Willy Tarreauf64d1412010-10-07 20:06:11 +02007847 /* if the cookie has an expiration date and the proxy wants to check
7848 * it, then we do that now. We first check if the cookie is too old,
7849 * then only if it has expired. We detect strict overflow because the
7850 * time resolution here is not great (4 seconds). Cookies with dates
7851 * in the future are ignored if their offset is beyond one day. This
7852 * allows an admin to fix timezone issues without expiring everyone
7853 * and at the same time avoids keeping unwanted side effects for too
7854 * long.
7855 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007856 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7857 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007858 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007859 txn->flags &= ~TX_CK_MASK;
7860 txn->flags |= TX_CK_OLD;
7861 delim = val_beg; // let's pretend we have not found the cookie
7862 txn->cookie_first_date = 0;
7863 txn->cookie_last_date = 0;
7864 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7866 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007867 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007868 txn->flags &= ~TX_CK_MASK;
7869 txn->flags |= TX_CK_EXPIRED;
7870 delim = val_beg; // let's pretend we have not found the cookie
7871 txn->cookie_first_date = 0;
7872 txn->cookie_last_date = 0;
7873 }
7874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007875 /* Here, we'll look for the first running server which supports the cookie.
7876 * This allows to share a same cookie between several servers, for example
7877 * to dedicate backup servers to specific servers only.
7878 * However, to prevent clients from sticking to cookie-less backup server
7879 * when they have incidentely learned an empty cookie, we simply ignore
7880 * empty cookies and mark them as invalid.
7881 * The same behaviour is applied when persistence must be ignored.
7882 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007883 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007885
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 while (srv) {
7887 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7888 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007889 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007890 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007891 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007892 /* we found the server and we can use it */
7893 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007894 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007895 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007896 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 break;
7898 } else {
7899 /* we found a server, but it's down,
7900 * mark it as such and go on in case
7901 * another one is available.
7902 */
7903 txn->flags &= ~TX_CK_MASK;
7904 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 srv = srv->next;
7908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909
Willy Tarreauf64d1412010-10-07 20:06:11 +02007910 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007911 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007913 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007914 txn->flags |= TX_CK_UNUSED;
7915 else
7916 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007917 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007919 /* depending on the cookie mode, we may have to either :
7920 * - delete the complete cookie if we're in insert+indirect mode, so that
7921 * the server never sees it ;
7922 * - remove the server id from the cookie value, and tag the cookie as an
7923 * application cookie so that it does not get accidentely removed later,
7924 * if we're in cookie prefix mode
7925 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007926 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007928
Willy Tarreau9b28e032012-10-12 23:49:43 +02007929 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 val_end += delta;
7931 next += delta;
7932 hdr_end += delta;
7933 hdr_next += delta;
7934 cur_hdr->len += delta;
7935 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 del_from = NULL;
7938 preserve_hdr = 1; /* we want to keep this cookie */
7939 }
7940 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007941 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 del_from = prev;
7943 }
7944 } else {
7945 /* This is not our cookie, so we must preserve it. But if we already
7946 * scheduled another cookie for removal, we cannot remove the
7947 * complete header, but we can remove the previous block itself.
7948 */
7949 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007951 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007953 if (att_beg >= del_from)
7954 att_beg += delta;
7955 if (att_end >= del_from)
7956 att_end += delta;
7957 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007958 val_end += delta;
7959 next += delta;
7960 hdr_end += delta;
7961 hdr_next += delta;
7962 cur_hdr->len += delta;
7963 http_msg_move_end(&txn->req, delta);
7964 prev = del_from;
7965 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007969 /* continue with next cookie on this header line */
7970 att_beg = next;
7971 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007973 /* There are no more cookies on this line.
7974 * We may still have one (or several) marked for deletion at the
7975 * end of the line. We must do this now in two ways :
7976 * - if some cookies must be preserved, we only delete from the
7977 * mark to the end of line ;
7978 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007982 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007983 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007984 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985 cur_hdr->len += delta;
7986 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007987 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988
7989 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007990 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7991 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007992 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007993 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007994 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007995 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007996 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 }
7998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007999 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01008000 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008001 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008002}
8003
8004
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005/* Iterate the same filter through all response headers contained in <rtr>.
8006 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8007 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008008int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 char *cur_ptr, *cur_end, *cur_next;
8011 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008012 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008014 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015
8016 last_hdr = 0;
8017
Willy Tarreau9b28e032012-10-12 23:49:43 +02008018 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 old_idx = 0;
8020
8021 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008024 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 (exp->action == ACT_ALLOW ||
8026 exp->action == ACT_DENY))
8027 return 0;
8028
8029 cur_idx = txn->hdr_idx.v[old_idx].next;
8030 if (!cur_idx)
8031 break;
8032
8033 cur_hdr = &txn->hdr_idx.v[cur_idx];
8034 cur_ptr = cur_next;
8035 cur_end = cur_ptr + cur_hdr->len;
8036 cur_next = cur_end + cur_hdr->cr + 1;
8037
8038 /* Now we have one header between cur_ptr and cur_end,
8039 * and the next header starts at cur_next.
8040 */
8041
Willy Tarreau15a53a42015-01-21 13:39:42 +01008042 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 switch (exp->action) {
8044 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008045 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 last_hdr = 1;
8047 break;
8048
8049 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008050 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 last_hdr = 1;
8052 break;
8053
8054 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008055 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8056 if (trash.len < 0)
8057 return -1;
8058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008059 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 /* FIXME: if the user adds a newline in the replacement, the
8061 * index will not be recalculated for now, and the new line
8062 * will not be counted as a new header.
8063 */
8064
8065 cur_end += delta;
8066 cur_next += delta;
8067 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008068 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 break;
8070
8071 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008072 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 cur_next += delta;
8074
Willy Tarreaufa355d42009-11-29 18:12:29 +01008075 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8077 txn->hdr_idx.used--;
8078 cur_hdr->len = 0;
8079 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008080 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081 break;
8082
8083 }
8084 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085
8086 /* keep the link from this header to next one in case of later
8087 * removal of next header.
8088 */
8089 old_idx = cur_idx;
8090 }
8091 return 0;
8092}
8093
8094
8095/* Apply the filter to the status line in the response buffer <rtr>.
8096 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8097 * or -1 if a replacement resulted in an invalid status line.
8098 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008099int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101 char *cur_ptr, *cur_end;
8102 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008103 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008104 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105
8106
Willy Tarreau3d300592007-03-18 18:34:41 +01008107 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008109 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 (exp->action == ACT_ALLOW ||
8111 exp->action == ACT_DENY))
8112 return 0;
8113 else if (exp->action == ACT_REMOVE)
8114 return 0;
8115
8116 done = 0;
8117
Willy Tarreau9b28e032012-10-12 23:49:43 +02008118 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008119 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120
8121 /* Now we have the status line between cur_ptr and cur_end */
8122
Willy Tarreau15a53a42015-01-21 13:39:42 +01008123 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 switch (exp->action) {
8125 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008126 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008127 done = 1;
8128 break;
8129
8130 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008131 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 done = 1;
8133 break;
8134
8135 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008136 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8137 if (trash.len < 0)
8138 return -1;
8139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008140 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 /* FIXME: if the user adds a newline in the replacement, the
8142 * index will not be recalculated for now, and the new line
8143 * will not be counted as a new header.
8144 */
8145
Willy Tarreaufa355d42009-11-29 18:12:29 +01008146 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008148 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008149 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 cur_ptr, cur_end + 1,
8151 NULL, NULL);
8152 if (unlikely(!cur_end))
8153 return -1;
8154
8155 /* we have a full respnse and we know that we have either a CR
8156 * or an LF at <ptr>.
8157 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008158 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008159 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 /* there is no point trying this regex on headers */
8161 return 1;
8162 }
8163 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008164 return done;
8165}
8166
8167
8168
8169/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008170 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008171 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8172 * unparsable response.
8173 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008174int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175{
Willy Tarreau192252e2015-04-04 01:47:55 +02008176 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008177 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008178 struct hdr_exp *exp;
8179
8180 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 int ret;
8182
8183 /*
8184 * The interleaving of transformations and verdicts
8185 * makes it difficult to decide to continue or stop
8186 * the evaluation.
8187 */
8188
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008189 if (txn->flags & TX_SVDENY)
8190 break;
8191
Willy Tarreau3d300592007-03-18 18:34:41 +01008192 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8194 exp->action == ACT_PASS)) {
8195 exp = exp->next;
8196 continue;
8197 }
8198
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008199 /* if this filter had a condition, evaluate it now and skip to
8200 * next filter if the condition does not match.
8201 */
8202 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008203 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008204 ret = acl_pass(ret);
8205 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8206 ret = !ret;
8207 if (!ret)
8208 continue;
8209 }
8210
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008212 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 if (unlikely(ret < 0))
8214 return -1;
8215
8216 if (likely(ret == 0)) {
8217 /* The filter did not match the response, it can be
8218 * iterated through all headers.
8219 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008220 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8221 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223 }
8224 return 0;
8225}
8226
8227
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008229 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 * desirable to call it only when needed. This function is also used when we
8231 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008233void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008235 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008236 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008237 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008238 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 char *hdr_beg, *hdr_end, *hdr_next;
8241 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 /* Iterate through the headers.
8244 * we start with the start line.
8245 */
8246 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008247 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248
8249 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8250 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008251 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008252
8253 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008254 hdr_beg = hdr_next;
8255 hdr_end = hdr_beg + cur_hdr->len;
8256 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008257
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 /* We have one full header between hdr_beg and hdr_end, and the
8259 * next header starts at hdr_next. We're only interested in
8260 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 */
8262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 is_cookie2 = 0;
8264 prev = hdr_beg + 10;
8265 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008266 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8268 if (!val) {
8269 old_idx = cur_idx;
8270 continue;
8271 }
8272 is_cookie2 = 1;
8273 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 }
8275
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8277 * <prev> points to the colon.
8278 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008279 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008280
Willy Tarreau24581ba2010-08-31 22:39:35 +02008281 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8282 * check-cache is enabled) and we are not interested in checking
8283 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008284 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008285 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008286 return;
8287
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 /* OK so now we know we have to process this response cookie.
8289 * The format of the Set-Cookie header is slightly different
8290 * from the format of the Cookie header in that it does not
8291 * support the comma as a cookie delimiter (thus the header
8292 * cannot be folded) because the Expires attribute described in
8293 * the original Netscape's spec may contain an unquoted date
8294 * with a comma inside. We have to live with this because
8295 * many browsers don't support Max-Age and some browsers don't
8296 * support quoted strings. However the Set-Cookie2 header is
8297 * clean.
8298 *
8299 * We have to keep multiple pointers in order to support cookie
8300 * removal at the beginning, middle or end of header without
8301 * corrupting the header (in case of set-cookie2). A special
8302 * pointer, <scav> points to the beginning of the set-cookie-av
8303 * fields after the first semi-colon. The <next> pointer points
8304 * either to the end of line (set-cookie) or next unquoted comma
8305 * (set-cookie2). All of these headers are valid :
8306 *
8307 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8308 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8309 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8310 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8311 * | | | | | | | | | |
8312 * | | | | | | | | +-> next hdr_end <--+
8313 * | | | | | | | +------------> scav
8314 * | | | | | | +--------------> val_end
8315 * | | | | | +--------------------> val_beg
8316 * | | | | +----------------------> equal
8317 * | | | +------------------------> att_end
8318 * | | +----------------------------> att_beg
8319 * | +------------------------------> prev
8320 * +-----------------------------------------> hdr_beg
8321 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 for (; prev < hdr_end; prev = next) {
8324 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 /* find att_beg */
8327 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008328 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008329 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008330
Willy Tarreau24581ba2010-08-31 22:39:35 +02008331 /* find att_end : this is the first character after the last non
8332 * space before the equal. It may be equal to hdr_end.
8333 */
8334 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 while (equal < hdr_end) {
8337 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8338 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008339 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008340 continue;
8341 att_end = equal;
8342 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008343
Willy Tarreau24581ba2010-08-31 22:39:35 +02008344 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8345 * is between <att_beg> and <equal>, both may be identical.
8346 */
8347
8348 /* look for end of cookie if there is an equal sign */
8349 if (equal < hdr_end && *equal == '=') {
8350 /* look for the beginning of the value */
8351 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008352 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008353 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008354
Willy Tarreau24581ba2010-08-31 22:39:35 +02008355 /* find the end of the value, respecting quotes */
8356 next = find_cookie_value_end(val_beg, hdr_end);
8357
8358 /* make val_end point to the first white space or delimitor after the value */
8359 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008360 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008361 val_end--;
8362 } else {
8363 /* <equal> points to next comma, semi-colon or EOL */
8364 val_beg = val_end = next = equal;
8365 }
8366
8367 if (next < hdr_end) {
8368 /* Set-Cookie2 supports multiple cookies, and <next> points to
8369 * a colon or semi-colon before the end. So skip all attr-value
8370 * pairs and look for the next comma. For Set-Cookie, since
8371 * commas are permitted in values, skip to the end.
8372 */
8373 if (is_cookie2)
8374 next = find_hdr_value_end(next, hdr_end);
8375 else
8376 next = hdr_end;
8377 }
8378
8379 /* Now everything is as on the diagram above */
8380
8381 /* Ignore cookies with no equal sign */
8382 if (equal == val_end)
8383 continue;
8384
8385 /* If there are spaces around the equal sign, we need to
8386 * strip them otherwise we'll get trouble for cookie captures,
8387 * or even for rewrites. Since this happens extremely rarely,
8388 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008389 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008390 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8391 int stripped_before = 0;
8392 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008393
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008395 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008396 equal += stripped_before;
8397 val_beg += stripped_before;
8398 }
8399
8400 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008401 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008402 val_beg += stripped_after;
8403 stripped_before += stripped_after;
8404 }
8405
8406 val_end += stripped_before;
8407 next += stripped_before;
8408 hdr_end += stripped_before;
8409 hdr_next += stripped_before;
8410 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008411 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008412 }
8413
8414 /* First, let's see if we want to capture this cookie. We check
8415 * that we don't already have a server side cookie, because we
8416 * can only capture one. Also as an optimisation, we ignore
8417 * cookies shorter than the declared name.
8418 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008419 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008420 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008421 (val_end - att_beg >= sess->fe->capture_namelen) &&
8422 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008423 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008424 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425 Alert("HTTP logging : out of memory.\n");
8426 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008427 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008428 if (log_len > sess->fe->capture_len)
8429 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008430 memcpy(txn->srv_cookie, att_beg, log_len);
8431 txn->srv_cookie[log_len] = 0;
8432 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 }
8434
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008435 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008437 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008438 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8439 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008440 /* assume passive cookie by default */
8441 txn->flags &= ~TX_SCK_MASK;
8442 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443
8444 /* If the cookie is in insert mode on a known server, we'll delete
8445 * this occurrence because we'll insert another one later.
8446 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008447 * a direct access.
8448 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008449 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008450 /* The "preserve" flag was set, we don't want to touch the
8451 * server's cookie.
8452 */
8453 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008454 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008455 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 /* this cookie must be deleted */
8457 if (*prev == ':' && next == hdr_end) {
8458 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008459 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008460 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8461 txn->hdr_idx.used--;
8462 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008463 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008464 hdr_next += delta;
8465 http_msg_move_end(&txn->rsp, delta);
8466 /* note: while both invalid now, <next> and <hdr_end>
8467 * are still equal, so the for() will stop as expected.
8468 */
8469 } else {
8470 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008471 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008472 next = prev;
8473 hdr_end += delta;
8474 hdr_next += delta;
8475 cur_hdr->len += delta;
8476 http_msg_move_end(&txn->rsp, delta);
8477 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008478 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008479 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008482 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008483 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 * with this server since we know it.
8485 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008486 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 next += delta;
8488 hdr_end += delta;
8489 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008490 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008491 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492
Willy Tarreauf1348312010-10-07 15:54:11 +02008493 txn->flags &= ~TX_SCK_MASK;
8494 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008496 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008498 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008499 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008500 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008501 next += delta;
8502 hdr_end += delta;
8503 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008505 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506
Willy Tarreau827aee92011-03-10 16:55:02 +01008507 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008508 txn->flags &= ~TX_SCK_MASK;
8509 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 }
8511 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008512 /* that's done for this cookie, check the next one on the same
8513 * line when next != hdr_end (only if is_cookie2).
8514 */
8515 }
8516 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008517 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008518 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519}
8520
8521
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008523 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008525void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008527 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008528 char *p1, *p2;
8529
8530 char *cur_ptr, *cur_end, *cur_next;
8531 int cur_idx;
8532
Willy Tarreau5df51872007-11-25 16:20:08 +01008533 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008534 return;
8535
8536 /* Iterate through the headers.
8537 * we start with the start line.
8538 */
8539 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008540 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541
8542 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8543 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008544 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008545
8546 cur_hdr = &txn->hdr_idx.v[cur_idx];
8547 cur_ptr = cur_next;
8548 cur_end = cur_ptr + cur_hdr->len;
8549 cur_next = cur_end + cur_hdr->cr + 1;
8550
8551 /* We have one full header between cur_ptr and cur_end, and the
8552 * next header starts at cur_next. We're only interested in
8553 * "Cookie:" headers.
8554 */
8555
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008556 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8557 if (val) {
8558 if ((cur_end - (cur_ptr + val) >= 8) &&
8559 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8560 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8561 return;
8562 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008563 }
8564
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008565 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8566 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008567 continue;
8568
8569 /* OK, right now we know we have a cache-control header at cur_ptr */
8570
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008571 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008572
8573 if (p1 >= cur_end) /* no more info */
8574 continue;
8575
8576 /* p1 is at the beginning of the value */
8577 p2 = p1;
8578
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008579 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008580 p2++;
8581
8582 /* we have a complete value between p1 and p2 */
8583 if (p2 < cur_end && *p2 == '=') {
8584 /* we have something of the form no-cache="set-cookie" */
8585 if ((cur_end - p1 >= 21) &&
8586 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8587 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008588 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008589 continue;
8590 }
8591
8592 /* OK, so we know that either p2 points to the end of string or to a comma */
8593 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008594 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008595 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8596 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8597 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008598 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008599 return;
8600 }
8601
8602 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008603 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008604 continue;
8605 }
8606 }
8607}
8608
Willy Tarreau58f10d72006-12-04 02:26:12 +01008609
Willy Tarreaub2513902006-12-17 14:52:38 +01008610/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008611 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008612 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008613 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008614 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008615 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008616 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008617 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008618 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008619int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008620{
8621 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008622 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008623 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008624
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008625 if (!uri_auth)
8626 return 0;
8627
Cyril Bonté70be45d2010-10-12 00:14:35 +02008628 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008629 return 0;
8630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008631 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008632 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008633 return 0;
8634
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008635 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008636 return 0;
8637
Willy Tarreaub2513902006-12-17 14:52:38 +01008638 return 1;
8639}
8640
Willy Tarreau4076a152009-04-02 15:18:36 +02008641/*
8642 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008643 * By default it tries to report the error position as msg->err_pos. However if
8644 * this one is not set, it will then report msg->next, which is the last known
8645 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008646 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008647 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008648void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008649 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008650 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008651{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008652 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008653 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008654 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008655
Willy Tarreauf3764b72016-03-31 13:45:10 +02008656 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008657 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008658 len1 = MIN(len1, es->len);
8659 len2 = es->len - len1; /* remaining data if buffer wraps */
8660
Willy Tarreauf3764b72016-03-31 13:45:10 +02008661 if (!es->buf)
8662 es->buf = malloc(global.tune.bufsize);
8663
8664 if (es->buf) {
8665 memcpy(es->buf, chn->buf->p, len1);
8666 if (len2)
8667 memcpy(es->buf + len1, chn->buf->data, len2);
8668 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008669
Willy Tarreau4076a152009-04-02 15:18:36 +02008670 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008671 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008672 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008673 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008674
Willy Tarreau4076a152009-04-02 15:18:36 +02008675 es->when = date; // user-visible date
8676 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008677 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008678 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008679 if (objt_conn(sess->origin))
8680 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008681 else
8682 memset(&es->src, 0, sizeof(es->src));
8683
Willy Tarreau078272e2010-12-12 12:46:33 +01008684 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008685 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008686 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008687 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008688 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008689 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008690 es->b_out = chn->buf->o;
8691 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008692 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008693 es->m_clen = msg->chunk_len;
8694 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008695}
Willy Tarreaub2513902006-12-17 14:52:38 +01008696
Willy Tarreau294c4732011-12-16 21:35:50 +01008697/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8698 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8699 * performed over the whole headers. Otherwise it must contain a valid header
8700 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8701 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8702 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8703 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008704 * -1. The value fetch stops at commas, so this function is suited for use with
8705 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008707 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008708unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 struct hdr_idx *idx, int occ,
8710 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008711{
Willy Tarreau294c4732011-12-16 21:35:50 +01008712 struct hdr_ctx local_ctx;
8713 char *ptr_hist[MAX_HDR_HISTORY];
8714 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008715 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008716 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008717
Willy Tarreau294c4732011-12-16 21:35:50 +01008718 if (!ctx) {
8719 local_ctx.idx = 0;
8720 ctx = &local_ctx;
8721 }
8722
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008724 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008725 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008726 occ--;
8727 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008728 *vptr = ctx->line + ctx->val;
8729 *vlen = ctx->vlen;
8730 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008731 }
8732 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008733 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008734 }
8735
8736 /* negative occurrence, we scan all the list then walk back */
8737 if (-occ > MAX_HDR_HISTORY)
8738 return 0;
8739
Willy Tarreau294c4732011-12-16 21:35:50 +01008740 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008741 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008742 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8743 len_hist[hist_ptr] = ctx->vlen;
8744 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008745 hist_ptr = 0;
8746 found++;
8747 }
8748 if (-occ > found)
8749 return 0;
8750 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008751 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8752 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8753 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008754 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008755 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008756 if (hist_ptr >= MAX_HDR_HISTORY)
8757 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008758 *vptr = ptr_hist[hist_ptr];
8759 *vlen = len_hist[hist_ptr];
8760 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008761}
8762
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008763/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8764 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8765 * performed over the whole headers. Otherwise it must contain a valid header
8766 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8767 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8768 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8769 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8770 * -1. This function differs from http_get_hdr() in that it only returns full
8771 * line header values and does not stop at commas.
8772 * The return value is 0 if nothing was found, or non-zero otherwise.
8773 */
8774unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8775 struct hdr_idx *idx, int occ,
8776 struct hdr_ctx *ctx, char **vptr, int *vlen)
8777{
8778 struct hdr_ctx local_ctx;
8779 char *ptr_hist[MAX_HDR_HISTORY];
8780 int len_hist[MAX_HDR_HISTORY];
8781 unsigned int hist_ptr;
8782 int found;
8783
8784 if (!ctx) {
8785 local_ctx.idx = 0;
8786 ctx = &local_ctx;
8787 }
8788
8789 if (occ >= 0) {
8790 /* search from the beginning */
8791 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8792 occ--;
8793 if (occ <= 0) {
8794 *vptr = ctx->line + ctx->val;
8795 *vlen = ctx->vlen;
8796 return 1;
8797 }
8798 }
8799 return 0;
8800 }
8801
8802 /* negative occurrence, we scan all the list then walk back */
8803 if (-occ > MAX_HDR_HISTORY)
8804 return 0;
8805
8806 found = hist_ptr = 0;
8807 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8808 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8809 len_hist[hist_ptr] = ctx->vlen;
8810 if (++hist_ptr >= MAX_HDR_HISTORY)
8811 hist_ptr = 0;
8812 found++;
8813 }
8814 if (-occ > found)
8815 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008816
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008817 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008818 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8819 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8820 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008821 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008822 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008823 if (hist_ptr >= MAX_HDR_HISTORY)
8824 hist_ptr -= MAX_HDR_HISTORY;
8825 *vptr = ptr_hist[hist_ptr];
8826 *vlen = len_hist[hist_ptr];
8827 return 1;
8828}
8829
Willy Tarreaubaaee002006-06-26 02:48:02 +02008830/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008831 * Print a debug line with a header. Always stop at the first CR or LF char,
8832 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8833 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008834 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008835void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008836{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008837 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008838 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008839
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008840 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008841 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008842 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008843 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 +02008844
8845 for (max = 0; start + max < end; max++)
8846 if (start[max] == '\r' || start[max] == '\n')
8847 break;
8848
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008849 UBOUND(max, trash.size - trash.len - 3);
8850 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8851 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008852 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008853}
8854
Willy Tarreaueee5b512015-04-03 23:46:31 +02008855
8856/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8857 * The hdr_idx is allocated as well. In case of allocation failure, everything
8858 * allocated is freed and NULL is returned. Otherwise the new transaction is
8859 * assigned to the stream and returned.
8860 */
8861struct http_txn *http_alloc_txn(struct stream *s)
8862{
8863 struct http_txn *txn = s->txn;
8864
8865 if (txn)
8866 return txn;
8867
8868 txn = pool_alloc2(pool2_http_txn);
8869 if (!txn)
8870 return txn;
8871
8872 txn->hdr_idx.size = global.tune.max_http_hdr;
8873 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8874 if (!txn->hdr_idx.v) {
8875 pool_free2(pool2_http_txn, txn);
8876 return NULL;
8877 }
8878
8879 s->txn = txn;
8880 return txn;
8881}
8882
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008883void http_txn_reset_req(struct http_txn *txn)
8884{
8885 txn->req.flags = 0;
8886 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8887 txn->req.next = 0;
8888 txn->req.chunk_len = 0LL;
8889 txn->req.body_len = 0LL;
8890 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8891}
8892
8893void http_txn_reset_res(struct http_txn *txn)
8894{
8895 txn->rsp.flags = 0;
8896 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8897 txn->rsp.next = 0;
8898 txn->rsp.chunk_len = 0LL;
8899 txn->rsp.body_len = 0LL;
8900 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8901}
8902
Willy Tarreau0937bc42009-12-22 15:03:09 +01008903/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008904 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008905 * the required fields are properly allocated and that we only need to (re)init
8906 * them. This should be used before processing any new request.
8907 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008908void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008910 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008911 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008912
8913 txn->flags = 0;
8914 txn->status = -1;
8915
Willy Tarreauf64d1412010-10-07 20:06:11 +02008916 txn->cookie_first_date = 0;
8917 txn->cookie_last_date = 0;
8918
Willy Tarreaueee5b512015-04-03 23:46:31 +02008919 txn->srv_cookie = NULL;
8920 txn->cli_cookie = NULL;
8921 txn->uri = NULL;
8922
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008923 http_txn_reset_req(txn);
8924 http_txn_reset_res(txn);
8925
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008926 txn->req.chn = &s->req;
8927 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008928
8929 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008930
8931 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8932 if (fe->options2 & PR_O2_REQBUG_OK)
8933 txn->req.err_pos = -1; /* let buggy requests pass */
8934
Willy Tarreau0937bc42009-12-22 15:03:09 +01008935 if (txn->hdr_idx.v)
8936 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008937
8938 vars_init(&s->vars_txn, SCOPE_TXN);
8939 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008940}
8941
8942/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008943void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008944{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008945 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008946 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947
8948 /* these ones will have been dynamically allocated */
8949 pool_free2(pool2_requri, txn->uri);
8950 pool_free2(pool2_capture, txn->cli_cookie);
8951 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008952 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008953
William Lallemanda73203e2012-03-12 12:48:57 +01008954 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008955 txn->uri = NULL;
8956 txn->srv_cookie = NULL;
8957 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008958
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008959 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008960 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008961 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008962 pool_free2(h->pool, s->req_cap[h->index]);
8963 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008964 }
8965
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008966 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008967 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008968 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008969 pool_free2(h->pool, s->res_cap[h->index]);
8970 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008971 }
8972
Willy Tarreau6204cd92016-03-10 16:33:04 +01008973 vars_prune(&s->vars_txn, s->sess, s);
8974 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008975}
8976
8977/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008978void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008979{
8980 http_end_txn(s);
8981 http_init_txn(s);
8982
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008983 /* reinitialise the current rule list pointer to NULL. We are sure that
8984 * any rulelist match the NULL pointer.
8985 */
8986 s->current_rule_list = NULL;
8987
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008988 s->be = strm_fe(s);
8989 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008990 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008991 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008992 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008993 /* re-init store persistence */
8994 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008995 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008996
Willy Tarreau0937bc42009-12-22 15:03:09 +01008997 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008998
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008999 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009000
Willy Tarreau739cfba2010-01-25 23:11:14 +01009001 /* We must trim any excess data from the response buffer, because we
9002 * may have blocked an invalid response from a server that we don't
9003 * want to accidentely forward once we disable the analysers, nor do
9004 * we want those data to come along with next response. A typical
9005 * example of such data would be from a buggy server responding to
9006 * a HEAD with some data, or sending more than the advertised
9007 * content-length.
9008 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009009 if (unlikely(s->res.buf->i))
9010 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009011
Christopher Fauletc0c672a2017-03-28 11:51:33 +02009012 /* Now we can realign the response buffer */
9013 buffer_realign(s->res.buf);
9014
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009015 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009016 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009017
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009018 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009019 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009020
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009021 s->req.rex = TICK_ETERNITY;
9022 s->req.wex = TICK_ETERNITY;
9023 s->req.analyse_exp = TICK_ETERNITY;
9024 s->res.rex = TICK_ETERNITY;
9025 s->res.wex = TICK_ETERNITY;
9026 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01009027 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009028}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009029
Sasha Pachev218f0642014-06-16 12:05:59 -06009030void free_http_res_rules(struct list *r)
9031{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009032 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009033
9034 list_for_each_entry_safe(pr, tr, r, list) {
9035 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009036 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009037 free(pr);
9038 }
9039}
9040
9041void free_http_req_rules(struct list *r)
9042{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009043 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009044
9045 list_for_each_entry_safe(pr, tr, r, list) {
9046 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009047 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009048 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009049
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009050 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009051 free(pr);
9052 }
9053}
9054
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009055/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009056struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009057{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009058 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009059 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009060 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009061 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009062
Vincent Bernat02779b62016-04-03 13:48:43 +02009063 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009064 if (!rule) {
9065 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009066 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009067 }
9068
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009069 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009070 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009071 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009072 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009073 int code;
9074 int hc;
9075
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009076 if (!strcmp(args[0], "tarpit")) {
9077 rule->action = ACT_HTTP_REQ_TARPIT;
9078 rule->deny_status = HTTP_ERR_500;
9079 }
9080 else {
9081 rule->action = ACT_ACTION_DENY;
9082 rule->deny_status = HTTP_ERR_403;
9083 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009084 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009085 if (strcmp(args[cur_arg], "deny_status") == 0) {
9086 cur_arg++;
9087 if (!args[cur_arg]) {
9088 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9089 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9090 goto out_err;
9091 }
9092
9093 code = atol(args[cur_arg]);
9094 cur_arg++;
9095 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9096 if (http_err_codes[hc] == code) {
9097 rule->deny_status = hc;
9098 break;
9099 }
9100 }
9101
9102 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009103 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9104 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009105 }
9106 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009107 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009108 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009109 cur_arg = 1;
9110
9111 while(*args[cur_arg]) {
9112 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009113 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009114 cur_arg+=2;
9115 continue;
9116 } else
9117 break;
9118 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009119 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009120 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009121 cur_arg = 1;
9122
9123 if (!*args[cur_arg] ||
9124 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9125 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9126 file, linenum, args[0]);
9127 goto out_err;
9128 }
9129 rule->arg.nice = atoi(args[cur_arg]);
9130 if (rule->arg.nice < -1024)
9131 rule->arg.nice = -1024;
9132 else if (rule->arg.nice > 1024)
9133 rule->arg.nice = 1024;
9134 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009135 } else if (!strcmp(args[0], "set-tos")) {
9136#ifdef IP_TOS
9137 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009138 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009139 cur_arg = 1;
9140
9141 if (!*args[cur_arg] ||
9142 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9143 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9144 file, linenum, args[0]);
9145 goto out_err;
9146 }
9147
9148 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9149 if (err && *err != '\0') {
9150 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9151 file, linenum, err, args[0]);
9152 goto out_err;
9153 }
9154 cur_arg++;
9155#else
9156 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9157 goto out_err;
9158#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009159 } else if (!strcmp(args[0], "set-mark")) {
9160#ifdef SO_MARK
9161 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009162 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009163 cur_arg = 1;
9164
9165 if (!*args[cur_arg] ||
9166 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9167 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9168 file, linenum, args[0]);
9169 goto out_err;
9170 }
9171
9172 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9173 if (err && *err != '\0') {
9174 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9175 file, linenum, err, args[0]);
9176 goto out_err;
9177 }
9178 cur_arg++;
9179 global.last_checks |= LSTCHK_NETADM;
9180#else
9181 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9182 goto out_err;
9183#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009184 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009185 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009186 cur_arg = 1;
9187
9188 if (!*args[cur_arg] ||
9189 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9190 bad_log_level:
9191 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9192 file, linenum, args[0]);
9193 goto out_err;
9194 }
9195 if (strcmp(args[cur_arg], "silent") == 0)
9196 rule->arg.loglevel = -1;
9197 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9198 goto bad_log_level;
9199 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009200 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009201 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009202 cur_arg = 1;
9203
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009204 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9205 (*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 +01009206 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9207 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009208 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009209 }
9210
9211 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9212 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9213 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009214
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009215 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009216 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009217 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 +01009218 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9219 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9220 file, linenum, args[0], error);
9221 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009222 goto out_err;
9223 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009224 free(proxy->conf.lfs_file);
9225 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9226 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009227 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009228 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009229 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009230 cur_arg = 1;
9231
9232 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009233 (*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 -06009234 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9235 file, linenum, args[0]);
9236 goto out_err;
9237 }
9238
9239 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9240 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9241 LIST_INIT(&rule->arg.hdr_add.fmt);
9242
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009243 error = NULL;
9244 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9245 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9246 args[cur_arg + 1], error);
9247 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009248 goto out_err;
9249 }
9250
9251 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009252 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009253 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 +01009254 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9255 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9256 file, linenum, args[0], error);
9257 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009258 goto out_err;
9259 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009260
9261 free(proxy->conf.lfs_file);
9262 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9263 proxy->conf.lfs_line = proxy->conf.args.line;
9264 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009265 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009266 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009267 cur_arg = 1;
9268
9269 if (!*args[cur_arg] ||
9270 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9271 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9272 file, linenum, args[0]);
9273 goto out_err;
9274 }
9275
9276 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9277 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9278
9279 proxy->conf.args.ctx = ARGC_HRQ;
9280 free(proxy->conf.lfs_file);
9281 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9282 proxy->conf.lfs_line = proxy->conf.args.line;
9283 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009284 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9285 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009286 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009287 struct sample_expr *expr;
9288 unsigned int where;
9289 char *err = NULL;
9290
9291 cur_arg = 1;
9292 proxy->conf.args.ctx = ARGC_TRK;
9293
9294 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9295 if (!expr) {
9296 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9297 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9298 free(err);
9299 goto out_err;
9300 }
9301
9302 where = 0;
9303 if (proxy->cap & PR_CAP_FE)
9304 where |= SMP_VAL_FE_HRQ_HDR;
9305 if (proxy->cap & PR_CAP_BE)
9306 where |= SMP_VAL_BE_HRQ_HDR;
9307
9308 if (!(expr->fetch->val & where)) {
9309 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9310 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9311 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9312 args[cur_arg-1], sample_src_names(expr->fetch->use));
9313 free(expr);
9314 goto out_err;
9315 }
9316
9317 if (strcmp(args[cur_arg], "table") == 0) {
9318 cur_arg++;
9319 if (!args[cur_arg]) {
9320 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9321 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9322 free(expr);
9323 goto out_err;
9324 }
9325 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009326 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009327 cur_arg++;
9328 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009329 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009330 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009331 } else if (strcmp(args[0], "redirect") == 0) {
9332 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009333 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009334
Willy Tarreaube4653b2015-05-28 15:26:58 +02009335 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009336 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9337 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9338 goto out_err;
9339 }
9340
9341 /* this redirect rule might already contain a parsed condition which
9342 * we'll pass to the http-request rule.
9343 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009344 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009345 rule->arg.redir = redir;
9346 rule->cond = redir->cond;
9347 redir->cond = NULL;
9348 cur_arg = 2;
9349 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009350 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9351 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009352 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009353 /*
9354 * '+ 8' for 'add-acl('
9355 * '- 9' for 'add-acl(' + trailing ')'
9356 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009357 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009358
9359 cur_arg = 1;
9360
9361 if (!*args[cur_arg] ||
9362 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9363 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9364 file, linenum, args[0]);
9365 goto out_err;
9366 }
9367
9368 LIST_INIT(&rule->arg.map.key);
9369 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009370 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009371 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009372 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9373 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9374 file, linenum, args[0], error);
9375 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009376 goto out_err;
9377 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009378 free(proxy->conf.lfs_file);
9379 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9380 proxy->conf.lfs_line = proxy->conf.args.line;
9381 cur_arg += 1;
9382 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9383 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009384 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009385 /*
9386 * '+ 8' for 'del-acl('
9387 * '- 9' for 'del-acl(' + trailing ')'
9388 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009389 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009390
9391 cur_arg = 1;
9392
9393 if (!*args[cur_arg] ||
9394 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9395 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9396 file, linenum, args[0]);
9397 goto out_err;
9398 }
9399
9400 LIST_INIT(&rule->arg.map.key);
9401 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009402 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009403 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009404 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9405 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9406 file, linenum, args[0], error);
9407 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009408 goto out_err;
9409 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009410 free(proxy->conf.lfs_file);
9411 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9412 proxy->conf.lfs_line = proxy->conf.args.line;
9413 cur_arg += 1;
9414 } else if (strncmp(args[0], "del-map", 7) == 0) {
9415 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009416 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009417 /*
9418 * '+ 8' for 'del-map('
9419 * '- 9' for 'del-map(' + trailing ')'
9420 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009421 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009422
9423 cur_arg = 1;
9424
9425 if (!*args[cur_arg] ||
9426 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9427 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9428 file, linenum, args[0]);
9429 goto out_err;
9430 }
9431
9432 LIST_INIT(&rule->arg.map.key);
9433 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009434 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009435 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009436 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9437 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9438 file, linenum, args[0], error);
9439 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009440 goto out_err;
9441 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009442 free(proxy->conf.lfs_file);
9443 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9444 proxy->conf.lfs_line = proxy->conf.args.line;
9445 cur_arg += 1;
9446 } else if (strncmp(args[0], "set-map", 7) == 0) {
9447 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009448 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009449 /*
9450 * '+ 8' for 'set-map('
9451 * '- 9' for 'set-map(' + trailing ')'
9452 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009453 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009454
9455 cur_arg = 1;
9456
9457 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9458 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9459 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9460 file, linenum, args[0]);
9461 goto out_err;
9462 }
9463
9464 LIST_INIT(&rule->arg.map.key);
9465 LIST_INIT(&rule->arg.map.value);
9466 proxy->conf.args.ctx = ARGC_HRQ;
9467
9468 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009469 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009470 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009471 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9472 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9473 file, linenum, args[0], error);
9474 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009475 goto out_err;
9476 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009477
9478 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009479 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009480 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 +01009481 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9482 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9483 file, linenum, args[0], error);
9484 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009485 goto out_err;
9486 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009487 free(proxy->conf.lfs_file);
9488 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9489 proxy->conf.lfs_line = proxy->conf.args.line;
9490
9491 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009492 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9493 char *errmsg = NULL;
9494 cur_arg = 1;
9495 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009496 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009497 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009498 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009499 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9500 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9501 free(errmsg);
9502 goto out_err;
9503 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009504 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009505 action_build_list(&http_req_keywords.list, &trash);
9506 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9507 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009508 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009509 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009510 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009511 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009512 }
9513
9514 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9515 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009516 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009517
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009518 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9519 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9520 file, linenum, args[0], errmsg);
9521 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009522 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009523 }
9524 rule->cond = cond;
9525 }
9526 else if (*args[cur_arg]) {
9527 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9528 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9529 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009530 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009531 }
9532
9533 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009534 out_err:
9535 free(rule);
9536 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009537}
9538
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009539/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009540struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009541{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009542 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009543 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009544 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009545 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009546
9547 rule = calloc(1, sizeof(*rule));
9548 if (!rule) {
9549 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9550 goto out_err;
9551 }
9552
9553 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009554 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009555 cur_arg = 1;
9556 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009557 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009558 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009559 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009560 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009561 cur_arg = 1;
9562
9563 if (!*args[cur_arg] ||
9564 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9565 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9566 file, linenum, args[0]);
9567 goto out_err;
9568 }
9569 rule->arg.nice = atoi(args[cur_arg]);
9570 if (rule->arg.nice < -1024)
9571 rule->arg.nice = -1024;
9572 else if (rule->arg.nice > 1024)
9573 rule->arg.nice = 1024;
9574 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009575 } else if (!strcmp(args[0], "set-tos")) {
9576#ifdef IP_TOS
9577 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009578 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009579 cur_arg = 1;
9580
9581 if (!*args[cur_arg] ||
9582 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9583 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9584 file, linenum, args[0]);
9585 goto out_err;
9586 }
9587
9588 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9589 if (err && *err != '\0') {
9590 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9591 file, linenum, err, args[0]);
9592 goto out_err;
9593 }
9594 cur_arg++;
9595#else
9596 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9597 goto out_err;
9598#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009599 } else if (!strcmp(args[0], "set-mark")) {
9600#ifdef SO_MARK
9601 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009602 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009603 cur_arg = 1;
9604
9605 if (!*args[cur_arg] ||
9606 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9607 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9608 file, linenum, args[0]);
9609 goto out_err;
9610 }
9611
9612 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9613 if (err && *err != '\0') {
9614 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9615 file, linenum, err, args[0]);
9616 goto out_err;
9617 }
9618 cur_arg++;
9619 global.last_checks |= LSTCHK_NETADM;
9620#else
9621 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9622 goto out_err;
9623#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009624 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009625 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009626 cur_arg = 1;
9627
9628 if (!*args[cur_arg] ||
9629 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9630 bad_log_level:
9631 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9632 file, linenum, args[0]);
9633 goto out_err;
9634 }
9635 if (strcmp(args[cur_arg], "silent") == 0)
9636 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009637 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009638 goto bad_log_level;
9639 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009640 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009641 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009642 cur_arg = 1;
9643
9644 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9645 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9646 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9647 file, linenum, args[0]);
9648 goto out_err;
9649 }
9650
9651 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9652 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9653 LIST_INIT(&rule->arg.hdr_add.fmt);
9654
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009655 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009656 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009657 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 +01009658 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9659 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9660 file, linenum, args[0], error);
9661 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009662 goto out_err;
9663 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009664 free(proxy->conf.lfs_file);
9665 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9666 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009667 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009668 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009669 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009670 cur_arg = 1;
9671
9672 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009673 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9674 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009675 file, linenum, args[0]);
9676 goto out_err;
9677 }
9678
9679 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9680 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9681 LIST_INIT(&rule->arg.hdr_add.fmt);
9682
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009683 error = NULL;
9684 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9685 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9686 args[cur_arg + 1], error);
9687 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009688 goto out_err;
9689 }
9690
9691 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009692 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009693 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 +01009694 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9695 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9696 file, linenum, args[0], error);
9697 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009698 goto out_err;
9699 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009700
9701 free(proxy->conf.lfs_file);
9702 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9703 proxy->conf.lfs_line = proxy->conf.args.line;
9704 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009705 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009706 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009707 cur_arg = 1;
9708
9709 if (!*args[cur_arg] ||
9710 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9711 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9712 file, linenum, args[0]);
9713 goto out_err;
9714 }
9715
9716 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9717 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9718
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009719 proxy->conf.args.ctx = ARGC_HRS;
9720 free(proxy->conf.lfs_file);
9721 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9722 proxy->conf.lfs_line = proxy->conf.args.line;
9723 cur_arg += 1;
9724 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9725 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009726 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009727 /*
9728 * '+ 8' for 'add-acl('
9729 * '- 9' for 'add-acl(' + trailing ')'
9730 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009731 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009732
9733 cur_arg = 1;
9734
9735 if (!*args[cur_arg] ||
9736 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9737 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9738 file, linenum, args[0]);
9739 goto out_err;
9740 }
9741
9742 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009743 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009744 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009745 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009746 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9747 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9748 file, linenum, args[0], error);
9749 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009750 goto out_err;
9751 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009752 free(proxy->conf.lfs_file);
9753 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9754 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009755
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009756 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009757 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9758 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009759 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009760 /*
9761 * '+ 8' for 'del-acl('
9762 * '- 9' for 'del-acl(' + trailing ')'
9763 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009764 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009765
9766 cur_arg = 1;
9767
9768 if (!*args[cur_arg] ||
9769 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9770 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9771 file, linenum, args[0]);
9772 goto out_err;
9773 }
9774
9775 LIST_INIT(&rule->arg.map.key);
9776 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009777 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009778 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009779 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9780 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9781 file, linenum, args[0], error);
9782 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009783 goto out_err;
9784 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009785 free(proxy->conf.lfs_file);
9786 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9787 proxy->conf.lfs_line = proxy->conf.args.line;
9788 cur_arg += 1;
9789 } else if (strncmp(args[0], "del-map", 7) == 0) {
9790 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009791 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009792 /*
9793 * '+ 8' for 'del-map('
9794 * '- 9' for 'del-map(' + trailing ')'
9795 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009796 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009797
9798 cur_arg = 1;
9799
9800 if (!*args[cur_arg] ||
9801 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9802 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9803 file, linenum, args[0]);
9804 goto out_err;
9805 }
9806
9807 LIST_INIT(&rule->arg.map.key);
9808 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009809 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009810 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009811 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9812 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9813 file, linenum, args[0], error);
9814 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009815 goto out_err;
9816 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009817 free(proxy->conf.lfs_file);
9818 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9819 proxy->conf.lfs_line = proxy->conf.args.line;
9820 cur_arg += 1;
9821 } else if (strncmp(args[0], "set-map", 7) == 0) {
9822 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009823 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009824 /*
9825 * '+ 8' for 'set-map('
9826 * '- 9' for 'set-map(' + trailing ')'
9827 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009828 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009829
9830 cur_arg = 1;
9831
9832 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9833 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9834 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9835 file, linenum, args[0]);
9836 goto out_err;
9837 }
9838
9839 LIST_INIT(&rule->arg.map.key);
9840 LIST_INIT(&rule->arg.map.value);
9841
9842 proxy->conf.args.ctx = ARGC_HRS;
9843
9844 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009845 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009846 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009847 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9848 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9849 file, linenum, args[0], error);
9850 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009851 goto out_err;
9852 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009853
9854 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009855 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009856 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 +01009857 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9858 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9859 file, linenum, args[0], error);
9860 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009861 goto out_err;
9862 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009863
9864 free(proxy->conf.lfs_file);
9865 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9866 proxy->conf.lfs_line = proxy->conf.args.line;
9867
9868 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009869 } else if (strcmp(args[0], "redirect") == 0) {
9870 struct redirect_rule *redir;
9871 char *errmsg = NULL;
9872
9873 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9874 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9875 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9876 goto out_err;
9877 }
9878
9879 /* this redirect rule might already contain a parsed condition which
9880 * we'll pass to the http-request rule.
9881 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009882 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009883 rule->arg.redir = redir;
9884 rule->cond = redir->cond;
9885 redir->cond = NULL;
9886 cur_arg = 2;
9887 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009888 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9889 args[0][9] == '\0' && args[0][8] >= '0' &&
9890 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9891 struct sample_expr *expr;
9892 unsigned int where;
9893 char *err = NULL;
9894
9895 cur_arg = 1;
9896 proxy->conf.args.ctx = ARGC_TRK;
9897
9898 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9899 if (!expr) {
9900 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9901 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9902 free(err);
9903 goto out_err;
9904 }
9905
9906 where = 0;
9907 if (proxy->cap & PR_CAP_FE)
9908 where |= SMP_VAL_FE_HRS_HDR;
9909 if (proxy->cap & PR_CAP_BE)
9910 where |= SMP_VAL_BE_HRS_HDR;
9911
9912 if (!(expr->fetch->val & where)) {
9913 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9914 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9915 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9916 args[cur_arg-1], sample_src_names(expr->fetch->use));
9917 free(expr);
9918 goto out_err;
9919 }
9920
9921 if (strcmp(args[cur_arg], "table") == 0) {
9922 cur_arg++;
9923 if (!args[cur_arg]) {
9924 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9925 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9926 free(expr);
9927 goto out_err;
9928 }
9929 /* we copy the table name for now, it will be resolved later */
9930 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9931 cur_arg++;
9932 }
9933 rule->arg.trk_ctr.expr = expr;
9934 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009935 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9936 char *errmsg = NULL;
9937 cur_arg = 1;
9938 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009939 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009940 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009941 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009942 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9943 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9944 free(errmsg);
9945 goto out_err;
9946 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009947 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009948 action_build_list(&http_res_keywords.list, &trash);
9949 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9950 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009951 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009952 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009953 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009954 goto out_err;
9955 }
9956
9957 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9958 struct acl_cond *cond;
9959 char *errmsg = NULL;
9960
9961 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9962 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9963 file, linenum, args[0], errmsg);
9964 free(errmsg);
9965 goto out_err;
9966 }
9967 rule->cond = cond;
9968 }
9969 else if (*args[cur_arg]) {
9970 Alert("parsing [%s:%d]: 'http-response %s' expects"
9971 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9972 file, linenum, args[0], args[cur_arg]);
9973 goto out_err;
9974 }
9975
9976 return rule;
9977 out_err:
9978 free(rule);
9979 return NULL;
9980}
9981
Willy Tarreau4baae242012-12-27 12:00:31 +01009982/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009983 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009984 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9985 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009986 */
9987struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009988 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009989{
9990 struct redirect_rule *rule;
9991 int cur_arg;
9992 int type = REDIRECT_TYPE_NONE;
9993 int code = 302;
9994 const char *destination = NULL;
9995 const char *cookie = NULL;
9996 int cookie_set = 0;
9997 unsigned int flags = REDIRECT_FLAG_NONE;
9998 struct acl_cond *cond = NULL;
9999
10000 cur_arg = 0;
10001 while (*(args[cur_arg])) {
10002 if (strcmp(args[cur_arg], "location") == 0) {
10003 if (!*args[cur_arg + 1])
10004 goto missing_arg;
10005
10006 type = REDIRECT_TYPE_LOCATION;
10007 cur_arg++;
10008 destination = args[cur_arg];
10009 }
10010 else if (strcmp(args[cur_arg], "prefix") == 0) {
10011 if (!*args[cur_arg + 1])
10012 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010013 type = REDIRECT_TYPE_PREFIX;
10014 cur_arg++;
10015 destination = args[cur_arg];
10016 }
10017 else if (strcmp(args[cur_arg], "scheme") == 0) {
10018 if (!*args[cur_arg + 1])
10019 goto missing_arg;
10020
10021 type = REDIRECT_TYPE_SCHEME;
10022 cur_arg++;
10023 destination = args[cur_arg];
10024 }
10025 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10026 if (!*args[cur_arg + 1])
10027 goto missing_arg;
10028
10029 cur_arg++;
10030 cookie = args[cur_arg];
10031 cookie_set = 1;
10032 }
10033 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10034 if (!*args[cur_arg + 1])
10035 goto missing_arg;
10036
10037 cur_arg++;
10038 cookie = args[cur_arg];
10039 cookie_set = 0;
10040 }
10041 else if (strcmp(args[cur_arg], "code") == 0) {
10042 if (!*args[cur_arg + 1])
10043 goto missing_arg;
10044
10045 cur_arg++;
10046 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010047 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010048 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010049 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010050 args[cur_arg - 1], args[cur_arg]);
10051 return NULL;
10052 }
10053 }
10054 else if (!strcmp(args[cur_arg],"drop-query")) {
10055 flags |= REDIRECT_FLAG_DROP_QS;
10056 }
10057 else if (!strcmp(args[cur_arg],"append-slash")) {
10058 flags |= REDIRECT_FLAG_APPEND_SLASH;
10059 }
10060 else if (strcmp(args[cur_arg], "if") == 0 ||
10061 strcmp(args[cur_arg], "unless") == 0) {
10062 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10063 if (!cond) {
10064 memprintf(errmsg, "error in condition: %s", *errmsg);
10065 return NULL;
10066 }
10067 break;
10068 }
10069 else {
10070 memprintf(errmsg,
10071 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10072 args[cur_arg]);
10073 return NULL;
10074 }
10075 cur_arg++;
10076 }
10077
10078 if (type == REDIRECT_TYPE_NONE) {
10079 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10080 return NULL;
10081 }
10082
Willy Tarreaube4653b2015-05-28 15:26:58 +020010083 if (dir && type != REDIRECT_TYPE_LOCATION) {
10084 memprintf(errmsg, "response only supports redirect type 'location'");
10085 return NULL;
10086 }
10087
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010088 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010089 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010090 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010091
10092 if (!use_fmt) {
10093 /* old-style static redirect rule */
10094 rule->rdr_str = strdup(destination);
10095 rule->rdr_len = strlen(destination);
10096 }
10097 else {
10098 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010099
10100 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10101 * if prefix == "/", we don't want to add anything, otherwise it
10102 * makes it hard for the user to configure a self-redirection.
10103 */
Godbachd9722032014-12-18 15:44:58 +080010104 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010105 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010106 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10107 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 +010010108 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10109 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010110 return NULL;
10111 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010112 free(curproxy->conf.lfs_file);
10113 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10114 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010115 }
10116 }
10117
Willy Tarreau4baae242012-12-27 12:00:31 +010010118 if (cookie) {
10119 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10120 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10121 */
10122 rule->cookie_len = strlen(cookie);
10123 if (cookie_set) {
10124 rule->cookie_str = malloc(rule->cookie_len + 10);
10125 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10126 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10127 rule->cookie_len += 9;
10128 } else {
10129 rule->cookie_str = malloc(rule->cookie_len + 21);
10130 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10131 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10132 rule->cookie_len += 20;
10133 }
10134 }
10135 rule->type = type;
10136 rule->code = code;
10137 rule->flags = flags;
10138 LIST_INIT(&rule->list);
10139 return rule;
10140
10141 missing_arg:
10142 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10143 return NULL;
10144}
10145
Willy Tarreau8797c062007-05-07 00:55:35 +020010146/************************************************************************/
10147/* The code below is dedicated to ACL parsing and matching */
10148/************************************************************************/
10149
10150
Willy Tarreau14174bc2012-04-16 14:34:04 +020010151/* This function ensures that the prerequisites for an L7 fetch are ready,
10152 * which means that a request or response is ready. If some data is missing,
10153 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010154 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10155 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010156 *
10157 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010158 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10159 * decide whether or not an HTTP message is present ;
10160 * 0 if the requested data cannot be fetched or if it is certain that
10161 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010162 * 1 if an HTTP message is ready
10163 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010164int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010165 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010166{
Willy Tarreau192252e2015-04-04 01:47:55 +020010167 struct http_txn *txn;
10168 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010169
Willy Tarreaube508f12016-03-10 11:47:01 +010010170 /* Note: it is possible that <s> is NULL when called before stream
10171 * initialization (eg: tcp-request connection), so this function is the
10172 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010173 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010174 if (!s)
10175 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010176
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010177 if (!s->txn) {
10178 if (unlikely(!http_alloc_txn(s)))
10179 return 0; /* not enough memory */
10180 http_init_txn(s);
10181 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010182 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010183 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010184
10185 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010186 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010187
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010188 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010189 /* If the buffer does not leave enough free space at the end,
10190 * we must first realign it.
10191 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010192 if (s->req.buf->p > s->req.buf->data &&
10193 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10194 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010195
Willy Tarreau14174bc2012-04-16 14:34:04 +020010196 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010197 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010198 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010199
10200 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010201 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010202 http_msg_analyzer(msg, &txn->hdr_idx);
10203
10204 /* Still no valid request ? */
10205 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010206 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010207 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010208 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010209 }
10210 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010211 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010212 return 0;
10213 }
10214
10215 /* OK we just got a valid HTTP request. We have some minor
10216 * preparation to perform so that further checks can rely
10217 * on HTTP tests.
10218 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010219
10220 /* If the request was parsed but was too large, we must absolutely
10221 * return an error so that it is not processed. At the moment this
10222 * cannot happen, but if the parsers are to change in the future,
10223 * we want this check to be maintained.
10224 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010225 if (unlikely(s->req.buf->i + s->req.buf->p >
10226 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010227 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010228 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010229 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010230 return 1;
10231 }
10232
Willy Tarreau9b28e032012-10-12 23:49:43 +020010233 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010234 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010235 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010236
Willy Tarreau506d0502013-07-06 13:29:24 +020010237 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10238 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010239 }
10240
Willy Tarreau506d0502013-07-06 13:29:24 +020010241 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010242 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010243 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010244
10245 /* otherwise everything's ready for the request */
10246 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010247 else {
10248 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010249 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10250 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010251 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010252 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010253 }
10254
10255 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010256 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010257 return 1;
10258}
Willy Tarreau8797c062007-05-07 00:55:35 +020010259
Willy Tarreau8797c062007-05-07 00:55:35 +020010260/* 1. Check on METHOD
10261 * We use the pre-parsed method if it is known, and store its number as an
10262 * integer. If it is unknown, we use the pointer and the length.
10263 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010264static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010265{
10266 int len, meth;
10267
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010268 len = strlen(text);
10269 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010270
10271 pattern->val.i = meth;
10272 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010273 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010274 pattern->len = len;
10275 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010276 else {
10277 pattern->ptr.str = NULL;
10278 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010279 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010280 return 1;
10281}
10282
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010283/* This function fetches the method of current HTTP request and stores
10284 * it in the global pattern struct as a chunk. There are two possibilities :
10285 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10286 * in <len> and <ptr> is NULL ;
10287 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10288 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010289 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010290 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010291static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010292smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010293{
10294 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010295 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010296
Willy Tarreau24e32d82012-04-23 23:55:44 +020010297 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010298
Willy Tarreaube508f12016-03-10 11:47:01 +010010299 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010300 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010301 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010302 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010303 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010304 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10305 /* ensure the indexes are not affected */
10306 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010307 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010308 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10309 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010310 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010311 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010312 return 1;
10313}
10314
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010315/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010316static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010317{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010318 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010319 struct pattern_list *lst;
10320 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010321
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010322 list_for_each_entry(lst, &expr->patterns, list) {
10323 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010324
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010325 /* well-known method */
10326 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010327 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010328 return pattern;
10329 else
10330 continue;
10331 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010332
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010333 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010334 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010335 continue;
10336
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010337 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010338 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10339 (!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 +010010340 return pattern;
10341 }
10342 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010343}
10344
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010345static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010346smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010347{
Willy Tarreaube508f12016-03-10 11:47:01 +010010348 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010349 char *ptr;
10350 int len;
10351
Willy Tarreauc0239e02012-04-16 14:42:55 +020010352 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010353
Willy Tarreaube508f12016-03-10 11:47:01 +010010354 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010355 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010356 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010357
10358 while ((len-- > 0) && (*ptr++ != '/'));
10359 if (len <= 0)
10360 return 0;
10361
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010362 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010363 smp->data.u.str.str = ptr;
10364 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010365
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010366 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010367 return 1;
10368}
10369
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010370static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010371smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010372{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010373 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010374 char *ptr;
10375 int len;
10376
Willy Tarreauc0239e02012-04-16 14:42:55 +020010377 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010378
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010379 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010380 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10381 return 0;
10382
Willy Tarreau8797c062007-05-07 00:55:35 +020010383 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010384 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010385
10386 while ((len-- > 0) && (*ptr++ != '/'));
10387 if (len <= 0)
10388 return 0;
10389
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010390 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010391 smp->data.u.str.str = ptr;
10392 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010393
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010394 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010395 return 1;
10396}
10397
10398/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010399static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010400smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010401{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010402 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010403 char *ptr;
10404 int len;
10405
Willy Tarreauc0239e02012-04-16 14:42:55 +020010406 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010407
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010408 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010409 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10410 return 0;
10411
Willy Tarreau8797c062007-05-07 00:55:35 +020010412 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010413 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010414
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010415 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010416 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010417 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010418 return 1;
10419}
10420
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010421static int
10422smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10423{
10424 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10425 return 0;
10426
10427 if (!smp->strm->unique_id) {
10428 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10429 return 0;
10430 smp->strm->unique_id[0] = '\0';
10431 }
10432 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10433 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10434
10435 smp->data.type = SMP_T_STR;
10436 smp->data.u.str.str = smp->strm->unique_id;
10437 smp->flags = SMP_F_CONST;
10438 return 1;
10439}
10440
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020010441/* Returns a string block containing all headers including the
10442 * empty line wich separes headers from the body. This is useful
10443 * form some headers analysis.
10444 */
10445static int
10446smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
10447{
10448 struct http_msg *msg;
10449 struct hdr_idx *idx;
10450 struct http_txn *txn;
10451
10452 CHECK_HTTP_MESSAGE_FIRST();
10453
10454 txn = smp->strm->txn;
10455 idx = &txn->hdr_idx;
10456 msg = &txn->req;
10457
10458 smp->data.type = SMP_T_STR;
10459 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
10460 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
10461 (msg->chn->buf->p[msg->eoh] == '\r');
10462
10463 return 1;
10464}
10465
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020010466/* Returns the header request in a length/value encoded format.
10467 * This is useful for exchanges with the SPOE.
10468 *
10469 * A "length value" is a multibyte code encoding numbers. It uses the
10470 * SPOE format. The encoding is the following:
10471 *
10472 * Each couple "header name" / "header value" is composed
10473 * like this:
10474 * "length value" "header name bytes"
10475 * "length value" "header value bytes"
10476 * When the last header is reached, the header name and the header
10477 * value are empty. Their length are 0
10478 */
10479static int
10480smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
10481{
10482 struct http_msg *msg;
10483 struct chunk *temp;
10484 struct hdr_idx *idx;
10485 const char *cur_ptr, *cur_next, *p;
10486 int old_idx, cur_idx;
10487 struct hdr_idx_elem *cur_hdr;
10488 const char *hn, *hv;
10489 int hnl, hvl;
10490 int ret;
10491 struct http_txn *txn;
10492 char *buf;
10493 char *end;
10494
10495 CHECK_HTTP_MESSAGE_FIRST();
10496
10497 temp = get_trash_chunk();
10498 buf = temp->str;
10499 end = temp->str + temp->size;
10500
10501 txn = smp->strm->txn;
10502 idx = &txn->hdr_idx;
10503 msg = &txn->req;
10504
10505 /* Build array of headers. */
10506 old_idx = 0;
10507 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
10508 while (1) {
10509 cur_idx = idx->v[old_idx].next;
10510 if (!cur_idx)
10511 break;
10512 old_idx = cur_idx;
10513
10514 cur_hdr = &idx->v[cur_idx];
10515 cur_ptr = cur_next;
10516 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
10517
10518 /* Now we have one full header at cur_ptr of len cur_hdr->len,
10519 * and the next header starts at cur_next. We'll check
10520 * this header in the list as well as against the default
10521 * rule.
10522 */
10523
10524 /* look for ': *'. */
10525 hn = cur_ptr;
10526 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
10527 if (p >= cur_ptr+cur_hdr->len)
10528 continue;
10529 hnl = p - hn;
10530 p++;
10531 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
10532 p++;
10533 if (p >= cur_ptr + cur_hdr->len)
10534 continue;
10535 hv = p;
10536 hvl = cur_ptr + cur_hdr->len-p;
10537
10538 /* encode the header name. */
10539 ret = encode_varint(hnl, &buf, end);
10540 if (ret == -1)
10541 return 0;
10542 if (buf + hnl > end)
10543 return 0;
10544 memcpy(buf, hn, hnl);
10545 buf += hnl;
10546
10547 /* encode and copy the value. */
10548 ret = encode_varint(hvl, &buf, end);
10549 if (ret == -1)
10550 return 0;
10551 if (buf + hvl > end)
10552 return 0;
10553 memcpy(buf, hv, hvl);
10554 buf += hvl;
10555 }
10556
10557 /* encode the end of the header list with empty
10558 * header name and header value.
10559 */
10560 ret = encode_varint(0, &buf, end);
10561 if (ret == -1)
10562 return 0;
10563 ret = encode_varint(0, &buf, end);
10564 if (ret == -1)
10565 return 0;
10566
10567 /* Initialise sample data which will be filled. */
10568 smp->data.type = SMP_T_BIN;
10569 smp->data.u.str.str = temp->str;
10570 smp->data.u.str.len = buf - temp->str;
10571 smp->data.u.str.size = temp->size;
10572
10573 return 1;
10574}
10575
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010576/* returns the longest available part of the body. This requires that the body
10577 * has been waited for using http-buffer-request.
10578 */
10579static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010580smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010581{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010582 struct http_msg *msg;
10583 unsigned long len;
10584 unsigned long block1;
10585 char *body;
10586 struct chunk *temp;
10587
10588 CHECK_HTTP_MESSAGE_FIRST();
10589
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010590 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010591 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010592 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010593 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010594
10595 len = http_body_bytes(msg);
10596 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10597
10598 block1 = len;
10599 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10600 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10601
10602 if (block1 == len) {
10603 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010604 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010605 smp->data.u.str.str = body;
10606 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010607 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10608 }
10609 else {
10610 /* buffer is wrapped, we need to defragment it */
10611 temp = get_trash_chunk();
10612 memcpy(temp->str, body, block1);
10613 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010614 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010615 smp->data.u.str.str = temp->str;
10616 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010617 smp->flags = SMP_F_VOL_TEST;
10618 }
10619 return 1;
10620}
10621
10622
10623/* returns the available length of the body. This requires that the body
10624 * has been waited for using http-buffer-request.
10625 */
10626static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010627smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010628{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010629 struct http_msg *msg;
10630
10631 CHECK_HTTP_MESSAGE_FIRST();
10632
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010634 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010635 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010636 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010637
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010638 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010639 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010640
10641 smp->flags = SMP_F_VOL_TEST;
10642 return 1;
10643}
10644
10645
10646/* returns the advertised length of the body, or the advertised size of the
10647 * chunks available in the buffer. This requires that the body has been waited
10648 * for using http-buffer-request.
10649 */
10650static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010651smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010652{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010653 struct http_msg *msg;
10654
10655 CHECK_HTTP_MESSAGE_FIRST();
10656
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010657 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010658 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010659 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010660 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010661
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010662 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010663 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010664
10665 smp->flags = SMP_F_VOL_TEST;
10666 return 1;
10667}
10668
10669
Willy Tarreau8797c062007-05-07 00:55:35 +020010670/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010672smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010673{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010674 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010675
Willy Tarreauc0239e02012-04-16 14:42:55 +020010676 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010678 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010679 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010680 smp->data.u.str.len = txn->req.sl.rq.u_l;
10681 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010682 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010683 return 1;
10684}
10685
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010686static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010687smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010688{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010689 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010690 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010691
Willy Tarreauc0239e02012-04-16 14:42:55 +020010692 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010693
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010694 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010695 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 +020010696 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010697 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010698
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010699 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010700 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010701 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010702 return 1;
10703}
10704
10705static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010706smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010707{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010708 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010709 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010710
Willy Tarreauc0239e02012-04-16 14:42:55 +020010711 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010712
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010713 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010714 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 +020010715 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10716 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010717
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010718 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010719 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010720 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010721 return 1;
10722}
10723
Willy Tarreau185b5c42012-04-26 15:11:51 +020010724/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10725 * Accepts an optional argument of type string containing the header field name,
10726 * and an optional argument of type signed or unsigned integer to request an
10727 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010728 * headers are considered from the first one. It does not stop on commas and
10729 * returns full lines instead (useful for User-Agent or Date for example).
10730 */
10731static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010732smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010733{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010734 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010735 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010736 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010737 int occ = 0;
10738 const char *name_str = NULL;
10739 int name_len = 0;
10740
10741 if (!ctx) {
10742 /* first call */
10743 ctx = &static_hdr_ctx;
10744 ctx->idx = 0;
10745 smp->ctx.a[0] = ctx;
10746 }
10747
10748 if (args) {
10749 if (args[0].type != ARGT_STR)
10750 return 0;
10751 name_str = args[0].data.str.str;
10752 name_len = args[0].data.str.len;
10753
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010754 if (args[1].type == ARGT_SINT)
10755 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010756 }
10757
10758 CHECK_HTTP_MESSAGE_FIRST();
10759
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010760 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010761 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 +020010762
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010763 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10764 /* search for header from the beginning */
10765 ctx->idx = 0;
10766
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010767 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010768 /* no explicit occurrence and single fetch => last header by default */
10769 occ = -1;
10770
10771 if (!occ)
10772 /* prepare to report multiple occurrences for ACL fetches */
10773 smp->flags |= SMP_F_NOT_LAST;
10774
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010775 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010776 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010777 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 +020010778 return 1;
10779
10780 smp->flags &= ~SMP_F_NOT_LAST;
10781 return 0;
10782}
10783
10784/* 6. Check on HTTP header count. The number of occurrences is returned.
10785 * Accepts exactly 1 argument of type string. It does not stop on commas and
10786 * returns full lines instead (useful for User-Agent or Date for example).
10787 */
10788static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010789smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010790{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010791 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010792 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010793 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010794 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010795 const char *name = NULL;
10796 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010797
Willy Tarreau601a4d12015-04-01 19:16:09 +020010798 if (args && args->type == ARGT_STR) {
10799 name = args->data.str.str;
10800 len = args->data.str.len;
10801 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010802
10803 CHECK_HTTP_MESSAGE_FIRST();
10804
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010805 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010806 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010807
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010808 ctx.idx = 0;
10809 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010810 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010811 cnt++;
10812
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010813 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010814 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010815 smp->flags = SMP_F_VOL_HDR;
10816 return 1;
10817}
10818
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010819static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010820smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010821{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010822 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010823 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010824 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010825 struct chunk *temp;
10826 char del = ',';
10827
10828 if (args && args->type == ARGT_STR)
10829 del = *args[0].data.str.str;
10830
10831 CHECK_HTTP_MESSAGE_FIRST();
10832
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010833 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010834 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 +020010835
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010836 temp = get_trash_chunk();
10837
10838 ctx.idx = 0;
10839 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10840 if (temp->len)
10841 temp->str[temp->len++] = del;
10842 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10843 temp->len += ctx.del;
10844 }
10845
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010846 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010847 smp->data.u.str.str = temp->str;
10848 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010849 smp->flags = SMP_F_VOL_HDR;
10850 return 1;
10851}
10852
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010853/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10854 * Accepts an optional argument of type string containing the header field name,
10855 * and an optional argument of type signed or unsigned integer to request an
10856 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010857 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010858 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010859static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010860smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010861{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010862 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010863 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010864 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010865 int occ = 0;
10866 const char *name_str = NULL;
10867 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010868
Willy Tarreaua890d072013-04-02 12:01:06 +020010869 if (!ctx) {
10870 /* first call */
10871 ctx = &static_hdr_ctx;
10872 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010873 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010874 }
10875
Willy Tarreau185b5c42012-04-26 15:11:51 +020010876 if (args) {
10877 if (args[0].type != ARGT_STR)
10878 return 0;
10879 name_str = args[0].data.str.str;
10880 name_len = args[0].data.str.len;
10881
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010882 if (args[1].type == ARGT_SINT)
10883 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010884 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010885
Willy Tarreaue333ec92012-04-16 16:26:40 +020010886 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010887
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010888 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010889 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 +020010890
Willy Tarreau185b5c42012-04-26 15:11:51 +020010891 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010892 /* search for header from the beginning */
10893 ctx->idx = 0;
10894
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010895 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010896 /* no explicit occurrence and single fetch => last header by default */
10897 occ = -1;
10898
10899 if (!occ)
10900 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010901 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010902
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010903 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010904 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010905 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 +020010906 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010907
Willy Tarreau37406352012-04-23 16:16:37 +020010908 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010909 return 0;
10910}
10911
Willy Tarreauc11416f2007-06-17 16:58:38 +020010912/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010913 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010914 */
10915static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010916smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010917{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010918 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010919 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010920 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010921 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010922 const char *name = NULL;
10923 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010924
Willy Tarreau601a4d12015-04-01 19:16:09 +020010925 if (args && args->type == ARGT_STR) {
10926 name = args->data.str.str;
10927 len = args->data.str.len;
10928 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010929
Willy Tarreaue333ec92012-04-16 16:26:40 +020010930 CHECK_HTTP_MESSAGE_FIRST();
10931
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010932 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010933 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 +020010934
Willy Tarreau33a7e692007-06-10 19:45:56 +020010935 ctx.idx = 0;
10936 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010937 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010938 cnt++;
10939
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010940 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010941 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010942 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010943 return 1;
10944}
10945
Willy Tarreau185b5c42012-04-26 15:11:51 +020010946/* Fetch an HTTP header's integer value. The integer value is returned. It
10947 * takes a mandatory argument of type string and an optional one of type int
10948 * to designate a specific occurrence. It returns an unsigned integer, which
10949 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010950 */
10951static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010952smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010953{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010954 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010955
Willy Tarreauf853c462012-04-23 18:53:56 +020010956 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010957 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010958 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010959 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010960
Willy Tarreaud53e2422012-04-16 17:21:11 +020010961 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010962}
10963
Cyril Bonté69fa9922012-10-25 00:01:06 +020010964/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10965 * and an optional one of type int to designate a specific occurrence.
10966 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010967 */
10968static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010969smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010970{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010971 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010972
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010973 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010974 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010975 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010976 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010977 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010978 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010979 if (smp->data.u.str.len < temp->size - 1) {
10980 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10981 temp->str[smp->data.u.str.len] = '\0';
10982 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010983 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010984 break;
10985 }
10986 }
10987 }
10988
Willy Tarreaud53e2422012-04-16 17:21:11 +020010989 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010990 if (!(smp->flags & SMP_F_NOT_LAST))
10991 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010992 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010993 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010994}
10995
Willy Tarreau737b0c12007-06-10 21:28:46 +020010996/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10997 * the first '/' after the possible hostname, and ends before the possible '?'.
10998 */
10999static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011000smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020011001{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011002 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011003 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020011004
Willy Tarreauc0239e02012-04-16 14:42:55 +020011005 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020011006
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011007 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020011008 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010011009 ptr = http_get_path(txn);
11010 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020011011 return 0;
11012
11013 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011014 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011015 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011016
11017 while (ptr < end && *ptr != '?')
11018 ptr++;
11019
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011020 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011021 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020011022 return 1;
11023}
11024
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011025/* This produces a concatenation of the first occurrence of the Host header
11026 * followed by the path component if it begins with a slash ('/'). This means
11027 * that '*' will not be added, resulting in exactly the first Host entry.
11028 * If no Host header is found, then the path is returned as-is. The returned
11029 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010011030 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011031 */
11032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011033smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011034{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011035 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011036 char *ptr, *end, *beg;
11037 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011038 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011039
11040 CHECK_HTTP_MESSAGE_FIRST();
11041
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011042 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011043 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011044 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011045 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011046
11047 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011048 temp = get_trash_chunk();
11049 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011050 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011051 smp->data.u.str.str = temp->str;
11052 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011053
11054 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011055 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011056 beg = http_get_path(txn);
11057 if (!beg)
11058 beg = end;
11059
11060 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11061
11062 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011063 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
11064 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011065 }
11066
11067 smp->flags = SMP_F_VOL_1ST;
11068 return 1;
11069}
11070
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011071/* This produces a 32-bit hash of the concatenation of the first occurrence of
11072 * the Host header followed by the path component if it begins with a slash ('/').
11073 * This means that '*' will not be added, resulting in exactly the first Host
11074 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011075 * is hashed using the path hash followed by a full avalanche hash and provides a
11076 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011077 * high-traffic sites without having to store whole paths.
11078 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020011079int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011080smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011081{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011082 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011083 struct hdr_ctx ctx;
11084 unsigned int hash = 0;
11085 char *ptr, *beg, *end;
11086 int len;
11087
11088 CHECK_HTTP_MESSAGE_FIRST();
11089
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011090 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011091 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011092 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011093 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11094 ptr = ctx.line + ctx.val;
11095 len = ctx.vlen;
11096 while (len--)
11097 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11098 }
11099
11100 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011101 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011102 beg = http_get_path(txn);
11103 if (!beg)
11104 beg = end;
11105
11106 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11107
11108 if (beg < ptr && *beg == '/') {
11109 while (beg < ptr)
11110 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11111 }
11112 hash = full_hash(hash);
11113
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011114 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011115 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011116 smp->flags = SMP_F_VOL_1ST;
11117 return 1;
11118}
11119
Willy Tarreau4a550602012-12-09 14:53:32 +010011120/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011121 * path as returned by smp_fetch_base32(). The idea is to have per-source and
11122 * per-path counters. The result is a binary block from 8 to 20 bytes depending
11123 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010011124 * that in environments where IPv6 is insignificant, truncating the output to
11125 * 8 bytes would still work.
11126 */
11127static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011128smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011129{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011130 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011131 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011132
11133 if (!cli_conn)
11134 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011135
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011136 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011137 return 0;
11138
Willy Tarreau47ca5452012-12-23 20:22:19 +010011139 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011140 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011141 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011142
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011143 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011144 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011145 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011146 temp->len += 4;
11147 break;
11148 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011149 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011150 temp->len += 16;
11151 break;
11152 default:
11153 return 0;
11154 }
11155
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011156 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011157 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011158 return 1;
11159}
11160
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011161/* Extracts the query string, which comes after the question mark '?'. If no
11162 * question mark is found, nothing is returned. Otherwise it returns a sample
11163 * of type string carrying the whole query string.
11164 */
11165static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011166smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011167{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011168 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011169 char *ptr, *end;
11170
11171 CHECK_HTTP_MESSAGE_FIRST();
11172
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011173 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011174 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11175 end = ptr + txn->req.sl.rq.u_l;
11176
11177 /* look up the '?' */
11178 do {
11179 if (ptr == end)
11180 return 0;
11181 } while (*ptr++ != '?');
11182
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011183 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011184 smp->data.u.str.str = ptr;
11185 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011186 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11187 return 1;
11188}
11189
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011190static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011191smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011192{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011193 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11194 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11195 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011196
Willy Tarreau24e32d82012-04-23 23:55:44 +020011197 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011198
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011199 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011200 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011201 return 1;
11202}
11203
Willy Tarreau7f18e522010-10-22 20:04:13 +020011204/* return a valid test if the current request is the first one on the connection */
11205static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011206smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011207{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011208 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011209 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011210 return 1;
11211}
11212
Willy Tarreau34db1082012-04-19 17:16:54 +020011213/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011214static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011215smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011216{
11217
Willy Tarreau24e32d82012-04-23 23:55:44 +020011218 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011219 return 0;
11220
Willy Tarreauc0239e02012-04-16 14:42:55 +020011221 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011222
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011223 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011224 return 0;
11225
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011226 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011227 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011228 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011229 return 1;
11230}
Willy Tarreau8797c062007-05-07 00:55:35 +020011231
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011232/* Accepts exactly 1 argument of type userlist */
11233static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011234smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011235{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011236 if (!args || args->type != ARGT_USR)
11237 return 0;
11238
11239 CHECK_HTTP_MESSAGE_FIRST();
11240
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011241 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011242 return 0;
11243
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011244 /* if the user does not belong to the userlist or has a wrong password,
11245 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011246 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011247 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011248 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11249 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011250 return 0;
11251
11252 /* pat_match_auth() will need the user list */
11253 smp->ctx.a[0] = args->data.usr;
11254
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011255 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011256 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011257 smp->data.u.str.str = smp->strm->txn->auth.user;
11258 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011259
11260 return 1;
11261}
11262
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011263/* Try to find the next occurrence of a cookie name in a cookie header value.
11264 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11265 * the cookie value is returned into *value and *value_l, and the function
11266 * returns a pointer to the next pointer to search from if the value was found.
11267 * Otherwise if the cookie was not found, NULL is returned and neither value
11268 * nor value_l are touched. The input <hdr> string should first point to the
11269 * header's value, and the <hdr_end> pointer must point to the first character
11270 * not part of the value. <list> must be non-zero if value may represent a list
11271 * of values (cookie headers). This makes it faster to abort parsing when no
11272 * list is expected.
11273 */
David Carlier4686f792015-09-25 14:10:50 +010011274char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011275extract_cookie_value(char *hdr, const char *hdr_end,
11276 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011277 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011278{
11279 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11280 char *next;
11281
11282 /* we search at least a cookie name followed by an equal, and more
11283 * generally something like this :
11284 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11285 */
11286 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11287 /* Iterate through all cookies on this line */
11288
Willy Tarreau2235b262016-11-05 15:50:20 +010011289 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011290 att_beg++;
11291
11292 /* find att_end : this is the first character after the last non
11293 * space before the equal. It may be equal to hdr_end.
11294 */
11295 equal = att_end = att_beg;
11296
11297 while (equal < hdr_end) {
11298 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11299 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011300 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011301 continue;
11302 att_end = equal;
11303 }
11304
11305 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11306 * is between <att_beg> and <equal>, both may be identical.
11307 */
11308
11309 /* look for end of cookie if there is an equal sign */
11310 if (equal < hdr_end && *equal == '=') {
11311 /* look for the beginning of the value */
11312 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011313 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 val_beg++;
11315
11316 /* find the end of the value, respecting quotes */
11317 next = find_cookie_value_end(val_beg, hdr_end);
11318
11319 /* make val_end point to the first white space or delimitor after the value */
11320 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011321 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011322 val_end--;
11323 } else {
11324 val_beg = val_end = next = equal;
11325 }
11326
11327 /* We have nothing to do with attributes beginning with '$'. However,
11328 * they will automatically be removed if a header before them is removed,
11329 * since they're supposed to be linked together.
11330 */
11331 if (*att_beg == '$')
11332 continue;
11333
11334 /* Ignore cookies with no equal sign */
11335 if (equal == next)
11336 continue;
11337
11338 /* Now we have the cookie name between att_beg and att_end, and
11339 * its value between val_beg and val_end.
11340 */
11341
11342 if (att_end - att_beg == cookie_name_l &&
11343 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11344 /* let's return this value and indicate where to go on from */
11345 *value = val_beg;
11346 *value_l = val_end - val_beg;
11347 return next + 1;
11348 }
11349
11350 /* Set-Cookie headers only have the name in the first attr=value part */
11351 if (!list)
11352 break;
11353 }
11354
11355 return NULL;
11356}
11357
William Lallemanda43ba4e2014-01-28 18:14:25 +010011358/* Fetch a captured HTTP request header. The index is the position of
11359 * the "capture" option in the configuration file
11360 */
11361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011362smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011363{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011364 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011365 int idx;
11366
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011367 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011368 return 0;
11369
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011370 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011371
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011372 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 +010011373 return 0;
11374
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011375 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011376 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011377 smp->data.u.str.str = smp->strm->req_cap[idx];
11378 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011379
11380 return 1;
11381}
11382
11383/* Fetch a captured HTTP response header. The index is the position of
11384 * the "capture" option in the configuration file
11385 */
11386static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011387smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011388{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011389 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011390 int idx;
11391
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011392 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011393 return 0;
11394
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011395 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011396
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011397 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 +010011398 return 0;
11399
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011400 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011401 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011402 smp->data.u.str.str = smp->strm->res_cap[idx];
11403 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011404
11405 return 1;
11406}
11407
William Lallemand65ad6e12014-01-31 15:08:02 +010011408/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11409static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011410smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011411{
11412 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011413 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011414 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011415
Willy Tarreau15e91e12015-04-04 00:52:09 +020011416 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011417 return 0;
11418
William Lallemand96a77852014-02-05 00:30:02 +010011419 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011420
William Lallemand96a77852014-02-05 00:30:02 +010011421 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11422 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011423
William Lallemand96a77852014-02-05 00:30:02 +010011424 temp = get_trash_chunk();
11425 temp->str = txn->uri;
11426 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011427 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011428 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011429 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011430
11431 return 1;
11432
11433}
11434
11435/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11436static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011437smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011438{
11439 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011440 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011441 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011442
Willy Tarreau15e91e12015-04-04 00:52:09 +020011443 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011444 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011445
William Lallemand65ad6e12014-01-31 15:08:02 +010011446 ptr = txn->uri;
11447
11448 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11449 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011450
William Lallemand65ad6e12014-01-31 15:08:02 +010011451 if (!*ptr)
11452 return 0;
11453
11454 ptr++; /* skip the space */
11455
11456 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011457 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011458 if (!ptr)
11459 return 0;
11460 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11461 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011462
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011463 smp->data.u.str = *temp;
11464 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011465 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011466 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011467
11468 return 1;
11469}
11470
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011471/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11472 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11473 */
11474static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011475smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011476{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011477 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011478
Willy Tarreau15e91e12015-04-04 00:52:09 +020011479 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011480 return 0;
11481
11482 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011483 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011484 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011485 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011486
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011487 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011488 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011489 smp->flags = SMP_F_CONST;
11490 return 1;
11491
11492}
11493
11494/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11495 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11496 */
11497static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011498smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011499{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011500 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011501
Willy Tarreau15e91e12015-04-04 00:52:09 +020011502 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011503 return 0;
11504
11505 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011506 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011507 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011508 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011509
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011510 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011511 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011512 smp->flags = SMP_F_CONST;
11513 return 1;
11514
11515}
11516
William Lallemand65ad6e12014-01-31 15:08:02 +010011517
Willy Tarreaue333ec92012-04-16 16:26:40 +020011518/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011519 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011520 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011521 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011522 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011523 * Accepts exactly 1 argument of type string. If the input options indicate
11524 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011525 * The returned sample is of type CSTR. Can be used to parse cookies in other
11526 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011527 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011528int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011529{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011530 struct http_txn *txn;
11531 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011532 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011533 const struct http_msg *msg;
11534 const char *hdr_name;
11535 int hdr_name_len;
11536 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011537 int occ = 0;
11538 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011539
Willy Tarreau24e32d82012-04-23 23:55:44 +020011540 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011541 return 0;
11542
Willy Tarreaua890d072013-04-02 12:01:06 +020011543 if (!ctx) {
11544 /* first call */
11545 ctx = &static_hdr_ctx;
11546 ctx->idx = 0;
11547 smp->ctx.a[2] = ctx;
11548 }
11549
Willy Tarreaue333ec92012-04-16 16:26:40 +020011550 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011551
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011552 txn = smp->strm->txn;
11553 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011554
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011555 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011556 msg = &txn->req;
11557 hdr_name = "Cookie";
11558 hdr_name_len = 6;
11559 } else {
11560 msg = &txn->rsp;
11561 hdr_name = "Set-Cookie";
11562 hdr_name_len = 10;
11563 }
11564
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011565 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011566 /* no explicit occurrence and single fetch => last cookie by default */
11567 occ = -1;
11568
11569 /* OK so basically here, either we want only one value and it's the
11570 * last one, or we want to iterate over all of them and we fetch the
11571 * next one.
11572 */
11573
Willy Tarreau9b28e032012-10-12 23:49:43 +020011574 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011575 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011576 /* search for the header from the beginning, we must first initialize
11577 * the search parameters.
11578 */
Willy Tarreau37406352012-04-23 16:16:37 +020011579 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011580 ctx->idx = 0;
11581 }
11582
Willy Tarreau28376d62012-04-26 21:26:10 +020011583 smp->flags |= SMP_F_VOL_HDR;
11584
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011585 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011586 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11587 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011588 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11589 goto out;
11590
Willy Tarreau24e32d82012-04-23 23:55:44 +020011591 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011592 continue;
11593
Willy Tarreau37406352012-04-23 16:16:37 +020011594 smp->ctx.a[0] = ctx->line + ctx->val;
11595 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011596 }
11597
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011598 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011599 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011600 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011601 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011602 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011603 &smp->data.u.str.str,
11604 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011605 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011606 found = 1;
11607 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011608 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011609 smp->flags |= SMP_F_NOT_LAST;
11610 return 1;
11611 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011612 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011613 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011614 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011615 /* all cookie headers and values were scanned. If we're looking for the
11616 * last occurrence, we may return it now.
11617 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011618 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011619 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011620 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011621}
11622
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011623/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011624 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011625 * multiple cookies may be parsed on the same line. The returned sample is of
11626 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011627 */
11628static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011629smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011630{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011631 struct http_txn *txn;
11632 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011633 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011634 const struct http_msg *msg;
11635 const char *hdr_name;
11636 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011637 int cnt;
11638 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011639 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011640
Willy Tarreau24e32d82012-04-23 23:55:44 +020011641 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011642 return 0;
11643
Willy Tarreaue333ec92012-04-16 16:26:40 +020011644 CHECK_HTTP_MESSAGE_FIRST();
11645
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011646 txn = smp->strm->txn;
11647 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011648
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011649 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011650 msg = &txn->req;
11651 hdr_name = "Cookie";
11652 hdr_name_len = 6;
11653 } else {
11654 msg = &txn->rsp;
11655 hdr_name = "Set-Cookie";
11656 hdr_name_len = 10;
11657 }
11658
Willy Tarreau9b28e032012-10-12 23:49:43 +020011659 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011660 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011661 ctx.idx = 0;
11662 cnt = 0;
11663
11664 while (1) {
11665 /* Note: val_beg == NULL every time we need to fetch a new header */
11666 if (!val_beg) {
11667 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11668 break;
11669
Willy Tarreau24e32d82012-04-23 23:55:44 +020011670 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011671 continue;
11672
11673 val_beg = ctx.line + ctx.val;
11674 val_end = val_beg + ctx.vlen;
11675 }
11676
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011677 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011678 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011679 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011680 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011681 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011682 &smp->data.u.str.str,
11683 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011684 cnt++;
11685 }
11686 }
11687
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011688 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011689 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011690 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011691 return 1;
11692}
11693
Willy Tarreau51539362012-05-08 12:46:28 +020011694/* Fetch an cookie's integer value. The integer value is returned. It
11695 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11696 */
11697static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011698smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011699{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011700 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011701
11702 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011703 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011704 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011705 }
11706
11707 return ret;
11708}
11709
Willy Tarreau8797c062007-05-07 00:55:35 +020011710/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011711/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011712/************************************************************************/
11713
David Cournapeau16023ee2010-12-23 20:55:41 +090011714/*
11715 * Given a path string and its length, find the position of beginning of the
11716 * query string. Returns NULL if no query string is found in the path.
11717 *
11718 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11719 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011720 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011721 */
bedis4c75cca2012-10-05 08:38:24 +020011722static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011723{
11724 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011725
bedis4c75cca2012-10-05 08:38:24 +020011726 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011727 return p ? p + 1 : NULL;
11728}
11729
bedis4c75cca2012-10-05 08:38:24 +020011730static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011731{
bedis4c75cca2012-10-05 08:38:24 +020011732 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011733}
11734
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011735/* after increasing a pointer value, it can exceed the first buffer
11736 * size. This function transform the value of <ptr> according with
11737 * the expected position. <chunks> is an array of the one or two
11738 * avalaible chunks. The first value is the start of the first chunk,
11739 * the second value if the end+1 of the first chunks. The third value
11740 * is NULL or the start of the second chunk and the fourth value is
11741 * the end+1 of the second chunk. The function returns 1 if does a
11742 * wrap, else returns 0.
11743 */
11744static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11745{
11746 if (*ptr < chunks[1])
11747 return 0;
11748 if (!chunks[2])
11749 return 0;
11750 *ptr = chunks[2] + ( *ptr - chunks[1] );
11751 return 1;
11752}
11753
David Cournapeau16023ee2010-12-23 20:55:41 +090011754/*
11755 * Given a url parameter, find the starting position of the first occurence,
11756 * or NULL if the parameter is not found.
11757 *
11758 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11759 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011760 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011761 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011762 * or the second chunk. The caller must be check the position before using the
11763 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011764 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011765static const char *
11766find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011767 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011768 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011769{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011770 const char *pos, *last, *equal;
11771 const char **bufs = chunks;
11772 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011773
David Cournapeau16023ee2010-12-23 20:55:41 +090011774
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011775 pos = bufs[0];
11776 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011777 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011778 /* Check the equal. */
11779 equal = pos + url_param_name_l;
11780 if (fix_pointer_if_wrap(chunks, &equal)) {
11781 if (equal >= chunks[3])
11782 return NULL;
11783 } else {
11784 if (equal >= chunks[1])
11785 return NULL;
11786 }
11787 if (*equal == '=') {
11788 if (pos + url_param_name_l > last) {
11789 /* process wrap case, we detect a wrap. In this case, the
11790 * comparison is performed in two parts.
11791 */
11792
11793 /* This is the end, we dont have any other chunk. */
11794 if (bufs != chunks || !bufs[2])
11795 return NULL;
11796
11797 /* Compute the length of each part of the comparison. */
11798 l1 = last - pos;
11799 l2 = url_param_name_l - l1;
11800
11801 /* The second buffer is too short to contain the compared string. */
11802 if (bufs[2] + l2 > bufs[3])
11803 return NULL;
11804
11805 if (memcmp(pos, url_param_name, l1) == 0 &&
11806 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11807 return pos;
11808
11809 /* Perform wrapping and jump the string who fail the comparison. */
11810 bufs += 2;
11811 pos = bufs[0] + l2;
11812 last = bufs[1];
11813
11814 } else {
11815 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011816 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11817 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011818 pos += url_param_name_l + 1;
11819 if (fix_pointer_if_wrap(chunks, &pos))
11820 last = bufs[2];
11821 }
11822 }
11823
11824 while (1) {
11825 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011826 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011827 pos++;
11828 if (pos < last)
11829 break;
11830 /* process buffer wrapping. */
11831 if (bufs != chunks || !bufs[2])
11832 return NULL;
11833 bufs += 2;
11834 pos = bufs[0];
11835 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011836 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011837 pos++;
11838 }
11839 return NULL;
11840}
11841
11842/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011843 * Given a url parameter name and a query string, find the next value.
11844 * An empty url_param_name matches the first available parameter.
11845 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11846 * respectively provide a pointer to the value and its end.
11847 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011848 */
11849static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011850find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011851 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011852 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011853{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011854 const char *arg_start, *qs_end;
11855 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011856
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011857 arg_start = chunks[0];
11858 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011859 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011860 /* Looks for an argument name. */
11861 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011862 url_param_name, url_param_name_l,
11863 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011864 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011865 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011866 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011867 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011868 if (!arg_start)
11869 return 0;
11870
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011871 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011872 while (1) {
11873 /* looks for the first argument. */
11874 value_start = memchr(arg_start, '=', qs_end - arg_start);
11875 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011876 /* Check for wrapping. */
11877 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011878 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011879 chunks[2]) {
11880 arg_start = chunks[2];
11881 qs_end = chunks[3];
11882 continue;
11883 }
11884 return 0;
11885 }
11886 break;
11887 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011888 value_start++;
11889 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011890 else {
11891 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011892 value_start = arg_start + url_param_name_l + 1;
11893
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011894 /* Check for pointer wrapping. */
11895 if (fix_pointer_if_wrap(chunks, &value_start)) {
11896 /* Update the end pointer. */
11897 qs_end = chunks[3];
11898
11899 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011900 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011901 return 0;
11902 }
11903 }
11904
David Cournapeau16023ee2010-12-23 20:55:41 +090011905 value_end = value_start;
11906
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011907 while (1) {
11908 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11909 value_end++;
11910 if (value_end < qs_end)
11911 break;
11912 /* process buffer wrapping. */
11913 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011914 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011915 chunks[2]) {
11916 value_end = chunks[2];
11917 qs_end = chunks[3];
11918 continue;
11919 }
11920 break;
11921 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011922
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011923 *vstart = value_start;
11924 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011925 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011926}
11927
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011928/* This scans a URL-encoded query string. It takes an optionally wrapping
11929 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11930 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11931 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011932 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011933static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011934smp_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 +090011935{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011936 const char *vstart, *vend;
11937 struct chunk *temp;
11938 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011939
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011940 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011941 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011942 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011943 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011944 return 0;
11945
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011946 /* Create sample. If the value is contiguous, return the pointer as CONST,
11947 * if the value is wrapped, copy-it in a buffer.
11948 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011949 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011950 if (chunks[2] &&
11951 vstart >= chunks[0] && vstart <= chunks[1] &&
11952 vend >= chunks[2] && vend <= chunks[3]) {
11953 /* Wrapped case. */
11954 temp = get_trash_chunk();
11955 memcpy(temp->str, vstart, chunks[1] - vstart);
11956 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011957 smp->data.u.str.str = temp->str;
11958 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011959 } else {
11960 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011961 smp->data.u.str.str = (char *)vstart;
11962 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011963 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11964 }
11965
11966 /* Update context, check wrapping. */
11967 chunks[0] = vend;
11968 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11969 chunks[1] = chunks[3];
11970 chunks[2] = NULL;
11971 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011972
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011973 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011974 smp->flags |= SMP_F_NOT_LAST;
11975
David Cournapeau16023ee2010-12-23 20:55:41 +090011976 return 1;
11977}
11978
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011979/* This function iterates over each parameter of the query string. It uses
11980 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011981 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11982 * An optional parameter name is passed in args[0], otherwise any parameter is
11983 * considered. It supports an optional delimiter argument for the beginning of
11984 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011985 */
11986static int
11987smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11988{
11989 struct http_msg *msg;
11990 char delim = '?';
11991 const char *name;
11992 int name_len;
11993
Dragan Dosen26f77e52015-05-25 10:02:11 +020011994 if (!args ||
11995 (args[0].type && args[0].type != ARGT_STR) ||
11996 (args[1].type && args[1].type != ARGT_STR))
11997 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011998
Dragan Dosen26f77e52015-05-25 10:02:11 +020011999 name = "";
12000 name_len = 0;
12001 if (args->type == ARGT_STR) {
12002 name = args->data.str.str;
12003 name_len = args->data.str.len;
12004 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012005
Dragan Dosen26f77e52015-05-25 10:02:11 +020012006 if (args[1].type)
12007 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012008
Dragan Dosen26f77e52015-05-25 10:02:11 +020012009 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012010 CHECK_HTTP_MESSAGE_FIRST();
12011
12012 msg = &smp->strm->txn->req;
12013
12014 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
12015 msg->sl.rq.u_l, delim);
12016 if (!smp->ctx.a[0])
12017 return 0;
12018
12019 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012020
12021 /* Assume that the context is filled with NULL pointer
12022 * before the first call.
12023 * smp->ctx.a[2] = NULL;
12024 * smp->ctx.a[3] = NULL;
12025 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012026 }
12027
12028 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
12029}
12030
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012031/* This function iterates over each parameter of the body. This requires
12032 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012033 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
12034 * contigous part of the body, and optionally ctx->a[2..3] to reference the
12035 * optional second part if the body wraps at the end of the buffer. An optional
12036 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012037 */
12038static int
12039smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
12040{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012041 struct http_msg *msg;
12042 unsigned long len;
12043 unsigned long block1;
12044 char *body;
12045 const char *name;
12046 int name_len;
12047
12048 if (!args || (args[0].type && args[0].type != ARGT_STR))
12049 return 0;
12050
12051 name = "";
12052 name_len = 0;
12053 if (args[0].type == ARGT_STR) {
12054 name = args[0].data.str.str;
12055 name_len = args[0].data.str.len;
12056 }
12057
12058 if (!smp->ctx.a[0]) { // first call, find the query string
12059 CHECK_HTTP_MESSAGE_FIRST();
12060
12061 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010012062 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012063 else
Willy Tarreaube508f12016-03-10 11:47:01 +010012064 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012065
12066 len = http_body_bytes(msg);
12067 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
12068
12069 block1 = len;
12070 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
12071 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
12072
12073 if (block1 == len) {
12074 /* buffer is not wrapped (or empty) */
12075 smp->ctx.a[0] = body;
12076 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012077
12078 /* Assume that the context is filled with NULL pointer
12079 * before the first call.
12080 * smp->ctx.a[2] = NULL;
12081 * smp->ctx.a[3] = NULL;
12082 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012083 }
12084 else {
12085 /* buffer is wrapped, we need to defragment it */
12086 smp->ctx.a[0] = body;
12087 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012088 smp->ctx.a[2] = msg->chn->buf->data;
12089 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012090 }
12091 }
12092 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
12093}
12094
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012095/* Return the signed integer value for the specified url parameter (see url_param
12096 * above).
12097 */
12098static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012099smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012100{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012101 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012102
12103 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012104 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012105 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012106 }
12107
12108 return ret;
12109}
12110
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012111/* This produces a 32-bit hash of the concatenation of the first occurrence of
12112 * the Host header followed by the path component if it begins with a slash ('/').
12113 * This means that '*' will not be added, resulting in exactly the first Host
12114 * entry. If no Host header is found, then the path is used. The resulting value
12115 * is hashed using the url hash followed by a full avalanche hash and provides a
12116 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
12117 * high-traffic sites without having to store whole paths.
12118 * this differs from the base32 functions in that it includes the url parameters
12119 * as well as the path
12120 */
12121static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012122smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012123{
Willy Tarreau15e91e12015-04-04 00:52:09 +020012124 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012125 struct hdr_ctx ctx;
12126 unsigned int hash = 0;
12127 char *ptr, *beg, *end;
12128 int len;
12129
12130 CHECK_HTTP_MESSAGE_FIRST();
12131
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012132 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012133 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012134 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012135 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12136 ptr = ctx.line + ctx.val;
12137 len = ctx.vlen;
12138 while (len--)
12139 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12140 }
12141
12142 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012143 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 +000012144 beg = http_get_path(txn);
12145 if (!beg)
12146 beg = end;
12147
12148 for (ptr = beg; ptr < end ; ptr++);
12149
12150 if (beg < ptr && *beg == '/') {
12151 while (beg < ptr)
12152 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12153 }
12154 hash = full_hash(hash);
12155
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012156 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012157 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012158 smp->flags = SMP_F_VOL_1ST;
12159 return 1;
12160}
12161
12162/* This concatenates the source address with the 32-bit hash of the Host and
12163 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12164 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12165 * on the source address length. The URL hash is stored before the address so
12166 * that in environments where IPv6 is insignificant, truncating the output to
12167 * 8 bytes would still work.
12168 */
12169static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012170smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012171{
12172 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012173 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012174
Dragan Dosendb5af612016-06-16 11:23:01 +020012175 if (!cli_conn)
12176 return 0;
12177
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012178 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012179 return 0;
12180
12181 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012182 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12183 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012184
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012185 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012186 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012187 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012188 temp->len += 4;
12189 break;
12190 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012191 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012192 temp->len += 16;
12193 break;
12194 default:
12195 return 0;
12196 }
12197
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012198 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012199 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012200 return 1;
12201}
12202
Willy Tarreau185b5c42012-04-26 15:11:51 +020012203/* This function is used to validate the arguments passed to any "hdr" fetch
12204 * keyword. These keywords support an optional positive or negative occurrence
12205 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12206 * is assumed that the types are already the correct ones. Returns 0 on error,
12207 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12208 * error message in case of error, that the caller is responsible for freeing.
12209 * The initial location must either be freeable or NULL.
12210 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012211int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012212{
12213 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012214 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012215 return 0;
12216 }
12217 return 1;
12218}
12219
Willy Tarreau276fae92013-07-25 14:36:01 +020012220/* takes an UINT value on input supposed to represent the time since EPOCH,
12221 * adds an optional offset found in args[0] and emits a string representing
12222 * the date in RFC-1123/5322 format.
12223 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012224static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012225{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012226 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012227 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12228 struct chunk *temp;
12229 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012230 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012231 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012232
12233 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012234 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012235 curr_date += args[0].data.sint;
12236
12237 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012238 if (!tm)
12239 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012240
12241 temp = get_trash_chunk();
12242 temp->len = snprintf(temp->str, temp->size - temp->len,
12243 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12244 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12245 tm->tm_hour, tm->tm_min, tm->tm_sec);
12246
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012247 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012248 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012249 return 1;
12250}
12251
Thierry FOURNIERad903512014-04-11 17:51:01 +020012252/* Match language range with language tag. RFC2616 14.4:
12253 *
12254 * A language-range matches a language-tag if it exactly equals
12255 * the tag, or if it exactly equals a prefix of the tag such
12256 * that the first tag character following the prefix is "-".
12257 *
12258 * Return 1 if the strings match, else return 0.
12259 */
12260static inline int language_range_match(const char *range, int range_len,
12261 const char *tag, int tag_len)
12262{
12263 const char *end = range + range_len;
12264 const char *tend = tag + tag_len;
12265 while (range < end) {
12266 if (*range == '-' && tag == tend)
12267 return 1;
12268 if (*range != *tag || tag == tend)
12269 return 0;
12270 range++;
12271 tag++;
12272 }
12273 /* Return true only if the last char of the tag is matched. */
12274 return tag == tend;
12275}
12276
12277/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012278static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012279{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012280 const char *al = smp->data.u.str.str;
12281 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012282 const char *token;
12283 int toklen;
12284 int qvalue;
12285 const char *str;
12286 const char *w;
12287 int best_q = 0;
12288
12289 /* Set the constant to the sample, because the output of the
12290 * function will be peek in the constant configuration string.
12291 */
12292 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012293 smp->data.u.str.size = 0;
12294 smp->data.u.str.str = "";
12295 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012296
12297 /* Parse the accept language */
12298 while (1) {
12299
12300 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012301 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012302 al++;
12303 if (al >= end)
12304 break;
12305
12306 /* Start of the fisrt word. */
12307 token = al;
12308
12309 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012310 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012311 al++;
12312 if (al == token)
12313 goto expect_comma;
12314
12315 /* Length of the token. */
12316 toklen = al - token;
12317 qvalue = 1000;
12318
12319 /* Check if the token exists in the list. If the token not exists,
12320 * jump to the next token.
12321 */
12322 str = args[0].data.str.str;
12323 w = str;
12324 while (1) {
12325 if (*str == ';' || *str == '\0') {
12326 if (language_range_match(token, toklen, w, str-w))
12327 goto look_for_q;
12328 if (*str == '\0')
12329 goto expect_comma;
12330 w = str + 1;
12331 }
12332 str++;
12333 }
12334 goto expect_comma;
12335
12336look_for_q:
12337
12338 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012339 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012340 al++;
12341 if (al >= end)
12342 goto process_value;
12343
12344 /* If ',' is found, process the result */
12345 if (*al == ',')
12346 goto process_value;
12347
12348 /* If the character is different from ';', look
12349 * for the end of the header part in best effort.
12350 */
12351 if (*al != ';')
12352 goto expect_comma;
12353
12354 /* Assumes that the char is ';', now expect "q=". */
12355 al++;
12356
12357 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012358 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012359 al++;
12360 if (al >= end)
12361 goto process_value;
12362
12363 /* Expect 'q'. If no 'q', continue in best effort */
12364 if (*al != 'q')
12365 goto process_value;
12366 al++;
12367
12368 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012369 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012370 al++;
12371 if (al >= end)
12372 goto process_value;
12373
12374 /* Expect '='. If no '=', continue in best effort */
12375 if (*al != '=')
12376 goto process_value;
12377 al++;
12378
12379 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012380 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012381 al++;
12382 if (al >= end)
12383 goto process_value;
12384
12385 /* Parse the q value. */
12386 qvalue = parse_qvalue(al, &al);
12387
12388process_value:
12389
12390 /* If the new q value is the best q value, then store the associated
12391 * language in the response. If qvalue is the biggest value (1000),
12392 * break the process.
12393 */
12394 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012395 smp->data.u.str.str = (char *)w;
12396 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012397 if (qvalue >= 1000)
12398 break;
12399 best_q = qvalue;
12400 }
12401
12402expect_comma:
12403
12404 /* Expect comma or end. If the end is detected, quit the loop. */
12405 while (al < end && *al != ',')
12406 al++;
12407 if (al >= end)
12408 break;
12409
12410 /* Comma is found, jump it and restart the analyzer. */
12411 al++;
12412 }
12413
12414 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012415 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12416 smp->data.u.str.str = args[1].data.str.str;
12417 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012418 }
12419
12420 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012421 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012422}
12423
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012424/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012425static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012426{
12427 /* If the constant flag is set or if not size is avalaible at
12428 * the end of the buffer, copy the string in other buffer
12429 * before decoding.
12430 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012431 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012432 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012433 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12434 smp->data.u.str.str = str->str;
12435 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012436 smp->flags &= ~SMP_F_CONST;
12437 }
12438
12439 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012440 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12441 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012442 return 1;
12443}
12444
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012445static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12446{
12447 struct proxy *fe = strm_fe(smp->strm);
12448 int idx, i;
12449 struct cap_hdr *hdr;
12450 int len;
12451
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012452 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012453 return 0;
12454
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012455 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012456
12457 /* Check the availibity of the capture id. */
12458 if (idx > fe->nb_req_cap - 1)
12459 return 0;
12460
12461 /* Look for the original configuration. */
12462 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12463 hdr != NULL && i != idx ;
12464 i--, hdr = hdr->next);
12465 if (!hdr)
12466 return 0;
12467
12468 /* check for the memory allocation */
12469 if (smp->strm->req_cap[hdr->index] == NULL)
12470 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12471 if (smp->strm->req_cap[hdr->index] == NULL)
12472 return 0;
12473
12474 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012475 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012476 if (len > hdr->len)
12477 len = hdr->len;
12478
12479 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012480 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012481 smp->strm->req_cap[idx][len] = '\0';
12482
12483 return 1;
12484}
12485
12486static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12487{
12488 struct proxy *fe = strm_fe(smp->strm);
12489 int idx, i;
12490 struct cap_hdr *hdr;
12491 int len;
12492
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012493 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012494 return 0;
12495
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012496 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012497
12498 /* Check the availibity of the capture id. */
12499 if (idx > fe->nb_rsp_cap - 1)
12500 return 0;
12501
12502 /* Look for the original configuration. */
12503 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12504 hdr != NULL && i != idx ;
12505 i--, hdr = hdr->next);
12506 if (!hdr)
12507 return 0;
12508
12509 /* check for the memory allocation */
12510 if (smp->strm->res_cap[hdr->index] == NULL)
12511 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12512 if (smp->strm->res_cap[hdr->index] == NULL)
12513 return 0;
12514
12515 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012516 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012517 if (len > hdr->len)
12518 len = hdr->len;
12519
12520 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012521 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012522 smp->strm->res_cap[idx][len] = '\0';
12523
12524 return 1;
12525}
12526
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012527/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012528 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012529 * the relevant part of the request line accordingly. Then it updates various
12530 * pointers to the next elements which were moved, and the total buffer length.
12531 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012532 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12533 * error, though this can be revisited when this code is finally exploited.
12534 *
12535 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12536 * query string and 3 to replace uri.
12537 *
12538 * In query string case, the mark question '?' must be set at the start of the
12539 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012540 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012541int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012542 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012543{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012544 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012545 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012546 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012547 int delta;
12548
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012549 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012550 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012551 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012552 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12553
12554 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012555 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012556 txn->req.sl.rq.m_l += delta;
12557 txn->req.sl.rq.u += delta;
12558 txn->req.sl.rq.v += delta;
12559 break;
12560
12561 case 1: // path
12562 cur_ptr = http_get_path(txn);
12563 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012564 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012565
12566 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012567 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 +010012568 cur_end++;
12569
12570 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012571 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012572 txn->req.sl.rq.u_l += delta;
12573 txn->req.sl.rq.v += delta;
12574 break;
12575
12576 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012577 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012578 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012579 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12580 while (cur_ptr < cur_end && *cur_ptr != '?')
12581 cur_ptr++;
12582
12583 /* skip the question mark or indicate that we must insert it
12584 * (but only if the format string is not empty then).
12585 */
12586 if (cur_ptr < cur_end)
12587 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012588 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012589 offset = 0;
12590
12591 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012592 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012593 txn->req.sl.rq.u_l += delta;
12594 txn->req.sl.rq.v += delta;
12595 break;
12596
12597 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012598 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012599 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12600
12601 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012602 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012603 txn->req.sl.rq.u_l += delta;
12604 txn->req.sl.rq.v += delta;
12605 break;
12606
12607 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012608 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012609 }
12610
12611 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012612 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012613 txn->req.sl.rq.l += delta;
12614 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012615 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012616 return 0;
12617}
12618
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012619/* This function replace the HTTP status code and the associated message. The
12620 * variable <status> contains the new status code. This function never fails.
12621 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012622void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012623{
12624 struct http_txn *txn = s->txn;
12625 char *cur_ptr, *cur_end;
12626 int delta;
12627 char *res;
12628 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012629 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012630 int msg_len;
12631
12632 chunk_reset(&trash);
12633
12634 res = ultoa_o(status, trash.str, trash.size);
12635 c_l = res - trash.str;
12636
12637 trash.str[c_l] = ' ';
12638 trash.len = c_l + 1;
12639
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012640 /* Do we have a custom reason format string? */
12641 if (msg == NULL)
12642 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012643 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012644 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12645 trash.len += msg_len;
12646
12647 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12648 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12649
12650 /* commit changes and adjust message */
12651 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12652
12653 /* adjust res line offsets and lengths */
12654 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12655 txn->rsp.sl.st.c_l = c_l;
12656 txn->rsp.sl.st.r_l = msg_len;
12657
12658 delta = trash.len - (cur_end - cur_ptr);
12659 txn->rsp.sl.st.l += delta;
12660 txn->hdr_idx.v[0].len += delta;
12661 http_msg_move_end(&txn->rsp, delta);
12662}
12663
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012664/* This function executes one of the set-{method,path,query,uri} actions. It
12665 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012666 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012667 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012668 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12669 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012670 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012671enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012672 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012673{
12674 chunk_reset(&trash);
12675
12676 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012677 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012678 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012679 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012680
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012681 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012682 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012683}
12684
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012685/* This function is just a compliant action wrapper for "set-status". */
12686enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012687 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012688{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012689 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012690 return ACT_RET_CONT;
12691}
12692
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012693/* parse an http-request action among :
12694 * set-method
12695 * set-path
12696 * set-query
12697 * set-uri
12698 *
12699 * All of them accept a single argument of type string representing a log-format.
12700 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12701 * 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 +020012702 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012703 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012704enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12705 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012706{
12707 int cur_arg = *orig_arg;
12708
Thierry FOURNIER42148732015-09-02 17:17:33 +020012709 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012710
12711 switch (args[0][4]) {
12712 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012713 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012714 rule->action_ptr = http_action_set_req_line;
12715 break;
12716 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012717 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012718 rule->action_ptr = http_action_set_req_line;
12719 break;
12720 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012721 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012722 rule->action_ptr = http_action_set_req_line;
12723 break;
12724 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012725 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012726 rule->action_ptr = http_action_set_req_line;
12727 break;
12728 default:
12729 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012730 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012731 }
12732
12733 if (!*args[cur_arg] ||
12734 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12735 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012736 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012737 }
12738
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012739 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012740 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012741 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012742 (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 +010012743 return ACT_RET_PRS_ERR;
12744 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012745
12746 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012747 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012748}
12749
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012750/* parse set-status action:
12751 * This action accepts a single argument of type int representing
12752 * an http status code. It returns ACT_RET_PRS_OK on success,
12753 * ACT_RET_PRS_ERR on error.
12754 */
12755enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12756 struct act_rule *rule, char **err)
12757{
12758 char *error;
12759
Thierry FOURNIER42148732015-09-02 17:17:33 +020012760 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012761 rule->action_ptr = action_http_set_status;
12762
12763 /* Check if an argument is available */
12764 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012765 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012766 return ACT_RET_PRS_ERR;
12767 }
12768
12769 /* convert status code as integer */
12770 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12771 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12772 memprintf(err, "expects an integer status code between 100 and 999");
12773 return ACT_RET_PRS_ERR;
12774 }
12775
12776 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012777
12778 /* set custom reason string */
12779 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12780 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12781 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12782 (*orig_arg)++;
12783 rule->arg.status.reason = strdup(args[*orig_arg]);
12784 (*orig_arg)++;
12785 }
12786
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012787 return ACT_RET_PRS_OK;
12788}
12789
Willy Tarreaua9083d02015-05-08 15:27:59 +020012790/* This function executes the "capture" action. It executes a fetch expression,
12791 * turns the result into a string and puts it in a capture slot. It always
12792 * returns 1. If an error occurs the action is cancelled, but the rule
12793 * processing continues.
12794 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012795enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012796 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012797{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012798 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012799 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012800 char **cap = s->req_cap;
12801 int len;
12802
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012803 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 +020012804 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012805 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012806
12807 if (cap[h->index] == NULL)
12808 cap[h->index] = pool_alloc2(h->pool);
12809
12810 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012811 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012812
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012813 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012814 if (len > h->len)
12815 len = h->len;
12816
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012817 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012818 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012819 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012820}
12821
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012822/* This function executes the "capture" action and store the result in a
12823 * capture slot if exists. It executes a fetch expression, turns the result
12824 * into a string and puts it in a capture slot. It always returns 1. If an
12825 * error occurs the action is cancelled, but the rule processing continues.
12826 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012827enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012828 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012829{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012830 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012831 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012832 char **cap = s->req_cap;
12833 struct proxy *fe = strm_fe(s);
12834 int len;
12835 int i;
12836
12837 /* Look for the original configuration. */
12838 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012839 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012840 i--, h = h->next);
12841 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012842 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012843
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012844 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 +020012845 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012846 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012847
12848 if (cap[h->index] == NULL)
12849 cap[h->index] = pool_alloc2(h->pool);
12850
12851 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012852 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012853
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012854 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012855 if (len > h->len)
12856 len = h->len;
12857
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012858 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012859 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012860 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012861}
12862
Willy Tarreaua9083d02015-05-08 15:27:59 +020012863/* parse an "http-request capture" action. It takes a single argument which is
12864 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012865 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012866 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012867 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012868enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12869 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012870{
12871 struct sample_expr *expr;
12872 struct cap_hdr *hdr;
12873 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012874 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012875
12876 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12877 if (strcmp(args[cur_arg], "if") == 0 ||
12878 strcmp(args[cur_arg], "unless") == 0)
12879 break;
12880
12881 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012882 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012883 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012884 }
12885
Willy Tarreaua9083d02015-05-08 15:27:59 +020012886 cur_arg = *orig_arg;
12887 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12888 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012889 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012890
12891 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12892 memprintf(err,
12893 "fetch method '%s' extracts information from '%s', none of which is available here",
12894 args[cur_arg-1], sample_src_names(expr->fetch->use));
12895 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012896 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012897 }
12898
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012899 if (!args[cur_arg] || !*args[cur_arg]) {
12900 memprintf(err, "expects 'len or 'id'");
12901 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012902 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012903 }
12904
Willy Tarreaua9083d02015-05-08 15:27:59 +020012905 if (strcmp(args[cur_arg], "len") == 0) {
12906 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012907
12908 if (!(px->cap & PR_CAP_FE)) {
12909 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012910 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012911 }
12912
12913 proxy->conf.args.ctx = ARGC_CAP;
12914
Willy Tarreaua9083d02015-05-08 15:27:59 +020012915 if (!args[cur_arg]) {
12916 memprintf(err, "missing length value");
12917 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012918 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012919 }
12920 /* we copy the table name for now, it will be resolved later */
12921 len = atoi(args[cur_arg]);
12922 if (len <= 0) {
12923 memprintf(err, "length must be > 0");
12924 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012925 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012926 }
12927 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012928
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012929 if (!len) {
12930 memprintf(err, "a positive 'len' argument is mandatory");
12931 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012932 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012933 }
12934
Vincent Bernat02779b62016-04-03 13:48:43 +020012935 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012936 hdr->next = px->req_cap;
12937 hdr->name = NULL; /* not a header capture */
12938 hdr->namelen = 0;
12939 hdr->len = len;
12940 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12941 hdr->index = px->nb_req_cap++;
12942
12943 px->req_cap = hdr;
12944 px->to_log |= LW_REQHDR;
12945
Thierry FOURNIER42148732015-09-02 17:17:33 +020012946 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012947 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012948 rule->arg.cap.expr = expr;
12949 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012950 }
12951
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012952 else if (strcmp(args[cur_arg], "id") == 0) {
12953 int id;
12954 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012955
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012956 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012957
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012958 if (!args[cur_arg]) {
12959 memprintf(err, "missing id value");
12960 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012961 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012962 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012963
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012964 id = strtol(args[cur_arg], &error, 10);
12965 if (*error != '\0') {
12966 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12967 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012968 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012969 }
12970 cur_arg++;
12971
12972 proxy->conf.args.ctx = ARGC_CAP;
12973
Thierry FOURNIER42148732015-09-02 17:17:33 +020012974 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012975 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012976 rule->arg.capid.expr = expr;
12977 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012978 }
12979
12980 else {
12981 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12982 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012983 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012984 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012985
12986 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012987 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012988}
12989
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012990/* This function executes the "capture" action and store the result in a
12991 * capture slot if exists. It executes a fetch expression, turns the result
12992 * into a string and puts it in a capture slot. It always returns 1. If an
12993 * error occurs the action is cancelled, but the rule processing continues.
12994 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012995enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012996 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012997{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012998 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012999 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013000 char **cap = s->res_cap;
13001 struct proxy *fe = strm_fe(s);
13002 int len;
13003 int i;
13004
13005 /* Look for the original configuration. */
13006 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013007 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013008 i--, h = h->next);
13009 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013010 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013011
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013012 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 +020013013 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013014 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013015
13016 if (cap[h->index] == NULL)
13017 cap[h->index] = pool_alloc2(h->pool);
13018
13019 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013020 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013021
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013022 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013023 if (len > h->len)
13024 len = h->len;
13025
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013026 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013027 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013028 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013029}
13030
13031/* parse an "http-response capture" action. It takes a single argument which is
13032 * a sample fetch expression. It stores the expression into arg->act.p[0] and
13033 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013034 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013035 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013036enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
13037 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013038{
13039 struct sample_expr *expr;
13040 int cur_arg;
13041 int id;
13042 char *error;
13043
13044 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
13045 if (strcmp(args[cur_arg], "if") == 0 ||
13046 strcmp(args[cur_arg], "unless") == 0)
13047 break;
13048
13049 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013050 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013051 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013052 }
13053
13054 cur_arg = *orig_arg;
13055 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
13056 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013057 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013058
13059 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
13060 memprintf(err,
13061 "fetch method '%s' extracts information from '%s', none of which is available here",
13062 args[cur_arg-1], sample_src_names(expr->fetch->use));
13063 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013064 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013065 }
13066
13067 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013068 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013069 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013070 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013071 }
13072
13073 if (strcmp(args[cur_arg], "id") != 0) {
13074 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
13075 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013076 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013077 }
13078
13079 cur_arg++;
13080
13081 if (!args[cur_arg]) {
13082 memprintf(err, "missing id value");
13083 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013084 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013085 }
13086
13087 id = strtol(args[cur_arg], &error, 10);
13088 if (*error != '\0') {
13089 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
13090 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013091 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013092 }
13093 cur_arg++;
13094
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013095 proxy->conf.args.ctx = ARGC_CAP;
13096
Thierry FOURNIER42148732015-09-02 17:17:33 +020013097 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013098 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013099 rule->arg.capid.expr = expr;
13100 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013101
13102 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013103 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013104}
13105
William Lallemand73025dd2014-04-24 14:38:37 +020013106/*
13107 * Return the struct http_req_action_kw associated to a keyword.
13108 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013109struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013110{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013111 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013112}
13113
13114/*
13115 * Return the struct http_res_action_kw associated to a keyword.
13116 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013117struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013118{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013119 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013120}
13121
Willy Tarreau12207b32016-11-22 19:48:51 +010013122
13123/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
13124 * now.
13125 */
13126static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
13127{
13128 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13129 return 1;
13130
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013131 if (*args[2]) {
13132 struct proxy *px;
13133
13134 px = proxy_find_by_name(args[2], 0, 0);
13135 if (px)
13136 appctx->ctx.errors.iid = px->uuid;
13137 else
13138 appctx->ctx.errors.iid = atoi(args[2]);
13139
13140 if (!appctx->ctx.errors.iid) {
13141 appctx->ctx.cli.msg = "No such proxy.\n";
13142 appctx->st0 = CLI_ST_PRINT;
13143 return 1;
13144 }
13145 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013146 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013147 appctx->ctx.errors.iid = -1; // dump all proxies
13148
Willy Tarreau35069f82016-11-25 09:16:37 +010013149 appctx->ctx.errors.flag = 0;
13150 if (strcmp(args[3], "request") == 0)
13151 appctx->ctx.errors.flag |= 4; // ignore response
13152 else if (strcmp(args[3], "response") == 0)
13153 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013154 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013155 return 0;
13156}
13157
13158/* This function dumps all captured errors onto the stream interface's
13159 * read buffer. It returns 0 if the output buffer is full and it needs
13160 * to be called again, otherwise non-zero.
13161 */
13162static int cli_io_handler_show_errors(struct appctx *appctx)
13163{
13164 struct stream_interface *si = appctx->owner;
13165 extern const char *monthname[12];
13166
13167 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13168 return 1;
13169
13170 chunk_reset(&trash);
13171
13172 if (!appctx->ctx.errors.px) {
13173 /* the function had not been called yet, let's prepare the
13174 * buffer for a response.
13175 */
13176 struct tm tm;
13177
13178 get_localtime(date.tv_sec, &tm);
13179 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13180 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13181 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13182 error_snapshot_id);
13183
13184 if (bi_putchk(si_ic(si), &trash) == -1) {
13185 /* Socket buffer full. Let's try again later from the same point */
13186 si_applet_cant_put(si);
13187 return 0;
13188 }
13189
13190 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013191 appctx->ctx.errors.bol = 0;
13192 appctx->ctx.errors.ptr = -1;
13193 }
13194
13195 /* we have two inner loops here, one for the proxy, the other one for
13196 * the buffer.
13197 */
13198 while (appctx->ctx.errors.px) {
13199 struct error_snapshot *es;
13200
Willy Tarreau35069f82016-11-25 09:16:37 +010013201 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013202 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013203 if (appctx->ctx.errors.flag & 2) // skip req
13204 goto next;
13205 }
13206 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013207 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013208 if (appctx->ctx.errors.flag & 4) // skip resp
13209 goto next;
13210 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013211
13212 if (!es->when.tv_sec)
13213 goto next;
13214
13215 if (appctx->ctx.errors.iid >= 0 &&
13216 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13217 es->oe->uuid != appctx->ctx.errors.iid)
13218 goto next;
13219
13220 if (appctx->ctx.errors.ptr < 0) {
13221 /* just print headers now */
13222
13223 char pn[INET6_ADDRSTRLEN];
13224 struct tm tm;
13225 int port;
13226
13227 get_localtime(es->when.tv_sec, &tm);
13228 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13229 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13230 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13231
13232 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13233 case AF_INET:
13234 case AF_INET6:
13235 port = get_host_port(&es->src);
13236 break;
13237 default:
13238 port = 0;
13239 }
13240
Willy Tarreau35069f82016-11-25 09:16:37 +010013241 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013242 case 0:
13243 chunk_appendf(&trash,
13244 " frontend %s (#%d): invalid request\n"
13245 " backend %s (#%d)",
13246 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13247 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13248 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13249 break;
13250 case 1:
13251 chunk_appendf(&trash,
13252 " backend %s (#%d): invalid response\n"
13253 " frontend %s (#%d)",
13254 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13255 es->oe->id, es->oe->uuid);
13256 break;
13257 }
13258
13259 chunk_appendf(&trash,
13260 ", server %s (#%d), event #%u\n"
13261 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013262 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013263 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13264 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13265 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13266 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13267 es->ev_id,
13268 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013269 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013270 es->m_clen, es->m_blen,
13271 es->b_flags, es->b_out, es->b_tot,
13272 es->len, es->b_wrap, es->pos);
13273
13274 if (bi_putchk(si_ic(si), &trash) == -1) {
13275 /* Socket buffer full. Let's try again later from the same point */
13276 si_applet_cant_put(si);
13277 return 0;
13278 }
13279 appctx->ctx.errors.ptr = 0;
13280 appctx->ctx.errors.sid = es->sid;
13281 }
13282
13283 if (appctx->ctx.errors.sid != es->sid) {
13284 /* the snapshot changed while we were dumping it */
13285 chunk_appendf(&trash,
13286 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13287 if (bi_putchk(si_ic(si), &trash) == -1) {
13288 si_applet_cant_put(si);
13289 return 0;
13290 }
13291 goto next;
13292 }
13293
13294 /* OK, ptr >= 0, so we have to dump the current line */
13295 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13296 int newptr;
13297 int newline;
13298
13299 newline = appctx->ctx.errors.bol;
13300 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13301 if (newptr == appctx->ctx.errors.ptr)
13302 return 0;
13303
13304 if (bi_putchk(si_ic(si), &trash) == -1) {
13305 /* Socket buffer full. Let's try again later from the same point */
13306 si_applet_cant_put(si);
13307 return 0;
13308 }
13309 appctx->ctx.errors.ptr = newptr;
13310 appctx->ctx.errors.bol = newline;
13311 };
13312 next:
13313 appctx->ctx.errors.bol = 0;
13314 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013315 appctx->ctx.errors.flag ^= 1;
13316 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013317 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013318 }
13319
13320 /* dump complete */
13321 return 1;
13322}
13323
13324/* register cli keywords */
13325static struct cli_kw_list cli_kws = {{ },{
13326 { { "show", "errors", NULL },
13327 "show errors : report last request and response errors for each proxy",
13328 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13329 },
13330 {{},}
13331}};
13332
Willy Tarreau4a568972010-05-12 08:08:50 +020013333/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013334/* All supported ACL keywords must be declared here. */
13335/************************************************************************/
13336
13337/* Note: must not be declared <const> as its list will be overwritten.
13338 * Please take care of keeping this list alphabetically sorted.
13339 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013340static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013341 { "base", "base", PAT_MATCH_STR },
13342 { "base_beg", "base", PAT_MATCH_BEG },
13343 { "base_dir", "base", PAT_MATCH_DIR },
13344 { "base_dom", "base", PAT_MATCH_DOM },
13345 { "base_end", "base", PAT_MATCH_END },
13346 { "base_len", "base", PAT_MATCH_LEN },
13347 { "base_reg", "base", PAT_MATCH_REG },
13348 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013349
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013350 { "cook", "req.cook", PAT_MATCH_STR },
13351 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13352 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13353 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13354 { "cook_end", "req.cook", PAT_MATCH_END },
13355 { "cook_len", "req.cook", PAT_MATCH_LEN },
13356 { "cook_reg", "req.cook", PAT_MATCH_REG },
13357 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013358
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013359 { "hdr", "req.hdr", PAT_MATCH_STR },
13360 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13361 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13362 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13363 { "hdr_end", "req.hdr", PAT_MATCH_END },
13364 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13365 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13366 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013367
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013368 /* these two declarations uses strings with list storage (in place
13369 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13370 * and delete functions are relative to the list management. The parse
13371 * and match method are related to the corresponding fetch methods. This
13372 * is very particular ACL declaration mode.
13373 */
13374 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13375 { "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 +020013376
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013377 { "path", "path", PAT_MATCH_STR },
13378 { "path_beg", "path", PAT_MATCH_BEG },
13379 { "path_dir", "path", PAT_MATCH_DIR },
13380 { "path_dom", "path", PAT_MATCH_DOM },
13381 { "path_end", "path", PAT_MATCH_END },
13382 { "path_len", "path", PAT_MATCH_LEN },
13383 { "path_reg", "path", PAT_MATCH_REG },
13384 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013385
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013386 { "req_ver", "req.ver", PAT_MATCH_STR },
13387 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013388
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013389 { "scook", "res.cook", PAT_MATCH_STR },
13390 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13391 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13392 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13393 { "scook_end", "res.cook", PAT_MATCH_END },
13394 { "scook_len", "res.cook", PAT_MATCH_LEN },
13395 { "scook_reg", "res.cook", PAT_MATCH_REG },
13396 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013397
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013398 { "shdr", "res.hdr", PAT_MATCH_STR },
13399 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13400 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13401 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13402 { "shdr_end", "res.hdr", PAT_MATCH_END },
13403 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13404 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13405 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013406
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013407 { "url", "url", PAT_MATCH_STR },
13408 { "url_beg", "url", PAT_MATCH_BEG },
13409 { "url_dir", "url", PAT_MATCH_DIR },
13410 { "url_dom", "url", PAT_MATCH_DOM },
13411 { "url_end", "url", PAT_MATCH_END },
13412 { "url_len", "url", PAT_MATCH_LEN },
13413 { "url_reg", "url", PAT_MATCH_REG },
13414 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013415
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013416 { "urlp", "urlp", PAT_MATCH_STR },
13417 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13418 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13419 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13420 { "urlp_end", "urlp", PAT_MATCH_END },
13421 { "urlp_len", "urlp", PAT_MATCH_LEN },
13422 { "urlp_reg", "urlp", PAT_MATCH_REG },
13423 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013424
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013425 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013426}};
13427
13428/************************************************************************/
13429/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013430/************************************************************************/
13431/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013432static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013433 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013434 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013435 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13436
Willy Tarreau87b09662015-04-03 00:22:06 +020013437 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013438 { "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 +020013439
13440 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013441 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13442 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13443 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013444
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013445 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13446 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013447
Willy Tarreau409bcde2013-01-08 00:31:00 +010013448 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13449 * are only here to match the ACL's name, are request-only and are used
13450 * for ACL compatibility only.
13451 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013452 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13453 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013454 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13455 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013456
13457 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13458 * only here to match the ACL's name, are request-only and are used for
13459 * ACL compatibility only.
13460 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013461 { "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 +020013462 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013463 { "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 +020013464 { "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 +010013465
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013466 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013467 { "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 +010013468 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013469 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013470 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013471 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013472
13473 /* HTTP protocol on the request path */
13474 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013475 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013476
13477 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013478 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13479 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013480
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013481 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013482 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13483 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013484 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013485
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020013486 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020013487 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13488
Willy Tarreau18ed2562013-01-14 15:56:36 +010013489 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013490 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13491 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013492
Willy Tarreau18ed2562013-01-14 15:56:36 +010013493 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013494 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013495 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13496 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013497
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013498 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013499 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013500 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013501 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013502 { "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 +010013503 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013504 { "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 +010013505
13506 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013507 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013508 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13509 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013510
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013511 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013512 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013513 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013514 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013515 { "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 +010013516 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013517 { "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 +010013518
Willy Tarreau409bcde2013-01-08 00:31:00 +010013519 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013520 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013521 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13522 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013523 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013524
13525 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013526 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013527 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013528 { "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 +020013529 { "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 +010013530
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013531 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013532 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013533 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013534 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013535 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013536 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013537 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013538 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13539 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013540 { "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 +010013541 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013542}};
13543
Willy Tarreau8797c062007-05-07 00:55:35 +020013544
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013545/************************************************************************/
13546/* All supported converter keywords must be declared here. */
13547/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013548/* Note: must not be declared <const> as its list will be overwritten */
13549static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013550 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013551 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013552 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13553 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013554 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013555 { NULL, NULL, 0, 0, 0 },
13556}};
13557
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013558
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013559/************************************************************************/
13560/* All supported http-request action keywords must be declared here. */
13561/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013562struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013563 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013564 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013565 { "set-method", parse_set_req_line },
13566 { "set-path", parse_set_req_line },
13567 { "set-query", parse_set_req_line },
13568 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013569 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013570 }
13571};
13572
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013573struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013574 .kw = {
13575 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013576 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013577 { NULL, NULL }
13578 }
13579};
13580
Willy Tarreau8797c062007-05-07 00:55:35 +020013581__attribute__((constructor))
13582static void __http_protocol_init(void)
13583{
13584 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013585 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013586 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013587 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013588 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013589 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013590}
13591
13592
Willy Tarreau58f10d72006-12-04 02:26:12 +010013593/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013594 * Local variables:
13595 * c-indent-level: 8
13596 * c-basic-offset: 8
13597 * End:
13598 */