Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 28 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 42 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 46 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 47 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 48 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 50 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 51 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 52 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 53 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 55 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 56 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 58 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 59 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 60 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 62 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 64 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 65 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 66 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 67 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 69 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 70 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 72 | const char HTTP_100[] = |
| 73 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 74 | |
| 75 | const struct chunk http_100_chunk = { |
| 76 | .str = (char *)&HTTP_100, |
| 77 | .len = sizeof(HTTP_100)-1 |
| 78 | }; |
| 79 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 80 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 81 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 82 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 84 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 85 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 88 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 90 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 91 | |
| 92 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 93 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 94 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 96 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 97 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 98 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 99 | |
| 100 | /* same as 302 except that the browser MUST retry with the same method */ |
| 101 | const char *HTTP_307 = |
| 102 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Content-length: 0\r\n" |
| 105 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 106 | |
| 107 | /* same as 301 except that the browser MUST retry with the same method */ |
| 108 | const char *HTTP_308 = |
| 109 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 110 | "Content-length: 0\r\n" |
| 111 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 112 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 114 | const char *HTTP_401_fmt = |
| 115 | "HTTP/1.0 401 Unauthorized\r\n" |
| 116 | "Cache-Control: no-cache\r\n" |
| 117 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 118 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 120 | "\r\n" |
| 121 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 122 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 123 | const char *HTTP_407_fmt = |
| 124 | "HTTP/1.0 407 Unauthorized\r\n" |
| 125 | "Cache-Control: no-cache\r\n" |
| 126 | "Connection: close\r\n" |
| 127 | "Content-Type: text/html\r\n" |
| 128 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 129 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 130 | "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 131 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 132 | |
| 133 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 134 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | [HTTP_ERR_400] = 400, |
| 136 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 137 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 139 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 140 | [HTTP_ERR_500] = 500, |
| 141 | [HTTP_ERR_502] = 502, |
| 142 | [HTTP_ERR_503] = 503, |
| 143 | [HTTP_ERR_504] = 504, |
| 144 | }; |
| 145 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 146 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 147 | [HTTP_ERR_200] = |
| 148 | "HTTP/1.0 200 OK\r\n" |
| 149 | "Cache-Control: no-cache\r\n" |
| 150 | "Connection: close\r\n" |
| 151 | "Content-Type: text/html\r\n" |
| 152 | "\r\n" |
| 153 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 154 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 155 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 156 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 157 | "Cache-Control: no-cache\r\n" |
| 158 | "Connection: close\r\n" |
| 159 | "Content-Type: text/html\r\n" |
| 160 | "\r\n" |
| 161 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 162 | |
| 163 | [HTTP_ERR_403] = |
| 164 | "HTTP/1.0 403 Forbidden\r\n" |
| 165 | "Cache-Control: no-cache\r\n" |
| 166 | "Connection: close\r\n" |
| 167 | "Content-Type: text/html\r\n" |
| 168 | "\r\n" |
| 169 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 170 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 171 | [HTTP_ERR_405] = |
| 172 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 173 | "Cache-Control: no-cache\r\n" |
| 174 | "Connection: close\r\n" |
| 175 | "Content-Type: text/html\r\n" |
| 176 | "\r\n" |
| 177 | "<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", |
| 178 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 179 | [HTTP_ERR_408] = |
| 180 | "HTTP/1.0 408 Request Time-out\r\n" |
| 181 | "Cache-Control: no-cache\r\n" |
| 182 | "Connection: close\r\n" |
| 183 | "Content-Type: text/html\r\n" |
| 184 | "\r\n" |
| 185 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 186 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 187 | [HTTP_ERR_429] = |
| 188 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 189 | "Cache-Control: no-cache\r\n" |
| 190 | "Connection: close\r\n" |
| 191 | "Content-Type: text/html\r\n" |
| 192 | "\r\n" |
| 193 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 194 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 195 | [HTTP_ERR_500] = |
| 196 | "HTTP/1.0 500 Server Error\r\n" |
| 197 | "Cache-Control: no-cache\r\n" |
| 198 | "Connection: close\r\n" |
| 199 | "Content-Type: text/html\r\n" |
| 200 | "\r\n" |
| 201 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 202 | |
| 203 | [HTTP_ERR_502] = |
| 204 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 205 | "Cache-Control: no-cache\r\n" |
| 206 | "Connection: close\r\n" |
| 207 | "Content-Type: text/html\r\n" |
| 208 | "\r\n" |
| 209 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 210 | |
| 211 | [HTTP_ERR_503] = |
| 212 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 213 | "Cache-Control: no-cache\r\n" |
| 214 | "Connection: close\r\n" |
| 215 | "Content-Type: text/html\r\n" |
| 216 | "\r\n" |
| 217 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 218 | |
| 219 | [HTTP_ERR_504] = |
| 220 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 221 | "Cache-Control: no-cache\r\n" |
| 222 | "Connection: close\r\n" |
| 223 | "Content-Type: text/html\r\n" |
| 224 | "\r\n" |
| 225 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 226 | |
| 227 | }; |
| 228 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 229 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 230 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 231 | [STAT_STATUS_DENY] = "DENY", |
| 232 | [STAT_STATUS_DONE] = "DONE", |
| 233 | [STAT_STATUS_ERRP] = "ERRP", |
| 234 | [STAT_STATUS_EXCD] = "EXCD", |
| 235 | [STAT_STATUS_NONE] = "NONE", |
| 236 | [STAT_STATUS_PART] = "PART", |
| 237 | [STAT_STATUS_UNKN] = "UNKN", |
| 238 | }; |
| 239 | |
| 240 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 241 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 242 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 243 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 244 | }; |
| 245 | |
| 246 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 247 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 248 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 249 | }; |
| 250 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 251 | /* We must put the messages here since GCC cannot initialize consts depending |
| 252 | * on strlen(). |
| 253 | */ |
| 254 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 255 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 256 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 257 | static struct hdr_ctx static_hdr_ctx; |
| 258 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 259 | #define FD_SETS_ARE_BITFIELDS |
| 260 | #ifdef FD_SETS_ARE_BITFIELDS |
| 261 | /* |
| 262 | * This map is used with all the FD_* macros to check whether a particular bit |
| 263 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 264 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 265 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 266 | * exclusively to the macros. |
| 267 | */ |
| 268 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 269 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 270 | fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 271 | |
| 272 | #else |
| 273 | #error "Check if your OS uses bitfields for fd_sets" |
| 274 | #endif |
| 275 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 276 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 277 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 278 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 279 | static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 280 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 281 | /* This function returns a reason associated with the HTTP status. |
| 282 | * This function never fails, a message is always returned. |
| 283 | */ |
| 284 | const char *get_reason(unsigned int status) |
| 285 | { |
| 286 | switch (status) { |
| 287 | case 100: return "Continue"; |
| 288 | case 101: return "Switching Protocols"; |
| 289 | case 102: return "Processing"; |
| 290 | case 200: return "OK"; |
| 291 | case 201: return "Created"; |
| 292 | case 202: return "Accepted"; |
| 293 | case 203: return "Non-Authoritative Information"; |
| 294 | case 204: return "No Content"; |
| 295 | case 205: return "Reset Content"; |
| 296 | case 206: return "Partial Content"; |
| 297 | case 207: return "Multi-Status"; |
| 298 | case 210: return "Content Different"; |
| 299 | case 226: return "IM Used"; |
| 300 | case 300: return "Multiple Choices"; |
| 301 | case 301: return "Moved Permanently"; |
| 302 | case 302: return "Moved Temporarily"; |
| 303 | case 303: return "See Other"; |
| 304 | case 304: return "Not Modified"; |
| 305 | case 305: return "Use Proxy"; |
| 306 | case 307: return "Temporary Redirect"; |
| 307 | case 308: return "Permanent Redirect"; |
| 308 | case 310: return "Too many Redirects"; |
| 309 | case 400: return "Bad Request"; |
| 310 | case 401: return "Unauthorized"; |
| 311 | case 402: return "Payment Required"; |
| 312 | case 403: return "Forbidden"; |
| 313 | case 404: return "Not Found"; |
| 314 | case 405: return "Method Not Allowed"; |
| 315 | case 406: return "Not Acceptable"; |
| 316 | case 407: return "Proxy Authentication Required"; |
| 317 | case 408: return "Request Time-out"; |
| 318 | case 409: return "Conflict"; |
| 319 | case 410: return "Gone"; |
| 320 | case 411: return "Length Required"; |
| 321 | case 412: return "Precondition Failed"; |
| 322 | case 413: return "Request Entity Too Large"; |
| 323 | case 414: return "Request-URI Too Long"; |
| 324 | case 415: return "Unsupported Media Type"; |
| 325 | case 416: return "Requested range unsatisfiable"; |
| 326 | case 417: return "Expectation failed"; |
| 327 | case 418: return "I'm a teapot"; |
| 328 | case 422: return "Unprocessable entity"; |
| 329 | case 423: return "Locked"; |
| 330 | case 424: return "Method failure"; |
| 331 | case 425: return "Unordered Collection"; |
| 332 | case 426: return "Upgrade Required"; |
| 333 | case 428: return "Precondition Required"; |
| 334 | case 429: return "Too Many Requests"; |
| 335 | case 431: return "Request Header Fields Too Large"; |
| 336 | case 449: return "Retry With"; |
| 337 | case 450: return "Blocked by Windows Parental Controls"; |
| 338 | case 451: return "Unavailable For Legal Reasons"; |
| 339 | case 456: return "Unrecoverable Error"; |
| 340 | case 499: return "client has closed connection"; |
| 341 | case 500: return "Internal Server Error"; |
| 342 | case 501: return "Not Implemented"; |
| 343 | case 502: return "Bad Gateway ou Proxy Error"; |
| 344 | case 503: return "Service Unavailable"; |
| 345 | case 504: return "Gateway Time-out"; |
| 346 | case 505: return "HTTP Version not supported"; |
| 347 | case 506: return "Variant also negociate"; |
| 348 | case 507: return "Insufficient storage"; |
| 349 | case 508: return "Loop detected"; |
| 350 | case 509: return "Bandwidth Limit Exceeded"; |
| 351 | case 510: return "Not extended"; |
| 352 | case 511: return "Network authentication required"; |
| 353 | case 520: return "Web server is returning an unknown error"; |
| 354 | default: |
| 355 | switch (status) { |
| 356 | case 100 ... 199: return "Informational"; |
| 357 | case 200 ... 299: return "Success"; |
| 358 | case 300 ... 399: return "Redirection"; |
| 359 | case 400 ... 499: return "Client Error"; |
| 360 | case 500 ... 599: return "Server Error"; |
| 361 | default: return "Other"; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 366 | void init_proto_http() |
| 367 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 368 | int i; |
| 369 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 370 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 371 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 372 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 373 | if (!http_err_msgs[msg]) { |
| 374 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 375 | abort(); |
| 376 | } |
| 377 | |
| 378 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 379 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 380 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 381 | |
| 382 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 383 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 384 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 385 | * printable characters above. |
| 386 | */ |
| 387 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 388 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 389 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 390 | for (i = 0; i < 32; i++) { |
| 391 | FD_SET(i, hdr_encode_map); |
| 392 | FD_SET(i, url_encode_map); |
| 393 | } |
| 394 | for (i = 127; i < 256; i++) { |
| 395 | FD_SET(i, hdr_encode_map); |
| 396 | FD_SET(i, url_encode_map); |
| 397 | } |
| 398 | |
| 399 | tmp = "\"#{|}"; |
| 400 | while (*tmp) { |
| 401 | FD_SET(*tmp, hdr_encode_map); |
| 402 | tmp++; |
| 403 | } |
| 404 | |
| 405 | tmp = "\"#"; |
| 406 | while (*tmp) { |
| 407 | FD_SET(*tmp, url_encode_map); |
| 408 | tmp++; |
| 409 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 410 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 411 | /* initialize the http header encoding map. The draft httpbis define the |
| 412 | * header content as: |
| 413 | * |
| 414 | * HTTP-message = start-line |
| 415 | * *( header-field CRLF ) |
| 416 | * CRLF |
| 417 | * [ message-body ] |
| 418 | * header-field = field-name ":" OWS field-value OWS |
| 419 | * field-value = *( field-content / obs-fold ) |
| 420 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 421 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 422 | * field-vchar = VCHAR / obs-text |
| 423 | * VCHAR = %x21-7E |
| 424 | * obs-text = %x80-FF |
| 425 | * |
| 426 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 427 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 428 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 429 | */ |
| 430 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 431 | for (i = 0x00; i <= 0x08; i++) |
| 432 | FD_SET(i, http_encode_map); |
| 433 | for (i = 0x0a; i <= 0x1f; i++) |
| 434 | FD_SET(i, http_encode_map); |
| 435 | FD_SET(0x7f, http_encode_map); |
| 436 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 437 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 438 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 439 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 440 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 441 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 442 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 443 | /* |
| 444 | * We have 26 list of methods (1 per first letter), each of which can have |
| 445 | * up to 3 entries (2 valid, 1 null). |
| 446 | */ |
| 447 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 448 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 449 | int len; |
| 450 | const char text[8]; |
| 451 | }; |
| 452 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 453 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 454 | ['C' - 'A'] = { |
| 455 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 456 | }, |
| 457 | ['D' - 'A'] = { |
| 458 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 459 | }, |
| 460 | ['G' - 'A'] = { |
| 461 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 462 | }, |
| 463 | ['H' - 'A'] = { |
| 464 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 465 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 466 | ['O' - 'A'] = { |
| 467 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 468 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 469 | ['P' - 'A'] = { |
| 470 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 471 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 472 | }, |
| 473 | ['T' - 'A'] = { |
| 474 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 475 | }, |
| 476 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 477 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 478 | */ |
| 479 | }; |
| 480 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 481 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 482 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 483 | [HTTP_METH_GET] = { "GET", 3 }, |
| 484 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 485 | [HTTP_METH_POST] = { "POST", 4 }, |
| 486 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 487 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 488 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 489 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 490 | }; |
| 491 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 492 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 493 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 494 | * RFC2616 for those chars. |
| 495 | */ |
| 496 | |
| 497 | const char http_is_spht[256] = { |
| 498 | [' '] = 1, ['\t'] = 1, |
| 499 | }; |
| 500 | |
| 501 | const char http_is_crlf[256] = { |
| 502 | ['\r'] = 1, ['\n'] = 1, |
| 503 | }; |
| 504 | |
| 505 | const char http_is_lws[256] = { |
| 506 | [' '] = 1, ['\t'] = 1, |
| 507 | ['\r'] = 1, ['\n'] = 1, |
| 508 | }; |
| 509 | |
| 510 | const char http_is_sep[256] = { |
| 511 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 512 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 513 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 514 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 515 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 516 | }; |
| 517 | |
| 518 | const char http_is_ctl[256] = { |
| 519 | [0 ... 31] = 1, |
| 520 | [127] = 1, |
| 521 | }; |
| 522 | |
| 523 | /* |
| 524 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 525 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 526 | * them correctly. Instead, define every non-CTL char's status. |
| 527 | */ |
| 528 | const char http_is_token[256] = { |
| 529 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 530 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 531 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 532 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 533 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 534 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 535 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 536 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 537 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 538 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 539 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 540 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 541 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 542 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 543 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 544 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 545 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 546 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 547 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 548 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 549 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 550 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 551 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 552 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 553 | }; |
| 554 | |
| 555 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 556 | /* |
| 557 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 558 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 559 | */ |
| 560 | const char http_is_ver_token[256] = { |
| 561 | ['.'] = 1, ['/'] = 1, |
| 562 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 563 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 564 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 568 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 569 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 570 | * the last CRLF. Text length is measured first, so it cannot be NULL. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 571 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 572 | * of headers is automatically adjusted. The number of bytes added is returned |
| 573 | * on success, otherwise <0 is returned indicating an error. |
| 574 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 575 | int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 576 | { |
| 577 | int bytes, len; |
| 578 | |
| 579 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 580 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 581 | if (!bytes) |
| 582 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 583 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 584 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 585 | } |
| 586 | |
| 587 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 588 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 589 | * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 590 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 591 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 592 | * of headers is automatically adjusted. The number of bytes added is returned |
| 593 | * on success, otherwise <0 is returned indicating an error. |
| 594 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 595 | int http_header_add_tail2(struct http_msg *msg, |
| 596 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 597 | { |
| 598 | int bytes; |
| 599 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 600 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 601 | if (!bytes) |
| 602 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 603 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 604 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 605 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 606 | |
| 607 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 608 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 609 | * If so, returns the position of the first non-space character relative to |
| 610 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 611 | * to return a pointer to the place after the first space. Returns 0 if the |
| 612 | * header name does not match. Checks are case-insensitive. |
| 613 | */ |
| 614 | int http_header_match2(const char *hdr, const char *end, |
| 615 | const char *name, int len) |
| 616 | { |
| 617 | const char *val; |
| 618 | |
| 619 | if (hdr + len >= end) |
| 620 | return 0; |
| 621 | if (hdr[len] != ':') |
| 622 | return 0; |
| 623 | if (strncasecmp(hdr, name, len) != 0) |
| 624 | return 0; |
| 625 | val = hdr + len + 1; |
| 626 | while (val < end && HTTP_IS_SPHT(*val)) |
| 627 | val++; |
| 628 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 629 | return len + 2; /* we may replace starting from second space */ |
| 630 | return val - hdr; |
| 631 | } |
| 632 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 633 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 634 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 635 | * structure holds everything necessary to use the header and find next |
| 636 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 637 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 638 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 639 | * http_find_header2() except that it is designed to work with full-line headers |
| 640 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 641 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 642 | * header is rescanned. This allows rescanning after a header deletion. |
| 643 | */ |
| 644 | int http_find_full_header2(const char *name, int len, |
| 645 | char *sol, struct hdr_idx *idx, |
| 646 | struct hdr_ctx *ctx) |
| 647 | { |
| 648 | char *eol, *sov; |
| 649 | int cur_idx, old_idx; |
| 650 | |
| 651 | cur_idx = ctx->idx; |
| 652 | if (cur_idx) { |
| 653 | /* We have previously returned a header, let's search another one */ |
| 654 | sol = ctx->line; |
| 655 | eol = sol + idx->v[cur_idx].len; |
| 656 | goto next_hdr; |
| 657 | } |
| 658 | |
| 659 | /* first request for this header */ |
| 660 | sol += hdr_idx_first_pos(idx); |
| 661 | old_idx = 0; |
| 662 | cur_idx = hdr_idx_first_idx(idx); |
| 663 | while (cur_idx) { |
| 664 | eol = sol + idx->v[cur_idx].len; |
| 665 | |
| 666 | if (len == 0) { |
| 667 | /* No argument was passed, we want any header. |
| 668 | * To achieve this, we simply build a fake request. */ |
| 669 | while (sol + len < eol && sol[len] != ':') |
| 670 | len++; |
| 671 | name = sol; |
| 672 | } |
| 673 | |
| 674 | if ((len < eol - sol) && |
| 675 | (sol[len] == ':') && |
| 676 | (strncasecmp(sol, name, len) == 0)) { |
| 677 | ctx->del = len; |
| 678 | sov = sol + len + 1; |
| 679 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 680 | sov++; |
| 681 | |
| 682 | ctx->line = sol; |
| 683 | ctx->prev = old_idx; |
| 684 | ctx->idx = cur_idx; |
| 685 | ctx->val = sov - sol; |
| 686 | ctx->tws = 0; |
| 687 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 688 | eol--; |
| 689 | ctx->tws++; |
| 690 | } |
| 691 | ctx->vlen = eol - sov; |
| 692 | return 1; |
| 693 | } |
| 694 | next_hdr: |
| 695 | sol = eol + idx->v[cur_idx].cr + 1; |
| 696 | old_idx = cur_idx; |
| 697 | cur_idx = idx->v[cur_idx].next; |
| 698 | } |
| 699 | return 0; |
| 700 | } |
| 701 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 702 | /* Find the first or next header field in message buffer <sol> using headers |
| 703 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 704 | * everything necessary to use the header and find next occurrence. If its |
| 705 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 706 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 707 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 708 | * no header name. |
| 709 | */ |
| 710 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 711 | { |
| 712 | char *eol, *sov; |
| 713 | int cur_idx, old_idx; |
| 714 | int len; |
| 715 | |
| 716 | cur_idx = ctx->idx; |
| 717 | if (cur_idx) { |
| 718 | /* We have previously returned a header, let's search another one */ |
| 719 | sol = ctx->line; |
| 720 | eol = sol + idx->v[cur_idx].len; |
| 721 | goto next_hdr; |
| 722 | } |
| 723 | |
| 724 | /* first request for this header */ |
| 725 | sol += hdr_idx_first_pos(idx); |
| 726 | old_idx = 0; |
| 727 | cur_idx = hdr_idx_first_idx(idx); |
| 728 | while (cur_idx) { |
| 729 | eol = sol + idx->v[cur_idx].len; |
| 730 | |
| 731 | len = 0; |
| 732 | while (1) { |
| 733 | if (len >= eol - sol) |
| 734 | goto next_hdr; |
| 735 | if (sol[len] == ':') |
| 736 | break; |
| 737 | len++; |
| 738 | } |
| 739 | |
| 740 | ctx->del = len; |
| 741 | sov = sol + len + 1; |
| 742 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 743 | sov++; |
| 744 | |
| 745 | ctx->line = sol; |
| 746 | ctx->prev = old_idx; |
| 747 | ctx->idx = cur_idx; |
| 748 | ctx->val = sov - sol; |
| 749 | ctx->tws = 0; |
| 750 | |
| 751 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 752 | eol--; |
| 753 | ctx->tws++; |
| 754 | } |
| 755 | ctx->vlen = eol - sov; |
| 756 | return 1; |
| 757 | |
| 758 | next_hdr: |
| 759 | sol = eol + idx->v[cur_idx].cr + 1; |
| 760 | old_idx = cur_idx; |
| 761 | cur_idx = idx->v[cur_idx].next; |
| 762 | } |
| 763 | return 0; |
| 764 | } |
| 765 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 766 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 767 | * par 2.2 for more information. Note that it requires a valid header to return |
| 768 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 769 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 770 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 771 | { |
| 772 | int quoted, qdpair; |
| 773 | |
| 774 | quoted = qdpair = 0; |
| 775 | for (; s < e; s++) { |
| 776 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 777 | else if (quoted) { |
| 778 | if (*s == '\\') qdpair = 1; |
| 779 | else if (*s == '"') quoted = 0; |
| 780 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 781 | else if (*s == '"') quoted = 1; |
| 782 | else if (*s == ',') return s; |
| 783 | } |
| 784 | return s; |
| 785 | } |
| 786 | |
| 787 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 788 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 789 | * structure holds everything necessary to use the header and find next |
| 790 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 791 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 792 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 793 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 794 | * is NULL when searching for a new values of a header, the current header is |
| 795 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 796 | */ |
| 797 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 798 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 799 | struct hdr_ctx *ctx) |
| 800 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 801 | char *eol, *sov; |
| 802 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 803 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 804 | cur_idx = ctx->idx; |
| 805 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 806 | /* We have previously returned a value, let's search |
| 807 | * another one on the same line. |
| 808 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 809 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 810 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 811 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 812 | eol = sol + idx->v[cur_idx].len; |
| 813 | |
| 814 | if (sov >= eol) |
| 815 | /* no more values in this header */ |
| 816 | goto next_hdr; |
| 817 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 818 | /* values remaining for this header, skip the comma but save it |
| 819 | * for later use (eg: for header deletion). |
| 820 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 821 | sov++; |
| 822 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 823 | sov++; |
| 824 | |
| 825 | goto return_hdr; |
| 826 | } |
| 827 | |
| 828 | /* first request for this header */ |
| 829 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 830 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 831 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 832 | while (cur_idx) { |
| 833 | eol = sol + idx->v[cur_idx].len; |
| 834 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 835 | if (len == 0) { |
| 836 | /* No argument was passed, we want any header. |
| 837 | * To achieve this, we simply build a fake request. */ |
| 838 | while (sol + len < eol && sol[len] != ':') |
| 839 | len++; |
| 840 | name = sol; |
| 841 | } |
| 842 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 843 | if ((len < eol - sol) && |
| 844 | (sol[len] == ':') && |
| 845 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 846 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 847 | sov = sol + len + 1; |
| 848 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 849 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 850 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 851 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 852 | ctx->prev = old_idx; |
| 853 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 854 | ctx->idx = cur_idx; |
| 855 | ctx->val = sov - sol; |
| 856 | |
| 857 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 858 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 859 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 860 | eol--; |
| 861 | ctx->tws++; |
| 862 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 863 | ctx->vlen = eol - sov; |
| 864 | return 1; |
| 865 | } |
| 866 | next_hdr: |
| 867 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 868 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 869 | cur_idx = idx->v[cur_idx].next; |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 875 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 876 | struct hdr_ctx *ctx) |
| 877 | { |
| 878 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 879 | } |
| 880 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 881 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 882 | * the http_find_header functions. The value is removed, as well as surrounding |
| 883 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 884 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 885 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 886 | * no more header, so any processing may stop. The ctx is always left in a form |
| 887 | * that can be handled by http_find_header2() to find next occurrence. |
| 888 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 889 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 890 | { |
| 891 | int cur_idx = ctx->idx; |
| 892 | char *sol = ctx->line; |
| 893 | struct hdr_idx_elem *hdr; |
| 894 | int delta, skip_comma; |
| 895 | |
| 896 | if (!cur_idx) |
| 897 | return 0; |
| 898 | |
| 899 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 900 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 901 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 902 | delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 903 | http_msg_move_end(msg, delta); |
| 904 | idx->used--; |
| 905 | hdr->len = 0; /* unused entry */ |
| 906 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 907 | if (idx->tail == ctx->idx) |
| 908 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 909 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 910 | ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 911 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 912 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 913 | return ctx->idx; |
| 914 | } |
| 915 | |
| 916 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 917 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 918 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 919 | */ |
| 920 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 921 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 922 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 923 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 924 | NULL, 0); |
| 925 | hdr->len += delta; |
| 926 | http_msg_move_end(msg, delta); |
| 927 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 928 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 929 | return ctx->idx; |
| 930 | } |
| 931 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 932 | /* This function handles a server error at the stream interface level. The |
| 933 | * stream interface is assumed to be already in a closed state. An optional |
| 934 | * message is copied into the input buffer, and an HTTP status code stored. |
| 935 | * The error flags are set to the values in arguments. Any pending request |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 936 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 937 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 938 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 939 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 940 | { |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 941 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 942 | channel_auto_read(si_oc(si)); |
| 943 | channel_abort(si_oc(si)); |
| 944 | channel_auto_close(si_oc(si)); |
| 945 | channel_erase(si_oc(si)); |
| 946 | channel_auto_close(si_ic(si)); |
| 947 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 948 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 949 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 950 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 951 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 952 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 953 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 954 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 955 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 956 | } |
| 957 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 958 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 959 | * and message. |
| 960 | */ |
| 961 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 962 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 963 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 964 | if (s->be->errmsg[msgnum].str) |
| 965 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 966 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 967 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 968 | else |
| 969 | return &http_err_chunks[msgnum]; |
| 970 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 971 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 972 | void |
| 973 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 974 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 975 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 976 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 977 | stream_int_retnclose(&s->si[0], msg); |
| 978 | } |
| 979 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 980 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 981 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 982 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 983 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 984 | enum http_meth_t find_http_meth(const char *str, const int len) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 985 | { |
| 986 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 987 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 988 | |
| 989 | m = ((unsigned)*str - 'A'); |
| 990 | |
| 991 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 992 | for (h = http_methods[m]; h->len > 0; h++) { |
| 993 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 994 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 995 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 996 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 997 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 998 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 999 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1002 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 1003 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 1004 | * It is returned otherwise. |
| 1005 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 1006 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1007 | { |
| 1008 | char *ptr, *end; |
| 1009 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1010 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1011 | end = ptr + txn->req.sl.rq.u_l; |
| 1012 | |
| 1013 | if (ptr >= end) |
| 1014 | return NULL; |
| 1015 | |
| 1016 | /* RFC2616, par. 5.1.2 : |
| 1017 | * Request-URI = "*" | absuri | abspath | authority |
| 1018 | */ |
| 1019 | |
| 1020 | if (*ptr == '*') |
| 1021 | return NULL; |
| 1022 | |
| 1023 | if (isalpha((unsigned char)*ptr)) { |
| 1024 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1025 | ptr++; |
| 1026 | while (ptr < end && |
| 1027 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1028 | ptr++; |
| 1029 | /* skip '://' */ |
| 1030 | if (ptr == end || *ptr++ != ':') |
| 1031 | return NULL; |
| 1032 | if (ptr == end || *ptr++ != '/') |
| 1033 | return NULL; |
| 1034 | if (ptr == end || *ptr++ != '/') |
| 1035 | return NULL; |
| 1036 | } |
| 1037 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1038 | |
| 1039 | while (ptr < end && *ptr != '/') |
| 1040 | ptr++; |
| 1041 | |
| 1042 | if (ptr == end) |
| 1043 | return NULL; |
| 1044 | |
| 1045 | /* OK, we got the '/' ! */ |
| 1046 | return ptr; |
| 1047 | } |
| 1048 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1049 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1050 | * If not found, return NULL. It is returned otherwise. |
| 1051 | */ |
| 1052 | static char * |
| 1053 | http_get_path_from_string(char *str) |
| 1054 | { |
| 1055 | char *ptr = str; |
| 1056 | |
| 1057 | /* RFC2616, par. 5.1.2 : |
| 1058 | * Request-URI = "*" | absuri | abspath | authority |
| 1059 | */ |
| 1060 | |
| 1061 | if (*ptr == '*') |
| 1062 | return NULL; |
| 1063 | |
| 1064 | if (isalpha((unsigned char)*ptr)) { |
| 1065 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1066 | ptr++; |
| 1067 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1068 | ptr++; |
| 1069 | /* skip '://' */ |
| 1070 | if (*ptr == '\0' || *ptr++ != ':') |
| 1071 | return NULL; |
| 1072 | if (*ptr == '\0' || *ptr++ != '/') |
| 1073 | return NULL; |
| 1074 | if (*ptr == '\0' || *ptr++ != '/') |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1078 | |
| 1079 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1080 | ptr++; |
| 1081 | |
| 1082 | if (*ptr == '\0' || *ptr == ' ') |
| 1083 | return NULL; |
| 1084 | |
| 1085 | /* OK, we got the '/' ! */ |
| 1086 | return ptr; |
| 1087 | } |
| 1088 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1089 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1090 | * in redirect mode. This may only be called just after the stream interface |
| 1091 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1092 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1093 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1094 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1095 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1096 | { |
| 1097 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1098 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1099 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1100 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1101 | |
| 1102 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1103 | trash.len = strlen(HTTP_302); |
| 1104 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1105 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1106 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1107 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1108 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1109 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1110 | return; |
| 1111 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1112 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1113 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1114 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1115 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1116 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1117 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1118 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1119 | * to temporarily rewind the buffer. |
| 1120 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1121 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1122 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1123 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1124 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1125 | len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1126 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1127 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1128 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1129 | if (!path) |
| 1130 | return; |
| 1131 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1132 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1133 | return; |
| 1134 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1135 | memcpy(trash.str + trash.len, path, len); |
| 1136 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1137 | |
| 1138 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1139 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1140 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1141 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1142 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1143 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1144 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1145 | |
| 1146 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1147 | si_shutr(si); |
| 1148 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1149 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1150 | si->state = SI_ST_CLO; |
| 1151 | |
| 1152 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1153 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1154 | |
| 1155 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 1156 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1157 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1158 | } |
| 1159 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1160 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1161 | * that the server side is closed. Note that err_type is actually a |
| 1162 | * bitmask, where almost only aborts may be cumulated with other |
| 1163 | * values. We consider that aborted operations are more important |
| 1164 | * than timeouts or errors due to the fact that nobody else in the |
| 1165 | * logs might explain incomplete retries. All others should avoid |
| 1166 | * being cumulated. It should normally not be possible to have multiple |
| 1167 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1168 | * Note that connection errors appearing on the second request of a keep-alive |
| 1169 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1170 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1171 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1172 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1173 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1174 | |
| 1175 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1176 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1177 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1178 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1179 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1180 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1181 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1182 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1183 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1184 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1185 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1186 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1187 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1188 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1189 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1190 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1191 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1192 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1193 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1194 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1195 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1196 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1197 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1198 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1199 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1200 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1201 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1202 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1205 | extern const char sess_term_cond[8]; |
| 1206 | extern const char sess_fin_state[8]; |
| 1207 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1208 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1209 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1210 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1211 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1212 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1213 | /* |
| 1214 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1215 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1216 | */ |
| 1217 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1218 | char **cap, struct cap_hdr *cap_hdr) |
| 1219 | { |
| 1220 | char *eol, *sol, *col, *sov; |
| 1221 | int cur_idx; |
| 1222 | struct cap_hdr *h; |
| 1223 | int len; |
| 1224 | |
| 1225 | sol = som + hdr_idx_first_pos(idx); |
| 1226 | cur_idx = hdr_idx_first_idx(idx); |
| 1227 | |
| 1228 | while (cur_idx) { |
| 1229 | eol = sol + idx->v[cur_idx].len; |
| 1230 | |
| 1231 | col = sol; |
| 1232 | while (col < eol && *col != ':') |
| 1233 | col++; |
| 1234 | |
| 1235 | sov = col + 1; |
| 1236 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1237 | sov++; |
| 1238 | |
| 1239 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1240 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1241 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1242 | if (cap[h->index] == NULL) |
| 1243 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1244 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1245 | |
| 1246 | if (cap[h->index] == NULL) { |
| 1247 | Alert("HTTP capture : out of memory.\n"); |
| 1248 | continue; |
| 1249 | } |
| 1250 | |
| 1251 | len = eol - sov; |
| 1252 | if (len > h->len) |
| 1253 | len = h->len; |
| 1254 | |
| 1255 | memcpy(cap[h->index], sov, len); |
| 1256 | cap[h->index][len]=0; |
| 1257 | } |
| 1258 | } |
| 1259 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1260 | cur_idx = idx->v[cur_idx].next; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1265 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1266 | */ |
| 1267 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1268 | |
| 1269 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1270 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1271 | */ |
| 1272 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1273 | ptr++; \ |
| 1274 | if (likely(ptr < end)) \ |
| 1275 | goto good; \ |
| 1276 | else { \ |
| 1277 | state = (st); \ |
| 1278 | goto http_msg_ood; \ |
| 1279 | } \ |
| 1280 | } while (0) |
| 1281 | |
| 1282 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1283 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1284 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1285 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1286 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1287 | * will give undefined results. |
| 1288 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1289 | * and that msg->sol points to the beginning of the response. |
| 1290 | * If a complete line is found (which implies that at least one CR or LF is |
| 1291 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1292 | * returned indicating an incomplete line (which does not mean that parts have |
| 1293 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1294 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1295 | * upon next call. |
| 1296 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1297 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1299 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1300 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1301 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1302 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1303 | enum ht_state state, const char *ptr, const char *end, |
| 1304 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1305 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1306 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1307 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1308 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1310 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1311 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1312 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1313 | |
| 1314 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1315 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1317 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1318 | state = HTTP_MSG_ERROR; |
| 1319 | break; |
| 1320 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1321 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1322 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1323 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1324 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1325 | goto http_msg_rpcode; |
| 1326 | } |
| 1327 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1328 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1329 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1330 | state = HTTP_MSG_ERROR; |
| 1331 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1334 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1335 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1336 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1337 | |
| 1338 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1339 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1340 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1341 | } |
| 1342 | |
| 1343 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1344 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | http_msg_rsp_reason: |
| 1346 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1347 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | msg->sl.st.r_l = 0; |
| 1349 | goto http_msg_rpline_eol; |
| 1350 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1351 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1352 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1354 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | goto http_msg_rpreason; |
| 1356 | } |
| 1357 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1358 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1359 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1360 | goto http_msg_rsp_reason; |
| 1361 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1363 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1364 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1365 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1366 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1367 | http_msg_rpline_eol: |
| 1368 | /* We have seen the end of line. Note that we do not |
| 1369 | * necessarily have the \n yet, but at least we know that we |
| 1370 | * have EITHER \r OR \n, otherwise the response would not be |
| 1371 | * complete. We can then record the response length and return |
| 1372 | * to the caller which will be able to register it. |
| 1373 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1374 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | return ptr; |
| 1376 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1378 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1379 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1380 | exit(1); |
| 1381 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1382 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1386 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1387 | if (ret_state) |
| 1388 | *ret_state = state; |
| 1389 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1390 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1391 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1394 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | * This function parses a request line between <ptr> and <end>, starting with |
| 1396 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1397 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER 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 request. |
| 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 Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1408 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | * 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 Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1411 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1412 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1413 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1414 | enum ht_state state, const char *ptr, const char *end, |
| 1415 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1416 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1417 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1419 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1421 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1423 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1426 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1428 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1431 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1432 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1434 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1435 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1436 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1438 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | msg->sl.rq.v_l = 0; |
| 1440 | goto http_msg_rqline_eol; |
| 1441 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1442 | state = HTTP_MSG_ERROR; |
| 1443 | break; |
| 1444 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1445 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1446 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1448 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | goto http_msg_rquri; |
| 1450 | } |
| 1451 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1452 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1453 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1454 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1455 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1457 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1458 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1459 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1462 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1464 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1465 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1466 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1467 | /* non-ASCII chars are forbidden unless option |
| 1468 | * accept-invalid-http-request is enabled in the frontend. |
| 1469 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1470 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1471 | if (msg->err_pos < -1) |
| 1472 | goto invalid_char; |
| 1473 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1474 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1475 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1476 | } |
| 1477 | |
| 1478 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1479 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1480 | goto http_msg_req09_uri_e; |
| 1481 | } |
| 1482 | |
| 1483 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1484 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1485 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1486 | state = HTTP_MSG_ERROR; |
| 1487 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1488 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1489 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1490 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1492 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | goto http_msg_rqver; |
| 1494 | } |
| 1495 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1496 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1497 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1498 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1499 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1501 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1502 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1504 | |
| 1505 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1506 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1507 | http_msg_rqline_eol: |
| 1508 | /* We have seen the end of line. Note that we do not |
| 1509 | * necessarily have the \n yet, but at least we know that we |
| 1510 | * have EITHER \r OR \n, otherwise the request would not be |
| 1511 | * complete. We can then record the request length and return |
| 1512 | * to the caller which will be able to register it. |
| 1513 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1514 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1515 | return ptr; |
| 1516 | } |
| 1517 | |
| 1518 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1519 | state = HTTP_MSG_ERROR; |
| 1520 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1521 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1523 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1525 | exit(1); |
| 1526 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1527 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1529 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1531 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | if (ret_state) |
| 1533 | *ret_state = state; |
| 1534 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1535 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1538 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1539 | /* |
| 1540 | * Returns the data from Authorization header. Function may be called more |
| 1541 | * than once so data is stored in txn->auth_data. When no header is found |
| 1542 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
Thierry FOURNIER | 98d9695 | 2014-01-23 12:13:02 +0100 | [diff] [blame] | 1543 | * searching again for something we are unable to find anyway. However, if |
| 1544 | * the result if valid, the cache is not reused because we would risk to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1545 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1546 | */ |
| 1547 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1548 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1549 | * set according to global.tune.bufsize. |
| 1550 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1551 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1552 | |
| 1553 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1554 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1555 | { |
| 1556 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1557 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1558 | struct chunk auth_method; |
| 1559 | struct hdr_ctx ctx; |
| 1560 | char *h, *p; |
| 1561 | int len; |
| 1562 | |
| 1563 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1564 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1565 | #endif |
| 1566 | |
| 1567 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1568 | return 0; |
| 1569 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1570 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1571 | |
| 1572 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1573 | |
| 1574 | if (txn->flags & TX_USE_PX_CONN) { |
| 1575 | h = "Proxy-Authorization"; |
| 1576 | len = strlen(h); |
| 1577 | } else { |
| 1578 | h = "Authorization"; |
| 1579 | len = strlen(h); |
| 1580 | } |
| 1581 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1582 | if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1583 | return 0; |
| 1584 | |
| 1585 | h = ctx.line + ctx.val; |
| 1586 | |
| 1587 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1588 | len = p - h; |
| 1589 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1590 | return 0; |
| 1591 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1592 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1593 | return 0; |
| 1594 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1595 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1596 | |
| 1597 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1598 | |
| 1599 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1600 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1601 | |
| 1602 | if (len < 0) |
| 1603 | return 0; |
| 1604 | |
| 1605 | |
| 1606 | get_http_auth_buff[len] = '\0'; |
| 1607 | |
| 1608 | p = strchr(get_http_auth_buff, ':'); |
| 1609 | |
| 1610 | if (!p) |
| 1611 | return 0; |
| 1612 | |
| 1613 | txn->auth.user = get_http_auth_buff; |
| 1614 | *p = '\0'; |
| 1615 | txn->auth.pass = p+1; |
| 1616 | |
| 1617 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1618 | return 1; |
| 1619 | } |
| 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1624 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1625 | /* |
| 1626 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1627 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1628 | * ensuring that the message does not wrap. The function can be preempted |
| 1629 | * everywhere when data are missing and recalled at the exact same location |
| 1630 | * with no information loss. The message may even be realigned between two |
| 1631 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1632 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1633 | * fields. Note that msg->sol will be initialized after completing the first |
| 1634 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1635 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1636 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1637 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1639 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1641 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1643 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1644 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1645 | ptr = buf->p + msg->next; |
| 1646 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1647 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1648 | if (unlikely(ptr >= end)) |
| 1649 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1650 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | /* |
| 1653 | * First, states that are specific to the response only. |
| 1654 | * We check them first so that request and headers are |
| 1655 | * closer to each other (accessed more often). |
| 1656 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1657 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1658 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1659 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1660 | /* we have a start of message, but we have to check |
| 1661 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1662 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1663 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1664 | if (unlikely(ptr != buf->p)) { |
| 1665 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1666 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1667 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1668 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1669 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1670 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1671 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1672 | hdr_idx_init(idx); |
| 1673 | state = HTTP_MSG_RPVER; |
| 1674 | goto http_msg_rpver; |
| 1675 | } |
| 1676 | |
| 1677 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1678 | goto http_msg_invalid; |
| 1679 | |
| 1680 | if (unlikely(*ptr == '\n')) |
| 1681 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1682 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1683 | /* stop here */ |
| 1684 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1685 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1686 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1687 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1688 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1689 | /* stop here */ |
| 1690 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1691 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1692 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1693 | case HTTP_MSG_RPVER_SP: |
| 1694 | case HTTP_MSG_RPCODE: |
| 1695 | case HTTP_MSG_RPCODE_SP: |
| 1696 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1697 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1698 | state, ptr, end, |
| 1699 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1700 | if (unlikely(!ptr)) |
| 1701 | return; |
| 1702 | |
| 1703 | /* we have a full response and we know that we have either a CR |
| 1704 | * or an LF at <ptr>. |
| 1705 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1706 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1707 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1708 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1709 | if (likely(*ptr == '\r')) |
| 1710 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1711 | goto http_msg_rpline_end; |
| 1712 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1713 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1714 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1715 | /* msg->sol must point to the first of CR or LF. */ |
| 1716 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1717 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1718 | /* stop here */ |
| 1719 | |
| 1720 | /* |
| 1721 | * Second, states that are specific to the request only |
| 1722 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1724 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1726 | /* we have a start of message, but we have to check |
| 1727 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1728 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1729 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1730 | if (likely(ptr != buf->p)) { |
| 1731 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1732 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1733 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1734 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1735 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1736 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1737 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1738 | state = HTTP_MSG_RQMETH; |
| 1739 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1741 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1743 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1744 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | if (unlikely(*ptr == '\n')) |
| 1746 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1747 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1748 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1749 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1750 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1751 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1753 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1754 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1755 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1756 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1757 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | case HTTP_MSG_RQMETH_SP: |
| 1759 | case HTTP_MSG_RQURI: |
| 1760 | case HTTP_MSG_RQURI_SP: |
| 1761 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1762 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1763 | state, ptr, end, |
| 1764 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1765 | if (unlikely(!ptr)) |
| 1766 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1767 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | /* we have a full request and we know that we have either a CR |
| 1769 | * or an LF at <ptr>. |
| 1770 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1772 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1773 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1774 | if (likely(*ptr == '\r')) |
| 1775 | EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1777 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1779 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1780 | /* check for HTTP/0.9 request : no version information available. |
| 1781 | * msg->sol must point to the first of CR or LF. |
| 1782 | */ |
| 1783 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1784 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1785 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1787 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1788 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1789 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1790 | /* |
| 1791 | * Common states below |
| 1792 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1794 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1795 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1796 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1797 | goto http_msg_hdr_name; |
| 1798 | } |
| 1799 | |
| 1800 | if (likely(*ptr == '\r')) |
| 1801 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1802 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1803 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1804 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1805 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | /* assumes msg->sol points to the first char */ |
| 1807 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1808 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1809 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1810 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1811 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1812 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1813 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1814 | goto http_msg_invalid; |
| 1815 | |
| 1816 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1817 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1818 | |
| 1819 | /* and we still accept this non-token character */ |
| 1820 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1822 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1823 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1824 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1826 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1829 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1830 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1832 | goto http_msg_hdr_val; |
| 1833 | } |
| 1834 | |
| 1835 | if (likely(*ptr == '\r')) |
| 1836 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1837 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1838 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1839 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1840 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1841 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1842 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1843 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1844 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1845 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1846 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1847 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1848 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1849 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1850 | goto http_msg_hdr_l1_sp; |
| 1851 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1852 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1853 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | goto http_msg_complete_header; |
| 1855 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1856 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1857 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1858 | /* assumes msg->sol points to the first char, and msg->sov |
| 1859 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1860 | */ |
| 1861 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1862 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1863 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1864 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1865 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1866 | * real header end in case it ends with lots of LWS, but is this |
| 1867 | * really needed ? |
| 1868 | */ |
| 1869 | if (likely(*ptr == '\r')) |
| 1870 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1871 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1872 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1873 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1874 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1875 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1876 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1877 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1878 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1879 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1880 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1881 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1882 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1883 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1884 | goto http_msg_hdr_val; |
| 1885 | } |
| 1886 | http_msg_complete_header: |
| 1887 | /* |
| 1888 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1889 | * Assumes msg->sol points to the first char, msg->sov points |
| 1890 | * to the first character of the value and msg->eol to the |
| 1891 | * first CR or LF so we know how the line ends. We insert last |
| 1892 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1893 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1894 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1895 | idx, idx->tail) < 0)) |
| 1896 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1897 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1898 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1900 | goto http_msg_hdr_name; |
| 1901 | } |
| 1902 | |
| 1903 | if (likely(*ptr == '\r')) |
| 1904 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1905 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1906 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1908 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1909 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1910 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1911 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1912 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1913 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1914 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1915 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1916 | msg->eoh = msg->sol; |
| 1917 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1918 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1919 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1920 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1921 | |
| 1922 | case HTTP_MSG_ERROR: |
| 1923 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1924 | break; |
| 1925 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1926 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1927 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1928 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1929 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1930 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1931 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1932 | } |
| 1933 | http_msg_ood: |
| 1934 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1935 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1936 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1937 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1938 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1939 | http_msg_invalid: |
| 1940 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1941 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1942 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1943 | return; |
| 1944 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1946 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1947 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1948 | * nothing is done and 1 is returned. |
| 1949 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1950 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1951 | { |
| 1952 | int delta; |
| 1953 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1954 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1955 | |
| 1956 | if (msg->sl.rq.v_l != 0) |
| 1957 | return 1; |
| 1958 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1959 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1960 | if (txn->meth != HTTP_METH_GET) |
| 1961 | return 0; |
| 1962 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1963 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1964 | |
| 1965 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1966 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1967 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1968 | } |
| 1969 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1970 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1971 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1972 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1973 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1974 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1975 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1976 | NULL, NULL); |
| 1977 | if (unlikely(!cur_end)) |
| 1978 | return 0; |
| 1979 | |
| 1980 | /* we have a full HTTP/1.0 request now and we know that |
| 1981 | * we have either a CR or an LF at <ptr>. |
| 1982 | */ |
| 1983 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1984 | return 1; |
| 1985 | } |
| 1986 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1987 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1988 | * and "keep-alive" values. If we already know that some headers may safely |
| 1989 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1990 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1991 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1992 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1993 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1994 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1995 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1996 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1997 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1998 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1999 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2000 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2001 | const char *hdr_val = "Connection"; |
| 2002 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2003 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2004 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2005 | return; |
| 2006 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2007 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2008 | hdr_val = "Proxy-Connection"; |
| 2009 | hdr_len = 16; |
| 2010 | } |
| 2011 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2012 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2013 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2014 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2015 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2016 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2017 | if (to_del & 2) |
| 2018 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2019 | else |
| 2020 | txn->flags |= TX_CON_KAL_SET; |
| 2021 | } |
| 2022 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2023 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2024 | if (to_del & 1) |
| 2025 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2026 | else |
| 2027 | txn->flags |= TX_CON_CLO_SET; |
| 2028 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2029 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2030 | txn->flags |= TX_HDR_CONN_UPG; |
| 2031 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2032 | } |
| 2033 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2034 | txn->flags |= TX_HDR_CONN_PRS; |
| 2035 | return; |
| 2036 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2037 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2038 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2039 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2040 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2041 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2042 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2043 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2044 | { |
| 2045 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2046 | const char *hdr_val = "Connection"; |
| 2047 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2048 | |
| 2049 | ctx.idx = 0; |
| 2050 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2051 | |
| 2052 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2053 | hdr_val = "Proxy-Connection"; |
| 2054 | hdr_len = 16; |
| 2055 | } |
| 2056 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2057 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2058 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2059 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2060 | if (wanted & TX_CON_KAL_SET) |
| 2061 | txn->flags |= TX_CON_KAL_SET; |
| 2062 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2063 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2064 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2065 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2066 | if (wanted & TX_CON_CLO_SET) |
| 2067 | txn->flags |= TX_CON_CLO_SET; |
| 2068 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2069 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2070 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2071 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2072 | |
| 2073 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2074 | return; |
| 2075 | |
| 2076 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2077 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2078 | hdr_val = "Connection: close"; |
| 2079 | hdr_len = 17; |
| 2080 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2081 | hdr_val = "Proxy-Connection: close"; |
| 2082 | hdr_len = 23; |
| 2083 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2084 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2088 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2089 | hdr_val = "Connection: keep-alive"; |
| 2090 | hdr_len = 22; |
| 2091 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2092 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2093 | hdr_len = 28; |
| 2094 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2095 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2096 | } |
| 2097 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2100 | /* Parse the chunk size at msg->next. Once done, caller should adjust ->next to |
| 2101 | * point to the first byte of data after the chunk size, so that we know we can |
| 2102 | * forward exactly msg->next bytes. msg->sol contains the exact number of bytes |
| 2103 | * forming the chunk size. That way it is always possible to differentiate |
| 2104 | * between the start of the body and the start of the data. Return the number |
| 2105 | * of byte parsed on success, 0 when some data is missing, <0 on error. Note: |
| 2106 | * this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2107 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2108 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2109 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2110 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2111 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2112 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2113 | const char *end = buf->data + buf->size; |
| 2114 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2115 | unsigned int chunk = 0; |
| 2116 | |
| 2117 | /* The chunk size is in the following form, though we are only |
| 2118 | * interested in the size and CRLF : |
| 2119 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2120 | */ |
| 2121 | while (1) { |
| 2122 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2123 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2124 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2126 | if (c < 0) /* not a hex digit anymore */ |
| 2127 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2128 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2129 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2130 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2131 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2132 | chunk = (chunk << 4) + c; |
| 2133 | } |
| 2134 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2135 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2136 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2137 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2138 | |
| 2139 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2140 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2141 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2142 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2143 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2146 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2147 | * for the end of chunk size. |
| 2148 | */ |
| 2149 | while (1) { |
| 2150 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2151 | /* we now have a CR or an LF at ptr */ |
| 2152 | if (likely(*ptr == '\r')) { |
| 2153 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2154 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2155 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2156 | return 0; |
| 2157 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2158 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2159 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2160 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2161 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2162 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2163 | /* done */ |
| 2164 | break; |
| 2165 | } |
| 2166 | else if (*ptr == ';') { |
| 2167 | /* chunk extension, ends at next CRLF */ |
| 2168 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2169 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2170 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2171 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2172 | |
| 2173 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2174 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2175 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2176 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2177 | return 0; |
| 2178 | } |
| 2179 | /* we have a CRLF now, loop above */ |
| 2180 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2181 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2182 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2183 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2184 | } |
| 2185 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2186 | /* OK we found our CRLF and now <ptr> points to the next byte, which may |
| 2187 | * or may not be present. We save the number of bytes parsed into |
| 2188 | * msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2189 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2190 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2191 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2192 | msg->sol += buf->size; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2193 | msg->chunk_len = chunk; |
| 2194 | msg->body_len += chunk; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2195 | return msg->sol; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2196 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2197 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2198 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2199 | } |
| 2200 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2201 | /* This function skips trailers in the buffer associated with HTTP message |
| 2202 | * <msg>. The first visited position is msg->next. If the end of the trailers is |
| 2203 | * found, the function returns >0. So, the caller can automatically schedul it |
| 2204 | * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough |
| 2205 | * data are available, the function does not change anything except maybe |
| 2206 | * msg->sol if it could parse some lines, and returns zero. If a parse error |
| 2207 | * is encountered, the function returns < 0 and does not change anything except |
| 2208 | * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS |
| 2209 | * state before calling this function, which implies that all non-trailers data |
| 2210 | * have already been scheduled for forwarding, and that msg->next exactly |
| 2211 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2212 | * important to note that this function is designed to be able to parse wrapped |
| 2213 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2214 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2215 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2216 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2217 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2218 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2219 | /* we have msg->next which points to next line. Look for CRLF. But |
| 2220 | * first, we reset msg->sol */ |
| 2221 | msg->sol = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2222 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2223 | const char *p1 = NULL, *p2 = NULL; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2224 | const char *start = b_ptr(buf, msg->next + msg->sol); |
| 2225 | const char *stop = bi_end(buf); |
| 2226 | const char *ptr = start; |
| 2227 | int bytes = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2228 | |
| 2229 | /* scan current line and stop at LF or CRLF */ |
| 2230 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2231 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2232 | return 0; |
| 2233 | |
| 2234 | if (*ptr == '\n') { |
| 2235 | if (!p1) |
| 2236 | p1 = ptr; |
| 2237 | p2 = ptr; |
| 2238 | break; |
| 2239 | } |
| 2240 | |
| 2241 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2242 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2243 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2244 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2245 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2246 | p1 = ptr; |
| 2247 | } |
| 2248 | |
| 2249 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2250 | if (ptr >= buf->data + buf->size) |
| 2251 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | /* after LF; point to beginning of next line */ |
| 2255 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2256 | if (p2 >= buf->data + buf->size) |
| 2257 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2258 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2259 | bytes = p2 - start; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2260 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2261 | bytes += buf->size; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2262 | msg->sol += bytes; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2263 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2264 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2265 | * Everything was scheduled for forwarding, there's nothing left |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2266 | * from this message. */ |
| 2267 | if (p1 == start) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2268 | return 1; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2269 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2270 | /* OK, next line then */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2274 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
| 2275 | * a possible LF alone at the end of a chunk. The caller should adjust msg->next |
| 2276 | * in order to include this part into the next forwarding phase. Note that the |
| 2277 | * caller must ensure that ->p points to the first byte to parse. It returns |
| 2278 | * the number of bytes parsed on success, so the caller can set msg_state to |
| 2279 | * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not |
| 2280 | * change anything and returns zero. If a parse error is encountered, the |
| 2281 | * function returns < 0. Note: this function is designed to parse wrapped CRLF |
| 2282 | * at the end of the buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2283 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2284 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2285 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2286 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2287 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2288 | int bytes; |
| 2289 | |
| 2290 | /* NB: we'll check data availabilty at the end. It's not a |
| 2291 | * problem because whatever we match first will be checked |
| 2292 | * against the correct length. |
| 2293 | */ |
| 2294 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2295 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2296 | if (*ptr == '\r') { |
| 2297 | bytes++; |
| 2298 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2299 | if (ptr >= buf->data + buf->size) |
| 2300 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2301 | } |
| 2302 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2303 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2304 | return 0; |
| 2305 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2306 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2307 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2308 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2309 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2310 | return bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2311 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2312 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2313 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2314 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2315 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2316 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2317 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2318 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2319 | { |
| 2320 | int q = 1000; |
| 2321 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2322 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2323 | goto out; |
| 2324 | q = (*qvalue++ - '0') * 1000; |
| 2325 | |
| 2326 | if (*qvalue++ != '.') |
| 2327 | goto out; |
| 2328 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2329 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2330 | goto out; |
| 2331 | q += (*qvalue++ - '0') * 100; |
| 2332 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2333 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2334 | goto out; |
| 2335 | q += (*qvalue++ - '0') * 10; |
| 2336 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2337 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2338 | goto out; |
| 2339 | q += (*qvalue++ - '0') * 1; |
| 2340 | out: |
| 2341 | if (q > 1000) |
| 2342 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2343 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2344 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2345 | return q; |
| 2346 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2347 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2348 | void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2349 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2350 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2351 | int tmp = TX_CON_WANT_KAL; |
| 2352 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2353 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2354 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2355 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2356 | tmp = TX_CON_WANT_TUN; |
| 2357 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2358 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2359 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2360 | tmp = TX_CON_WANT_TUN; |
| 2361 | } |
| 2362 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2363 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2364 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2365 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2366 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2367 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2368 | tmp = TX_CON_WANT_CLO; |
| 2369 | else |
| 2370 | tmp = TX_CON_WANT_SCL; |
| 2371 | } |
| 2372 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2373 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2374 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2375 | tmp = TX_CON_WANT_CLO; |
| 2376 | |
| 2377 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2378 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2379 | |
| 2380 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2381 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2382 | /* parse the Connection header and possibly clean it */ |
| 2383 | int to_del = 0; |
| 2384 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2385 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2386 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2387 | to_del |= 2; /* remove "keep-alive" */ |
| 2388 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2389 | to_del |= 1; /* remove "close" */ |
| 2390 | http_parse_connection_header(txn, msg, to_del); |
| 2391 | } |
| 2392 | |
| 2393 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2394 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2395 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2396 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2397 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2398 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2399 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2400 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2401 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2402 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2403 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2404 | * processing can continue on next analysers, or zero if it either needs more |
| 2405 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2406 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2407 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2408 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2409 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2410 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2411 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2412 | /* |
| 2413 | * We will parse the partial (or complete) lines. |
| 2414 | * We will check the request syntax, and also join multi-line |
| 2415 | * headers. An index of all the lines will be elaborated while |
| 2416 | * parsing. |
| 2417 | * |
| 2418 | * For the parsing, we use a 28 states FSM. |
| 2419 | * |
| 2420 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2421 | * req->buf->p = beginning of request |
| 2422 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2423 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2424 | * msg->eol = end of current header or line (LF or CRLF) |
| 2425 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2426 | * |
| 2427 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2428 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2429 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2430 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2432 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2433 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2434 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2435 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2436 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2437 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2438 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2439 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2440 | now_ms, __FUNCTION__, |
| 2441 | s, |
| 2442 | req, |
| 2443 | req->rex, req->wex, |
| 2444 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2445 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2446 | req->analysers); |
| 2447 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2448 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2449 | s->srv_error = http_return_srv_error; |
| 2450 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2451 | /* There's a protected area at the end of the buffer for rewriting |
| 2452 | * purposes. We don't want to start to parse the request if the |
| 2453 | * protected area is affected, because we may have to move processed |
| 2454 | * data later, which is much more complicated. |
| 2455 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2456 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2457 | |
| 2458 | /* This point is executed when some data is avalaible for analysis, |
| 2459 | * so we log the end of the idle time. */ |
| 2460 | if (s->logs.t_idle == -1) |
| 2461 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 2462 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2463 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2464 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2465 | if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2466 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2467 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2468 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2469 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2470 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2471 | return 0; |
| 2472 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2473 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2474 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2475 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2478 | /* Note that we have the same problem with the response ; we |
| 2479 | * may want to send a redirect, error or anything which requires |
| 2480 | * some spare space. So we'll ensure that we have at least |
| 2481 | * maxrewrite bytes available in the response buffer before |
| 2482 | * processing that one. This will only affect pipelined |
| 2483 | * keep-alive requests. |
| 2484 | */ |
| 2485 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2486 | unlikely(!channel_is_rewritable(&s->res) || |
| 2487 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2488 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2489 | if (s->res.buf->o) { |
| 2490 | if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2491 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2492 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2493 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2494 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2495 | s->res.flags |= CF_WAKE_WRITE; |
| 2496 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2497 | return 0; |
| 2498 | } |
| 2499 | } |
| 2500 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2501 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2502 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2503 | } |
| 2504 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2505 | /* 1: we might have to print this header in debug mode */ |
| 2506 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2507 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2508 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2509 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2510 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2511 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2512 | /* this is a bit complex : in case of error on the request line, |
| 2513 | * we know that rq.l is still zero, so we display only the part |
| 2514 | * up to the end of the line (truncated by debug_hdr). |
| 2515 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2516 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2517 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2519 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2520 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2521 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2522 | while (cur_idx) { |
| 2523 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2524 | debug_hdr("clihdr", s, sol, eol); |
| 2525 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2526 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2527 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2528 | } |
| 2529 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2530 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2531 | /* |
| 2532 | * Now we quickly check if we have found a full valid request. |
| 2533 | * If not so, we check the FD and buffer states before leaving. |
| 2534 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2535 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2536 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2537 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2538 | * we note the error in the stream flags but don't set any state. |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2539 | * Since the error will be noted there, it will not be counted by |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2540 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2541 | * Last, we may increase some tracked counters' http request errors on |
| 2542 | * the cases that are deliberately the client's fault. For instance, |
| 2543 | * a timeout or connection reset is not counted as an error. However |
| 2544 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2545 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2546 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2547 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2548 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2549 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2550 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2551 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2552 | stream_inc_http_req_ctr(s); |
| 2553 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2554 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2555 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2556 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2557 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2558 | /* 1: Since we are in header mode, if there's no space |
| 2559 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2560 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2561 | * must terminate it now. |
| 2562 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2563 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2564 | /* FIXME: check if URI is set and return Status |
| 2565 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2566 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2567 | stream_inc_http_req_ctr(s); |
| 2568 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2569 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2570 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2571 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | goto return_bad_req; |
| 2573 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2574 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2575 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2576 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2577 | if (!(s->flags & SF_ERR_MASK)) |
| 2578 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2579 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2580 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2581 | goto failed_keep_alive; |
| 2582 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2583 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2584 | goto failed_keep_alive; |
| 2585 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2587 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2588 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2589 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2590 | } |
| 2591 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2592 | txn->status = 400; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2593 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2594 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2595 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2596 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2597 | proxy_inc_fe_req_ctr(sess->fe); |
| 2598 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2599 | if (sess->listener->counters) |
| 2600 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2601 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2602 | if (!(s->flags & SF_FINST_MASK)) |
| 2603 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | return 0; |
| 2605 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2606 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2607 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2608 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2609 | if (!(s->flags & SF_ERR_MASK)) |
| 2610 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2611 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2612 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2613 | goto failed_keep_alive; |
| 2614 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2615 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2616 | goto failed_keep_alive; |
| 2617 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2618 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2619 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2620 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2621 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2622 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2623 | txn->status = 408; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2624 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2625 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2626 | req->analysers &= AN_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2628 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2629 | proxy_inc_fe_req_ctr(sess->fe); |
| 2630 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2631 | if (sess->listener->counters) |
| 2632 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2633 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2634 | if (!(s->flags & SF_FINST_MASK)) |
| 2635 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2636 | return 0; |
| 2637 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2638 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2639 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2640 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2641 | if (!(s->flags & SF_ERR_MASK)) |
| 2642 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2643 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2644 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2645 | goto failed_keep_alive; |
| 2646 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2647 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2648 | goto failed_keep_alive; |
| 2649 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2650 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2651 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | txn->status = 400; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2653 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2654 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2655 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2656 | stream_inc_http_err_ctr(s); |
| 2657 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2658 | proxy_inc_fe_req_ctr(sess->fe); |
| 2659 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2660 | if (sess->listener->counters) |
| 2661 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2663 | if (!(s->flags & SF_FINST_MASK)) |
| 2664 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2665 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2666 | } |
| 2667 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2668 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2669 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2670 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2671 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2672 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2673 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2674 | /* We need more data, we have to re-enable quick-ack in case we |
| 2675 | * previously disabled it, otherwise we might cause the client |
| 2676 | * to delay next data. |
| 2677 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2678 | setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2679 | } |
| 2680 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2681 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2682 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2683 | /* If the client starts to talk, let's fall back to |
| 2684 | * request timeout processing. |
| 2685 | */ |
| 2686 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2687 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2688 | } |
| 2689 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2691 | if (!tick_isset(req->analyse_exp)) { |
| 2692 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2693 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2694 | tick_isset(s->be->timeout.httpka)) |
| 2695 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2696 | else |
| 2697 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2698 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2699 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2700 | /* we're not ready yet */ |
| 2701 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2702 | |
| 2703 | failed_keep_alive: |
| 2704 | /* Here we process low-level errors for keep-alive requests. In |
| 2705 | * short, if the request is not the first one and it experiences |
| 2706 | * a timeout, read error or shutdown, we just silently close so |
| 2707 | * that the client can try again. |
| 2708 | */ |
| 2709 | txn->status = 0; |
| 2710 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2711 | req->analysers &= AN_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2712 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2713 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2714 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2715 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2716 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2717 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2718 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2719 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2720 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2721 | * later. At this point, we have the last CRLF at req->buf->data + msg->eoh. |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2722 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2723 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2724 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2725 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2726 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2727 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2728 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2729 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2730 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2731 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2732 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2733 | /* kill the pending keep-alive timeout */ |
| 2734 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2735 | req->analyse_exp = TICK_ETERNITY; |
| 2736 | } |
| 2737 | |
| 2738 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2739 | /* Maybe we found in invalid header name while we were configured not |
| 2740 | * to block on that, so we have to capture it now. |
| 2741 | */ |
| 2742 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2743 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2744 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2745 | /* |
| 2746 | * 1: identify the method |
| 2747 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2748 | txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2749 | |
| 2750 | /* we can make use of server redirect on GET and HEAD */ |
| 2751 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2752 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2753 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2754 | /* |
| 2755 | * 2: check if the URI matches the monitor_uri. |
| 2756 | * We have to do this for every request which gets in, because |
| 2757 | * the monitor-uri is defined by the frontend. |
| 2758 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2759 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2760 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2761 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2762 | sess->fe->monitor_uri, |
| 2763 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2764 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2765 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2766 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2767 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2768 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2769 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2770 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2771 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2772 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2773 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2774 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2775 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2776 | ret = acl_pass(ret); |
| 2777 | if (cond->pol == ACL_COND_UNLESS) |
| 2778 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2779 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2780 | if (ret) { |
| 2781 | /* we fail this request, let's return 503 service unavail */ |
| 2782 | txn->status = 503; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2783 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2784 | if (!(s->flags & SF_ERR_MASK)) |
| 2785 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2786 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2787 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2788 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2789 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2790 | /* nothing to fail, let's reply normaly */ |
| 2791 | txn->status = 200; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2792 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2793 | if (!(s->flags & SF_ERR_MASK)) |
| 2794 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2795 | goto return_prx_cond; |
| 2796 | } |
| 2797 | |
| 2798 | /* |
| 2799 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2800 | * Note: we cannot log anymore if the request has been |
| 2801 | * classified as invalid. |
| 2802 | */ |
| 2803 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2804 | /* we have a complete HTTP request that we must log */ |
| 2805 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2806 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2807 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2808 | if (urilen >= REQURI_LEN) |
| 2809 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2810 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2811 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2813 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2814 | s->do_log(s); |
| 2815 | } else { |
| 2816 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2817 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2818 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2820 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2821 | * exactly one digit "." one digit. This check may be disabled using |
| 2822 | * option accept-invalid-http-request. |
| 2823 | */ |
| 2824 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2825 | if (msg->sl.rq.v_l != 8) { |
| 2826 | msg->err_pos = msg->sl.rq.v; |
| 2827 | goto return_bad_req; |
| 2828 | } |
| 2829 | |
| 2830 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2831 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2832 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2833 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2834 | msg->err_pos = msg->sl.rq.v + 4; |
| 2835 | goto return_bad_req; |
| 2836 | } |
| 2837 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2838 | else { |
| 2839 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2840 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2841 | goto return_bad_req; |
| 2842 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2843 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2844 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2845 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2846 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2847 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2848 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2849 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2850 | |
| 2851 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2852 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2854 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2855 | * we have what looks like a proxied connection instead of a connection, |
| 2856 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2857 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2858 | * happen to do that despite being non-standard :-( |
| 2859 | * We consider that a request not beginning with either '/' or '*' is |
| 2860 | * a proxied connection, which covers both "scheme://location" and |
| 2861 | * CONNECT ip:port. |
| 2862 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2863 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2864 | req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2865 | txn->flags |= TX_USE_PX_CONN; |
| 2866 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2867 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2868 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2869 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2870 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2871 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2872 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2873 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2874 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2875 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2876 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2877 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2878 | * The length of a message body is determined by one of the following |
| 2879 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2880 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2881 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2882 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2883 | * code is always terminated by the first empty line after the |
| 2884 | * header fields, regardless of the header fields present in the |
| 2885 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2886 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2887 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2888 | * the connection will become a tunnel immediately after the empty |
| 2889 | * line that concludes the header fields. A client MUST ignore any |
| 2890 | * Content-Length or Transfer-Encoding header fields received in |
| 2891 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2892 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2893 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2894 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2895 | * body length is determined by reading and decoding the chunked |
| 2896 | * data until the transfer coding indicates the data is complete. |
| 2897 | * |
| 2898 | * If a Transfer-Encoding header field is present in a response and |
| 2899 | * the chunked transfer coding is not the final encoding, the |
| 2900 | * message body length is determined by reading the connection until |
| 2901 | * it is closed by the server. If a Transfer-Encoding header field |
| 2902 | * is present in a request and the chunked transfer coding is not |
| 2903 | * the final encoding, the message body length cannot be determined |
| 2904 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2905 | * status code and then close the connection. |
| 2906 | * |
| 2907 | * If a message is received with both a Transfer-Encoding and a |
| 2908 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2909 | * Content-Length. Such a message might indicate an attempt to |
| 2910 | * perform request smuggling (Section 9.5) or response splitting |
| 2911 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2912 | * remove the received Content-Length field prior to forwarding such |
| 2913 | * a message downstream. |
| 2914 | * |
| 2915 | * 4. If a message is received without Transfer-Encoding and with |
| 2916 | * either multiple Content-Length header fields having differing |
| 2917 | * field-values or a single Content-Length header field having an |
| 2918 | * invalid value, then the message framing is invalid and the |
| 2919 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2920 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2921 | * status code and then close the connection. If this is a response |
| 2922 | * message received by a proxy, the proxy MUST close the connection |
| 2923 | * to the server, discard the received response, and send a 502 (Bad |
| 2924 | * Gateway) response to the client. If this is a response message |
| 2925 | * received by a user agent, the user agent MUST close the |
| 2926 | * connection to the server and discard the received response. |
| 2927 | * |
| 2928 | * 5. If a valid Content-Length header field is present without |
| 2929 | * Transfer-Encoding, its decimal value defines the expected message |
| 2930 | * body length in octets. If the sender closes the connection or |
| 2931 | * the recipient times out before the indicated number of octets are |
| 2932 | * received, the recipient MUST consider the message to be |
| 2933 | * incomplete and close the connection. |
| 2934 | * |
| 2935 | * 6. If this is a request message and none of the above are true, then |
| 2936 | * the message body length is zero (no message body is present). |
| 2937 | * |
| 2938 | * 7. Otherwise, this is a response message without a declared message |
| 2939 | * body length, so the message body length is determined by the |
| 2940 | * number of octets received prior to the server closing the |
| 2941 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2942 | */ |
| 2943 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2944 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2945 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2946 | while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2947 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2948 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2949 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2950 | /* chunked not last, return badreq */ |
| 2951 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2952 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2955 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2956 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2957 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2958 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2959 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2960 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2961 | else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2962 | signed long long cl; |
| 2963 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2964 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2965 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2966 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2967 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2968 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2969 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2970 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2971 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2972 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2974 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2975 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2976 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2977 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2978 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2979 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2980 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2981 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2982 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2983 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2984 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2985 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2986 | } |
| 2987 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2988 | /* even bodyless requests have a known length */ |
| 2989 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2990 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2991 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2992 | * only change if both the request and the config reference something else. |
| 2993 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2994 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2995 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2996 | * if FE and BE have the same settings (common). The method consists in |
| 2997 | * checking if options changed between the two calls (implying that either |
| 2998 | * one is non-null, or one of them is non-null and we are there for the first |
| 2999 | * time. |
| 3000 | */ |
| 3001 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3002 | ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 3003 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3004 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3005 | /* we may have to wait for the request's body */ |
| 3006 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3007 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3008 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3009 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3010 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3011 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3012 | req->analyse_exp = TICK_ETERNITY; |
| 3013 | return 1; |
| 3014 | |
| 3015 | return_bad_req: |
| 3016 | /* We centralize bad requests processing here */ |
| 3017 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3018 | /* we detected a parsing error. We want to archive this request |
| 3019 | * in the dedicated proxy area for later troubleshooting. |
| 3020 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3021 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3025 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3026 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3027 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3028 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3029 | if (sess->listener->counters) |
| 3030 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3031 | |
| 3032 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3033 | if (!(s->flags & SF_ERR_MASK)) |
| 3034 | s->flags |= SF_ERR_PRXCOND; |
| 3035 | if (!(s->flags & SF_FINST_MASK)) |
| 3036 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3037 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3038 | req->analysers &= AN_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3039 | req->analyse_exp = TICK_ETERNITY; |
| 3040 | return 0; |
| 3041 | } |
| 3042 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3043 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3044 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3045 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3046 | * and program a response message if something was unexpected. It cannot fail |
| 3047 | * and always relies on the stats applet to complete the job. It does not touch |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3048 | * analysers nor counters, which are left to the caller. It does not touch |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3049 | * s->target which is supposed to already point to the stats applet. The caller |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3050 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3051 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3052 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3053 | { |
| 3054 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3055 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3056 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3057 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3058 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3059 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3060 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3061 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3062 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3063 | appctx = si_appctx(si); |
| 3064 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3065 | appctx->st1 = appctx->st2 = 0; |
| 3066 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3067 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3068 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 3069 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3070 | |
| 3071 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3072 | lookup = uri + uri_auth->uri_len; |
| 3073 | |
| 3074 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3075 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3076 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3077 | break; |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | if (uri_auth->refresh) { |
| 3082 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3083 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3084 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3085 | break; |
| 3086 | } |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3091 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3092 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3093 | break; |
| 3094 | } |
| 3095 | } |
| 3096 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 3097 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 3098 | if (memcmp(h, ";typed", 6) == 0) { |
| 3099 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 3100 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3101 | break; |
| 3102 | } |
| 3103 | } |
| 3104 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3105 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3106 | if (memcmp(h, ";st=", 4) == 0) { |
| 3107 | int i; |
| 3108 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3109 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3110 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3111 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3112 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3113 | break; |
| 3114 | } |
| 3115 | } |
| 3116 | break; |
| 3117 | } |
| 3118 | } |
| 3119 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3120 | appctx->ctx.stats.scope_str = 0; |
| 3121 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3122 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3123 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3124 | int itx = 0; |
| 3125 | const char *h2; |
| 3126 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3127 | const char *err; |
| 3128 | |
| 3129 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3130 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3131 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3132 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3133 | itx++; |
| 3134 | h++; |
| 3135 | } |
| 3136 | |
| 3137 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3138 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3139 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3140 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3141 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3142 | memcpy(scope_txt, h2, itx); |
| 3143 | scope_txt[itx] = '\0'; |
| 3144 | err = invalid_char(scope_txt); |
| 3145 | if (err) { |
| 3146 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3147 | appctx->ctx.stats.scope_str = 0; |
| 3148 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3149 | } |
| 3150 | break; |
| 3151 | } |
| 3152 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3153 | |
| 3154 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3155 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3156 | int ret = 1; |
| 3157 | |
| 3158 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3159 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3160 | ret = acl_pass(ret); |
| 3161 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3162 | ret = !ret; |
| 3163 | } |
| 3164 | |
| 3165 | if (ret) { |
| 3166 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3167 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3168 | break; |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3173 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3174 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3175 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3176 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3177 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3178 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3179 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3180 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3181 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3182 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3183 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3184 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3185 | else { |
| 3186 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3187 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3188 | } |
| 3189 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3190 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3191 | return 1; |
| 3192 | } |
| 3193 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3194 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3195 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3196 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3197 | void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3198 | { |
| 3199 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3200 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3201 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3202 | #endif |
| 3203 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3204 | if (from->ss_family == AF_INET6) { |
| 3205 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3206 | /* v4-mapped addresses need IP_TOS */ |
| 3207 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3208 | else |
| 3209 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3210 | } |
| 3211 | #endif |
| 3212 | } |
| 3213 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3214 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3215 | const char* name, unsigned int name_len, |
| 3216 | const char *str, struct my_regex *re, |
| 3217 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3218 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3219 | struct hdr_ctx ctx; |
| 3220 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3221 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3222 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3223 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3224 | struct chunk *output = get_trash_chunk(); |
| 3225 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3226 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3227 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3228 | /* Choose the header browsing function. */ |
| 3229 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3230 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3231 | http_find_hdr_func = http_find_header2; |
| 3232 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3233 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3234 | http_find_hdr_func = http_find_full_header2; |
| 3235 | break; |
| 3236 | default: /* impossible */ |
| 3237 | return -1; |
| 3238 | } |
| 3239 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3240 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3241 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3242 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3243 | char *val = ctx.line + ctx.val; |
| 3244 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3245 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3246 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3247 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3248 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3249 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3250 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3251 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3252 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3253 | delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3254 | |
| 3255 | hdr->len += delta; |
| 3256 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3257 | |
| 3258 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3259 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | return 0; |
| 3263 | } |
| 3264 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3265 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3266 | const char* name, unsigned int name_len, |
| 3267 | struct list *fmt, struct my_regex *re, |
| 3268 | int action) |
| 3269 | { |
| 3270 | struct chunk *replace = get_trash_chunk(); |
| 3271 | |
| 3272 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3273 | if (replace->len >= replace->size - 1) |
| 3274 | return -1; |
| 3275 | |
| 3276 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3277 | } |
| 3278 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3279 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3280 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3281 | * further processing of the request (auth, deny, ...), and defaults to |
| 3282 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3283 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3284 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 3285 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 3286 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3287 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3288 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3289 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3290 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3291 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3292 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3293 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3294 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3295 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3296 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3297 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3298 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3299 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3300 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3301 | * resume condition. If a resume is needed it is always in the action |
| 3302 | * and never in the ACL or converters. In this case, we initialise the |
| 3303 | * current rule, and go to the action execution point. |
| 3304 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3305 | if (s->current_rule) { |
| 3306 | rule = s->current_rule; |
| 3307 | s->current_rule = NULL; |
| 3308 | if (s->current_rule_list == rules) |
| 3309 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3310 | } |
| 3311 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3312 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3313 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3314 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3315 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3316 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3317 | int ret; |
| 3318 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3319 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3320 | ret = acl_pass(ret); |
| 3321 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3322 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3323 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3324 | |
| 3325 | if (!ret) /* condition not matched */ |
| 3326 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3327 | } |
| 3328 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3329 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3330 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3331 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3332 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3333 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3334 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3335 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3336 | if (deny_status) |
| 3337 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3338 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3339 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3340 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3341 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3342 | if (deny_status) |
| 3343 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3344 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3345 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3346 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3347 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3348 | auth_realm = rule->arg.auth.realm; |
| 3349 | if (!auth_realm) { |
| 3350 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3351 | auth_realm = STATS_DEFAULT_REALM; |
| 3352 | else |
| 3353 | auth_realm = px->id; |
| 3354 | } |
| 3355 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3356 | * count one error, because normal browsing won't significantly |
| 3357 | * increase the counter but brute force attempts will. |
| 3358 | */ |
| 3359 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3360 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3361 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3362 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3363 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3364 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3365 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3366 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3367 | return HTTP_RULE_RES_BADREQ; |
| 3368 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3369 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3370 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3371 | s->task->nice = rule->arg.nice; |
| 3372 | break; |
| 3373 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3374 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3375 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3376 | inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3377 | break; |
| 3378 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3379 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3380 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3381 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3382 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3383 | #endif |
| 3384 | break; |
| 3385 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3386 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3387 | s->logs.level = rule->arg.loglevel; |
| 3388 | break; |
| 3389 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3390 | case ACT_HTTP_REPLACE_HDR: |
| 3391 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3392 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3393 | rule->arg.hdr_add.name_len, |
| 3394 | &rule->arg.hdr_add.fmt, |
| 3395 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3396 | return HTTP_RULE_RES_BADREQ; |
| 3397 | break; |
| 3398 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3399 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3400 | ctx.idx = 0; |
| 3401 | /* remove all occurrences of the header */ |
| 3402 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3403 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3404 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3405 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3406 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3407 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3408 | case ACT_HTTP_SET_HDR: |
| 3409 | case ACT_HTTP_ADD_HDR: |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3410 | /* The scope of the trash buffer must be limited to this function. The |
| 3411 | * build_logline() function can execute a lot of other function which |
| 3412 | * can use the trash buffer. So for limiting the scope of this global |
| 3413 | * buffer, we build first the header value using build_logline, and |
| 3414 | * after we store the header name. |
| 3415 | */ |
| 3416 | len = rule->arg.hdr_add.name_len + 2, |
| 3417 | len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3418 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3419 | trash.str[rule->arg.hdr_add.name_len] = ':'; |
| 3420 | trash.str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 3421 | trash.len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3422 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3423 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3424 | /* remove all occurrences of the header */ |
| 3425 | ctx.idx = 0; |
| 3426 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3427 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3428 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3429 | } |
| 3430 | } |
| 3431 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3432 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3433 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3434 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3435 | case ACT_HTTP_DEL_ACL: |
| 3436 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3437 | struct pat_ref *ref; |
| 3438 | char *key; |
| 3439 | int len; |
| 3440 | |
| 3441 | /* collect reference */ |
| 3442 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3443 | if (!ref) |
| 3444 | continue; |
| 3445 | |
| 3446 | /* collect key */ |
| 3447 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3448 | key = trash.str; |
| 3449 | key[len] = '\0'; |
| 3450 | |
| 3451 | /* perform update */ |
| 3452 | /* returned code: 1=ok, 0=ko */ |
| 3453 | pat_ref_delete(ref, key); |
| 3454 | |
| 3455 | break; |
| 3456 | } |
| 3457 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3458 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3459 | struct pat_ref *ref; |
| 3460 | char *key; |
| 3461 | struct chunk *trash_key; |
| 3462 | int len; |
| 3463 | |
| 3464 | trash_key = get_trash_chunk(); |
| 3465 | |
| 3466 | /* collect reference */ |
| 3467 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3468 | if (!ref) |
| 3469 | continue; |
| 3470 | |
| 3471 | /* collect key */ |
| 3472 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3473 | key = trash_key->str; |
| 3474 | key[len] = '\0'; |
| 3475 | |
| 3476 | /* perform update */ |
| 3477 | /* add entry only if it does not already exist */ |
| 3478 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3479 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3480 | |
| 3481 | break; |
| 3482 | } |
| 3483 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3484 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3485 | struct pat_ref *ref; |
| 3486 | char *key, *value; |
| 3487 | struct chunk *trash_key, *trash_value; |
| 3488 | int len; |
| 3489 | |
| 3490 | trash_key = get_trash_chunk(); |
| 3491 | trash_value = get_trash_chunk(); |
| 3492 | |
| 3493 | /* collect reference */ |
| 3494 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3495 | if (!ref) |
| 3496 | continue; |
| 3497 | |
| 3498 | /* collect key */ |
| 3499 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3500 | key = trash_key->str; |
| 3501 | key[len] = '\0'; |
| 3502 | |
| 3503 | /* collect value */ |
| 3504 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3505 | value = trash_value->str; |
| 3506 | value[len] = '\0'; |
| 3507 | |
| 3508 | /* perform update */ |
| 3509 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3510 | /* update entry if it exists */ |
| 3511 | pat_ref_set(ref, key, value, NULL); |
| 3512 | else |
| 3513 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3514 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3515 | |
| 3516 | break; |
| 3517 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3518 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3519 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3520 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3521 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3522 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3523 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3524 | case ACT_RET_ERR: |
| 3525 | case ACT_RET_CONT: |
| 3526 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3527 | case ACT_RET_STOP: |
| 3528 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3529 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3530 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3531 | return HTTP_RULE_RES_YIELD; |
| 3532 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3533 | break; |
| 3534 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3535 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3536 | /* Note: only the first valid tracking parameter of each |
| 3537 | * applies. |
| 3538 | */ |
| 3539 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3540 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3541 | struct stktable *t; |
| 3542 | struct stksess *ts; |
| 3543 | struct stktable_key *key; |
| 3544 | void *ptr; |
| 3545 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3546 | t = rule->arg.trk_ctr.table.t; |
| 3547 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3548 | |
| 3549 | if (key && (ts = stktable_get_entry(t, key))) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3550 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3551 | |
| 3552 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3553 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3554 | if (ptr) |
| 3555 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3556 | |
| 3557 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3558 | if (ptr) |
| 3559 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3560 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3561 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3562 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3563 | if (sess->fe != s->be) |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3564 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3565 | } |
| 3566 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3567 | break; |
| 3568 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3569 | /* other flags exists, but normaly, they never be matched. */ |
| 3570 | default: |
| 3571 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3572 | } |
| 3573 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3574 | |
| 3575 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3576 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3577 | } |
| 3578 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3579 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3580 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3581 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3582 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3583 | * is returned, the process can continue the evaluation of next rule list. If |
| 3584 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3585 | * is returned, it means the operation could not be processed and a server error |
| 3586 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3587 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3588 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3589 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3590 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3591 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3592 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3593 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3594 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3595 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3596 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3597 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3598 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3599 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3600 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3601 | * resume condition. If a resume is needed it is always in the action |
| 3602 | * and never in the ACL or converters. In this case, we initialise the |
| 3603 | * current rule, and go to the action execution point. |
| 3604 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3605 | if (s->current_rule) { |
| 3606 | rule = s->current_rule; |
| 3607 | s->current_rule = NULL; |
| 3608 | if (s->current_rule_list == rules) |
| 3609 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3610 | } |
| 3611 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3612 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3613 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3614 | |
| 3615 | /* check optional condition */ |
| 3616 | if (rule->cond) { |
| 3617 | int ret; |
| 3618 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3619 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3620 | ret = acl_pass(ret); |
| 3621 | |
| 3622 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3623 | ret = !ret; |
| 3624 | |
| 3625 | if (!ret) /* condition not matched */ |
| 3626 | continue; |
| 3627 | } |
| 3628 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3629 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3630 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3631 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3632 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3633 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3634 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3635 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3636 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3637 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3638 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3639 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3640 | s->task->nice = rule->arg.nice; |
| 3641 | break; |
| 3642 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3643 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3644 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3645 | inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3646 | break; |
| 3647 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3648 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3649 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3650 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3651 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3652 | #endif |
| 3653 | break; |
| 3654 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3655 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3656 | s->logs.level = rule->arg.loglevel; |
| 3657 | break; |
| 3658 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3659 | case ACT_HTTP_REPLACE_HDR: |
| 3660 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3661 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3662 | rule->arg.hdr_add.name_len, |
| 3663 | &rule->arg.hdr_add.fmt, |
| 3664 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3665 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3666 | break; |
| 3667 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3668 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3669 | ctx.idx = 0; |
| 3670 | /* remove all occurrences of the header */ |
| 3671 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3672 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3673 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3674 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3675 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3676 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3677 | case ACT_HTTP_SET_HDR: |
| 3678 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3679 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3680 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3681 | trash.len = rule->arg.hdr_add.name_len; |
| 3682 | trash.str[trash.len++] = ':'; |
| 3683 | trash.str[trash.len++] = ' '; |
| 3684 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3685 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3686 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3687 | /* remove all occurrences of the header */ |
| 3688 | ctx.idx = 0; |
| 3689 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3690 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3691 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3692 | } |
| 3693 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3694 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3695 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3696 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3697 | case ACT_HTTP_DEL_ACL: |
| 3698 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3699 | struct pat_ref *ref; |
| 3700 | char *key; |
| 3701 | int len; |
| 3702 | |
| 3703 | /* collect reference */ |
| 3704 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3705 | if (!ref) |
| 3706 | continue; |
| 3707 | |
| 3708 | /* collect key */ |
| 3709 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3710 | key = trash.str; |
| 3711 | key[len] = '\0'; |
| 3712 | |
| 3713 | /* perform update */ |
| 3714 | /* returned code: 1=ok, 0=ko */ |
| 3715 | pat_ref_delete(ref, key); |
| 3716 | |
| 3717 | break; |
| 3718 | } |
| 3719 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3720 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3721 | struct pat_ref *ref; |
| 3722 | char *key; |
| 3723 | struct chunk *trash_key; |
| 3724 | int len; |
| 3725 | |
| 3726 | trash_key = get_trash_chunk(); |
| 3727 | |
| 3728 | /* collect reference */ |
| 3729 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3730 | if (!ref) |
| 3731 | continue; |
| 3732 | |
| 3733 | /* collect key */ |
| 3734 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3735 | key = trash_key->str; |
| 3736 | key[len] = '\0'; |
| 3737 | |
| 3738 | /* perform update */ |
| 3739 | /* check if the entry already exists */ |
| 3740 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3741 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3742 | |
| 3743 | break; |
| 3744 | } |
| 3745 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3746 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3747 | struct pat_ref *ref; |
| 3748 | char *key, *value; |
| 3749 | struct chunk *trash_key, *trash_value; |
| 3750 | int len; |
| 3751 | |
| 3752 | trash_key = get_trash_chunk(); |
| 3753 | trash_value = get_trash_chunk(); |
| 3754 | |
| 3755 | /* collect reference */ |
| 3756 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3757 | if (!ref) |
| 3758 | continue; |
| 3759 | |
| 3760 | /* collect key */ |
| 3761 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3762 | key = trash_key->str; |
| 3763 | key[len] = '\0'; |
| 3764 | |
| 3765 | /* collect value */ |
| 3766 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3767 | value = trash_value->str; |
| 3768 | value[len] = '\0'; |
| 3769 | |
| 3770 | /* perform update */ |
| 3771 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3772 | /* update entry if it exists */ |
| 3773 | pat_ref_set(ref, key, value, NULL); |
| 3774 | else |
| 3775 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3776 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3777 | |
| 3778 | break; |
| 3779 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3780 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3781 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3782 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3783 | return HTTP_RULE_RES_BADREQ; |
| 3784 | return HTTP_RULE_RES_DONE; |
| 3785 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3786 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3787 | /* Note: only the first valid tracking parameter of each |
| 3788 | * applies. |
| 3789 | */ |
| 3790 | |
| 3791 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
| 3792 | struct stktable *t; |
| 3793 | struct stksess *ts; |
| 3794 | struct stktable_key *key; |
| 3795 | void *ptr; |
| 3796 | |
| 3797 | t = rule->arg.trk_ctr.table.t; |
| 3798 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3799 | |
| 3800 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3801 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
| 3802 | |
| 3803 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3804 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3805 | if (ptr) |
| 3806 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3807 | |
| 3808 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3809 | if (ptr) |
| 3810 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3811 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3812 | |
| 3813 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3814 | if (sess->fe != s->be) |
| 3815 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3816 | |
| 3817 | /* When the client triggers a 4xx from the server, it's most often due |
| 3818 | * to a missing object or permission. These events should be tracked |
| 3819 | * because if they happen often, it may indicate a brute force or a |
| 3820 | * vulnerability scan. Normally this is done when receiving the response |
| 3821 | * but here we're tracking after this ought to have been done so we have |
| 3822 | * to do it on purpose. |
| 3823 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3824 | if ((unsigned)(txn->status - 400) < 100) { |
| 3825 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3826 | if (ptr) |
| 3827 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3828 | |
| 3829 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3830 | if (ptr) |
| 3831 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3832 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3833 | } |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3834 | } |
| 3835 | } |
| 3836 | break; |
| 3837 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3838 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3839 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3840 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3841 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3842 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3843 | case ACT_RET_ERR: |
| 3844 | case ACT_RET_CONT: |
| 3845 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3846 | case ACT_RET_STOP: |
| 3847 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3848 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3849 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3850 | return HTTP_RULE_RES_YIELD; |
| 3851 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3852 | break; |
| 3853 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3854 | /* other flags exists, but normaly, they never be matched. */ |
| 3855 | default: |
| 3856 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3861 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3865 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3866 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3867 | * as too large a request to build a valid response. |
| 3868 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3869 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3870 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3871 | struct http_msg *req = &txn->req; |
| 3872 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3873 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3874 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3875 | |
| 3876 | /* build redirect message */ |
| 3877 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3878 | case 308: |
| 3879 | msg_fmt = HTTP_308; |
| 3880 | break; |
| 3881 | case 307: |
| 3882 | msg_fmt = HTTP_307; |
| 3883 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3884 | case 303: |
| 3885 | msg_fmt = HTTP_303; |
| 3886 | break; |
| 3887 | case 301: |
| 3888 | msg_fmt = HTTP_301; |
| 3889 | break; |
| 3890 | case 302: |
| 3891 | default: |
| 3892 | msg_fmt = HTTP_302; |
| 3893 | break; |
| 3894 | } |
| 3895 | |
| 3896 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3897 | return 0; |
| 3898 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3899 | location = trash.str + trash.len; |
| 3900 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3901 | switch(rule->type) { |
| 3902 | case REDIRECT_TYPE_SCHEME: { |
| 3903 | const char *path; |
| 3904 | const char *host; |
| 3905 | struct hdr_ctx ctx; |
| 3906 | int pathlen; |
| 3907 | int hostlen; |
| 3908 | |
| 3909 | host = ""; |
| 3910 | hostlen = 0; |
| 3911 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3912 | if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3913 | host = ctx.line + ctx.val; |
| 3914 | hostlen = ctx.vlen; |
| 3915 | } |
| 3916 | |
| 3917 | path = http_get_path(txn); |
| 3918 | /* build message using path */ |
| 3919 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3920 | pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3921 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3922 | int qs = 0; |
| 3923 | while (qs < pathlen) { |
| 3924 | if (path[qs] == '?') { |
| 3925 | pathlen = qs; |
| 3926 | break; |
| 3927 | } |
| 3928 | qs++; |
| 3929 | } |
| 3930 | } |
| 3931 | } else { |
| 3932 | path = "/"; |
| 3933 | pathlen = 1; |
| 3934 | } |
| 3935 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3936 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3937 | /* check if we can add scheme + "://" + host + path */ |
| 3938 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3939 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3940 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3941 | /* add scheme */ |
| 3942 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3943 | trash.len += rule->rdr_len; |
| 3944 | } |
| 3945 | else { |
| 3946 | /* add scheme with executing log format */ |
| 3947 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3948 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3949 | /* check if we can add scheme + "://" + host + path */ |
| 3950 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3951 | return 0; |
| 3952 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3953 | /* add "://" */ |
| 3954 | memcpy(trash.str + trash.len, "://", 3); |
| 3955 | trash.len += 3; |
| 3956 | |
| 3957 | /* add host */ |
| 3958 | memcpy(trash.str + trash.len, host, hostlen); |
| 3959 | trash.len += hostlen; |
| 3960 | |
| 3961 | /* add path */ |
| 3962 | memcpy(trash.str + trash.len, path, pathlen); |
| 3963 | trash.len += pathlen; |
| 3964 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3965 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3966 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3967 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3968 | if (trash.len > trash.size - 5) |
| 3969 | return 0; |
| 3970 | trash.str[trash.len] = '/'; |
| 3971 | trash.len++; |
| 3972 | } |
| 3973 | |
| 3974 | break; |
| 3975 | } |
| 3976 | case REDIRECT_TYPE_PREFIX: { |
| 3977 | const char *path; |
| 3978 | int pathlen; |
| 3979 | |
| 3980 | path = http_get_path(txn); |
| 3981 | /* build message using path */ |
| 3982 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3983 | pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3984 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3985 | int qs = 0; |
| 3986 | while (qs < pathlen) { |
| 3987 | if (path[qs] == '?') { |
| 3988 | pathlen = qs; |
| 3989 | break; |
| 3990 | } |
| 3991 | qs++; |
| 3992 | } |
| 3993 | } |
| 3994 | } else { |
| 3995 | path = "/"; |
| 3996 | pathlen = 1; |
| 3997 | } |
| 3998 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3999 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4000 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4001 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4002 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4003 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4004 | * add anything, otherwise it makes it hard for the user to |
| 4005 | * configure a self-redirection. |
| 4006 | */ |
| 4007 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4008 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4009 | trash.len += rule->rdr_len; |
| 4010 | } |
| 4011 | } |
| 4012 | else { |
| 4013 | /* add prefix with executing log format */ |
| 4014 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4015 | |
| 4016 | /* Check length */ |
| 4017 | if (trash.len + pathlen > trash.size - 4) |
| 4018 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | /* add path */ |
| 4022 | memcpy(trash.str + trash.len, path, pathlen); |
| 4023 | trash.len += pathlen; |
| 4024 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4025 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4026 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4027 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4028 | if (trash.len > trash.size - 5) |
| 4029 | return 0; |
| 4030 | trash.str[trash.len] = '/'; |
| 4031 | trash.len++; |
| 4032 | } |
| 4033 | |
| 4034 | break; |
| 4035 | } |
| 4036 | case REDIRECT_TYPE_LOCATION: |
| 4037 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4038 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4039 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4040 | return 0; |
| 4041 | |
| 4042 | /* add location */ |
| 4043 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4044 | trash.len += rule->rdr_len; |
| 4045 | } |
| 4046 | else { |
| 4047 | /* add location with executing log format */ |
| 4048 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4049 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4050 | /* Check left length */ |
| 4051 | if (trash.len > trash.size - 4) |
| 4052 | return 0; |
| 4053 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4054 | break; |
| 4055 | } |
| 4056 | |
| 4057 | if (rule->cookie_len) { |
| 4058 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4059 | trash.len += 14; |
| 4060 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4061 | trash.len += rule->cookie_len; |
| 4062 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4063 | trash.len += 2; |
| 4064 | } |
| 4065 | |
| 4066 | /* add end of headers and the keep-alive/close status. |
| 4067 | * We may choose to set keep-alive if the Location begins |
| 4068 | * with a slash, because the client will come back to the |
| 4069 | * same server. |
| 4070 | */ |
| 4071 | txn->status = rule->code; |
| 4072 | /* let's log the request time */ |
| 4073 | s->logs.tv_request = now; |
| 4074 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4075 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4076 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4077 | ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4078 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4079 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4080 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4081 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4082 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4083 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4084 | trash.len += 30; |
| 4085 | } else { |
| 4086 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4087 | trash.len += 24; |
| 4088 | } |
| 4089 | } |
| 4090 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4091 | trash.len += 4; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4092 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash)); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4093 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4094 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4095 | bi_fast_delete(req->chn->buf, req->sov); |
| 4096 | req->next -= req->sov; |
| 4097 | req->sov = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4098 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END); |
| 4099 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4100 | req->msg_state = HTTP_MSG_CLOSED; |
| 4101 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4102 | /* Trim any possible response */ |
| 4103 | res->chn->buf->i = 0; |
| 4104 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4105 | } else { |
| 4106 | /* keep-alive not possible */ |
| 4107 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4108 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4109 | trash.len += 29; |
| 4110 | } else { |
| 4111 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4112 | trash.len += 23; |
| 4113 | } |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4114 | http_reply_and_close(s, txn->status, &trash); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4115 | req->chn->analysers &= AN_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4116 | } |
| 4117 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4118 | if (!(s->flags & SF_ERR_MASK)) |
| 4119 | s->flags |= SF_ERR_LOCAL; |
| 4120 | if (!(s->flags & SF_FINST_MASK)) |
| 4121 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4122 | |
| 4123 | return 1; |
| 4124 | } |
| 4125 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4126 | /* This stream analyser runs all HTTP request processing which is common to |
| 4127 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4128 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4129 | * It returns 1 if the processing can continue on next analysers, or zero if it |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4130 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4131 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4132 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4133 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4134 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4135 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4136 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4137 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4138 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4139 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4140 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4141 | int deny_status = HTTP_ERR_403; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4142 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4143 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4144 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4145 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4146 | } |
| 4147 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4148 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4149 | now_ms, __FUNCTION__, |
| 4150 | s, |
| 4151 | req, |
| 4152 | req->rex, req->wex, |
| 4153 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4154 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4155 | req->analysers); |
| 4156 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4157 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4158 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4159 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4160 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4161 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4162 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4163 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4164 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4165 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4166 | goto return_prx_yield; |
| 4167 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4168 | case HTTP_RULE_RES_CONT: |
| 4169 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4170 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4171 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4172 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4173 | if (txn->flags & TX_CLTARPIT) |
| 4174 | goto tarpit; |
| 4175 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4176 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4177 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4178 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4179 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4180 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4181 | goto done; |
| 4182 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4183 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4184 | goto return_bad_req; |
| 4185 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4186 | } |
| 4187 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4188 | /* OK at this stage, we know that the request was accepted according to |
| 4189 | * the http-request rules, we can check for the stats. Note that the |
| 4190 | * URI is detected *before* the req* rules in order not to be affected |
| 4191 | * by a possible reqrep, while they are processed *after* so that a |
| 4192 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4193 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4194 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4195 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4196 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4197 | txn->status = 500; |
| 4198 | s->logs.tv_request = now; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4199 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4200 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4201 | if (!(s->flags & SF_ERR_MASK)) |
| 4202 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4203 | goto return_prx_cond; |
| 4204 | } |
| 4205 | |
| 4206 | /* parse the whole stats request and extract the relevant information */ |
| 4207 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4208 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4209 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4210 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4211 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4212 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4213 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4214 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4215 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4218 | /* evaluate the req* rules except reqadd */ |
| 4219 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4220 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4221 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4222 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4223 | if (txn->flags & TX_CLDENY) |
| 4224 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4225 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4226 | if (txn->flags & TX_CLTARPIT) |
| 4227 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4228 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4229 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4230 | /* add request headers from the rule sets in the same order */ |
| 4231 | list_for_each_entry(wl, &px->req_add, list) { |
| 4232 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4233 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4234 | ret = acl_pass(ret); |
| 4235 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4236 | ret = !ret; |
| 4237 | if (!ret) |
| 4238 | continue; |
| 4239 | } |
| 4240 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4241 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4242 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4243 | } |
| 4244 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4245 | |
| 4246 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4247 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4248 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4249 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4250 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4251 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4252 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4253 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4254 | if (!(s->flags & SF_FINST_MASK)) |
| 4255 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4256 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4257 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 4258 | req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4259 | req->analysers &= ~AN_FLT_XFER_DATA; |
| 4260 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4261 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4262 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4263 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4264 | /* check whether we have some ACLs set to redirect this request */ |
| 4265 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4266 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4267 | int ret; |
| 4268 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4269 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4270 | ret = acl_pass(ret); |
| 4271 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4272 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4273 | if (!ret) |
| 4274 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4275 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4276 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4277 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4278 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4279 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4280 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4281 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4282 | * If this happens, then the data will not come immediately, so we must |
| 4283 | * send all what we have without waiting. Note that due to the small gain |
| 4284 | * in waiting for the body of the request, it's easier to simply put the |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4285 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4286 | * itself once used. |
| 4287 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4288 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4289 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4290 | done: /* done with this analyser, continue with next ones that the calling |
| 4291 | * points will have set, if any. |
| 4292 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4293 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4294 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4295 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4296 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4297 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4298 | tarpit: |
| 4299 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4300 | * which may be the same as the connect timeout if unspecified. |
| 4301 | * If unset, then set it to zero because we really want it to |
| 4302 | * eventually expire. We build the tarpit as an analyser. |
| 4303 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4304 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4305 | |
| 4306 | /* wipe the request out so that we can drop the connection early |
| 4307 | * if the client closes first. |
| 4308 | */ |
| 4309 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4310 | |
| 4311 | /* Allow cookie logging |
| 4312 | */ |
| 4313 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4314 | manage_client_side_cookies(s, req); |
| 4315 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4316 | req->analysers &= AN_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4317 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4318 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4319 | if (!req->analyse_exp) |
| 4320 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4321 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4322 | sess->fe->fe_counters.denied_req++; |
| 4323 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4324 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4325 | if (sess->listener->counters) |
| 4326 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4327 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4328 | |
| 4329 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4330 | |
| 4331 | /* Allow cookie logging |
| 4332 | */ |
| 4333 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4334 | manage_client_side_cookies(s, req); |
| 4335 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4336 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4337 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4338 | s->logs.tv_request = now; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4339 | http_reply_and_close(s, txn->status, http_error_message(s, deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4340 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4341 | sess->fe->fe_counters.denied_req++; |
| 4342 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4343 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4344 | if (sess->listener->counters) |
| 4345 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4346 | goto return_prx_cond; |
| 4347 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4348 | return_bad_req: |
| 4349 | /* We centralize bad requests processing here */ |
| 4350 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4351 | /* we detected a parsing error. We want to archive this request |
| 4352 | * in the dedicated proxy area for later troubleshooting. |
| 4353 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4354 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4355 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4356 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4357 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4358 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4359 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4360 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4361 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4362 | if (sess->listener->counters) |
| 4363 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4364 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4365 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4366 | if (!(s->flags & SF_ERR_MASK)) |
| 4367 | s->flags |= SF_ERR_PRXCOND; |
| 4368 | if (!(s->flags & SF_FINST_MASK)) |
| 4369 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4370 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4371 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4372 | req->analyse_exp = TICK_ETERNITY; |
| 4373 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4374 | |
| 4375 | return_prx_yield: |
| 4376 | channel_dont_connect(req); |
| 4377 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4378 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4379 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4380 | /* This function performs all the processing enabled for the current request. |
| 4381 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4382 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4383 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4384 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4385 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4386 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4387 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4388 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4389 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4390 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4391 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4392 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4393 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4394 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4395 | return 0; |
| 4396 | } |
| 4397 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4398 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4399 | now_ms, __FUNCTION__, |
| 4400 | s, |
| 4401 | req, |
| 4402 | req->rex, req->wex, |
| 4403 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4404 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4405 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4406 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4407 | /* |
| 4408 | * Right now, we know that we have processed the entire headers |
| 4409 | * and that unwanted requests have been filtered out. We can do |
| 4410 | * whatever we want with the remaining request. Also, now we |
| 4411 | * may have separate values for ->fe, ->be. |
| 4412 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4413 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4414 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4415 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4416 | * incoming request. Note that this requires that a connection is |
| 4417 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4418 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4419 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4420 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4421 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4422 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4423 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4424 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4425 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4426 | txn->status = 500; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4427 | req->analysers &= AN_FLT_END; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4428 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4429 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4430 | if (!(s->flags & SF_ERR_MASK)) |
| 4431 | s->flags |= SF_ERR_RESOURCE; |
| 4432 | if (!(s->flags & SF_FINST_MASK)) |
| 4433 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4434 | |
| 4435 | return 0; |
| 4436 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4437 | |
| 4438 | path = http_get_path(txn); |
| 4439 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4440 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 4441 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4442 | /* if the path was found, we have to remove everything between |
| 4443 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4444 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4445 | * u_l characters by a single "/". |
| 4446 | */ |
| 4447 | if (path) { |
| 4448 | char *cur_ptr = req->buf->p; |
| 4449 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4450 | int delta; |
| 4451 | |
| 4452 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4453 | http_msg_move_end(&txn->req, delta); |
| 4454 | cur_end += delta; |
| 4455 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4456 | goto return_bad_req; |
| 4457 | } |
| 4458 | else { |
| 4459 | char *cur_ptr = req->buf->p; |
| 4460 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4461 | int delta; |
| 4462 | |
| 4463 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4464 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4465 | http_msg_move_end(&txn->req, delta); |
| 4466 | cur_end += delta; |
| 4467 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4468 | goto return_bad_req; |
| 4469 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4470 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4471 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4472 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4473 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4474 | * Note that doing so might move headers in the request, but |
| 4475 | * the fields will stay coherent and the URI will not move. |
| 4476 | * This should only be performed in the backend. |
| 4477 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4478 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4479 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4480 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4481 | /* add unique-id if "header-unique-id" is specified */ |
| 4482 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 4483 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4484 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4485 | goto return_bad_req; |
| 4486 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4487 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4488 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4489 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4490 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4491 | chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4492 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4493 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4494 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4495 | goto return_bad_req; |
| 4496 | } |
| 4497 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4498 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4499 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4500 | * asks for it. |
| 4501 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4502 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4503 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4504 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4505 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4506 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4507 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4508 | /* The header is set to be added only if none is present |
| 4509 | * and we found it, so don't do anything. |
| 4510 | */ |
| 4511 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4512 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4513 | /* Add an X-Forwarded-For header unless the source IP is |
| 4514 | * in the 'except' network range. |
| 4515 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4516 | if ((!sess->fe->except_mask.s_addr || |
| 4517 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4518 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4519 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4520 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4521 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4522 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4523 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4524 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4525 | |
| 4526 | /* Note: we rely on the backend to get the header name to be used for |
| 4527 | * x-forwarded-for, because the header is really meant for the backends. |
| 4528 | * However, if the backend did not specify any option, we have to rely |
| 4529 | * on the frontend's header name. |
| 4530 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4531 | if (s->be->fwdfor_hdr_len) { |
| 4532 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4533 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4534 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4535 | len = sess->fe->fwdfor_hdr_len; |
| 4536 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4537 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4538 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 4539 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4540 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4541 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4542 | } |
| 4543 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4544 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4545 | /* FIXME: for the sake of completeness, we should also support |
| 4546 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4547 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4548 | int len; |
| 4549 | char pn[INET6_ADDRSTRLEN]; |
| 4550 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4551 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4552 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4553 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4554 | /* Note: we rely on the backend to get the header name to be used for |
| 4555 | * x-forwarded-for, because the header is really meant for the backends. |
| 4556 | * However, if the backend did not specify any option, we have to rely |
| 4557 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4558 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4559 | if (s->be->fwdfor_hdr_len) { |
| 4560 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4561 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4562 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4563 | len = sess->fe->fwdfor_hdr_len; |
| 4564 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4565 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4566 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4567 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4568 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4569 | goto return_bad_req; |
| 4570 | } |
| 4571 | } |
| 4572 | |
| 4573 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4574 | * 10: add X-Original-To if either the frontend or the backend |
| 4575 | * asks for it. |
| 4576 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4577 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4578 | |
| 4579 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4580 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4581 | /* Add an X-Original-To header unless the destination IP is |
| 4582 | * in the 'except' network range. |
| 4583 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4584 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4585 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4586 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4587 | ((!sess->fe->except_mask_to.s_addr || |
| 4588 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4589 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4590 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4591 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4592 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4593 | int len; |
| 4594 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4595 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4596 | |
| 4597 | /* Note: we rely on the backend to get the header name to be used for |
| 4598 | * x-original-to, because the header is really meant for the backends. |
| 4599 | * However, if the backend did not specify any option, we have to rely |
| 4600 | * on the frontend's header name. |
| 4601 | */ |
| 4602 | if (s->be->orgto_hdr_len) { |
| 4603 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4604 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4605 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4606 | len = sess->fe->orgto_hdr_len; |
| 4607 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4608 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4609 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4610 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4611 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4612 | goto return_bad_req; |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4617 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4618 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4619 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4620 | * "Upgrade" is present in the Connection header. |
| 4621 | */ |
| 4622 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4623 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4624 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4625 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4626 | unsigned int want_flags = 0; |
| 4627 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4628 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4629 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4630 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4631 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4632 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4633 | want_flags |= TX_CON_CLO_SET; |
| 4634 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4635 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4636 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4637 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4638 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4639 | want_flags |= TX_CON_KAL_SET; |
| 4640 | } |
| 4641 | |
| 4642 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4643 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4644 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4645 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4646 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4647 | /* If we have no server assigned yet and we're balancing on url_param |
| 4648 | * with a POST request, we may be interested in checking the body for |
| 4649 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4650 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4651 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4652 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4653 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4654 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4655 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4656 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4657 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4658 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4659 | req->analysers &= ~AN_FLT_XFER_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4660 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4661 | #ifdef TCP_QUICKACK |
| 4662 | /* We expect some data from the client. Unless we know for sure |
| 4663 | * we already have a full request, we have to re-enable quick-ack |
| 4664 | * in case we previously disabled it, otherwise we might cause |
| 4665 | * the client to delay further data. |
| 4666 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4667 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4668 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4669 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4670 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4671 | setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4672 | #endif |
| 4673 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4674 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4675 | /************************************************************* |
| 4676 | * OK, that's finished for the headers. We have done what we * |
| 4677 | * could. Let's switch to the DATA state. * |
| 4678 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4679 | req->analyse_exp = TICK_ETERNITY; |
| 4680 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4681 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4682 | /* if the server closes the connection, we want to immediately react |
| 4683 | * and close the socket to save packets and syscalls. |
| 4684 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4685 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4686 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4688 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4689 | /* OK let's go on with the BODY now */ |
| 4690 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4691 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4692 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4693 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4694 | /* we detected a parsing error. We want to archive this request |
| 4695 | * in the dedicated proxy area for later troubleshooting. |
| 4696 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4697 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4698 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4699 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4700 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4701 | txn->status = 400; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4702 | req->analysers &= AN_FLT_END; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4703 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4704 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4705 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4706 | if (sess->listener->counters) |
| 4707 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4708 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4709 | if (!(s->flags & SF_ERR_MASK)) |
| 4710 | s->flags |= SF_ERR_PRXCOND; |
| 4711 | if (!(s->flags & SF_FINST_MASK)) |
| 4712 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4713 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4714 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4715 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4716 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4717 | * returns zero, at the beginning because it prevents any other processing |
| 4718 | * from occurring, and at the end because it terminates the request. |
| 4719 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4720 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4721 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4722 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4723 | |
| 4724 | /* This connection is being tarpitted. The CLIENT side has |
| 4725 | * already set the connect expiration date to the right |
| 4726 | * timeout. We just have to check that the client is still |
| 4727 | * there and that the timeout has not expired. |
| 4728 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4729 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4730 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4731 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4732 | return 0; |
| 4733 | |
| 4734 | /* We will set the queue timer to the time spent, just for |
| 4735 | * logging purposes. We fake a 500 server error, so that the |
| 4736 | * attacker will not suspect his connection has been tarpitted. |
| 4737 | * It will not cause trouble to the logs because we can exclude |
| 4738 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4739 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4740 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4741 | |
| 4742 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4743 | if (!(req->flags & CF_READ_ERROR)) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4744 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4745 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4746 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4747 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4748 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4749 | if (!(s->flags & SF_ERR_MASK)) |
| 4750 | s->flags |= SF_ERR_PRXCOND; |
| 4751 | if (!(s->flags & SF_FINST_MASK)) |
| 4752 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4753 | return 0; |
| 4754 | } |
| 4755 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4756 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4757 | * for either the buffer to be full, or the full advertised contents to have |
| 4758 | * reached the buffer. It must only be called after the standard HTTP request |
| 4759 | * processing has occurred, because it expects the request to be parsed and will |
| 4760 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4761 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4762 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4763 | * needs to read more data, or 1 once it has completed its analysis. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4764 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4765 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4766 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4767 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4768 | struct http_txn *txn = s->txn; |
| 4769 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4770 | |
| 4771 | /* We have to parse the HTTP request body to find any required data. |
| 4772 | * "balance url_param check_post" should have been the only way to get |
| 4773 | * into this. We were brought here after HTTP header analysis, so all |
| 4774 | * related structures are ready. |
| 4775 | */ |
| 4776 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4777 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4778 | /* This is the first call */ |
| 4779 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4780 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4781 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4782 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4783 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4784 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4785 | */ |
| 4786 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4787 | struct hdr_ctx ctx; |
| 4788 | ctx.idx = 0; |
| 4789 | /* Expect is allowed in 1.1, look for it */ |
| 4790 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4791 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4792 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4793 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4794 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4795 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4796 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4797 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4798 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4799 | * req->buf->p still points to the beginning of the message. We |
| 4800 | * must save the body in msg->next because it survives buffer |
| 4801 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4802 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4803 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4804 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4805 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4806 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4807 | else |
| 4808 | msg->msg_state = HTTP_MSG_DATA; |
| 4809 | } |
| 4810 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4811 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4812 | /* We're in content-length mode, we just have to wait for enough data. */ |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4813 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4814 | goto missing_data; |
| 4815 | |
| 4816 | /* OK we have everything we need now */ |
| 4817 | goto http_end; |
| 4818 | } |
| 4819 | |
| 4820 | /* OK here we're parsing a chunked-encoded message */ |
| 4821 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4822 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4823 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4824 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4825 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4826 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4827 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4828 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4829 | if (!ret) |
| 4830 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4831 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4832 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4833 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4834 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4835 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4836 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4839 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4840 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4841 | * for at least a whole chunk or the whole content length bytes after |
| 4842 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4843 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4844 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4845 | goto http_end; |
| 4846 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4847 | if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4848 | goto http_end; |
| 4849 | |
| 4850 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4851 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4852 | * we have the maximum we can expect. |
| 4853 | */ |
| 4854 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4855 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4856 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4857 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4858 | txn->status = 408; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4859 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4860 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4861 | if (!(s->flags & SF_ERR_MASK)) |
| 4862 | s->flags |= SF_ERR_CLITO; |
| 4863 | if (!(s->flags & SF_FINST_MASK)) |
| 4864 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4865 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4866 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4867 | |
| 4868 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4869 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4870 | /* Not enough data. We'll re-use the http-request |
| 4871 | * timeout here. Ideally, we should set the timeout |
| 4872 | * relative to the accept() date. We just set the |
| 4873 | * request timeout once at the beginning of the |
| 4874 | * request. |
| 4875 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4876 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4877 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4878 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4879 | return 0; |
| 4880 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4881 | |
| 4882 | http_end: |
| 4883 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4884 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4885 | req->analysers &= ~an_bit; |
| 4886 | req->analyse_exp = TICK_ETERNITY; |
| 4887 | return 1; |
| 4888 | |
| 4889 | return_bad_req: /* let's centralize all bad requests */ |
| 4890 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4891 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4892 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4893 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4894 | if (!(s->flags & SF_ERR_MASK)) |
| 4895 | s->flags |= SF_ERR_PRXCOND; |
| 4896 | if (!(s->flags & SF_FINST_MASK)) |
| 4897 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4898 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4899 | return_err_msg: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4900 | req->analysers &= AN_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4901 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4902 | if (sess->listener->counters) |
| 4903 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4904 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4905 | } |
| 4906 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4907 | /* send a server's name with an outgoing request over an established connection. |
| 4908 | * Note: this function is designed to be called once the request has been scheduled |
| 4909 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4910 | * proceeding. |
| 4911 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4912 | int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4913 | |
| 4914 | struct hdr_ctx ctx; |
| 4915 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4916 | char *hdr_name = be->server_id_hdr_name; |
| 4917 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4918 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4919 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4920 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4921 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4922 | ctx.idx = 0; |
| 4923 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4924 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4925 | if (old_o) { |
| 4926 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4927 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4928 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4929 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4930 | } |
| 4931 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4932 | old_i = chn->buf->i; |
| 4933 | while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4934 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4935 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4939 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4940 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4941 | hdr_val += hdr_name_len; |
| 4942 | *hdr_val++ = ':'; |
| 4943 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4944 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4945 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4946 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4947 | if (old_o) { |
| 4948 | /* If this was a forwarded request, we must readjust the amount of |
| 4949 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4950 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4951 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4952 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4953 | old_o += chn->buf->i - old_i; |
| 4954 | b_adv(chn->buf, old_o); |
| 4955 | txn->req.next -= old_o; |
| 4956 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4957 | } |
| 4958 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4959 | return 0; |
| 4960 | } |
| 4961 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4962 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4963 | * right now but it works. |
| 4964 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4965 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4966 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4967 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4968 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4969 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4970 | struct connection *srv_conn; |
| 4971 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4972 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4973 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4974 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4975 | * server's connections. We need a safer way to reinitialize buffer |
| 4976 | * flags. We also need a more accurate method for computing per-request |
| 4977 | * data. |
| 4978 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4979 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4980 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4981 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4982 | * to the server. |
| 4983 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4984 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4985 | !si_conn_ready(&s->si[1])) { |
| 4986 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4987 | si_shutr(&s->si[1]); |
| 4988 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4989 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4990 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4991 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4992 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4993 | if (unlikely(s->srv_conn)) |
| 4994 | sess_change_server(s, NULL); |
| 4995 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4996 | |
| 4997 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4998 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4999 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5000 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5001 | int n; |
| 5002 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5003 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5004 | if (n < 1 || n > 5) |
| 5005 | n = 0; |
| 5006 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5007 | if (fe->mode == PR_MODE_HTTP) { |
| 5008 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5009 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5010 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5011 | (be->mode == PR_MODE_HTTP)) { |
| 5012 | be->be_counters.p.http.rsp[n]++; |
| 5013 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5014 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5018 | s->logs.bytes_in -= s->req.buf->i; |
| 5019 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5020 | |
| 5021 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5022 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5023 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5024 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5025 | s->do_log(s); |
| 5026 | } |
| 5027 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5028 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5029 | stream_stop_content_counters(s); |
| 5030 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5031 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5032 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5033 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 5034 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 5035 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5036 | tv_zero(&s->logs.tv_request); |
| 5037 | s->logs.t_queue = -1; |
| 5038 | s->logs.t_connect = -1; |
| 5039 | s->logs.t_data = -1; |
| 5040 | s->logs.t_close = 0; |
| 5041 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5042 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5043 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5044 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5045 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5046 | |
| 5047 | if (s->pend_pos) |
| 5048 | pendconn_free(s->pend_pos); |
| 5049 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5050 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5051 | if (s->flags & SF_CURR_SESS) { |
| 5052 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5053 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5054 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5055 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5056 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5059 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5060 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5061 | /* only release our endpoint if we don't intend to reuse the |
| 5062 | * connection. |
| 5063 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5064 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5065 | !si_conn_ready(&s->si[1])) { |
| 5066 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5067 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5070 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5071 | s->si[1].err_type = SI_ET_NONE; |
| 5072 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5073 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5074 | s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5075 | 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); |
| 5076 | 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 Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5077 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5078 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5079 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5080 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5081 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5082 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5083 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5084 | |
| 5085 | if (prev_status == 401 || prev_status == 407) { |
| 5086 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5087 | * a chance of being able to send the visitor again to the same |
| 5088 | * server over the same connection. This is required by some |
| 5089 | * broken protocols such as NTLM, and anyway whenever there is |
| 5090 | * an opportunity for sending the challenge to the proper place, |
| 5091 | * it's better to do it (at least it helps with debugging). |
| 5092 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5093 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5094 | if (srv_conn) |
| 5095 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5096 | } |
| 5097 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5098 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 5099 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 5100 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5101 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5102 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5103 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5104 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5105 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5106 | s->req.flags |= CF_NEVER_WAIT; |
| 5107 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5108 | } |
| 5109 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5110 | /* if the request buffer is not empty, it means we're |
| 5111 | * about to process another request, so send pending |
| 5112 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5113 | * Just don't do this if the buffer is close to be full, |
| 5114 | * because the request will wait for it to flush a little |
| 5115 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5116 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5117 | if (s->req.buf->i) { |
| 5118 | if (s->res.buf->o && |
| 5119 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5120 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5121 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5122 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5123 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 5124 | /* we're removing the analysers, we MUST re-enable events detection. |
| 5125 | * We don't enable close on the response channel since it's either |
| 5126 | * already closed, or in keep-alive with an idle connection handler. |
| 5127 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5128 | channel_auto_read(&s->req); |
| 5129 | channel_auto_close(&s->req); |
| 5130 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5131 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5132 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5133 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5134 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5135 | if (!srv) |
| 5136 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5137 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5138 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5139 | else if (prev_flags & TX_NOT_FIRST) |
| 5140 | /* note: we check the request, not the connection, but |
| 5141 | * this is valid for strategies SAFE and AGGR, and in |
| 5142 | * case of ALWS, we don't care anyway. |
| 5143 | */ |
| 5144 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5145 | else |
| 5146 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5147 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5148 | |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5149 | if (HAS_FILTERS(s)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5150 | s->req.analysers &= AN_FLT_END; |
| 5151 | s->res.analysers &= AN_FLT_END; |
| 5152 | } |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5153 | else { |
| 5154 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 5155 | s->res.analysers = 0; |
| 5156 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | |
| 5160 | /* This function updates the request state machine according to the response |
| 5161 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5162 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5163 | * it is only used to find when a request/response couple is complete. Both |
| 5164 | * this function and its equivalent should loop until both return zero. It |
| 5165 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5166 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5167 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5168 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5169 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5170 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5171 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5172 | unsigned int old_state = txn->req.msg_state; |
| 5173 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5174 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5175 | return 0; |
| 5176 | |
| 5177 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5178 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5179 | * We can shut the read side unless we want to abort_on_close, |
| 5180 | * or we have a POST request. The issue with POST requests is |
| 5181 | * that some browsers still send a CRLF after the request, and |
| 5182 | * this CRLF must be read so that it does not remain in the kernel |
| 5183 | * buffers, otherwise a close could cause an RST on some systems |
| 5184 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5185 | * Note that if we're using keep-alive on the client side, we'd |
| 5186 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5187 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5188 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5189 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5190 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5191 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5192 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5193 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5194 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5195 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5196 | /* if the server closes the connection, we want to immediately react |
| 5197 | * and close the socket to save packets and syscalls. |
| 5198 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5199 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5200 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5201 | /* In any case we've finished parsing the request so we must |
| 5202 | * disable Nagle when sending data because 1) we're not going |
| 5203 | * to shut this side, and 2) the server is waiting for us to |
| 5204 | * send pending data. |
| 5205 | */ |
| 5206 | chn->flags |= CF_NEVER_WAIT; |
| 5207 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5208 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5209 | goto wait_other_side; |
| 5210 | |
| 5211 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5212 | /* The server has not finished to respond, so we |
| 5213 | * don't want to move in order not to upset it. |
| 5214 | */ |
| 5215 | goto wait_other_side; |
| 5216 | } |
| 5217 | |
| 5218 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5219 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5220 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5221 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5222 | goto wait_other_side; |
| 5223 | } |
| 5224 | |
| 5225 | /* When we get here, it means that both the request and the |
| 5226 | * response have finished receiving. Depending on the connection |
| 5227 | * mode, we'll have to wait for the last bytes to leave in either |
| 5228 | * direction, and sometimes for a close to be effective. |
| 5229 | */ |
| 5230 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5231 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5232 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5233 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5234 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5235 | } |
| 5236 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5237 | /* Option forceclose is set, or either side wants to close, |
| 5238 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5239 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5240 | * once both states are CLOSED. |
| 5241 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5242 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5243 | channel_shutr_now(chn); |
| 5244 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5245 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5246 | } |
| 5247 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5248 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5249 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5250 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5251 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5252 | channel_auto_read(chn); |
| 5253 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5254 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5255 | } |
| 5256 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5257 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5258 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5259 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5260 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5261 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5262 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5263 | goto http_msg_closing; |
| 5264 | } |
| 5265 | else { |
| 5266 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5267 | goto http_msg_closed; |
| 5268 | } |
| 5269 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5270 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5274 | http_msg_closing: |
| 5275 | /* nothing else to forward, just waiting for the output buffer |
| 5276 | * to be empty and for the shutw_now to take effect. |
| 5277 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5278 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5279 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5280 | goto http_msg_closed; |
| 5281 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5282 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5283 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5284 | goto wait_other_side; |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5289 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5290 | /* see above in MSG_DONE why we only do this in these states */ |
| 5291 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5292 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5293 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5294 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5295 | goto wait_other_side; |
| 5296 | } |
| 5297 | |
| 5298 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5299 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | |
| 5303 | /* This function updates the response state machine according to the request |
| 5304 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5305 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5306 | * it is only used to find when a request/response couple is complete. Both |
| 5307 | * this function and its equivalent should loop until both return zero. It |
| 5308 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5309 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5310 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5311 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5312 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5313 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5314 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5315 | unsigned int old_state = txn->rsp.msg_state; |
| 5316 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5318 | return 0; |
| 5319 | |
| 5320 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5321 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5322 | * still monitor the server connection for a possible close |
| 5323 | * while the request is being uploaded, so we don't disable |
| 5324 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5325 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5326 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5327 | |
| 5328 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5329 | goto wait_other_side; |
| 5330 | |
| 5331 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5332 | /* The client seems to still be sending data, probably |
| 5333 | * because we got an error response during an upload. |
| 5334 | * We have the choice of either breaking the connection |
| 5335 | * or letting it pass through. Let's do the later. |
| 5336 | */ |
| 5337 | goto wait_other_side; |
| 5338 | } |
| 5339 | |
| 5340 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5341 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5342 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5343 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5344 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5345 | goto wait_other_side; |
| 5346 | } |
| 5347 | |
| 5348 | /* When we get here, it means that both the request and the |
| 5349 | * response have finished receiving. Depending on the connection |
| 5350 | * mode, we'll have to wait for the last bytes to leave in either |
| 5351 | * direction, and sometimes for a close to be effective. |
| 5352 | */ |
| 5353 | |
| 5354 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5355 | /* Server-close mode : shut read and wait for the request |
| 5356 | * side to close its output buffer. The caller will detect |
| 5357 | * when we're in DONE and the other is in CLOSED and will |
| 5358 | * catch that for the final cleanup. |
| 5359 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5360 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5361 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5362 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5363 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5364 | /* Option forceclose is set, or either side wants to close, |
| 5365 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5366 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5367 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5368 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5369 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5370 | channel_shutr_now(chn); |
| 5371 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5372 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5373 | } |
| 5374 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5375 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5376 | * need to forward remaining data. Keep-alive will need to monitor |
| 5377 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5378 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5379 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5380 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5381 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5382 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | } |
| 5384 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5385 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5386 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5387 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5388 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5389 | goto http_msg_closing; |
| 5390 | } |
| 5391 | else { |
| 5392 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5393 | goto http_msg_closed; |
| 5394 | } |
| 5395 | } |
| 5396 | goto wait_other_side; |
| 5397 | } |
| 5398 | |
| 5399 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5400 | http_msg_closing: |
| 5401 | /* nothing else to forward, just waiting for the output buffer |
| 5402 | * to be empty and for the shutw_now to take effect. |
| 5403 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5404 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5405 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5406 | goto http_msg_closed; |
| 5407 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5408 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5409 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5410 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5411 | if (objt_server(s->target)) |
| 5412 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5413 | goto wait_other_side; |
| 5414 | } |
| 5415 | } |
| 5416 | |
| 5417 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5418 | http_msg_closed: |
| 5419 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5420 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5421 | channel_auto_close(chn); |
| 5422 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5423 | goto wait_other_side; |
| 5424 | } |
| 5425 | |
| 5426 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5427 | /* We force the response to leave immediately if we're waiting for the |
| 5428 | * other side, since there is no pending shutdown to push it out. |
| 5429 | */ |
| 5430 | if (!channel_is_empty(chn)) |
| 5431 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5432 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | |
| 5436 | /* Resync the request and response state machines. Return 1 if either state |
| 5437 | * changes. |
| 5438 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5439 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5440 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5441 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5442 | int old_req_state = txn->req.msg_state; |
| 5443 | int old_res_state = txn->rsp.msg_state; |
| 5444 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5445 | http_sync_req_state(s); |
| 5446 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5447 | if (!http_sync_res_state(s)) |
| 5448 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5449 | if (!http_sync_req_state(s)) |
| 5450 | break; |
| 5451 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5452 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5453 | /* OK, both state machines agree on a compatible state. |
| 5454 | * There are a few cases we're interested in : |
| 5455 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5456 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5457 | * directions, so let's simply disable both analysers. |
| 5458 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5459 | * request. |
| 5460 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5461 | * with server-close mode means we've completed one request and we |
| 5462 | * must re-initialize the server connection. |
| 5463 | */ |
| 5464 | |
| 5465 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5466 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5467 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5468 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5469 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5470 | channel_auto_close(&s->req); |
| 5471 | channel_auto_read(&s->req); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5472 | s->res.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5473 | channel_auto_close(&s->res); |
| 5474 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5475 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5476 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5477 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5478 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5479 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5480 | s->res.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5481 | channel_auto_close(&s->res); |
| 5482 | channel_auto_read(&s->res); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5483 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5484 | channel_abort(&s->req); |
| 5485 | channel_auto_close(&s->req); |
| 5486 | channel_auto_read(&s->req); |
| 5487 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5488 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5489 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5490 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5491 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5492 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5493 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5494 | /* server-close/keep-alive: terminate this transaction, |
| 5495 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5496 | * a fresh-new transaction, but only once we're sure there's |
| 5497 | * enough room in the request and response buffer to process |
| 5498 | * another request. The request buffer must not hold any |
| 5499 | * pending output data and the request buffer must not have |
| 5500 | * output data occupying the reserve. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5501 | */ |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5502 | if (s->req.buf->o) |
| 5503 | s->req.flags |= CF_WAKE_WRITE; |
| 5504 | else if (channel_congested(&s->res)) |
| 5505 | s->res.flags |= CF_WAKE_WRITE; |
| 5506 | else |
| 5507 | http_end_txn_clean_session(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5508 | } |
| 5509 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5510 | return txn->req.msg_state != old_req_state || |
| 5511 | txn->rsp.msg_state != old_res_state; |
| 5512 | } |
| 5513 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5514 | /* This function is an analyser which forwards request body (including chunk |
| 5515 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5516 | * zero byte. The only situation where it must not be called is when we're in |
| 5517 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5518 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5519 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5520 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5521 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5522 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5523 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5524 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5525 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5526 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5527 | struct http_txn *txn = s->txn; |
| 5528 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5529 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5530 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5531 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5532 | return 0; |
| 5533 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5534 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5535 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5536 | /* Output closed while we were sending data. We must abort and |
| 5537 | * wake the other side up. |
| 5538 | */ |
| 5539 | msg->msg_state = HTTP_MSG_ERROR; |
| 5540 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5541 | return 1; |
| 5542 | } |
| 5543 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5544 | /* Note that we don't have to send 100-continue back because we don't |
| 5545 | * need the data to complete our job, and it's up to the server to |
| 5546 | * decide whether to return 100, 417 or anything else in return of |
| 5547 | * an "Expect: 100-continue" header. |
| 5548 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5549 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5550 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5551 | ? HTTP_MSG_CHUNK_SIZE |
| 5552 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5553 | |
| 5554 | /* TODO/filters: when http-buffer-request option is set or if a |
| 5555 | * rule on url_param exists, the first chunk size could be |
| 5556 | * already parsed. In that case, msg->next is after the chunk |
| 5557 | * size (including the CRLF after the size). So this case should |
| 5558 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5559 | } |
| 5560 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5561 | /* Some post-connect processing might want us to refrain from starting to |
| 5562 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5563 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5564 | */ |
| 5565 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5566 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5567 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5568 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5569 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5570 | } |
| 5571 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5572 | } |
| 5573 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5574 | /* in most states, we should abort in case of early close */ |
| 5575 | channel_auto_close(req); |
| 5576 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5577 | if (req->to_forward) { |
| 5578 | /* We can't process the buffer's contents yet */ |
| 5579 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5580 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5581 | } |
| 5582 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5583 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5584 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5585 | ? http_msg_forward_chunked_body(s, msg) |
| 5586 | : http_msg_forward_body(s, msg)); |
| 5587 | if (!ret) |
| 5588 | goto missing_data_or_waiting; |
| 5589 | if (ret < 0) |
| 5590 | goto return_bad_req; |
| 5591 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5592 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5593 | /* other states, DONE...TUNNEL */ |
| 5594 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 5595 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5596 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5597 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5598 | ret = msg->msg_state; |
| 5599 | if (http_resync_states(s)) { |
| 5600 | /* some state changes occurred, maybe the analyser |
| 5601 | * was disabled too. */ |
| 5602 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5603 | if (req->flags & CF_SHUTW) { |
| 5604 | /* request errors are most likely due to the |
| 5605 | * server aborting the transfer. */ |
| 5606 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5607 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5608 | if (msg->err_pos >= 0) |
| 5609 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be); |
| 5610 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5611 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5612 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5613 | } |
| 5614 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5615 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 5616 | * the client's connection and forward any shutdown notification to the |
| 5617 | * server, which will decide whether to close or to go on processing the |
| 5618 | * request. We only do that in tunnel mode, and not in other modes since |
| 5619 | * it can be abused to exhaust source ports. */ |
| 5620 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 5621 | channel_auto_read(req); |
| 5622 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5623 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5624 | s->si[1].flags |= SI_FL_NOLINGER; |
| 5625 | channel_auto_close(req); |
| 5626 | } |
| 5627 | else if (s->txn->meth == HTTP_METH_POST) { |
| 5628 | /* POST requests may require to read extra CRLF sent by broken |
| 5629 | * browsers and which could cause an RST to be sent upon close |
| 5630 | * on some systems (eg: Linux). */ |
| 5631 | channel_auto_read(req); |
| 5632 | } |
| 5633 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5634 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5635 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5636 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5637 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5638 | if (!(s->flags & SF_ERR_MASK)) |
| 5639 | s->flags |= SF_ERR_CLICL; |
| 5640 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5641 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5642 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5643 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5644 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5645 | } |
| 5646 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5647 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5648 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5649 | if (objt_server(s->target)) |
| 5650 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5651 | |
| 5652 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5653 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5654 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5655 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5656 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5657 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5658 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5659 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5660 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5661 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5662 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5663 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5664 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5665 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5666 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5667 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5668 | * modes are already handled by the stream sock layer. We must not do |
| 5669 | * this in content-length mode because it could present the MSG_MORE |
| 5670 | * flag with the last block of forwarded data, which would cause an |
| 5671 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5672 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5673 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5674 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5675 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5676 | return 0; |
| 5677 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5678 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5679 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5680 | if (sess->listener->counters) |
| 5681 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5682 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5683 | return_bad_req_stats_ok: |
| 5684 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5685 | if (txn->status) { |
| 5686 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5687 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5688 | } else { |
| 5689 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5690 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5691 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5692 | req->analysers &= AN_FLT_END; |
| 5693 | s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5694 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5695 | if (!(s->flags & SF_ERR_MASK)) |
| 5696 | s->flags |= SF_ERR_PRXCOND; |
| 5697 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5698 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5699 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5700 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5701 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5702 | } |
| 5703 | return 0; |
| 5704 | |
| 5705 | aborted_xfer: |
| 5706 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5707 | if (txn->status) { |
| 5708 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5709 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5710 | } else { |
| 5711 | txn->status = 502; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5712 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5713 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5714 | req->analysers &= AN_FLT_END; |
| 5715 | s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5716 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5717 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5718 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5719 | if (objt_server(s->target)) |
| 5720 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5721 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5722 | if (!(s->flags & SF_ERR_MASK)) |
| 5723 | s->flags |= SF_ERR_SRVCL; |
| 5724 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5725 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5726 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5727 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5728 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5729 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5730 | return 0; |
| 5731 | } |
| 5732 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5733 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5734 | * processing can continue on next analysers, or zero if it either needs more |
| 5735 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5736 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5737 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5738 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5739 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5740 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5741 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5742 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5743 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5744 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5745 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5746 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5747 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5748 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5749 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5750 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 5751 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5752 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5753 | rep, |
| 5754 | rep->rex, rep->wex, |
| 5755 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5756 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5757 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5758 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5759 | /* |
| 5760 | * Now parse the partial (or complete) lines. |
| 5761 | * We will check the response syntax, and also join multi-line |
| 5762 | * headers. An index of all the lines will be elaborated while |
| 5763 | * parsing. |
| 5764 | * |
| 5765 | * For the parsing, we use a 28 states FSM. |
| 5766 | * |
| 5767 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5768 | * rep->buf->p = beginning of response |
| 5769 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5770 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5771 | * msg->eol = end of current header or line (LF or CRLF) |
| 5772 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5773 | */ |
| 5774 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5775 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5776 | /* There's a protected area at the end of the buffer for rewriting |
| 5777 | * purposes. We don't want to start to parse the request if the |
| 5778 | * protected area is affected, because we may have to move processed |
| 5779 | * data later, which is much more complicated. |
| 5780 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5781 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5782 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5783 | /* some data has still not left the buffer, wake us once that's done */ |
| 5784 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5785 | goto abort_response; |
| 5786 | channel_dont_close(rep); |
| 5787 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5788 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5789 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5790 | } |
| 5791 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5792 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5793 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5794 | buffer_slow_realign(rep->buf); |
| 5795 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5796 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5797 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5798 | } |
| 5799 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5800 | /* 1: we might have to print this header in debug mode */ |
| 5801 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5802 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5803 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5804 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5805 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5806 | sol = rep->buf->p; |
| 5807 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5808 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5809 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5810 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5811 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5812 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5813 | while (cur_idx) { |
| 5814 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5815 | debug_hdr("srvhdr", s, sol, eol); |
| 5816 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5817 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5818 | } |
| 5819 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5820 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5821 | /* |
| 5822 | * Now we quickly check if we have found a full valid response. |
| 5823 | * If not so, we check the FD and buffer states before leaving. |
| 5824 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5825 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5826 | * responses are checked first. |
| 5827 | * |
| 5828 | * Depending on whether the client is still there or not, we |
| 5829 | * may send an error response back or not. Note that normally |
| 5830 | * we should only check for HTTP status there, and check I/O |
| 5831 | * errors somewhere else. |
| 5832 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5833 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5834 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5835 | /* Invalid response */ |
| 5836 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5837 | /* we detected a parsing error. We want to archive this response |
| 5838 | * in the dedicated proxy area for later troubleshooting. |
| 5839 | */ |
| 5840 | hdr_response_bad: |
| 5841 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5842 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5843 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5844 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5845 | if (objt_server(s->target)) { |
| 5846 | objt_server(s->target)->counters.failed_resp++; |
| 5847 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5848 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5849 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5850 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5851 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5852 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5853 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5854 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5855 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5856 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5857 | if (!(s->flags & SF_ERR_MASK)) |
| 5858 | s->flags |= SF_ERR_PRXCOND; |
| 5859 | if (!(s->flags & SF_FINST_MASK)) |
| 5860 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5861 | |
| 5862 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5863 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5864 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5865 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5866 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5867 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5868 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5869 | goto hdr_response_bad; |
| 5870 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5871 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5872 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5873 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5874 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5875 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5876 | else if (txn->flags & TX_NOT_FIRST) |
| 5877 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5878 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5879 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5880 | if (objt_server(s->target)) { |
| 5881 | objt_server(s->target)->counters.failed_resp++; |
| 5882 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5883 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5884 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5885 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5886 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5887 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5888 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5889 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5890 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5891 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5892 | if (!(s->flags & SF_ERR_MASK)) |
| 5893 | s->flags |= SF_ERR_SRVCL; |
| 5894 | if (!(s->flags & SF_FINST_MASK)) |
| 5895 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5896 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5897 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5898 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5899 | /* read timeout : return a 504 to the client. */ |
| 5900 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5901 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5902 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5903 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5904 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5905 | if (objt_server(s->target)) { |
| 5906 | objt_server(s->target)->counters.failed_resp++; |
| 5907 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5908 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5909 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5910 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5911 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5912 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5913 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5914 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5915 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5916 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5917 | if (!(s->flags & SF_ERR_MASK)) |
| 5918 | s->flags |= SF_ERR_SRVTO; |
| 5919 | if (!(s->flags & SF_FINST_MASK)) |
| 5920 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5921 | return 0; |
| 5922 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5923 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5924 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5925 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5926 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5927 | s->be->be_counters.cli_aborts++; |
| 5928 | if (objt_server(s->target)) |
| 5929 | objt_server(s->target)->counters.cli_aborts++; |
| 5930 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5931 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5932 | channel_auto_close(rep); |
| 5933 | |
| 5934 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5935 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5936 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5937 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5938 | if (!(s->flags & SF_ERR_MASK)) |
| 5939 | s->flags |= SF_ERR_CLICL; |
| 5940 | if (!(s->flags & SF_FINST_MASK)) |
| 5941 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5942 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5943 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5944 | return 0; |
| 5945 | } |
| 5946 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5947 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5948 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5949 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5950 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5951 | else if (txn->flags & TX_NOT_FIRST) |
| 5952 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5953 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5954 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5955 | if (objt_server(s->target)) { |
| 5956 | objt_server(s->target)->counters.failed_resp++; |
| 5957 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5958 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5959 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5960 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5961 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5962 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5963 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5964 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5965 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5966 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5967 | if (!(s->flags & SF_ERR_MASK)) |
| 5968 | s->flags |= SF_ERR_SRVCL; |
| 5969 | if (!(s->flags & SF_FINST_MASK)) |
| 5970 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5971 | return 0; |
| 5972 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5973 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5974 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5975 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5976 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5977 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5978 | else if (txn->flags & TX_NOT_FIRST) |
| 5979 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5980 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5981 | s->be->be_counters.failed_resp++; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5982 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5983 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5984 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5985 | if (!(s->flags & SF_ERR_MASK)) |
| 5986 | s->flags |= SF_ERR_CLICL; |
| 5987 | if (!(s->flags & SF_FINST_MASK)) |
| 5988 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5989 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5990 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5991 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5992 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5993 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5994 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5995 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5996 | return 0; |
| 5997 | } |
| 5998 | |
| 5999 | /* More interesting part now : we know that we have a complete |
| 6000 | * response which at least looks like HTTP. We have an indicator |
| 6001 | * of each header's length, so we can parse them quickly. |
| 6002 | */ |
| 6003 | |
| 6004 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6005 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6006 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6007 | /* |
| 6008 | * 1: get the status code |
| 6009 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6010 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6011 | if (n < 1 || n > 5) |
| 6012 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6013 | /* when the client triggers a 4xx from the server, it's most often due |
| 6014 | * to a missing object or permission. These events should be tracked |
| 6015 | * because if they happen often, it may indicate a brute force or a |
| 6016 | * vulnerability scan. |
| 6017 | */ |
| 6018 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6019 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6020 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6021 | if (objt_server(s->target)) |
| 6022 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6023 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6024 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6025 | * exactly one digit "." one digit. This check may be disabled using |
| 6026 | * option accept-invalid-http-response. |
| 6027 | */ |
| 6028 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6029 | if (msg->sl.st.v_l != 8) { |
| 6030 | msg->err_pos = 0; |
| 6031 | goto hdr_response_bad; |
| 6032 | } |
| 6033 | |
| 6034 | if (rep->buf->p[4] != '/' || |
| 6035 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6036 | rep->buf->p[6] != '.' || |
| 6037 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6038 | msg->err_pos = 4; |
| 6039 | goto hdr_response_bad; |
| 6040 | } |
| 6041 | } |
| 6042 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6043 | /* check if the response is HTTP/1.1 or above */ |
| 6044 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6045 | ((rep->buf->p[5] > '1') || |
| 6046 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6047 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6048 | |
| 6049 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6050 | 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 Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6051 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6052 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6053 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6054 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6055 | txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6056 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6057 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6058 | * and 505 are triggered on demand by client request, so we must not |
| 6059 | * count them as server failures. |
| 6060 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6061 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6062 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6063 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6064 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6065 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6066 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6067 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6068 | /* |
| 6069 | * 2: check for cacheability. |
| 6070 | */ |
| 6071 | |
| 6072 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6073 | case 100: |
| 6074 | /* |
| 6075 | * We may be facing a 100-continue response, in which case this |
| 6076 | * is not the right response, and we're waiting for the next one. |
| 6077 | * Let's allow this response to go to the client and wait for the |
| 6078 | * next one. |
| 6079 | */ |
| 6080 | hdr_idx_init(&txn->hdr_idx); |
| 6081 | msg->next -= channel_forward(rep, msg->next); |
| 6082 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6083 | txn->status = 0; |
| 6084 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6085 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6086 | goto next_one; |
| 6087 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6088 | case 200: |
| 6089 | case 203: |
| 6090 | case 206: |
| 6091 | case 300: |
| 6092 | case 301: |
| 6093 | case 410: |
| 6094 | /* RFC2616 @13.4: |
| 6095 | * "A response received with a status code of |
| 6096 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6097 | * by a cache (...) unless a cache-control |
| 6098 | * directive prohibits caching." |
| 6099 | * |
| 6100 | * RFC2616 @9.5: POST method : |
| 6101 | * "Responses to this method are not cacheable, |
| 6102 | * unless the response includes appropriate |
| 6103 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6104 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6105 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6106 | ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6107 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6108 | break; |
| 6109 | default: |
| 6110 | break; |
| 6111 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6112 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6113 | /* |
| 6114 | * 3: we may need to capture headers |
| 6115 | */ |
| 6116 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6117 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6118 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6119 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6120 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6121 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6122 | * by RFC7230#3.3.3 : |
| 6123 | * |
| 6124 | * The length of a message body is determined by one of the following |
| 6125 | * (in order of precedence): |
| 6126 | * |
| 6127 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6128 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6129 | * code is always terminated by the first empty line after the |
| 6130 | * header fields, regardless of the header fields present in the |
| 6131 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6132 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6133 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6134 | * the connection will become a tunnel immediately after the empty |
| 6135 | * line that concludes the header fields. A client MUST ignore any |
| 6136 | * Content-Length or Transfer-Encoding header fields received in |
| 6137 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6138 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6139 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6140 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6141 | * body length is determined by reading and decoding the chunked |
| 6142 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6143 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6144 | * If a Transfer-Encoding header field is present in a response and |
| 6145 | * the chunked transfer coding is not the final encoding, the |
| 6146 | * message body length is determined by reading the connection until |
| 6147 | * it is closed by the server. If a Transfer-Encoding header field |
| 6148 | * is present in a request and the chunked transfer coding is not |
| 6149 | * the final encoding, the message body length cannot be determined |
| 6150 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6151 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6152 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6153 | * If a message is received with both a Transfer-Encoding and a |
| 6154 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6155 | * Content-Length. Such a message might indicate an attempt to |
| 6156 | * perform request smuggling (Section 9.5) or response splitting |
| 6157 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6158 | * remove the received Content-Length field prior to forwarding such |
| 6159 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6160 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6161 | * 4. If a message is received without Transfer-Encoding and with |
| 6162 | * either multiple Content-Length header fields having differing |
| 6163 | * field-values or a single Content-Length header field having an |
| 6164 | * invalid value, then the message framing is invalid and the |
| 6165 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6166 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6167 | * status code and then close the connection. If this is a response |
| 6168 | * message received by a proxy, the proxy MUST close the connection |
| 6169 | * to the server, discard the received response, and send a 502 (Bad |
| 6170 | * Gateway) response to the client. If this is a response message |
| 6171 | * received by a user agent, the user agent MUST close the |
| 6172 | * connection to the server and discard the received response. |
| 6173 | * |
| 6174 | * 5. If a valid Content-Length header field is present without |
| 6175 | * Transfer-Encoding, its decimal value defines the expected message |
| 6176 | * body length in octets. If the sender closes the connection or |
| 6177 | * the recipient times out before the indicated number of octets are |
| 6178 | * received, the recipient MUST consider the message to be |
| 6179 | * incomplete and close the connection. |
| 6180 | * |
| 6181 | * 6. If this is a request message and none of the above are true, then |
| 6182 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6183 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6184 | * 7. Otherwise, this is a response message without a declared message |
| 6185 | * body length, so the message body length is determined by the |
| 6186 | * number of octets received prior to the server closing the |
| 6187 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6188 | */ |
| 6189 | |
| 6190 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6191 | * remain 0 as well as the chunk_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6192 | * the real header value, and we note that we know the response's length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6193 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6194 | */ |
| 6195 | if (txn->meth == HTTP_METH_HEAD || |
| 6196 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6197 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6198 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6199 | goto skip_content_length; |
| 6200 | } |
| 6201 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6202 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6203 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6204 | while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6205 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6206 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6207 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6208 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6209 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6210 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6211 | break; |
| 6212 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6213 | } |
| 6214 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6215 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6216 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6217 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6218 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6219 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6220 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6221 | else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6222 | signed long long cl; |
| 6223 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6224 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6225 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6226 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6227 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6228 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6229 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6230 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6231 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6232 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6233 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6234 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6235 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6236 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6237 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6238 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6239 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6240 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6241 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6242 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6243 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6244 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6245 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6249 | /* Now we have to check if we need to modify the Connection header. |
| 6250 | * This is more difficult on the response than it is on the request, |
| 6251 | * because we can have two different HTTP versions and we don't know |
| 6252 | * how the client will interprete a response. For instance, let's say |
| 6253 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6254 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6255 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6256 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6257 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6258 | * indicating how a request MAY be understood by the client. In case |
| 6259 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6260 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6261 | * will fall back to explicit close. Note that we won't take risks with |
| 6262 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6263 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6264 | */ |
| 6265 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6266 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6267 | txn->status == 101)) { |
| 6268 | /* Either we've established an explicit tunnel, or we're |
| 6269 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6270 | * to understand the next protocols. We have to switch to tunnel |
| 6271 | * mode, so that we transfer the request and responses then let |
| 6272 | * this protocol pass unmodified. When we later implement specific |
| 6273 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6274 | * header which contains information about that protocol for |
| 6275 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6276 | */ |
| 6277 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6278 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6279 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6280 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6281 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6282 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6283 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6284 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6285 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6286 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6287 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6288 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6289 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6290 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6291 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6292 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6293 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6294 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6295 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6296 | /* now adjust header transformations depending on current state */ |
| 6297 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6298 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6299 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6300 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6301 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6302 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6303 | else { /* SCL / KAL */ |
| 6304 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6305 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6306 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6307 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6308 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6309 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6310 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6311 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6312 | /* Some keep-alive responses are converted to Server-close if |
| 6313 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6314 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6315 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6316 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6317 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6318 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6319 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6320 | } |
| 6321 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6322 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6323 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6324 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6325 | /* end of job, return OK */ |
| 6326 | rep->analysers &= ~an_bit; |
| 6327 | rep->analyse_exp = TICK_ETERNITY; |
| 6328 | channel_auto_close(rep); |
| 6329 | return 1; |
| 6330 | |
| 6331 | abort_keep_alive: |
| 6332 | /* A keep-alive request to the server failed on a network error. |
| 6333 | * The client is required to retry. We need to close without returning |
| 6334 | * any other information so that the client retries. |
| 6335 | */ |
| 6336 | txn->status = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6337 | rep->analysers &= AN_FLT_END; |
| 6338 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6339 | channel_auto_close(rep); |
| 6340 | s->logs.logwait = 0; |
| 6341 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6342 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6343 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6344 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6345 | return 0; |
| 6346 | } |
| 6347 | |
| 6348 | /* This function performs all the processing enabled for the current response. |
| 6349 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6350 | * and updates s->res.analysers. It might make sense to explode it into several |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6351 | * other functions. It works like process_request (see indications above). |
| 6352 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6353 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6354 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6355 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6356 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6357 | struct http_msg *msg = &txn->rsp; |
| 6358 | struct proxy *cur_proxy; |
| 6359 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6360 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6361 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6362 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6363 | now_ms, __FUNCTION__, |
| 6364 | s, |
| 6365 | rep, |
| 6366 | rep->rex, rep->wex, |
| 6367 | rep->flags, |
| 6368 | rep->buf->i, |
| 6369 | rep->analysers); |
| 6370 | |
| 6371 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6372 | return 0; |
| 6373 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6374 | /* The stats applet needs to adjust the Connection header but we don't |
| 6375 | * apply any filter there. |
| 6376 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6377 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6378 | rep->analysers &= ~an_bit; |
| 6379 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6380 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6381 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6382 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6383 | /* |
| 6384 | * We will have to evaluate the filters. |
| 6385 | * As opposed to version 1.2, now they will be evaluated in the |
| 6386 | * filters order and not in the header order. This means that |
| 6387 | * each filter has to be validated among all headers. |
| 6388 | * |
| 6389 | * Filters are tried with ->be first, then with ->fe if it is |
| 6390 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6391 | * |
| 6392 | * Maybe we are in resume condiion. In this case I choose the |
| 6393 | * "struct proxy" which contains the rule list matching the resume |
| 6394 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6395 | * the process with the first one. |
| 6396 | * |
| 6397 | * In fact, I check only correspondance betwwen the current list |
| 6398 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6399 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6400 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6401 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6402 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6403 | else |
| 6404 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6405 | while (1) { |
| 6406 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6407 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6408 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6409 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6410 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 6411 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6412 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6413 | goto return_srv_prx_502; |
| 6414 | |
| 6415 | if (ret == HTTP_RULE_RES_DONE) { |
| 6416 | rep->analysers &= ~an_bit; |
| 6417 | rep->analyse_exp = TICK_ETERNITY; |
| 6418 | return 1; |
| 6419 | } |
| 6420 | } |
| 6421 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6422 | /* we need to be called again. */ |
| 6423 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6424 | channel_dont_close(rep); |
| 6425 | return 0; |
| 6426 | } |
| 6427 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6428 | /* try headers filters */ |
| 6429 | if (rule_set->rsp_exp != NULL) { |
| 6430 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6431 | return_bad_resp: |
| 6432 | if (objt_server(s->target)) { |
| 6433 | objt_server(s->target)->counters.failed_resp++; |
| 6434 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6435 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6436 | s->be->be_counters.failed_resp++; |
| 6437 | return_srv_prx_502: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6438 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6439 | txn->status = 502; |
| 6440 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6441 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6442 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6443 | http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6444 | if (!(s->flags & SF_ERR_MASK)) |
| 6445 | s->flags |= SF_ERR_PRXCOND; |
| 6446 | if (!(s->flags & SF_FINST_MASK)) |
| 6447 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6448 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6449 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6450 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6451 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6452 | /* has the response been denied ? */ |
| 6453 | if (txn->flags & TX_SVDENY) { |
| 6454 | if (objt_server(s->target)) |
| 6455 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6456 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6457 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6458 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6459 | if (sess->listener->counters) |
| 6460 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6461 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6462 | goto return_srv_prx_502; |
| 6463 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6464 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6465 | /* add response headers from the rule sets in the same order */ |
| 6466 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6467 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6468 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6469 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6470 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6471 | ret = acl_pass(ret); |
| 6472 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6473 | ret = !ret; |
| 6474 | if (!ret) |
| 6475 | continue; |
| 6476 | } |
| 6477 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6478 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6479 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6480 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6481 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6482 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6483 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6484 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6485 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6486 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6487 | /* After this point, this anayzer can't return yield, so we can |
| 6488 | * remove the bit corresponding to this analyzer from the list. |
| 6489 | * |
| 6490 | * Note that the intermediate returns and goto found previously |
| 6491 | * reset the analyzers. |
| 6492 | */ |
| 6493 | rep->analysers &= ~an_bit; |
| 6494 | rep->analyse_exp = TICK_ETERNITY; |
| 6495 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6496 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6497 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6498 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6499 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6500 | /* |
| 6501 | * Now check for a server cookie. |
| 6502 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6503 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6504 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6505 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6506 | /* |
| 6507 | * Check for cache-control or pragma headers if required. |
| 6508 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6509 | if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6510 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6511 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6512 | /* |
| 6513 | * Add server cookie in the response if needed |
| 6514 | */ |
| 6515 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6516 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6517 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6518 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6519 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6520 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6521 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6522 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6523 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6524 | /* the server is known, it's not the one the client requested, or the |
| 6525 | * cookie's last seen date needs to be refreshed. We have to |
| 6526 | * insert a set-cookie here, except if we want to insert only on POST |
| 6527 | * requests and this one isn't. Note that servers which don't have cookies |
| 6528 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6529 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6530 | if (!objt_server(s->target)->cookie) { |
| 6531 | chunk_printf(&trash, |
| 6532 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6533 | s->be->cookie_name); |
| 6534 | } |
| 6535 | else { |
| 6536 | chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6537 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6538 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6539 | /* emit last_date, which is mandatory */ |
| 6540 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6541 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6542 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6543 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6544 | if (s->be->cookie_maxlife) { |
| 6545 | /* emit first_date, which is either the original one or |
| 6546 | * the current date. |
| 6547 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6548 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6549 | s30tob64(txn->cookie_first_date ? |
| 6550 | txn->cookie_first_date >> 2 : |
| 6551 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6552 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6553 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6554 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6555 | chunk_appendf(&trash, "; path=/"); |
| 6556 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6557 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6558 | if (s->be->cookie_domain) |
| 6559 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6560 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6561 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6562 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6563 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6564 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6565 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6566 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6567 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6568 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6569 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6570 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6571 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6572 | /* the server did not change, only the date was updated */ |
| 6573 | txn->flags |= TX_SCK_UPDATED; |
| 6574 | else |
| 6575 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6576 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6577 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6578 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6579 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6580 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6581 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6582 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6583 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6584 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6585 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6586 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6587 | "Cache-control: private", 22) < 0)) |
| 6588 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6589 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6590 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6591 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6592 | /* |
| 6593 | * Check if result will be cacheable with a cookie. |
| 6594 | * We'll block the response if security checks have caught |
| 6595 | * nasty things such as a cacheable cookie. |
| 6596 | */ |
| 6597 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6598 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6599 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6600 | /* we're in presence of a cacheable response containing |
| 6601 | * a set-cookie header. We'll block it as requested by |
| 6602 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6603 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6604 | if (objt_server(s->target)) |
| 6605 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6606 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6607 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6608 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6609 | if (sess->listener->counters) |
| 6610 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6611 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6612 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6613 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6614 | send_log(s->be, LOG_ALERT, |
| 6615 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6616 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6617 | goto return_srv_prx_502; |
| 6618 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6619 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6620 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6621 | /* |
| 6622 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6623 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6624 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6625 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6626 | * want to touch any 101 response either since it's switching to another |
| 6627 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6628 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6629 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6630 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6631 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6632 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6633 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6634 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6635 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6636 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6637 | /* we want a keep-alive response here. Keep-alive header |
| 6638 | * required if either side is not 1.1. |
| 6639 | */ |
| 6640 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6641 | want_flags |= TX_CON_KAL_SET; |
| 6642 | } |
| 6643 | else { |
| 6644 | /* we want a close response here. Close header required if |
| 6645 | * the server is 1.1, regardless of the client. |
| 6646 | */ |
| 6647 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6648 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6649 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6650 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6651 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6652 | http_change_connection_header(txn, msg, want_flags); |
| 6653 | } |
| 6654 | |
| 6655 | skip_header_mangling: |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6656 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6657 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 6658 | rep->analysers &= ~AN_FLT_XFER_DATA; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6659 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6660 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6661 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6662 | /* if the user wants to log as soon as possible, without counting |
| 6663 | * bytes from the server, then this is the right moment. We have |
| 6664 | * to temporarily assign bytes_out to log what we currently have. |
| 6665 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6666 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6667 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6668 | s->logs.bytes_out = txn->rsp.eoh; |
| 6669 | s->do_log(s); |
| 6670 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6671 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6672 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6673 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6674 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6675 | /* This function is an analyser which forwards response body (including chunk |
| 6676 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6677 | * zero byte. The only situation where it must not be called is when we're in |
| 6678 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6679 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6680 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6681 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 6682 | * |
| 6683 | * It is capable of compressing response data both in content-length mode and |
| 6684 | * in chunked mode. The state machines follows different flows depending on |
| 6685 | * whether content-length and chunked modes are used, since there are no |
| 6686 | * trailers in content-length : |
| 6687 | * |
| 6688 | * chk-mode cl-mode |
| 6689 | * ,----- BODY -----. |
| 6690 | * / \ |
| 6691 | * V size > 0 V chk-mode |
| 6692 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6693 | * | | size == 0 | last byte | |
| 6694 | * | v final crlf v inspected | |
| 6695 | * | TRAILERS -----------> DONE | |
| 6696 | * | | |
| 6697 | * `----------------------------------------------' |
| 6698 | * |
| 6699 | * Compression only happens in the DATA state, and must be flushed in final |
| 6700 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6701 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6702 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6703 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6704 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6705 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6706 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6707 | struct http_txn *txn = s->txn; |
| 6708 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6709 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6710 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6711 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6712 | return 0; |
| 6713 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6714 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6715 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6716 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6717 | /* Output closed while we were sending data. We must abort and |
| 6718 | * wake the other side up. |
| 6719 | */ |
| 6720 | msg->msg_state = HTTP_MSG_ERROR; |
| 6721 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6722 | return 1; |
| 6723 | } |
| 6724 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6725 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6726 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6727 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6728 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6729 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6730 | ? HTTP_MSG_CHUNK_SIZE |
| 6731 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6732 | } |
| 6733 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6734 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6735 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6736 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6737 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6738 | } |
| 6739 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6740 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6741 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6742 | ? http_msg_forward_chunked_body(s, msg) |
| 6743 | : http_msg_forward_body(s, msg)); |
| 6744 | if (!ret) |
| 6745 | goto missing_data_or_waiting; |
| 6746 | if (ret < 0) |
| 6747 | goto return_bad_res; |
| 6748 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6749 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6750 | /* other states, DONE...TUNNEL */ |
| 6751 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6752 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6753 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6754 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6755 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6756 | ret = msg->msg_state; |
| 6757 | if (http_resync_states(s)) { |
| 6758 | /* some state changes occurred, maybe the analyser was disabled |
| 6759 | * too. */ |
| 6760 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6761 | if (res->flags & CF_SHUTW) { |
| 6762 | /* response errors are most likely due to the |
| 6763 | * client aborting the transfer. */ |
| 6764 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6765 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6766 | if (msg->err_pos >= 0) |
| 6767 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s)); |
| 6768 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6769 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6770 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6771 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6772 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6773 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6774 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6775 | if (res->flags & CF_SHUTW) |
| 6776 | goto aborted_xfer; |
| 6777 | |
| 6778 | /* stop waiting for data if the input is closed before the end. If the |
| 6779 | * client side was already closed, it means that the client has aborted, |
| 6780 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6781 | * server abort. |
| 6782 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6783 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6784 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6785 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6786 | /* If we have some pending data, we continue the processing */ |
| 6787 | if (!buffer_pending(res->buf)) { |
| 6788 | if (!(s->flags & SF_ERR_MASK)) |
| 6789 | s->flags |= SF_ERR_SRVCL; |
| 6790 | s->be->be_counters.srv_aborts++; |
| 6791 | if (objt_server(s->target)) |
| 6792 | objt_server(s->target)->counters.srv_aborts++; |
| 6793 | goto return_bad_res_stats_ok; |
| 6794 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6795 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6796 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6797 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6798 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6799 | goto return_bad_res; |
| 6800 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6801 | /* When TE: chunked is used, we need to get there again to parse |
| 6802 | * remaining chunks even if the server has closed, so we don't want to |
| 6803 | * set CF_DONTCLOSE. Similarly, if the body length is undefined, if |
| 6804 | * keep-alive is set on the client side or if there are filters |
| 6805 | * registered on the stream, we don't want to forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6806 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6807 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len || |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6808 | HAS_FILTERS(s) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6809 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6810 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6811 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6812 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6813 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6814 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 6815 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 6816 | * modes are already handled by the stream sock layer. We must not do |
| 6817 | * this in content-length mode because it could present the MSG_MORE |
| 6818 | * flag with the last block of forwarded data, which would cause an |
| 6819 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6820 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6821 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6822 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6823 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6824 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6825 | return 0; |
| 6826 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6827 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6828 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6829 | if (objt_server(s->target)) |
| 6830 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6831 | |
| 6832 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6833 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6834 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6835 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6836 | res->analysers &= AN_FLT_END; |
| 6837 | s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6838 | if (objt_server(s->target)) |
| 6839 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6840 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6841 | if (!(s->flags & SF_ERR_MASK)) |
| 6842 | s->flags |= SF_ERR_PRXCOND; |
| 6843 | if (!(s->flags & SF_FINST_MASK)) |
| 6844 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6845 | return 0; |
| 6846 | |
| 6847 | aborted_xfer: |
| 6848 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6849 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6850 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6851 | res->analysers &= AN_FLT_END; |
| 6852 | s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6853 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6854 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6855 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6856 | if (objt_server(s->target)) |
| 6857 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6858 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6859 | if (!(s->flags & SF_ERR_MASK)) |
| 6860 | s->flags |= SF_ERR_CLICL; |
| 6861 | if (!(s->flags & SF_FINST_MASK)) |
| 6862 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6863 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6864 | } |
| 6865 | |
| 6866 | |
| 6867 | static inline int |
| 6868 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6869 | { |
| 6870 | struct channel *chn = msg->chn; |
| 6871 | int ret; |
| 6872 | |
| 6873 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6874 | |
| 6875 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6876 | goto ending; |
| 6877 | |
| 6878 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6879 | * read the body until the server connection is closed. In that case, we |
| 6880 | * eat data as they come. Of course, this happens for response only. */ |
| 6881 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6882 | unsigned long long len = (chn->buf->i - msg->next); |
| 6883 | msg->chunk_len += len; |
| 6884 | msg->body_len += len; |
| 6885 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6886 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6887 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6888 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6889 | msg->next += ret; |
| 6890 | msg->chunk_len -= ret; |
| 6891 | if (msg->chunk_len) { |
| 6892 | /* input empty or output full */ |
| 6893 | if (chn->buf->i > msg->next) |
| 6894 | chn->flags |= CF_WAKE_WRITE; |
| 6895 | goto missing_data_or_waiting; |
| 6896 | } |
| 6897 | |
| 6898 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) { |
| 6899 | /* The server still sending data */ |
| 6900 | goto missing_data_or_waiting; |
| 6901 | } |
| 6902 | msg->msg_state = HTTP_MSG_ENDING; |
| 6903 | |
| 6904 | ending: |
| 6905 | /* we may have some pending data starting at res->buf->p such as a last |
| 6906 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6907 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6908 | /* default_ret */ msg->next, |
| 6909 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6910 | b_adv(chn->buf, ret); |
| 6911 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6912 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6913 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6914 | if (msg->next) |
| 6915 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6916 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6917 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6918 | /* default_ret */ 1, |
| 6919 | /* on_error */ goto error, |
| 6920 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6921 | msg->msg_state = HTTP_MSG_DONE; |
| 6922 | return 1; |
| 6923 | |
| 6924 | missing_data_or_waiting: |
| 6925 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6926 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6927 | /* default_ret */ msg->next, |
| 6928 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6929 | b_adv(chn->buf, ret); |
| 6930 | msg->next -= ret; |
| 6931 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6932 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6933 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6934 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6935 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6936 | return 0; |
| 6937 | error: |
| 6938 | return -1; |
| 6939 | } |
| 6940 | |
| 6941 | static inline int |
| 6942 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6943 | { |
| 6944 | struct channel *chn = msg->chn; |
| 6945 | int ret; |
| 6946 | |
| 6947 | /* Here we have the guarantee to be in one of the following state: |
| 6948 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6949 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6950 | |
| 6951 | switch_states: |
| 6952 | switch (msg->msg_state) { |
| 6953 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6954 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6955 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6956 | /* on_error */ goto error); |
| 6957 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6958 | msg->chunk_len -= ret; |
| 6959 | if (msg->chunk_len) { |
| 6960 | /* input empty or output full */ |
| 6961 | if (chn->buf->i > msg->next) |
| 6962 | chn->flags |= CF_WAKE_WRITE; |
| 6963 | goto missing_data_or_waiting; |
| 6964 | } |
| 6965 | |
| 6966 | /* nothing left to forward for this chunk*/ |
| 6967 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6968 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6969 | |
| 6970 | case HTTP_MSG_CHUNK_CRLF: |
| 6971 | /* we want the CRLF after the data */ |
| 6972 | ret = http_skip_chunk_crlf(msg); |
| 6973 | if (ret == 0) |
| 6974 | goto missing_data_or_waiting; |
| 6975 | if (ret < 0) |
| 6976 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6977 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6978 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6979 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6980 | |
| 6981 | case HTTP_MSG_CHUNK_SIZE: |
| 6982 | /* read the chunk size and assign it to ->chunk_len, |
| 6983 | * then set ->next to point to the body and switch to |
| 6984 | * DATA or TRAILERS state. |
| 6985 | */ |
| 6986 | ret = http_parse_chunk_size(msg); |
| 6987 | if (ret == 0) |
| 6988 | goto missing_data_or_waiting; |
| 6989 | if (ret < 0) |
| 6990 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6991 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6992 | if (msg->chunk_len) { |
| 6993 | msg->msg_state = HTTP_MSG_DATA; |
| 6994 | goto switch_states; |
| 6995 | } |
| 6996 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6997 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6998 | |
| 6999 | case HTTP_MSG_TRAILERS: |
| 7000 | ret = http_forward_trailers(msg); |
| 7001 | if (ret < 0) |
| 7002 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7003 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 7004 | /* default_ret */ 1, |
| 7005 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7006 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7007 | if (!ret) |
| 7008 | goto missing_data_or_waiting; |
| 7009 | break; |
| 7010 | |
| 7011 | case HTTP_MSG_ENDING: |
| 7012 | goto ending; |
| 7013 | |
| 7014 | default: |
| 7015 | /* This should no happen in this function */ |
| 7016 | goto error; |
| 7017 | } |
| 7018 | |
| 7019 | msg->msg_state = HTTP_MSG_ENDING; |
| 7020 | ending: |
| 7021 | /* we may have some pending data starting at res->buf->p such as a last |
| 7022 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7023 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7024 | /* default_ret */ msg->next, |
| 7025 | /* on_error */ goto error); |
| 7026 | b_adv(chn->buf, ret); |
| 7027 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7028 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7029 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7030 | if (msg->next) |
| 7031 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7032 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7033 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7034 | /* default_ret */ 1, |
| 7035 | /* on_error */ goto error, |
| 7036 | /* on_wait */ goto waiting); |
| 7037 | msg->msg_state = HTTP_MSG_DONE; |
| 7038 | return 1; |
| 7039 | |
| 7040 | missing_data_or_waiting: |
| 7041 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7042 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7043 | /* default_ret */ msg->next, |
| 7044 | /* on_error */ goto error); |
| 7045 | b_adv(chn->buf, ret); |
| 7046 | msg->next -= ret; |
| 7047 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7048 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7049 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7050 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7051 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7052 | return 0; |
| 7053 | |
| 7054 | chunk_parsing_error: |
| 7055 | if (msg->err_pos >= 0) { |
| 7056 | if (chn->flags & CF_ISRESP) |
| 7057 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 7058 | msg->msg_state, strm_fe(s)); |
| 7059 | else |
| 7060 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 7061 | msg, msg->msg_state, s->be); |
| 7062 | } |
| 7063 | error: |
| 7064 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7065 | } |
| 7066 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7067 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7068 | /* Iterate the same filter through all request headers. |
| 7069 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7070 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7071 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7073 | int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7074 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7075 | char *cur_ptr, *cur_end, *cur_next; |
| 7076 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7077 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7078 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7079 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7081 | last_hdr = 0; |
| 7082 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7083 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7084 | old_idx = 0; |
| 7085 | |
| 7086 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7087 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7088 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7089 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7090 | (exp->action == ACT_ALLOW || |
| 7091 | exp->action == ACT_DENY || |
| 7092 | exp->action == ACT_TARPIT)) |
| 7093 | return 0; |
| 7094 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7095 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7096 | if (!cur_idx) |
| 7097 | break; |
| 7098 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7099 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7100 | cur_ptr = cur_next; |
| 7101 | cur_end = cur_ptr + cur_hdr->len; |
| 7102 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7103 | |
| 7104 | /* Now we have one header between cur_ptr and cur_end, |
| 7105 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7106 | */ |
| 7107 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7108 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7109 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7110 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7111 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7112 | last_hdr = 1; |
| 7113 | break; |
| 7114 | |
| 7115 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7116 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7117 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7118 | break; |
| 7119 | |
| 7120 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7121 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7122 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7123 | break; |
| 7124 | |
| 7125 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7126 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7127 | if (trash.len < 0) |
| 7128 | return -1; |
| 7129 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7130 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7131 | /* FIXME: if the user adds a newline in the replacement, the |
| 7132 | * index will not be recalculated for now, and the new line |
| 7133 | * will not be counted as a new header. |
| 7134 | */ |
| 7135 | |
| 7136 | cur_end += delta; |
| 7137 | cur_next += delta; |
| 7138 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7139 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7140 | break; |
| 7141 | |
| 7142 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7143 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7144 | cur_next += delta; |
| 7145 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7146 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7147 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7148 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7149 | cur_hdr->len = 0; |
| 7150 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7151 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7152 | break; |
| 7153 | |
| 7154 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7155 | } |
| 7156 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7157 | /* keep the link from this header to next one in case of later |
| 7158 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7159 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7160 | old_idx = cur_idx; |
| 7161 | } |
| 7162 | return 0; |
| 7163 | } |
| 7164 | |
| 7165 | |
| 7166 | /* Apply the filter to the request line. |
| 7167 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7168 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7169 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7170 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7171 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7172 | int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7173 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7174 | char *cur_ptr, *cur_end; |
| 7175 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7176 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7177 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7178 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7179 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7180 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7181 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7182 | (exp->action == ACT_ALLOW || |
| 7183 | exp->action == ACT_DENY || |
| 7184 | exp->action == ACT_TARPIT)) |
| 7185 | return 0; |
| 7186 | else if (exp->action == ACT_REMOVE) |
| 7187 | return 0; |
| 7188 | |
| 7189 | done = 0; |
| 7190 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7191 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7192 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7193 | |
| 7194 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7195 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7196 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7197 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7198 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7199 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7200 | done = 1; |
| 7201 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7202 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7203 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7204 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7205 | done = 1; |
| 7206 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7207 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7208 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7209 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7210 | done = 1; |
| 7211 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7212 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7213 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7214 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7215 | if (trash.len < 0) |
| 7216 | return -1; |
| 7217 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7218 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7219 | /* FIXME: if the user adds a newline in the replacement, the |
| 7220 | * index will not be recalculated for now, and the new line |
| 7221 | * will not be counted as a new header. |
| 7222 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7223 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7224 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7225 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7226 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7227 | HTTP_MSG_RQMETH, |
| 7228 | cur_ptr, cur_end + 1, |
| 7229 | NULL, NULL); |
| 7230 | if (unlikely(!cur_end)) |
| 7231 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7232 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7233 | /* we have a full request and we know that we have either a CR |
| 7234 | * or an LF at <ptr>. |
| 7235 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7236 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7237 | hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r'); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7238 | /* there is no point trying this regex on headers */ |
| 7239 | return 1; |
| 7240 | } |
| 7241 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7242 | return done; |
| 7243 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7244 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7245 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7247 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7248 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7249 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7250 | * unparsable request. Since it can manage the switch to another backend, it |
| 7251 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7252 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7253 | int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7254 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7255 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7256 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7257 | struct hdr_exp *exp; |
| 7258 | |
| 7259 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7260 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7262 | /* |
| 7263 | * The interleaving of transformations and verdicts |
| 7264 | * makes it difficult to decide to continue or stop |
| 7265 | * the evaluation. |
| 7266 | */ |
| 7267 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7268 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7269 | break; |
| 7270 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7271 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7272 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7273 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7274 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7275 | |
| 7276 | /* if this filter had a condition, evaluate it now and skip to |
| 7277 | * next filter if the condition does not match. |
| 7278 | */ |
| 7279 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7280 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7281 | ret = acl_pass(ret); |
| 7282 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7283 | ret = !ret; |
| 7284 | |
| 7285 | if (!ret) |
| 7286 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7287 | } |
| 7288 | |
| 7289 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7290 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7291 | if (unlikely(ret < 0)) |
| 7292 | return -1; |
| 7293 | |
| 7294 | if (likely(ret == 0)) { |
| 7295 | /* The filter did not match the request, it can be |
| 7296 | * iterated through all headers. |
| 7297 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7298 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7299 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7300 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7301 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7302 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7303 | } |
| 7304 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7305 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7306 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7307 | * same way as with headers above except that the semi-colon also ends a token. |
| 7308 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7309 | * return a valid result. |
| 7310 | */ |
| 7311 | char *find_cookie_value_end(char *s, const char *e) |
| 7312 | { |
| 7313 | int quoted, qdpair; |
| 7314 | |
| 7315 | quoted = qdpair = 0; |
| 7316 | for (; s < e; s++) { |
| 7317 | if (qdpair) qdpair = 0; |
| 7318 | else if (quoted) { |
| 7319 | if (*s == '\\') qdpair = 1; |
| 7320 | else if (*s == '"') quoted = 0; |
| 7321 | } |
| 7322 | else if (*s == '"') quoted = 1; |
| 7323 | else if (*s == ',' || *s == ';') return s; |
| 7324 | } |
| 7325 | return s; |
| 7326 | } |
| 7327 | |
| 7328 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7329 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7330 | * the first delimiter is updated if required. The function tries as much as |
| 7331 | * possible to respect the following principles : |
| 7332 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7333 | * colon, in which case <next> is simply removed |
| 7334 | * - set exactly one space character after the new first delimiter, unless |
| 7335 | * there are not enough characters in the block being moved to do so. |
| 7336 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7337 | * one. |
| 7338 | * |
| 7339 | * It is the caller's responsibility to ensure that : |
| 7340 | * - <from> points to a valid delimiter or the colon ; |
| 7341 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7342 | * - there are non-space chars before <from> ; |
| 7343 | * - there is a CR/LF at or after <next>. |
| 7344 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7345 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7346 | { |
| 7347 | char *prev = *from; |
| 7348 | |
| 7349 | if (*prev == ':') { |
| 7350 | /* We're removing the first value, preserve the colon and add a |
| 7351 | * space if possible. |
| 7352 | */ |
| 7353 | if (!http_is_crlf[(unsigned char)*next]) |
| 7354 | next++; |
| 7355 | prev++; |
| 7356 | if (prev < next) |
| 7357 | *prev++ = ' '; |
| 7358 | |
| 7359 | while (http_is_spht[(unsigned char)*next]) |
| 7360 | next++; |
| 7361 | } else { |
| 7362 | /* Remove useless spaces before the old delimiter. */ |
| 7363 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7364 | prev--; |
| 7365 | *from = prev; |
| 7366 | |
| 7367 | /* copy the delimiter and if possible a space if we're |
| 7368 | * not at the end of the line. |
| 7369 | */ |
| 7370 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7371 | *prev++ = *next++; |
| 7372 | if (prev + 1 < next) |
| 7373 | *prev++ = ' '; |
| 7374 | while (http_is_spht[(unsigned char)*next]) |
| 7375 | next++; |
| 7376 | } |
| 7377 | } |
| 7378 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7379 | } |
| 7380 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7381 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7382 | * Manage client-side cookie. It can impact performance by about 2% so it is |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7383 | * desirable to call it only when needed. This code is quite complex because |
| 7384 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7385 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7386 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7387 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7388 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7389 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7390 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7391 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7392 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7393 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7394 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7395 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7396 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7397 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7398 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7399 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7400 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7401 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7402 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7403 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7404 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7405 | hdr_beg = hdr_next; |
| 7406 | hdr_end = hdr_beg + cur_hdr->len; |
| 7407 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7408 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7409 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7410 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7411 | * "Cookie:" headers. |
| 7412 | */ |
| 7413 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7414 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7415 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7416 | old_idx = cur_idx; |
| 7417 | continue; |
| 7418 | } |
| 7419 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7420 | del_from = NULL; /* nothing to be deleted */ |
| 7421 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7422 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7423 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7424 | * attributes whose name begin with a '$', and associate them with |
| 7425 | * the right cookie, if we want to delete this cookie. |
| 7426 | * So there are 3 cases for each cookie read : |
| 7427 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7428 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7429 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7430 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7431 | * "special" cookie. |
| 7432 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7433 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7434 | * *MUST* delete it. |
| 7435 | * |
| 7436 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7437 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7438 | * the RFC explicitly allows spaces before it, and not within the |
| 7439 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7440 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7441 | * after the equal sign too, resulting in some (rare) buggy |
| 7442 | * implementations trying to do that. So let's do what servers do. |
| 7443 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7444 | * allowed quoted strings in values, with any possible character |
| 7445 | * after a backslash, including control chars and delimitors, which |
| 7446 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7447 | * within values even without quotes. |
| 7448 | * |
| 7449 | * We have to keep multiple pointers in order to support cookie |
| 7450 | * removal at the beginning, middle or end of header without |
| 7451 | * corrupting the header. All of these headers are valid : |
| 7452 | * |
| 7453 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7454 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7455 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7456 | * | | | | | | | | | |
| 7457 | * | | | | | | | | hdr_end <--+ |
| 7458 | * | | | | | | | +--> next |
| 7459 | * | | | | | | +----> val_end |
| 7460 | * | | | | | +-----------> val_beg |
| 7461 | * | | | | +--------------> equal |
| 7462 | * | | | +----------------> att_end |
| 7463 | * | | +---------------------> att_beg |
| 7464 | * | +--------------------------> prev |
| 7465 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7466 | */ |
| 7467 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7468 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7469 | /* Iterate through all cookies on this line */ |
| 7470 | |
| 7471 | /* find att_beg */ |
| 7472 | att_beg = prev + 1; |
| 7473 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7474 | att_beg++; |
| 7475 | |
| 7476 | /* find att_end : this is the first character after the last non |
| 7477 | * space before the equal. It may be equal to hdr_end. |
| 7478 | */ |
| 7479 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7480 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7481 | while (equal < hdr_end) { |
| 7482 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7483 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7484 | if (http_is_spht[(unsigned char)*equal++]) |
| 7485 | continue; |
| 7486 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7487 | } |
| 7488 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7489 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7490 | * is between <att_beg> and <equal>, both may be identical. |
| 7491 | */ |
| 7492 | |
| 7493 | /* look for end of cookie if there is an equal sign */ |
| 7494 | if (equal < hdr_end && *equal == '=') { |
| 7495 | /* look for the beginning of the value */ |
| 7496 | val_beg = equal + 1; |
| 7497 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7498 | val_beg++; |
| 7499 | |
| 7500 | /* find the end of the value, respecting quotes */ |
| 7501 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7502 | |
| 7503 | /* make val_end point to the first white space or delimitor after the value */ |
| 7504 | val_end = next; |
| 7505 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7506 | val_end--; |
| 7507 | } else { |
| 7508 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7509 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7510 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7511 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7512 | * they will automatically be removed if a header before them is removed, |
| 7513 | * since they're supposed to be linked together. |
| 7514 | */ |
| 7515 | if (*att_beg == '$') |
| 7516 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7517 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7518 | /* Ignore cookies with no equal sign */ |
| 7519 | if (equal == next) { |
| 7520 | /* This is not our cookie, so we must preserve it. But if we already |
| 7521 | * scheduled another cookie for removal, we cannot remove the |
| 7522 | * complete header, but we can remove the previous block itself. |
| 7523 | */ |
| 7524 | preserve_hdr = 1; |
| 7525 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7526 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7527 | val_end += delta; |
| 7528 | next += delta; |
| 7529 | hdr_end += delta; |
| 7530 | hdr_next += delta; |
| 7531 | cur_hdr->len += delta; |
| 7532 | http_msg_move_end(&txn->req, delta); |
| 7533 | prev = del_from; |
| 7534 | del_from = NULL; |
| 7535 | } |
| 7536 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7537 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7538 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7539 | /* if there are spaces around the equal sign, we need to |
| 7540 | * strip them otherwise we'll get trouble for cookie captures, |
| 7541 | * or even for rewrites. Since this happens extremely rarely, |
| 7542 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7543 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7544 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7545 | int stripped_before = 0; |
| 7546 | int stripped_after = 0; |
| 7547 | |
| 7548 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7549 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7550 | equal += stripped_before; |
| 7551 | val_beg += stripped_before; |
| 7552 | } |
| 7553 | |
| 7554 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7555 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7556 | val_beg += stripped_after; |
| 7557 | stripped_before += stripped_after; |
| 7558 | } |
| 7559 | |
| 7560 | val_end += stripped_before; |
| 7561 | next += stripped_before; |
| 7562 | hdr_end += stripped_before; |
| 7563 | hdr_next += stripped_before; |
| 7564 | cur_hdr->len += stripped_before; |
| 7565 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7566 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7567 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7568 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7569 | /* First, let's see if we want to capture this cookie. We check |
| 7570 | * that we don't already have a client side cookie, because we |
| 7571 | * can only capture one. Also as an optimisation, we ignore |
| 7572 | * cookies shorter than the declared name. |
| 7573 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7574 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7575 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7576 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7577 | int log_len = val_end - att_beg; |
| 7578 | |
| 7579 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7580 | Alert("HTTP logging : out of memory.\n"); |
| 7581 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7582 | if (log_len > sess->fe->capture_len) |
| 7583 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7584 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7585 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7586 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7587 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7588 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7589 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7590 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7591 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7592 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7593 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7594 | * For cookies in prefix mode, the form is : |
| 7595 | * |
| 7596 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7597 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7598 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7599 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7600 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7601 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7602 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7603 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7604 | * have the server ID between val_beg and delim, and the original cookie between |
| 7605 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7606 | * |
| 7607 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7608 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7609 | * | || || | |+-> next |
| 7610 | * | || || | +--> val_end |
| 7611 | * | || || +---------> delim |
| 7612 | * | || |+------------> val_beg |
| 7613 | * | || +-------------> att_end = equal |
| 7614 | * | |+-----------------> att_beg |
| 7615 | * | +------------------> prev |
| 7616 | * +-------------------------> hdr_beg |
| 7617 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7618 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7619 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7620 | for (delim = val_beg; delim < val_end; delim++) |
| 7621 | if (*delim == COOKIE_DELIM) |
| 7622 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7623 | } else { |
| 7624 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7625 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7626 | /* Now check if the cookie contains a date field, which would |
| 7627 | * appear after a vertical bar ('|') just after the server name |
| 7628 | * and before the delimiter. |
| 7629 | */ |
| 7630 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7631 | if (vbar1) { |
| 7632 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7633 | * right is the last seen date. It is a base64 encoded |
| 7634 | * 30-bit value representing the UNIX date since the |
| 7635 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7636 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7637 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7638 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7639 | if (val_end - vbar1 >= 5) { |
| 7640 | val = b64tos30(vbar1); |
| 7641 | if (val > 0) |
| 7642 | txn->cookie_last_date = val << 2; |
| 7643 | } |
| 7644 | /* look for a second vertical bar */ |
| 7645 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7646 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7647 | val = b64tos30(vbar1 + 1); |
| 7648 | if (val > 0) |
| 7649 | txn->cookie_first_date = val << 2; |
| 7650 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7651 | } |
| 7652 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7653 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7654 | /* if the cookie has an expiration date and the proxy wants to check |
| 7655 | * it, then we do that now. We first check if the cookie is too old, |
| 7656 | * then only if it has expired. We detect strict overflow because the |
| 7657 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7658 | * in the future are ignored if their offset is beyond one day. This |
| 7659 | * allows an admin to fix timezone issues without expiring everyone |
| 7660 | * and at the same time avoids keeping unwanted side effects for too |
| 7661 | * long. |
| 7662 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7663 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7664 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7665 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7666 | txn->flags &= ~TX_CK_MASK; |
| 7667 | txn->flags |= TX_CK_OLD; |
| 7668 | delim = val_beg; // let's pretend we have not found the cookie |
| 7669 | txn->cookie_first_date = 0; |
| 7670 | txn->cookie_last_date = 0; |
| 7671 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7672 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7673 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 7674 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7675 | txn->flags &= ~TX_CK_MASK; |
| 7676 | txn->flags |= TX_CK_EXPIRED; |
| 7677 | delim = val_beg; // let's pretend we have not found the cookie |
| 7678 | txn->cookie_first_date = 0; |
| 7679 | txn->cookie_last_date = 0; |
| 7680 | } |
| 7681 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7682 | /* Here, we'll look for the first running server which supports the cookie. |
| 7683 | * This allows to share a same cookie between several servers, for example |
| 7684 | * to dedicate backup servers to specific servers only. |
| 7685 | * However, to prevent clients from sticking to cookie-less backup server |
| 7686 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7687 | * empty cookies and mark them as invalid. |
| 7688 | * The same behaviour is applied when persistence must be ignored. |
| 7689 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7690 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7691 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7692 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7693 | while (srv) { |
| 7694 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7695 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7696 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7697 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7698 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7699 | /* we found the server and we can use it */ |
| 7700 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7701 | txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7702 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7703 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7704 | break; |
| 7705 | } else { |
| 7706 | /* we found a server, but it's down, |
| 7707 | * mark it as such and go on in case |
| 7708 | * another one is available. |
| 7709 | */ |
| 7710 | txn->flags &= ~TX_CK_MASK; |
| 7711 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7712 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7713 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7714 | srv = srv->next; |
| 7715 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7716 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7717 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7718 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7719 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7720 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7721 | txn->flags |= TX_CK_UNUSED; |
| 7722 | else |
| 7723 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7724 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7725 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7726 | /* depending on the cookie mode, we may have to either : |
| 7727 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7728 | * the server never sees it ; |
| 7729 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7730 | * application cookie so that it does not get accidentely removed later, |
| 7731 | * if we're in cookie prefix mode |
| 7732 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7733 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7734 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7735 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7736 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7737 | val_end += delta; |
| 7738 | next += delta; |
| 7739 | hdr_end += delta; |
| 7740 | hdr_next += delta; |
| 7741 | cur_hdr->len += delta; |
| 7742 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7743 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7744 | del_from = NULL; |
| 7745 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7746 | } |
| 7747 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7748 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7749 | del_from = prev; |
| 7750 | } |
| 7751 | } else { |
| 7752 | /* This is not our cookie, so we must preserve it. But if we already |
| 7753 | * scheduled another cookie for removal, we cannot remove the |
| 7754 | * complete header, but we can remove the previous block itself. |
| 7755 | */ |
| 7756 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7757 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7758 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7759 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7760 | if (att_beg >= del_from) |
| 7761 | att_beg += delta; |
| 7762 | if (att_end >= del_from) |
| 7763 | att_end += delta; |
| 7764 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7765 | val_end += delta; |
| 7766 | next += delta; |
| 7767 | hdr_end += delta; |
| 7768 | hdr_next += delta; |
| 7769 | cur_hdr->len += delta; |
| 7770 | http_msg_move_end(&txn->req, delta); |
| 7771 | prev = del_from; |
| 7772 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7773 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7774 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7775 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7776 | /* continue with next cookie on this header line */ |
| 7777 | att_beg = next; |
| 7778 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7779 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7780 | /* There are no more cookies on this line. |
| 7781 | * We may still have one (or several) marked for deletion at the |
| 7782 | * end of the line. We must do this now in two ways : |
| 7783 | * - if some cookies must be preserved, we only delete from the |
| 7784 | * mark to the end of line ; |
| 7785 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7786 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7787 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7788 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7789 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7790 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7791 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7792 | cur_hdr->len += delta; |
| 7793 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7794 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7795 | |
| 7796 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7797 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7798 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7799 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7800 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7801 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7802 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7803 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7804 | } |
| 7805 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7806 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7807 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7808 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7809 | } |
| 7810 | |
| 7811 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7812 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7813 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7814 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7815 | int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7816 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7817 | char *cur_ptr, *cur_end, *cur_next; |
| 7818 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7819 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7820 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7821 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7822 | |
| 7823 | last_hdr = 0; |
| 7824 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7825 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7826 | old_idx = 0; |
| 7827 | |
| 7828 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7829 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7830 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7831 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7832 | (exp->action == ACT_ALLOW || |
| 7833 | exp->action == ACT_DENY)) |
| 7834 | return 0; |
| 7835 | |
| 7836 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7837 | if (!cur_idx) |
| 7838 | break; |
| 7839 | |
| 7840 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7841 | cur_ptr = cur_next; |
| 7842 | cur_end = cur_ptr + cur_hdr->len; |
| 7843 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7844 | |
| 7845 | /* Now we have one header between cur_ptr and cur_end, |
| 7846 | * and the next header starts at cur_next. |
| 7847 | */ |
| 7848 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7849 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7850 | switch (exp->action) { |
| 7851 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7852 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | last_hdr = 1; |
| 7854 | break; |
| 7855 | |
| 7856 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7857 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7858 | last_hdr = 1; |
| 7859 | break; |
| 7860 | |
| 7861 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7862 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7863 | if (trash.len < 0) |
| 7864 | return -1; |
| 7865 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7866 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7867 | /* FIXME: if the user adds a newline in the replacement, the |
| 7868 | * index will not be recalculated for now, and the new line |
| 7869 | * will not be counted as a new header. |
| 7870 | */ |
| 7871 | |
| 7872 | cur_end += delta; |
| 7873 | cur_next += delta; |
| 7874 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7875 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7876 | break; |
| 7877 | |
| 7878 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7879 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7880 | cur_next += delta; |
| 7881 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7882 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7883 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7884 | txn->hdr_idx.used--; |
| 7885 | cur_hdr->len = 0; |
| 7886 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7887 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7888 | break; |
| 7889 | |
| 7890 | } |
| 7891 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7892 | |
| 7893 | /* keep the link from this header to next one in case of later |
| 7894 | * removal of next header. |
| 7895 | */ |
| 7896 | old_idx = cur_idx; |
| 7897 | } |
| 7898 | return 0; |
| 7899 | } |
| 7900 | |
| 7901 | |
| 7902 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7903 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7904 | * or -1 if a replacement resulted in an invalid status line. |
| 7905 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7906 | int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7907 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7908 | char *cur_ptr, *cur_end; |
| 7909 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7910 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7911 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7912 | |
| 7913 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7914 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7915 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7916 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7917 | (exp->action == ACT_ALLOW || |
| 7918 | exp->action == ACT_DENY)) |
| 7919 | return 0; |
| 7920 | else if (exp->action == ACT_REMOVE) |
| 7921 | return 0; |
| 7922 | |
| 7923 | done = 0; |
| 7924 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7925 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7926 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7927 | |
| 7928 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7929 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7930 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7931 | switch (exp->action) { |
| 7932 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7933 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7934 | done = 1; |
| 7935 | break; |
| 7936 | |
| 7937 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7938 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7939 | done = 1; |
| 7940 | break; |
| 7941 | |
| 7942 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7943 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7944 | if (trash.len < 0) |
| 7945 | return -1; |
| 7946 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7947 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7948 | /* FIXME: if the user adds a newline in the replacement, the |
| 7949 | * index will not be recalculated for now, and the new line |
| 7950 | * will not be counted as a new header. |
| 7951 | */ |
| 7952 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7953 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7954 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7955 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7956 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7957 | cur_ptr, cur_end + 1, |
| 7958 | NULL, NULL); |
| 7959 | if (unlikely(!cur_end)) |
| 7960 | return -1; |
| 7961 | |
| 7962 | /* we have a full respnse and we know that we have either a CR |
| 7963 | * or an LF at <ptr>. |
| 7964 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7965 | txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7966 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r'); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7967 | /* there is no point trying this regex on headers */ |
| 7968 | return 1; |
| 7969 | } |
| 7970 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7971 | return done; |
| 7972 | } |
| 7973 | |
| 7974 | |
| 7975 | |
| 7976 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7977 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7978 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7979 | * unparsable response. |
| 7980 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7981 | int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7982 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7983 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7984 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7985 | struct hdr_exp *exp; |
| 7986 | |
| 7987 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7988 | int ret; |
| 7989 | |
| 7990 | /* |
| 7991 | * The interleaving of transformations and verdicts |
| 7992 | * makes it difficult to decide to continue or stop |
| 7993 | * the evaluation. |
| 7994 | */ |
| 7995 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7996 | if (txn->flags & TX_SVDENY) |
| 7997 | break; |
| 7998 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7999 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8000 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8001 | exp->action == ACT_PASS)) { |
| 8002 | exp = exp->next; |
| 8003 | continue; |
| 8004 | } |
| 8005 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8006 | /* if this filter had a condition, evaluate it now and skip to |
| 8007 | * next filter if the condition does not match. |
| 8008 | */ |
| 8009 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8010 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8011 | ret = acl_pass(ret); |
| 8012 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8013 | ret = !ret; |
| 8014 | if (!ret) |
| 8015 | continue; |
| 8016 | } |
| 8017 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8018 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8019 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8020 | if (unlikely(ret < 0)) |
| 8021 | return -1; |
| 8022 | |
| 8023 | if (likely(ret == 0)) { |
| 8024 | /* The filter did not match the response, it can be |
| 8025 | * iterated through all headers. |
| 8026 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8027 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8028 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8029 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8030 | } |
| 8031 | return 0; |
| 8032 | } |
| 8033 | |
| 8034 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8035 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8036 | * Manage server-side cookies. It can impact performance by about 2% so it is |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8037 | * desirable to call it only when needed. This function is also used when we |
| 8038 | * just need to know if there is a cookie (eg: for check-cache). |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8039 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8040 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8041 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8042 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8043 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8044 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8045 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8046 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8047 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8048 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8049 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | /* Iterate through the headers. |
| 8051 | * we start with the start line. |
| 8052 | */ |
| 8053 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8054 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8055 | |
| 8056 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8057 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8058 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8059 | |
| 8060 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8061 | hdr_beg = hdr_next; |
| 8062 | hdr_end = hdr_beg + cur_hdr->len; |
| 8063 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8064 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8065 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8066 | * next header starts at hdr_next. We're only interested in |
| 8067 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8068 | */ |
| 8069 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8070 | is_cookie2 = 0; |
| 8071 | prev = hdr_beg + 10; |
| 8072 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8073 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8074 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8075 | if (!val) { |
| 8076 | old_idx = cur_idx; |
| 8077 | continue; |
| 8078 | } |
| 8079 | is_cookie2 = 1; |
| 8080 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8081 | } |
| 8082 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8083 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8084 | * <prev> points to the colon. |
| 8085 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8086 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8087 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8088 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8089 | * check-cache is enabled) and we are not interested in checking |
| 8090 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8091 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8092 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8093 | return; |
| 8094 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8095 | /* OK so now we know we have to process this response cookie. |
| 8096 | * The format of the Set-Cookie header is slightly different |
| 8097 | * from the format of the Cookie header in that it does not |
| 8098 | * support the comma as a cookie delimiter (thus the header |
| 8099 | * cannot be folded) because the Expires attribute described in |
| 8100 | * the original Netscape's spec may contain an unquoted date |
| 8101 | * with a comma inside. We have to live with this because |
| 8102 | * many browsers don't support Max-Age and some browsers don't |
| 8103 | * support quoted strings. However the Set-Cookie2 header is |
| 8104 | * clean. |
| 8105 | * |
| 8106 | * We have to keep multiple pointers in order to support cookie |
| 8107 | * removal at the beginning, middle or end of header without |
| 8108 | * corrupting the header (in case of set-cookie2). A special |
| 8109 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8110 | * fields after the first semi-colon. The <next> pointer points |
| 8111 | * either to the end of line (set-cookie) or next unquoted comma |
| 8112 | * (set-cookie2). All of these headers are valid : |
| 8113 | * |
| 8114 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8115 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8116 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8117 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8118 | * | | | | | | | | | | |
| 8119 | * | | | | | | | | +-> next hdr_end <--+ |
| 8120 | * | | | | | | | +------------> scav |
| 8121 | * | | | | | | +--------------> val_end |
| 8122 | * | | | | | +--------------------> val_beg |
| 8123 | * | | | | +----------------------> equal |
| 8124 | * | | | +------------------------> att_end |
| 8125 | * | | +----------------------------> att_beg |
| 8126 | * | +------------------------------> prev |
| 8127 | * +-----------------------------------------> hdr_beg |
| 8128 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8129 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8130 | for (; prev < hdr_end; prev = next) { |
| 8131 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8132 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8133 | /* find att_beg */ |
| 8134 | att_beg = prev + 1; |
| 8135 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8136 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8137 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8138 | /* find att_end : this is the first character after the last non |
| 8139 | * space before the equal. It may be equal to hdr_end. |
| 8140 | */ |
| 8141 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8142 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8143 | while (equal < hdr_end) { |
| 8144 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8145 | break; |
| 8146 | if (http_is_spht[(unsigned char)*equal++]) |
| 8147 | continue; |
| 8148 | att_end = equal; |
| 8149 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8150 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8151 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8152 | * is between <att_beg> and <equal>, both may be identical. |
| 8153 | */ |
| 8154 | |
| 8155 | /* look for end of cookie if there is an equal sign */ |
| 8156 | if (equal < hdr_end && *equal == '=') { |
| 8157 | /* look for the beginning of the value */ |
| 8158 | val_beg = equal + 1; |
| 8159 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8160 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8161 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8162 | /* find the end of the value, respecting quotes */ |
| 8163 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8164 | |
| 8165 | /* make val_end point to the first white space or delimitor after the value */ |
| 8166 | val_end = next; |
| 8167 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8168 | val_end--; |
| 8169 | } else { |
| 8170 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8171 | val_beg = val_end = next = equal; |
| 8172 | } |
| 8173 | |
| 8174 | if (next < hdr_end) { |
| 8175 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8176 | * a colon or semi-colon before the end. So skip all attr-value |
| 8177 | * pairs and look for the next comma. For Set-Cookie, since |
| 8178 | * commas are permitted in values, skip to the end. |
| 8179 | */ |
| 8180 | if (is_cookie2) |
| 8181 | next = find_hdr_value_end(next, hdr_end); |
| 8182 | else |
| 8183 | next = hdr_end; |
| 8184 | } |
| 8185 | |
| 8186 | /* Now everything is as on the diagram above */ |
| 8187 | |
| 8188 | /* Ignore cookies with no equal sign */ |
| 8189 | if (equal == val_end) |
| 8190 | continue; |
| 8191 | |
| 8192 | /* If there are spaces around the equal sign, we need to |
| 8193 | * strip them otherwise we'll get trouble for cookie captures, |
| 8194 | * or even for rewrites. Since this happens extremely rarely, |
| 8195 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8196 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8197 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8198 | int stripped_before = 0; |
| 8199 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8200 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8201 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8202 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8203 | equal += stripped_before; |
| 8204 | val_beg += stripped_before; |
| 8205 | } |
| 8206 | |
| 8207 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8208 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8209 | val_beg += stripped_after; |
| 8210 | stripped_before += stripped_after; |
| 8211 | } |
| 8212 | |
| 8213 | val_end += stripped_before; |
| 8214 | next += stripped_before; |
| 8215 | hdr_end += stripped_before; |
| 8216 | hdr_next += stripped_before; |
| 8217 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8218 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8219 | } |
| 8220 | |
| 8221 | /* First, let's see if we want to capture this cookie. We check |
| 8222 | * that we don't already have a server side cookie, because we |
| 8223 | * can only capture one. Also as an optimisation, we ignore |
| 8224 | * cookies shorter than the declared name. |
| 8225 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8226 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8227 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8228 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8229 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8230 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8231 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8232 | Alert("HTTP logging : out of memory.\n"); |
| 8233 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8234 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8235 | if (log_len > sess->fe->capture_len) |
| 8236 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8237 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8238 | txn->srv_cookie[log_len] = 0; |
| 8239 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8240 | } |
| 8241 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8242 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8243 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8244 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8245 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8246 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8247 | /* assume passive cookie by default */ |
| 8248 | txn->flags &= ~TX_SCK_MASK; |
| 8249 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8250 | |
| 8251 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8252 | * this occurrence because we'll insert another one later. |
| 8253 | * We'll delete it too if the "indirect" option is set and we're in |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8254 | * a direct access. |
| 8255 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8256 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8257 | /* The "preserve" flag was set, we don't want to touch the |
| 8258 | * server's cookie. |
| 8259 | */ |
| 8260 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8261 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8262 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8263 | /* this cookie must be deleted */ |
| 8264 | if (*prev == ':' && next == hdr_end) { |
| 8265 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8266 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8267 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8268 | txn->hdr_idx.used--; |
| 8269 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8270 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8271 | hdr_next += delta; |
| 8272 | http_msg_move_end(&txn->rsp, delta); |
| 8273 | /* note: while both invalid now, <next> and <hdr_end> |
| 8274 | * are still equal, so the for() will stop as expected. |
| 8275 | */ |
| 8276 | } else { |
| 8277 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8278 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8279 | next = prev; |
| 8280 | hdr_end += delta; |
| 8281 | hdr_next += delta; |
| 8282 | cur_hdr->len += delta; |
| 8283 | http_msg_move_end(&txn->rsp, delta); |
| 8284 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8285 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8286 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8287 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8288 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8289 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8290 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8291 | * with this server since we know it. |
| 8292 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8293 | delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8294 | next += delta; |
| 8295 | hdr_end += delta; |
| 8296 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8297 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8298 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8299 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8300 | txn->flags &= ~TX_SCK_MASK; |
| 8301 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8302 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8303 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8304 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8305 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8306 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8307 | delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8308 | next += delta; |
| 8309 | hdr_end += delta; |
| 8310 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8311 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8312 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8313 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8314 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8315 | txn->flags &= ~TX_SCK_MASK; |
| 8316 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8317 | } |
| 8318 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8319 | /* that's done for this cookie, check the next one on the same |
| 8320 | * line when next != hdr_end (only if is_cookie2). |
| 8321 | */ |
| 8322 | } |
| 8323 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8324 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8325 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8326 | } |
| 8327 | |
| 8328 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8329 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8330 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8331 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8332 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8333 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8334 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8335 | char *p1, *p2; |
| 8336 | |
| 8337 | char *cur_ptr, *cur_end, *cur_next; |
| 8338 | int cur_idx; |
| 8339 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8340 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8341 | return; |
| 8342 | |
| 8343 | /* Iterate through the headers. |
| 8344 | * we start with the start line. |
| 8345 | */ |
| 8346 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8347 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8348 | |
| 8349 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8350 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8351 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8352 | |
| 8353 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8354 | cur_ptr = cur_next; |
| 8355 | cur_end = cur_ptr + cur_hdr->len; |
| 8356 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8357 | |
| 8358 | /* We have one full header between cur_ptr and cur_end, and the |
| 8359 | * next header starts at cur_next. We're only interested in |
| 8360 | * "Cookie:" headers. |
| 8361 | */ |
| 8362 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8363 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8364 | if (val) { |
| 8365 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8366 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8367 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8368 | return; |
| 8369 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8370 | } |
| 8371 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8372 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8373 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8374 | continue; |
| 8375 | |
| 8376 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8377 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8378 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8379 | |
| 8380 | if (p1 >= cur_end) /* no more info */ |
| 8381 | continue; |
| 8382 | |
| 8383 | /* p1 is at the beginning of the value */ |
| 8384 | p2 = p1; |
| 8385 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8386 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8387 | p2++; |
| 8388 | |
| 8389 | /* we have a complete value between p1 and p2 */ |
| 8390 | if (p2 < cur_end && *p2 == '=') { |
| 8391 | /* we have something of the form no-cache="set-cookie" */ |
| 8392 | if ((cur_end - p1 >= 21) && |
| 8393 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8394 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8395 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8396 | continue; |
| 8397 | } |
| 8398 | |
| 8399 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8400 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8401 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8402 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8403 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8404 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8405 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8406 | return; |
| 8407 | } |
| 8408 | |
| 8409 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8410 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8411 | continue; |
| 8412 | } |
| 8413 | } |
| 8414 | } |
| 8415 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8416 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8417 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8418 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8419 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8420 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8421 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8422 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8423 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8424 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8425 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8426 | int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8427 | { |
| 8428 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8429 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8430 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8431 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8432 | if (!uri_auth) |
| 8433 | return 0; |
| 8434 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8435 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8436 | return 0; |
| 8437 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8438 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8439 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8440 | return 0; |
| 8441 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8442 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8443 | return 0; |
| 8444 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8445 | return 1; |
| 8446 | } |
| 8447 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8448 | /* |
| 8449 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8450 | * By default it tries to report the error position as msg->err_pos. However if |
| 8451 | * this one is not set, it will then report msg->next, which is the last known |
| 8452 | * parsing point. The function is able to deal with wrapping buffers. It always |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8453 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8454 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8455 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8456 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8457 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8458 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8459 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8460 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8461 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8462 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8463 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8464 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8465 | len1 = MIN(len1, es->len); |
| 8466 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8467 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8468 | if (!es->buf) |
| 8469 | es->buf = malloc(global.tune.bufsize); |
| 8470 | |
| 8471 | if (es->buf) { |
| 8472 | memcpy(es->buf, chn->buf->p, len1); |
| 8473 | if (len2) |
| 8474 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 8475 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8476 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8477 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8478 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8479 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8480 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8481 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8482 | es->when = date; // user-visible date |
| 8483 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8484 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8485 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8486 | if (objt_conn(sess->origin)) |
| 8487 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8488 | else |
| 8489 | memset(&es->src, 0, sizeof(es->src)); |
| 8490 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8491 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8492 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8493 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8494 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8495 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8496 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8497 | es->b_out = chn->buf->o; |
| 8498 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8499 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8500 | es->m_clen = msg->chunk_len; |
| 8501 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8502 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8503 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8504 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8505 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8506 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8507 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8508 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8509 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8510 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8511 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8512 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8513 | * The return value is 0 if nothing was found, or non-zero otherwise. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8514 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8515 | unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen, |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8516 | struct hdr_idx *idx, int occ, |
| 8517 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8518 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8519 | struct hdr_ctx local_ctx; |
| 8520 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8521 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8522 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8523 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8524 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8525 | if (!ctx) { |
| 8526 | local_ctx.idx = 0; |
| 8527 | ctx = &local_ctx; |
| 8528 | } |
| 8529 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8530 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8531 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8532 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8533 | occ--; |
| 8534 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8535 | *vptr = ctx->line + ctx->val; |
| 8536 | *vlen = ctx->vlen; |
| 8537 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8538 | } |
| 8539 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8540 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8541 | } |
| 8542 | |
| 8543 | /* negative occurrence, we scan all the list then walk back */ |
| 8544 | if (-occ > MAX_HDR_HISTORY) |
| 8545 | return 0; |
| 8546 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8547 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8548 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8549 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8550 | len_hist[hist_ptr] = ctx->vlen; |
| 8551 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8552 | hist_ptr = 0; |
| 8553 | found++; |
| 8554 | } |
| 8555 | if (-occ > found) |
| 8556 | return 0; |
| 8557 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8558 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8559 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8560 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8561 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8562 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8563 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8564 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8565 | *vptr = ptr_hist[hist_ptr]; |
| 8566 | *vlen = len_hist[hist_ptr]; |
| 8567 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8568 | } |
| 8569 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8570 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8571 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8572 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8573 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8574 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8575 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8576 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8577 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8578 | * line header values and does not stop at commas. |
| 8579 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8580 | */ |
| 8581 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8582 | struct hdr_idx *idx, int occ, |
| 8583 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8584 | { |
| 8585 | struct hdr_ctx local_ctx; |
| 8586 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8587 | int len_hist[MAX_HDR_HISTORY]; |
| 8588 | unsigned int hist_ptr; |
| 8589 | int found; |
| 8590 | |
| 8591 | if (!ctx) { |
| 8592 | local_ctx.idx = 0; |
| 8593 | ctx = &local_ctx; |
| 8594 | } |
| 8595 | |
| 8596 | if (occ >= 0) { |
| 8597 | /* search from the beginning */ |
| 8598 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8599 | occ--; |
| 8600 | if (occ <= 0) { |
| 8601 | *vptr = ctx->line + ctx->val; |
| 8602 | *vlen = ctx->vlen; |
| 8603 | return 1; |
| 8604 | } |
| 8605 | } |
| 8606 | return 0; |
| 8607 | } |
| 8608 | |
| 8609 | /* negative occurrence, we scan all the list then walk back */ |
| 8610 | if (-occ > MAX_HDR_HISTORY) |
| 8611 | return 0; |
| 8612 | |
| 8613 | found = hist_ptr = 0; |
| 8614 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8615 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8616 | len_hist[hist_ptr] = ctx->vlen; |
| 8617 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8618 | hist_ptr = 0; |
| 8619 | found++; |
| 8620 | } |
| 8621 | if (-occ > found) |
| 8622 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8623 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8624 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8625 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8626 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8627 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8628 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8629 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8630 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8631 | hist_ptr -= MAX_HDR_HISTORY; |
| 8632 | *vptr = ptr_hist[hist_ptr]; |
| 8633 | *vlen = len_hist[hist_ptr]; |
| 8634 | return 1; |
| 8635 | } |
| 8636 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8637 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8638 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8639 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8640 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8641 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8642 | void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8643 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8644 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8645 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8646 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8647 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8648 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8649 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1, |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 8650 | objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8651 | |
| 8652 | for (max = 0; start + max < end; max++) |
| 8653 | if (start[max] == '\r' || start[max] == '\n') |
| 8654 | break; |
| 8655 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8656 | UBOUND(max, trash.size - trash.len - 3); |
| 8657 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8658 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8659 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8660 | } |
| 8661 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8662 | |
| 8663 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8664 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8665 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8666 | * assigned to the stream and returned. |
| 8667 | */ |
| 8668 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8669 | { |
| 8670 | struct http_txn *txn = s->txn; |
| 8671 | |
| 8672 | if (txn) |
| 8673 | return txn; |
| 8674 | |
| 8675 | txn = pool_alloc2(pool2_http_txn); |
| 8676 | if (!txn) |
| 8677 | return txn; |
| 8678 | |
| 8679 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8680 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8681 | if (!txn->hdr_idx.v) { |
| 8682 | pool_free2(pool2_http_txn, txn); |
| 8683 | return NULL; |
| 8684 | } |
| 8685 | |
| 8686 | s->txn = txn; |
| 8687 | return txn; |
| 8688 | } |
| 8689 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8690 | void http_txn_reset_req(struct http_txn *txn) |
| 8691 | { |
| 8692 | txn->req.flags = 0; |
| 8693 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8694 | txn->req.next = 0; |
| 8695 | txn->req.chunk_len = 0LL; |
| 8696 | txn->req.body_len = 0LL; |
| 8697 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8698 | } |
| 8699 | |
| 8700 | void http_txn_reset_res(struct http_txn *txn) |
| 8701 | { |
| 8702 | txn->rsp.flags = 0; |
| 8703 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8704 | txn->rsp.next = 0; |
| 8705 | txn->rsp.chunk_len = 0LL; |
| 8706 | txn->rsp.body_len = 0LL; |
| 8707 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8708 | } |
| 8709 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8710 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8711 | * Initialize a new HTTP transaction for stream <s>. It is assumed that all |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8712 | * the required fields are properly allocated and that we only need to (re)init |
| 8713 | * them. This should be used before processing any new request. |
| 8714 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8715 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8716 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8717 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8718 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8719 | |
| 8720 | txn->flags = 0; |
| 8721 | txn->status = -1; |
| 8722 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8723 | txn->cookie_first_date = 0; |
| 8724 | txn->cookie_last_date = 0; |
| 8725 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8726 | txn->srv_cookie = NULL; |
| 8727 | txn->cli_cookie = NULL; |
| 8728 | txn->uri = NULL; |
| 8729 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8730 | http_txn_reset_req(txn); |
| 8731 | http_txn_reset_res(txn); |
| 8732 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8733 | txn->req.chn = &s->req; |
| 8734 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8735 | |
| 8736 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8737 | |
| 8738 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8739 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8740 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8741 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8742 | if (txn->hdr_idx.v) |
| 8743 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8744 | |
| 8745 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8746 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8747 | } |
| 8748 | |
| 8749 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8750 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8751 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8752 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8753 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8754 | |
| 8755 | /* these ones will have been dynamically allocated */ |
| 8756 | pool_free2(pool2_requri, txn->uri); |
| 8757 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8758 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8759 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8760 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8761 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8762 | txn->uri = NULL; |
| 8763 | txn->srv_cookie = NULL; |
| 8764 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8765 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8766 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8767 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8768 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8769 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8770 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8771 | } |
| 8772 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8773 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8774 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8775 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8776 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8777 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8778 | } |
| 8779 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8780 | vars_prune(&s->vars_txn, s->sess, s); |
| 8781 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8782 | } |
| 8783 | |
| 8784 | /* to be used at the end of a transaction to prepare a new one */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8785 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8786 | { |
| 8787 | http_end_txn(s); |
| 8788 | http_init_txn(s); |
| 8789 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8790 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8791 | * any rulelist match the NULL pointer. |
| 8792 | */ |
| 8793 | s->current_rule_list = NULL; |
| 8794 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8795 | s->be = strm_fe(s); |
| 8796 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8797 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8798 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8799 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8800 | /* re-init store persistence */ |
| 8801 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8802 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8803 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8804 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8805 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8806 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8807 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8808 | /* We must trim any excess data from the response buffer, because we |
| 8809 | * may have blocked an invalid response from a server that we don't |
| 8810 | * want to accidentely forward once we disable the analysers, nor do |
| 8811 | * we want those data to come along with next response. A typical |
| 8812 | * example of such data would be from a buggy server responding to |
| 8813 | * a HEAD with some data, or sending more than the advertised |
| 8814 | * content-length. |
| 8815 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8816 | if (unlikely(s->res.buf->i)) |
| 8817 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8818 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8819 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8820 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8821 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8822 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8823 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8824 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8825 | s->req.rex = TICK_ETERNITY; |
| 8826 | s->req.wex = TICK_ETERNITY; |
| 8827 | s->req.analyse_exp = TICK_ETERNITY; |
| 8828 | s->res.rex = TICK_ETERNITY; |
| 8829 | s->res.wex = TICK_ETERNITY; |
| 8830 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8831 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8832 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8833 | void free_http_res_rules(struct list *r) |
| 8834 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8835 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8836 | |
| 8837 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8838 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8839 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8840 | free(pr); |
| 8841 | } |
| 8842 | } |
| 8843 | |
| 8844 | void free_http_req_rules(struct list *r) |
| 8845 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8846 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8847 | |
| 8848 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8849 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8850 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8851 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8852 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8853 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8854 | free(pr); |
| 8855 | } |
| 8856 | } |
| 8857 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8858 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8859 | struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8860 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8861 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8862 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8863 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8864 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8865 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8866 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8867 | if (!rule) { |
| 8868 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8869 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8870 | } |
| 8871 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8872 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8873 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8874 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8875 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8876 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8877 | int code; |
| 8878 | int hc; |
| 8879 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8880 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8881 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8882 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8883 | cur_arg++; |
| 8884 | if (!args[cur_arg]) { |
| 8885 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8886 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8887 | goto out_err; |
| 8888 | } |
| 8889 | |
| 8890 | code = atol(args[cur_arg]); |
| 8891 | cur_arg++; |
| 8892 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8893 | if (http_err_codes[hc] == code) { |
| 8894 | rule->deny_status = hc; |
| 8895 | break; |
| 8896 | } |
| 8897 | } |
| 8898 | |
| 8899 | if (hc >= HTTP_ERR_SIZE) { |
| 8900 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 8901 | file, linenum, code); |
| 8902 | } |
| 8903 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8904 | } else if (!strcmp(args[0], "tarpit")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8905 | rule->action = ACT_HTTP_REQ_TARPIT; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8906 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8907 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8908 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8909 | cur_arg = 1; |
| 8910 | |
| 8911 | while(*args[cur_arg]) { |
| 8912 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8913 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8914 | cur_arg+=2; |
| 8915 | continue; |
| 8916 | } else |
| 8917 | break; |
| 8918 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8919 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8920 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8921 | cur_arg = 1; |
| 8922 | |
| 8923 | if (!*args[cur_arg] || |
| 8924 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8925 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8926 | file, linenum, args[0]); |
| 8927 | goto out_err; |
| 8928 | } |
| 8929 | rule->arg.nice = atoi(args[cur_arg]); |
| 8930 | if (rule->arg.nice < -1024) |
| 8931 | rule->arg.nice = -1024; |
| 8932 | else if (rule->arg.nice > 1024) |
| 8933 | rule->arg.nice = 1024; |
| 8934 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8935 | } else if (!strcmp(args[0], "set-tos")) { |
| 8936 | #ifdef IP_TOS |
| 8937 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8938 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8939 | cur_arg = 1; |
| 8940 | |
| 8941 | if (!*args[cur_arg] || |
| 8942 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8943 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8944 | file, linenum, args[0]); |
| 8945 | goto out_err; |
| 8946 | } |
| 8947 | |
| 8948 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8949 | if (err && *err != '\0') { |
| 8950 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8951 | file, linenum, err, args[0]); |
| 8952 | goto out_err; |
| 8953 | } |
| 8954 | cur_arg++; |
| 8955 | #else |
| 8956 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8957 | goto out_err; |
| 8958 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8959 | } else if (!strcmp(args[0], "set-mark")) { |
| 8960 | #ifdef SO_MARK |
| 8961 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8962 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8963 | cur_arg = 1; |
| 8964 | |
| 8965 | if (!*args[cur_arg] || |
| 8966 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8967 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8968 | file, linenum, args[0]); |
| 8969 | goto out_err; |
| 8970 | } |
| 8971 | |
| 8972 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8973 | if (err && *err != '\0') { |
| 8974 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8975 | file, linenum, err, args[0]); |
| 8976 | goto out_err; |
| 8977 | } |
| 8978 | cur_arg++; |
| 8979 | global.last_checks |= LSTCHK_NETADM; |
| 8980 | #else |
| 8981 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8982 | goto out_err; |
| 8983 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8984 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8985 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8986 | cur_arg = 1; |
| 8987 | |
| 8988 | if (!*args[cur_arg] || |
| 8989 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8990 | bad_log_level: |
| 8991 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8992 | file, linenum, args[0]); |
| 8993 | goto out_err; |
| 8994 | } |
| 8995 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8996 | rule->arg.loglevel = -1; |
| 8997 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8998 | goto bad_log_level; |
| 8999 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9000 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9001 | rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9002 | cur_arg = 1; |
| 9003 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9004 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9005 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9006 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9007 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9008 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9009 | } |
| 9010 | |
| 9011 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9012 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9013 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9014 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9015 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9016 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9017 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9018 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9019 | free(proxy->conf.lfs_file); |
| 9020 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9021 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9022 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9023 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9024 | rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9025 | cur_arg = 1; |
| 9026 | |
| 9027 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9028 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9029 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9030 | file, linenum, args[0]); |
| 9031 | goto out_err; |
| 9032 | } |
| 9033 | |
| 9034 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9035 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9036 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9037 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9038 | error = NULL; |
| 9039 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9040 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9041 | args[cur_arg + 1], error); |
| 9042 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9043 | goto out_err; |
| 9044 | } |
| 9045 | |
| 9046 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9047 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9048 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9049 | file, linenum); |
| 9050 | |
| 9051 | free(proxy->conf.lfs_file); |
| 9052 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9053 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9054 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9055 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9056 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9057 | cur_arg = 1; |
| 9058 | |
| 9059 | if (!*args[cur_arg] || |
| 9060 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9061 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9062 | file, linenum, args[0]); |
| 9063 | goto out_err; |
| 9064 | } |
| 9065 | |
| 9066 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9067 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9068 | |
| 9069 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9070 | free(proxy->conf.lfs_file); |
| 9071 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9072 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9073 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9074 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9075 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9076 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9077 | struct sample_expr *expr; |
| 9078 | unsigned int where; |
| 9079 | char *err = NULL; |
| 9080 | |
| 9081 | cur_arg = 1; |
| 9082 | proxy->conf.args.ctx = ARGC_TRK; |
| 9083 | |
| 9084 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9085 | if (!expr) { |
| 9086 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9087 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9088 | free(err); |
| 9089 | goto out_err; |
| 9090 | } |
| 9091 | |
| 9092 | where = 0; |
| 9093 | if (proxy->cap & PR_CAP_FE) |
| 9094 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9095 | if (proxy->cap & PR_CAP_BE) |
| 9096 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9097 | |
| 9098 | if (!(expr->fetch->val & where)) { |
| 9099 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9100 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9101 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9102 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9103 | free(expr); |
| 9104 | goto out_err; |
| 9105 | } |
| 9106 | |
| 9107 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9108 | cur_arg++; |
| 9109 | if (!args[cur_arg]) { |
| 9110 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9111 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9112 | free(expr); |
| 9113 | goto out_err; |
| 9114 | } |
| 9115 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9116 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9117 | cur_arg++; |
| 9118 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9119 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9120 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9121 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9122 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9123 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9124 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9125 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) { |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9126 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9127 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9128 | goto out_err; |
| 9129 | } |
| 9130 | |
| 9131 | /* this redirect rule might already contain a parsed condition which |
| 9132 | * we'll pass to the http-request rule. |
| 9133 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9134 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9135 | rule->arg.redir = redir; |
| 9136 | rule->cond = redir->cond; |
| 9137 | redir->cond = NULL; |
| 9138 | cur_arg = 2; |
| 9139 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9140 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9141 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9142 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9143 | /* |
| 9144 | * '+ 8' for 'add-acl(' |
| 9145 | * '- 9' for 'add-acl(' + trailing ')' |
| 9146 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9147 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9148 | |
| 9149 | cur_arg = 1; |
| 9150 | |
| 9151 | if (!*args[cur_arg] || |
| 9152 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9153 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9154 | file, linenum, args[0]); |
| 9155 | goto out_err; |
| 9156 | } |
| 9157 | |
| 9158 | LIST_INIT(&rule->arg.map.key); |
| 9159 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9160 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9161 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9162 | file, linenum); |
| 9163 | free(proxy->conf.lfs_file); |
| 9164 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9165 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9166 | cur_arg += 1; |
| 9167 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9168 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9169 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9170 | /* |
| 9171 | * '+ 8' for 'del-acl(' |
| 9172 | * '- 9' for 'del-acl(' + trailing ')' |
| 9173 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9174 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9175 | |
| 9176 | cur_arg = 1; |
| 9177 | |
| 9178 | if (!*args[cur_arg] || |
| 9179 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9180 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9181 | file, linenum, args[0]); |
| 9182 | goto out_err; |
| 9183 | } |
| 9184 | |
| 9185 | LIST_INIT(&rule->arg.map.key); |
| 9186 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9187 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9188 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9189 | file, linenum); |
| 9190 | free(proxy->conf.lfs_file); |
| 9191 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9192 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9193 | cur_arg += 1; |
| 9194 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9195 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9196 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9197 | /* |
| 9198 | * '+ 8' for 'del-map(' |
| 9199 | * '- 9' for 'del-map(' + trailing ')' |
| 9200 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9201 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9202 | |
| 9203 | cur_arg = 1; |
| 9204 | |
| 9205 | if (!*args[cur_arg] || |
| 9206 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9207 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9208 | file, linenum, args[0]); |
| 9209 | goto out_err; |
| 9210 | } |
| 9211 | |
| 9212 | LIST_INIT(&rule->arg.map.key); |
| 9213 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9214 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9215 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9216 | file, linenum); |
| 9217 | free(proxy->conf.lfs_file); |
| 9218 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9219 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9220 | cur_arg += 1; |
| 9221 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9222 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9223 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9224 | /* |
| 9225 | * '+ 8' for 'set-map(' |
| 9226 | * '- 9' for 'set-map(' + trailing ')' |
| 9227 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9228 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9229 | |
| 9230 | cur_arg = 1; |
| 9231 | |
| 9232 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9233 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9234 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9235 | file, linenum, args[0]); |
| 9236 | goto out_err; |
| 9237 | } |
| 9238 | |
| 9239 | LIST_INIT(&rule->arg.map.key); |
| 9240 | LIST_INIT(&rule->arg.map.value); |
| 9241 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9242 | |
| 9243 | /* key pattern */ |
| 9244 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9245 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9246 | file, linenum); |
| 9247 | |
| 9248 | /* value pattern */ |
| 9249 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9250 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9251 | file, linenum); |
| 9252 | free(proxy->conf.lfs_file); |
| 9253 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9254 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9255 | |
| 9256 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9257 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9258 | char *errmsg = NULL; |
| 9259 | cur_arg = 1; |
| 9260 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9261 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9262 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9263 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9264 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9265 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9266 | free(errmsg); |
| 9267 | goto out_err; |
| 9268 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9269 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9270 | action_build_list(&http_req_keywords.list, &trash); |
| 9271 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9272 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9273 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9274 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9275 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9276 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9277 | } |
| 9278 | |
| 9279 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9280 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9281 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9282 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9283 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9284 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9285 | file, linenum, args[0], errmsg); |
| 9286 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9287 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9288 | } |
| 9289 | rule->cond = cond; |
| 9290 | } |
| 9291 | else if (*args[cur_arg]) { |
| 9292 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9293 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9294 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9295 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9296 | } |
| 9297 | |
| 9298 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9299 | out_err: |
| 9300 | free(rule); |
| 9301 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9302 | } |
| 9303 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9304 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9305 | struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9306 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9307 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9308 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9309 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9310 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9311 | |
| 9312 | rule = calloc(1, sizeof(*rule)); |
| 9313 | if (!rule) { |
| 9314 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9315 | goto out_err; |
| 9316 | } |
| 9317 | |
| 9318 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9319 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9320 | cur_arg = 1; |
| 9321 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9322 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9323 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9324 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9325 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9326 | cur_arg = 1; |
| 9327 | |
| 9328 | if (!*args[cur_arg] || |
| 9329 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9330 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9331 | file, linenum, args[0]); |
| 9332 | goto out_err; |
| 9333 | } |
| 9334 | rule->arg.nice = atoi(args[cur_arg]); |
| 9335 | if (rule->arg.nice < -1024) |
| 9336 | rule->arg.nice = -1024; |
| 9337 | else if (rule->arg.nice > 1024) |
| 9338 | rule->arg.nice = 1024; |
| 9339 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9340 | } else if (!strcmp(args[0], "set-tos")) { |
| 9341 | #ifdef IP_TOS |
| 9342 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9343 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9344 | cur_arg = 1; |
| 9345 | |
| 9346 | if (!*args[cur_arg] || |
| 9347 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9348 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9349 | file, linenum, args[0]); |
| 9350 | goto out_err; |
| 9351 | } |
| 9352 | |
| 9353 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9354 | if (err && *err != '\0') { |
| 9355 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9356 | file, linenum, err, args[0]); |
| 9357 | goto out_err; |
| 9358 | } |
| 9359 | cur_arg++; |
| 9360 | #else |
| 9361 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9362 | goto out_err; |
| 9363 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9364 | } else if (!strcmp(args[0], "set-mark")) { |
| 9365 | #ifdef SO_MARK |
| 9366 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9367 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9368 | cur_arg = 1; |
| 9369 | |
| 9370 | if (!*args[cur_arg] || |
| 9371 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9372 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9373 | file, linenum, args[0]); |
| 9374 | goto out_err; |
| 9375 | } |
| 9376 | |
| 9377 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9378 | if (err && *err != '\0') { |
| 9379 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9380 | file, linenum, err, args[0]); |
| 9381 | goto out_err; |
| 9382 | } |
| 9383 | cur_arg++; |
| 9384 | global.last_checks |= LSTCHK_NETADM; |
| 9385 | #else |
| 9386 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9387 | goto out_err; |
| 9388 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9389 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9390 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 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 | bad_log_level: |
| 9396 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9397 | file, linenum, args[0]); |
| 9398 | goto out_err; |
| 9399 | } |
| 9400 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9401 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 9402 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9403 | goto bad_log_level; |
| 9404 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9405 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9406 | rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9407 | cur_arg = 1; |
| 9408 | |
| 9409 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9410 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9411 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9412 | file, linenum, args[0]); |
| 9413 | goto out_err; |
| 9414 | } |
| 9415 | |
| 9416 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9417 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9418 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9419 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9420 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9421 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9422 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9423 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9424 | free(proxy->conf.lfs_file); |
| 9425 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9426 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9427 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9428 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9429 | rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9430 | cur_arg = 1; |
| 9431 | |
| 9432 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9433 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9434 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9435 | file, linenum, args[0]); |
| 9436 | goto out_err; |
| 9437 | } |
| 9438 | |
| 9439 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9440 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9441 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9442 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9443 | error = NULL; |
| 9444 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9445 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9446 | args[cur_arg + 1], error); |
| 9447 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9448 | goto out_err; |
| 9449 | } |
| 9450 | |
| 9451 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9452 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9453 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9454 | file, linenum); |
| 9455 | |
| 9456 | free(proxy->conf.lfs_file); |
| 9457 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9458 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9459 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9460 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9461 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9462 | cur_arg = 1; |
| 9463 | |
| 9464 | if (!*args[cur_arg] || |
| 9465 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9466 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9467 | file, linenum, args[0]); |
| 9468 | goto out_err; |
| 9469 | } |
| 9470 | |
| 9471 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9472 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9473 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9474 | proxy->conf.args.ctx = ARGC_HRS; |
| 9475 | free(proxy->conf.lfs_file); |
| 9476 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9477 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9478 | cur_arg += 1; |
| 9479 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9480 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9481 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9482 | /* |
| 9483 | * '+ 8' for 'add-acl(' |
| 9484 | * '- 9' for 'add-acl(' + trailing ')' |
| 9485 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9486 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9487 | |
| 9488 | cur_arg = 1; |
| 9489 | |
| 9490 | if (!*args[cur_arg] || |
| 9491 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9492 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9493 | file, linenum, args[0]); |
| 9494 | goto out_err; |
| 9495 | } |
| 9496 | |
| 9497 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9498 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9499 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9500 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9501 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9502 | free(proxy->conf.lfs_file); |
| 9503 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9504 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9505 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9506 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9507 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9508 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9509 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9510 | /* |
| 9511 | * '+ 8' for 'del-acl(' |
| 9512 | * '- 9' for 'del-acl(' + trailing ')' |
| 9513 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9514 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9515 | |
| 9516 | cur_arg = 1; |
| 9517 | |
| 9518 | if (!*args[cur_arg] || |
| 9519 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9520 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9521 | file, linenum, args[0]); |
| 9522 | goto out_err; |
| 9523 | } |
| 9524 | |
| 9525 | LIST_INIT(&rule->arg.map.key); |
| 9526 | proxy->conf.args.ctx = ARGC_HRS; |
| 9527 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9528 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9529 | file, linenum); |
| 9530 | free(proxy->conf.lfs_file); |
| 9531 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9532 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9533 | cur_arg += 1; |
| 9534 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9535 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9536 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9537 | /* |
| 9538 | * '+ 8' for 'del-map(' |
| 9539 | * '- 9' for 'del-map(' + trailing ')' |
| 9540 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9541 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9542 | |
| 9543 | cur_arg = 1; |
| 9544 | |
| 9545 | if (!*args[cur_arg] || |
| 9546 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9547 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9548 | file, linenum, args[0]); |
| 9549 | goto out_err; |
| 9550 | } |
| 9551 | |
| 9552 | LIST_INIT(&rule->arg.map.key); |
| 9553 | proxy->conf.args.ctx = ARGC_HRS; |
| 9554 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9555 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9556 | file, linenum); |
| 9557 | free(proxy->conf.lfs_file); |
| 9558 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9559 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9560 | cur_arg += 1; |
| 9561 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9562 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9563 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9564 | /* |
| 9565 | * '+ 8' for 'set-map(' |
| 9566 | * '- 9' for 'set-map(' + trailing ')' |
| 9567 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9568 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9569 | |
| 9570 | cur_arg = 1; |
| 9571 | |
| 9572 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9573 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9574 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9575 | file, linenum, args[0]); |
| 9576 | goto out_err; |
| 9577 | } |
| 9578 | |
| 9579 | LIST_INIT(&rule->arg.map.key); |
| 9580 | LIST_INIT(&rule->arg.map.value); |
| 9581 | |
| 9582 | proxy->conf.args.ctx = ARGC_HRS; |
| 9583 | |
| 9584 | /* key pattern */ |
| 9585 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9586 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9587 | file, linenum); |
| 9588 | |
| 9589 | /* value pattern */ |
| 9590 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9591 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9592 | file, linenum); |
| 9593 | |
| 9594 | free(proxy->conf.lfs_file); |
| 9595 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9596 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9597 | |
| 9598 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9599 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9600 | struct redirect_rule *redir; |
| 9601 | char *errmsg = NULL; |
| 9602 | |
| 9603 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9604 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9605 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9606 | goto out_err; |
| 9607 | } |
| 9608 | |
| 9609 | /* this redirect rule might already contain a parsed condition which |
| 9610 | * we'll pass to the http-request rule. |
| 9611 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9612 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9613 | rule->arg.redir = redir; |
| 9614 | rule->cond = redir->cond; |
| 9615 | redir->cond = NULL; |
| 9616 | cur_arg = 2; |
| 9617 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9618 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9619 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 9620 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 9621 | struct sample_expr *expr; |
| 9622 | unsigned int where; |
| 9623 | char *err = NULL; |
| 9624 | |
| 9625 | cur_arg = 1; |
| 9626 | proxy->conf.args.ctx = ARGC_TRK; |
| 9627 | |
| 9628 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9629 | if (!expr) { |
| 9630 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9631 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9632 | free(err); |
| 9633 | goto out_err; |
| 9634 | } |
| 9635 | |
| 9636 | where = 0; |
| 9637 | if (proxy->cap & PR_CAP_FE) |
| 9638 | where |= SMP_VAL_FE_HRS_HDR; |
| 9639 | if (proxy->cap & PR_CAP_BE) |
| 9640 | where |= SMP_VAL_BE_HRS_HDR; |
| 9641 | |
| 9642 | if (!(expr->fetch->val & where)) { |
| 9643 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9644 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9645 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9646 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9647 | free(expr); |
| 9648 | goto out_err; |
| 9649 | } |
| 9650 | |
| 9651 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9652 | cur_arg++; |
| 9653 | if (!args[cur_arg]) { |
| 9654 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9655 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9656 | free(expr); |
| 9657 | goto out_err; |
| 9658 | } |
| 9659 | /* we copy the table name for now, it will be resolved later */ |
| 9660 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9661 | cur_arg++; |
| 9662 | } |
| 9663 | rule->arg.trk_ctr.expr = expr; |
| 9664 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9665 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9666 | char *errmsg = NULL; |
| 9667 | cur_arg = 1; |
| 9668 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9669 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9670 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9671 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9672 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9673 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9674 | free(errmsg); |
| 9675 | goto out_err; |
| 9676 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9677 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9678 | action_build_list(&http_res_keywords.list, &trash); |
| 9679 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9680 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9681 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9682 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9683 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9684 | goto out_err; |
| 9685 | } |
| 9686 | |
| 9687 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9688 | struct acl_cond *cond; |
| 9689 | char *errmsg = NULL; |
| 9690 | |
| 9691 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9692 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9693 | file, linenum, args[0], errmsg); |
| 9694 | free(errmsg); |
| 9695 | goto out_err; |
| 9696 | } |
| 9697 | rule->cond = cond; |
| 9698 | } |
| 9699 | else if (*args[cur_arg]) { |
| 9700 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9701 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9702 | file, linenum, args[0], args[cur_arg]); |
| 9703 | goto out_err; |
| 9704 | } |
| 9705 | |
| 9706 | return rule; |
| 9707 | out_err: |
| 9708 | free(rule); |
| 9709 | return NULL; |
| 9710 | } |
| 9711 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9712 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9713 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9714 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9715 | * the direction of the rule, and equals 0 for request, non-zero for responses. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9716 | */ |
| 9717 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9718 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9719 | { |
| 9720 | struct redirect_rule *rule; |
| 9721 | int cur_arg; |
| 9722 | int type = REDIRECT_TYPE_NONE; |
| 9723 | int code = 302; |
| 9724 | const char *destination = NULL; |
| 9725 | const char *cookie = NULL; |
| 9726 | int cookie_set = 0; |
| 9727 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9728 | struct acl_cond *cond = NULL; |
| 9729 | |
| 9730 | cur_arg = 0; |
| 9731 | while (*(args[cur_arg])) { |
| 9732 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9733 | if (!*args[cur_arg + 1]) |
| 9734 | goto missing_arg; |
| 9735 | |
| 9736 | type = REDIRECT_TYPE_LOCATION; |
| 9737 | cur_arg++; |
| 9738 | destination = args[cur_arg]; |
| 9739 | } |
| 9740 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9741 | if (!*args[cur_arg + 1]) |
| 9742 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9743 | type = REDIRECT_TYPE_PREFIX; |
| 9744 | cur_arg++; |
| 9745 | destination = args[cur_arg]; |
| 9746 | } |
| 9747 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9748 | if (!*args[cur_arg + 1]) |
| 9749 | goto missing_arg; |
| 9750 | |
| 9751 | type = REDIRECT_TYPE_SCHEME; |
| 9752 | cur_arg++; |
| 9753 | destination = args[cur_arg]; |
| 9754 | } |
| 9755 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9756 | if (!*args[cur_arg + 1]) |
| 9757 | goto missing_arg; |
| 9758 | |
| 9759 | cur_arg++; |
| 9760 | cookie = args[cur_arg]; |
| 9761 | cookie_set = 1; |
| 9762 | } |
| 9763 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9764 | if (!*args[cur_arg + 1]) |
| 9765 | goto missing_arg; |
| 9766 | |
| 9767 | cur_arg++; |
| 9768 | cookie = args[cur_arg]; |
| 9769 | cookie_set = 0; |
| 9770 | } |
| 9771 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9772 | if (!*args[cur_arg + 1]) |
| 9773 | goto missing_arg; |
| 9774 | |
| 9775 | cur_arg++; |
| 9776 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9777 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9778 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9779 | "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)", |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9780 | args[cur_arg - 1], args[cur_arg]); |
| 9781 | return NULL; |
| 9782 | } |
| 9783 | } |
| 9784 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9785 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9786 | } |
| 9787 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9788 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9789 | } |
| 9790 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9791 | strcmp(args[cur_arg], "unless") == 0) { |
| 9792 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9793 | if (!cond) { |
| 9794 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9795 | return NULL; |
| 9796 | } |
| 9797 | break; |
| 9798 | } |
| 9799 | else { |
| 9800 | memprintf(errmsg, |
| 9801 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9802 | args[cur_arg]); |
| 9803 | return NULL; |
| 9804 | } |
| 9805 | cur_arg++; |
| 9806 | } |
| 9807 | |
| 9808 | if (type == REDIRECT_TYPE_NONE) { |
| 9809 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9810 | return NULL; |
| 9811 | } |
| 9812 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9813 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9814 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9815 | return NULL; |
| 9816 | } |
| 9817 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9818 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9819 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9820 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9821 | |
| 9822 | if (!use_fmt) { |
| 9823 | /* old-style static redirect rule */ |
| 9824 | rule->rdr_str = strdup(destination); |
| 9825 | rule->rdr_len = strlen(destination); |
| 9826 | } |
| 9827 | else { |
| 9828 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9829 | |
| 9830 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9831 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9832 | * makes it hard for the user to configure a self-redirection. |
| 9833 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9834 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9835 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9836 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9837 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 9838 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9839 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9840 | free(curproxy->conf.lfs_file); |
| 9841 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9842 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9843 | } |
| 9844 | } |
| 9845 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9846 | if (cookie) { |
| 9847 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9848 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9849 | */ |
| 9850 | rule->cookie_len = strlen(cookie); |
| 9851 | if (cookie_set) { |
| 9852 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9853 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9854 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9855 | rule->cookie_len += 9; |
| 9856 | } else { |
| 9857 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9858 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9859 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9860 | rule->cookie_len += 20; |
| 9861 | } |
| 9862 | } |
| 9863 | rule->type = type; |
| 9864 | rule->code = code; |
| 9865 | rule->flags = flags; |
| 9866 | LIST_INIT(&rule->list); |
| 9867 | return rule; |
| 9868 | |
| 9869 | missing_arg: |
| 9870 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9871 | return NULL; |
| 9872 | } |
| 9873 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9874 | /************************************************************************/ |
| 9875 | /* The code below is dedicated to ACL parsing and matching */ |
| 9876 | /************************************************************************/ |
| 9877 | |
| 9878 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9879 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9880 | * which means that a request or response is ready. If some data is missing, |
| 9881 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9882 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9883 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9884 | * |
| 9885 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9886 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9887 | * decide whether or not an HTTP message is present ; |
| 9888 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9889 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9890 | * 1 if an HTTP message is ready |
| 9891 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9892 | int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9893 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9894 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9895 | struct http_txn *txn; |
| 9896 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9897 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9898 | /* Note: it is possible that <s> is NULL when called before stream |
| 9899 | * initialization (eg: tcp-request connection), so this function is the |
| 9900 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9901 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9902 | if (!s) |
| 9903 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9904 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9905 | if (!s->txn) { |
| 9906 | if (unlikely(!http_alloc_txn(s))) |
| 9907 | return 0; /* not enough memory */ |
| 9908 | http_init_txn(s); |
| 9909 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9910 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9911 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9912 | |
| 9913 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9914 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9915 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9916 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9917 | /* If the buffer does not leave enough free space at the end, |
| 9918 | * we must first realign it. |
| 9919 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9920 | if (s->req.buf->p > s->req.buf->data && |
| 9921 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9922 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9923 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9924 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9925 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9926 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9927 | |
| 9928 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9929 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9930 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9931 | |
| 9932 | /* Still no valid request ? */ |
| 9933 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9934 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9935 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9936 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9937 | } |
| 9938 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9939 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9940 | return 0; |
| 9941 | } |
| 9942 | |
| 9943 | /* OK we just got a valid HTTP request. We have some minor |
| 9944 | * preparation to perform so that further checks can rely |
| 9945 | * on HTTP tests. |
| 9946 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9947 | |
| 9948 | /* If the request was parsed but was too large, we must absolutely |
| 9949 | * return an error so that it is not processed. At the moment this |
| 9950 | * cannot happen, but if the parsers are to change in the future, |
| 9951 | * we want this check to be maintained. |
| 9952 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9953 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9954 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9955 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9956 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9957 | return 1; |
| 9958 | } |
| 9959 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9960 | txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l); |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9961 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9962 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9963 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9964 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9965 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9966 | } |
| 9967 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9968 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9969 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9970 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9971 | |
| 9972 | /* otherwise everything's ready for the request */ |
| 9973 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9974 | else { |
| 9975 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9976 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9977 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9978 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9979 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9980 | } |
| 9981 | |
| 9982 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9983 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9984 | return 1; |
| 9985 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9986 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9987 | /* 1. Check on METHOD |
| 9988 | * We use the pre-parsed method if it is known, and store its number as an |
| 9989 | * integer. If it is unknown, we use the pointer and the length. |
| 9990 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9991 | static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9992 | { |
| 9993 | int len, meth; |
| 9994 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9995 | len = strlen(text); |
| 9996 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9997 | |
| 9998 | pattern->val.i = meth; |
| 9999 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10000 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10001 | pattern->len = len; |
| 10002 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10003 | else { |
| 10004 | pattern->ptr.str = NULL; |
| 10005 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10006 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10007 | return 1; |
| 10008 | } |
| 10009 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10010 | /* This function fetches the method of current HTTP request and stores |
| 10011 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10012 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10013 | * in <len> and <ptr> is NULL ; |
| 10014 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10015 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10016 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10017 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10018 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10019 | smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10020 | { |
| 10021 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10022 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10023 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10024 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10025 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10026 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10027 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10028 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10029 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10030 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10031 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10032 | /* ensure the indexes are not affected */ |
| 10033 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10034 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10035 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10036 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10037 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10038 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10039 | return 1; |
| 10040 | } |
| 10041 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10042 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10043 | static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10044 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10045 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10046 | struct pattern_list *lst; |
| 10047 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10048 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10049 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10050 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10051 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10052 | /* well-known method */ |
| 10053 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10054 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10055 | return pattern; |
| 10056 | else |
| 10057 | continue; |
| 10058 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10059 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10060 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10061 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10062 | continue; |
| 10063 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10064 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10065 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10066 | (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10067 | return pattern; |
| 10068 | } |
| 10069 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10070 | } |
| 10071 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10072 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10073 | smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10074 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10075 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10076 | char *ptr; |
| 10077 | int len; |
| 10078 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10079 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10080 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10081 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10082 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10083 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10084 | |
| 10085 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10086 | if (len <= 0) |
| 10087 | return 0; |
| 10088 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10089 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10090 | smp->data.u.str.str = ptr; |
| 10091 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10092 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10093 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10094 | return 1; |
| 10095 | } |
| 10096 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10097 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10098 | smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10099 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10100 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10101 | char *ptr; |
| 10102 | int len; |
| 10103 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10104 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10105 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10106 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10107 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10108 | return 0; |
| 10109 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10110 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10111 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10112 | |
| 10113 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10114 | if (len <= 0) |
| 10115 | return 0; |
| 10116 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10117 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10118 | smp->data.u.str.str = ptr; |
| 10119 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10120 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10121 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10122 | return 1; |
| 10123 | } |
| 10124 | |
| 10125 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10126 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10127 | smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10128 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10129 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10130 | char *ptr; |
| 10131 | int len; |
| 10132 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10133 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10134 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10135 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10136 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10137 | return 0; |
| 10138 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10139 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10140 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10141 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10142 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10143 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10144 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10145 | return 1; |
| 10146 | } |
| 10147 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 10148 | static int |
| 10149 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10150 | { |
| 10151 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 10152 | return 0; |
| 10153 | |
| 10154 | if (!smp->strm->unique_id) { |
| 10155 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 10156 | return 0; |
| 10157 | smp->strm->unique_id[0] = '\0'; |
| 10158 | } |
| 10159 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 10160 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 10161 | |
| 10162 | smp->data.type = SMP_T_STR; |
| 10163 | smp->data.u.str.str = smp->strm->unique_id; |
| 10164 | smp->flags = SMP_F_CONST; |
| 10165 | return 1; |
| 10166 | } |
| 10167 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10168 | /* returns the longest available part of the body. This requires that the body |
| 10169 | * has been waited for using http-buffer-request. |
| 10170 | */ |
| 10171 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10172 | smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10173 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10174 | struct http_msg *msg; |
| 10175 | unsigned long len; |
| 10176 | unsigned long block1; |
| 10177 | char *body; |
| 10178 | struct chunk *temp; |
| 10179 | |
| 10180 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10181 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10182 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10183 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10184 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10185 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10186 | |
| 10187 | len = http_body_bytes(msg); |
| 10188 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10189 | |
| 10190 | block1 = len; |
| 10191 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10192 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10193 | |
| 10194 | if (block1 == len) { |
| 10195 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10196 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10197 | smp->data.u.str.str = body; |
| 10198 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10199 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10200 | } |
| 10201 | else { |
| 10202 | /* buffer is wrapped, we need to defragment it */ |
| 10203 | temp = get_trash_chunk(); |
| 10204 | memcpy(temp->str, body, block1); |
| 10205 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10206 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10207 | smp->data.u.str.str = temp->str; |
| 10208 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10209 | smp->flags = SMP_F_VOL_TEST; |
| 10210 | } |
| 10211 | return 1; |
| 10212 | } |
| 10213 | |
| 10214 | |
| 10215 | /* returns the available length of the body. This requires that the body |
| 10216 | * has been waited for using http-buffer-request. |
| 10217 | */ |
| 10218 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10219 | smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10220 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10221 | struct http_msg *msg; |
| 10222 | |
| 10223 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10224 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10225 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10226 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10227 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10228 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10229 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10230 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10231 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10232 | |
| 10233 | smp->flags = SMP_F_VOL_TEST; |
| 10234 | return 1; |
| 10235 | } |
| 10236 | |
| 10237 | |
| 10238 | /* returns the advertised length of the body, or the advertised size of the |
| 10239 | * chunks available in the buffer. This requires that the body has been waited |
| 10240 | * for using http-buffer-request. |
| 10241 | */ |
| 10242 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10243 | smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10244 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10245 | struct http_msg *msg; |
| 10246 | |
| 10247 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10248 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10249 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10250 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10251 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10252 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10253 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10254 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10255 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10256 | |
| 10257 | smp->flags = SMP_F_VOL_TEST; |
| 10258 | return 1; |
| 10259 | } |
| 10260 | |
| 10261 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10262 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10263 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10264 | smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10265 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10266 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10267 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10268 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10269 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10270 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10271 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10272 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10273 | smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10274 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10275 | return 1; |
| 10276 | } |
| 10277 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10278 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10279 | smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10280 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10281 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10282 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10283 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10284 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10285 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10286 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10287 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10288 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10289 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10290 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10291 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10292 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10293 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10294 | return 1; |
| 10295 | } |
| 10296 | |
| 10297 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10298 | smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10299 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10300 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10301 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10302 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10303 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10304 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10305 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10306 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10307 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10308 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10309 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10310 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10311 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10312 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10313 | return 1; |
| 10314 | } |
| 10315 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10316 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10317 | * Accepts an optional argument of type string containing the header field name, |
| 10318 | * and an optional argument of type signed or unsigned integer to request an |
| 10319 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10320 | * headers are considered from the first one. It does not stop on commas and |
| 10321 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10322 | */ |
| 10323 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10324 | smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10325 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10326 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10327 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10328 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10329 | int occ = 0; |
| 10330 | const char *name_str = NULL; |
| 10331 | int name_len = 0; |
| 10332 | |
| 10333 | if (!ctx) { |
| 10334 | /* first call */ |
| 10335 | ctx = &static_hdr_ctx; |
| 10336 | ctx->idx = 0; |
| 10337 | smp->ctx.a[0] = ctx; |
| 10338 | } |
| 10339 | |
| 10340 | if (args) { |
| 10341 | if (args[0].type != ARGT_STR) |
| 10342 | return 0; |
| 10343 | name_str = args[0].data.str.str; |
| 10344 | name_len = args[0].data.str.len; |
| 10345 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10346 | if (args[1].type == ARGT_SINT) |
| 10347 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10348 | } |
| 10349 | |
| 10350 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10351 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10352 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10353 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10354 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10355 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10356 | /* search for header from the beginning */ |
| 10357 | ctx->idx = 0; |
| 10358 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10359 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10360 | /* no explicit occurrence and single fetch => last header by default */ |
| 10361 | occ = -1; |
| 10362 | |
| 10363 | if (!occ) |
| 10364 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10365 | smp->flags |= SMP_F_NOT_LAST; |
| 10366 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10367 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10368 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10369 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10370 | return 1; |
| 10371 | |
| 10372 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10373 | return 0; |
| 10374 | } |
| 10375 | |
| 10376 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10377 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10378 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10379 | */ |
| 10380 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10381 | smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10382 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10383 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10384 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10385 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10386 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10387 | const char *name = NULL; |
| 10388 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10389 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10390 | if (args && args->type == ARGT_STR) { |
| 10391 | name = args->data.str.str; |
| 10392 | len = args->data.str.len; |
| 10393 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10394 | |
| 10395 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10396 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10397 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10398 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10399 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10400 | ctx.idx = 0; |
| 10401 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10402 | while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10403 | cnt++; |
| 10404 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10405 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10406 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10407 | smp->flags = SMP_F_VOL_HDR; |
| 10408 | return 1; |
| 10409 | } |
| 10410 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10411 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10412 | smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10413 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10414 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10415 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10416 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10417 | struct chunk *temp; |
| 10418 | char del = ','; |
| 10419 | |
| 10420 | if (args && args->type == ARGT_STR) |
| 10421 | del = *args[0].data.str.str; |
| 10422 | |
| 10423 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10424 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10425 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10426 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10427 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10428 | temp = get_trash_chunk(); |
| 10429 | |
| 10430 | ctx.idx = 0; |
| 10431 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10432 | if (temp->len) |
| 10433 | temp->str[temp->len++] = del; |
| 10434 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10435 | temp->len += ctx.del; |
| 10436 | } |
| 10437 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10438 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10439 | smp->data.u.str.str = temp->str; |
| 10440 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10441 | smp->flags = SMP_F_VOL_HDR; |
| 10442 | return 1; |
| 10443 | } |
| 10444 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10445 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10446 | * Accepts an optional argument of type string containing the header field name, |
| 10447 | * and an optional argument of type signed or unsigned integer to request an |
| 10448 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10449 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10450 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10451 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10452 | smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10453 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10454 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10455 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10456 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10457 | int occ = 0; |
| 10458 | const char *name_str = NULL; |
| 10459 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10460 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10461 | if (!ctx) { |
| 10462 | /* first call */ |
| 10463 | ctx = &static_hdr_ctx; |
| 10464 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10465 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10466 | } |
| 10467 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10468 | if (args) { |
| 10469 | if (args[0].type != ARGT_STR) |
| 10470 | return 0; |
| 10471 | name_str = args[0].data.str.str; |
| 10472 | name_len = args[0].data.str.len; |
| 10473 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10474 | if (args[1].type == ARGT_SINT) |
| 10475 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10476 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10477 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10478 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10479 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10480 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10481 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10482 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10483 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10484 | /* search for header from the beginning */ |
| 10485 | ctx->idx = 0; |
| 10486 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10487 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10488 | /* no explicit occurrence and single fetch => last header by default */ |
| 10489 | occ = -1; |
| 10490 | |
| 10491 | if (!occ) |
| 10492 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10493 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10494 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10495 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10496 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10497 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10498 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10499 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10500 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10501 | return 0; |
| 10502 | } |
| 10503 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10504 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10505 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10506 | */ |
| 10507 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10508 | smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10509 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10510 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10511 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10512 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10513 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10514 | const char *name = NULL; |
| 10515 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10516 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10517 | if (args && args->type == ARGT_STR) { |
| 10518 | name = args->data.str.str; |
| 10519 | len = args->data.str.len; |
| 10520 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10521 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10522 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10523 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10524 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10525 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10526 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10527 | ctx.idx = 0; |
| 10528 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10529 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10530 | cnt++; |
| 10531 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10532 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10533 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10534 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10535 | return 1; |
| 10536 | } |
| 10537 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10538 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10539 | * takes a mandatory argument of type string and an optional one of type int |
| 10540 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10541 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10542 | */ |
| 10543 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10544 | smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10545 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10546 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10547 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10548 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10549 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10550 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10551 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10552 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10553 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10554 | } |
| 10555 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10556 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10557 | * and an optional one of type int to designate a specific occurrence. |
| 10558 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10559 | */ |
| 10560 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10561 | smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10562 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10563 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10564 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10565 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10566 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10567 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10568 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10569 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10570 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10571 | if (smp->data.u.str.len < temp->size - 1) { |
| 10572 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10573 | temp->str[smp->data.u.str.len] = '\0'; |
| 10574 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10575 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10576 | break; |
| 10577 | } |
| 10578 | } |
| 10579 | } |
| 10580 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10581 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10582 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10583 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10584 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10585 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10586 | } |
| 10587 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10588 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10589 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10590 | */ |
| 10591 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10592 | smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10593 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10594 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10595 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10596 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10597 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10598 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10599 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10600 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 10601 | ptr = http_get_path(txn); |
| 10602 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10603 | return 0; |
| 10604 | |
| 10605 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10606 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10607 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10608 | |
| 10609 | while (ptr < end && *ptr != '?') |
| 10610 | ptr++; |
| 10611 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10612 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10613 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10614 | return 1; |
| 10615 | } |
| 10616 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10617 | /* This produces a concatenation of the first occurrence of the Host header |
| 10618 | * followed by the path component if it begins with a slash ('/'). This means |
| 10619 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10620 | * If no Host header is found, then the path is returned as-is. The returned |
| 10621 | * value is stored in the trash so it does not need to be marked constant. |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10622 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10623 | */ |
| 10624 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10625 | smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10626 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10627 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10628 | char *ptr, *end, *beg; |
| 10629 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10630 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10631 | |
| 10632 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10633 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10634 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10635 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10636 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10637 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10638 | |
| 10639 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10640 | temp = get_trash_chunk(); |
| 10641 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10642 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10643 | smp->data.u.str.str = temp->str; |
| 10644 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10645 | |
| 10646 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10647 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10648 | beg = http_get_path(txn); |
| 10649 | if (!beg) |
| 10650 | beg = end; |
| 10651 | |
| 10652 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10653 | |
| 10654 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10655 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10656 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10657 | } |
| 10658 | |
| 10659 | smp->flags = SMP_F_VOL_1ST; |
| 10660 | return 1; |
| 10661 | } |
| 10662 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10663 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10664 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10665 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10666 | * entry. If no Host header is found, then the path is used. The resulting value |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10667 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10668 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10669 | * high-traffic sites without having to store whole paths. |
| 10670 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10671 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10672 | smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10673 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10674 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10675 | struct hdr_ctx ctx; |
| 10676 | unsigned int hash = 0; |
| 10677 | char *ptr, *beg, *end; |
| 10678 | int len; |
| 10679 | |
| 10680 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10681 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10682 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10683 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10684 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10685 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10686 | ptr = ctx.line + ctx.val; |
| 10687 | len = ctx.vlen; |
| 10688 | while (len--) |
| 10689 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10690 | } |
| 10691 | |
| 10692 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10693 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10694 | beg = http_get_path(txn); |
| 10695 | if (!beg) |
| 10696 | beg = end; |
| 10697 | |
| 10698 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10699 | |
| 10700 | if (beg < ptr && *beg == '/') { |
| 10701 | while (beg < ptr) |
| 10702 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10703 | } |
| 10704 | hash = full_hash(hash); |
| 10705 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10706 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10707 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10708 | smp->flags = SMP_F_VOL_1ST; |
| 10709 | return 1; |
| 10710 | } |
| 10711 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10712 | /* This concatenates the source address with the 32-bit hash of the Host and |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10713 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10714 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10715 | * on the source address length. The path hash is stored before the address so |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10716 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10717 | * 8 bytes would still work. |
| 10718 | */ |
| 10719 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10720 | smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10721 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10722 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10723 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10724 | |
| 10725 | if (!cli_conn) |
| 10726 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10727 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10728 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10729 | return 0; |
| 10730 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10731 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10732 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10733 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10734 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10735 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10736 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10737 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10738 | temp->len += 4; |
| 10739 | break; |
| 10740 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10741 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10742 | temp->len += 16; |
| 10743 | break; |
| 10744 | default: |
| 10745 | return 0; |
| 10746 | } |
| 10747 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10748 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10749 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10750 | return 1; |
| 10751 | } |
| 10752 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10753 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10754 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10755 | * of type string carrying the whole query string. |
| 10756 | */ |
| 10757 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10758 | smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10759 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10760 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10761 | char *ptr, *end; |
| 10762 | |
| 10763 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10764 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10765 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10766 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10767 | end = ptr + txn->req.sl.rq.u_l; |
| 10768 | |
| 10769 | /* look up the '?' */ |
| 10770 | do { |
| 10771 | if (ptr == end) |
| 10772 | return 0; |
| 10773 | } while (*ptr++ != '?'); |
| 10774 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10775 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10776 | smp->data.u.str.str = ptr; |
| 10777 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10778 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10779 | return 1; |
| 10780 | } |
| 10781 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10782 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10783 | smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10784 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10785 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10786 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10787 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10788 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10789 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10790 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10791 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10792 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10793 | return 1; |
| 10794 | } |
| 10795 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10796 | /* return a valid test if the current request is the first one on the connection */ |
| 10797 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10798 | smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10799 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10800 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10801 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10802 | return 1; |
| 10803 | } |
| 10804 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10805 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10806 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10807 | smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10808 | { |
| 10809 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10810 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10811 | return 0; |
| 10812 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10813 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10814 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10815 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10816 | return 0; |
| 10817 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10818 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10819 | smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user, |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10820 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10821 | return 1; |
| 10822 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10823 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10824 | /* Accepts exactly 1 argument of type userlist */ |
| 10825 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10826 | smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10827 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10828 | if (!args || args->type != ARGT_USR) |
| 10829 | return 0; |
| 10830 | |
| 10831 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10832 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10833 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10834 | return 0; |
| 10835 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10836 | /* if the user does not belong to the userlist or has a wrong password, |
| 10837 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10838 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10839 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10840 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10841 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10842 | return 0; |
| 10843 | |
| 10844 | /* pat_match_auth() will need the user list */ |
| 10845 | smp->ctx.a[0] = args->data.usr; |
| 10846 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10847 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10848 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10849 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10850 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10851 | |
| 10852 | return 1; |
| 10853 | } |
| 10854 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10855 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10856 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10857 | * the cookie value is returned into *value and *value_l, and the function |
| 10858 | * returns a pointer to the next pointer to search from if the value was found. |
| 10859 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10860 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10861 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10862 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10863 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10864 | * list is expected. |
| 10865 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10866 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10867 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10868 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10869 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10870 | { |
| 10871 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10872 | char *next; |
| 10873 | |
| 10874 | /* we search at least a cookie name followed by an equal, and more |
| 10875 | * generally something like this : |
| 10876 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10877 | */ |
| 10878 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10879 | /* Iterate through all cookies on this line */ |
| 10880 | |
| 10881 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10882 | att_beg++; |
| 10883 | |
| 10884 | /* find att_end : this is the first character after the last non |
| 10885 | * space before the equal. It may be equal to hdr_end. |
| 10886 | */ |
| 10887 | equal = att_end = att_beg; |
| 10888 | |
| 10889 | while (equal < hdr_end) { |
| 10890 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10891 | break; |
| 10892 | if (http_is_spht[(unsigned char)*equal++]) |
| 10893 | continue; |
| 10894 | att_end = equal; |
| 10895 | } |
| 10896 | |
| 10897 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10898 | * is between <att_beg> and <equal>, both may be identical. |
| 10899 | */ |
| 10900 | |
| 10901 | /* look for end of cookie if there is an equal sign */ |
| 10902 | if (equal < hdr_end && *equal == '=') { |
| 10903 | /* look for the beginning of the value */ |
| 10904 | val_beg = equal + 1; |
| 10905 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10906 | val_beg++; |
| 10907 | |
| 10908 | /* find the end of the value, respecting quotes */ |
| 10909 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10910 | |
| 10911 | /* make val_end point to the first white space or delimitor after the value */ |
| 10912 | val_end = next; |
| 10913 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10914 | val_end--; |
| 10915 | } else { |
| 10916 | val_beg = val_end = next = equal; |
| 10917 | } |
| 10918 | |
| 10919 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10920 | * they will automatically be removed if a header before them is removed, |
| 10921 | * since they're supposed to be linked together. |
| 10922 | */ |
| 10923 | if (*att_beg == '$') |
| 10924 | continue; |
| 10925 | |
| 10926 | /* Ignore cookies with no equal sign */ |
| 10927 | if (equal == next) |
| 10928 | continue; |
| 10929 | |
| 10930 | /* Now we have the cookie name between att_beg and att_end, and |
| 10931 | * its value between val_beg and val_end. |
| 10932 | */ |
| 10933 | |
| 10934 | if (att_end - att_beg == cookie_name_l && |
| 10935 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10936 | /* let's return this value and indicate where to go on from */ |
| 10937 | *value = val_beg; |
| 10938 | *value_l = val_end - val_beg; |
| 10939 | return next + 1; |
| 10940 | } |
| 10941 | |
| 10942 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10943 | if (!list) |
| 10944 | break; |
| 10945 | } |
| 10946 | |
| 10947 | return NULL; |
| 10948 | } |
| 10949 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10950 | /* Fetch a captured HTTP request header. The index is the position of |
| 10951 | * the "capture" option in the configuration file |
| 10952 | */ |
| 10953 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10954 | smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10955 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10956 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10957 | int idx; |
| 10958 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10959 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10960 | return 0; |
| 10961 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10962 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10963 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10964 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10965 | return 0; |
| 10966 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10967 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10968 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10969 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10970 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10971 | |
| 10972 | return 1; |
| 10973 | } |
| 10974 | |
| 10975 | /* Fetch a captured HTTP response header. The index is the position of |
| 10976 | * the "capture" option in the configuration file |
| 10977 | */ |
| 10978 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10979 | smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10980 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10981 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10982 | int idx; |
| 10983 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10984 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10985 | return 0; |
| 10986 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10987 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10988 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10989 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10990 | return 0; |
| 10991 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10992 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10993 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10994 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10995 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10996 | |
| 10997 | return 1; |
| 10998 | } |
| 10999 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11000 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11001 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11002 | smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11003 | { |
| 11004 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11005 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11006 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11007 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11008 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11009 | return 0; |
| 11010 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11011 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11012 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11013 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11014 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11015 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11016 | temp = get_trash_chunk(); |
| 11017 | temp->str = txn->uri; |
| 11018 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11019 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11020 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11021 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11022 | |
| 11023 | return 1; |
| 11024 | |
| 11025 | } |
| 11026 | |
| 11027 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11028 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11029 | smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11030 | { |
| 11031 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11032 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11033 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11034 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11035 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11036 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11037 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11038 | ptr = txn->uri; |
| 11039 | |
| 11040 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11041 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11042 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11043 | if (!*ptr) |
| 11044 | return 0; |
| 11045 | |
| 11046 | ptr++; /* skip the space */ |
| 11047 | |
| 11048 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11049 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11050 | if (!ptr) |
| 11051 | return 0; |
| 11052 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11053 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11054 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11055 | smp->data.u.str = *temp; |
| 11056 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11057 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11058 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11059 | |
| 11060 | return 1; |
| 11061 | } |
| 11062 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11063 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11064 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11065 | */ |
| 11066 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11067 | smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11068 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11069 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11070 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11071 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11072 | return 0; |
| 11073 | |
| 11074 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11075 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11076 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11077 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11078 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11079 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11080 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11081 | smp->flags = SMP_F_CONST; |
| 11082 | return 1; |
| 11083 | |
| 11084 | } |
| 11085 | |
| 11086 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11087 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11088 | */ |
| 11089 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11090 | smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11091 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11092 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11093 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11094 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11095 | return 0; |
| 11096 | |
| 11097 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11098 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11099 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11100 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11101 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11102 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11103 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11104 | smp->flags = SMP_F_CONST; |
| 11105 | return 1; |
| 11106 | |
| 11107 | } |
| 11108 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11109 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11110 | /* Iterate over all cookies present in a message. The context is stored in |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11111 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11112 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11113 | * the direction, multiple cookies may be parsed on the same line or not. |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11114 | * The cookie name is in args and the name length in args->data.str.len. |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11115 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11116 | * that no iterating is desired, then only last value is fetched if any. |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 11117 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11118 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11119 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11120 | int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11121 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11122 | struct http_txn *txn; |
| 11123 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11124 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11125 | const struct http_msg *msg; |
| 11126 | const char *hdr_name; |
| 11127 | int hdr_name_len; |
| 11128 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11129 | int occ = 0; |
| 11130 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11131 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11132 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11133 | return 0; |
| 11134 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11135 | if (!ctx) { |
| 11136 | /* first call */ |
| 11137 | ctx = &static_hdr_ctx; |
| 11138 | ctx->idx = 0; |
| 11139 | smp->ctx.a[2] = ctx; |
| 11140 | } |
| 11141 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11142 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11143 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11144 | txn = smp->strm->txn; |
| 11145 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11146 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11147 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11148 | msg = &txn->req; |
| 11149 | hdr_name = "Cookie"; |
| 11150 | hdr_name_len = 6; |
| 11151 | } else { |
| 11152 | msg = &txn->rsp; |
| 11153 | hdr_name = "Set-Cookie"; |
| 11154 | hdr_name_len = 10; |
| 11155 | } |
| 11156 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11157 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11158 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11159 | occ = -1; |
| 11160 | |
| 11161 | /* OK so basically here, either we want only one value and it's the |
| 11162 | * last one, or we want to iterate over all of them and we fetch the |
| 11163 | * next one. |
| 11164 | */ |
| 11165 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11166 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11167 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11168 | /* search for the header from the beginning, we must first initialize |
| 11169 | * the search parameters. |
| 11170 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11171 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11172 | ctx->idx = 0; |
| 11173 | } |
| 11174 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11175 | smp->flags |= SMP_F_VOL_HDR; |
| 11176 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11177 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11178 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11179 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11180 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11181 | goto out; |
| 11182 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11183 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11184 | continue; |
| 11185 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11186 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11187 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11188 | } |
| 11189 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11190 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11191 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11192 | smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11193 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11194 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11195 | &smp->data.u.str.str, |
| 11196 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11197 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11198 | found = 1; |
| 11199 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11200 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11201 | smp->flags |= SMP_F_NOT_LAST; |
| 11202 | return 1; |
| 11203 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11204 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11205 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11206 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11207 | /* all cookie headers and values were scanned. If we're looking for the |
| 11208 | * last occurrence, we may return it now. |
| 11209 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11210 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11211 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11212 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11213 | } |
| 11214 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11215 | /* Iterate over all cookies present in a request to count how many occurrences |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11216 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11217 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11218 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11219 | */ |
| 11220 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11221 | smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11222 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11223 | struct http_txn *txn; |
| 11224 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11225 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11226 | const struct http_msg *msg; |
| 11227 | const char *hdr_name; |
| 11228 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11229 | int cnt; |
| 11230 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11231 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11232 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11233 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11234 | return 0; |
| 11235 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11236 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11237 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11238 | txn = smp->strm->txn; |
| 11239 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11240 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11241 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11242 | msg = &txn->req; |
| 11243 | hdr_name = "Cookie"; |
| 11244 | hdr_name_len = 6; |
| 11245 | } else { |
| 11246 | msg = &txn->rsp; |
| 11247 | hdr_name = "Set-Cookie"; |
| 11248 | hdr_name_len = 10; |
| 11249 | } |
| 11250 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11251 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11252 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11253 | ctx.idx = 0; |
| 11254 | cnt = 0; |
| 11255 | |
| 11256 | while (1) { |
| 11257 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11258 | if (!val_beg) { |
| 11259 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11260 | break; |
| 11261 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11262 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11263 | continue; |
| 11264 | |
| 11265 | val_beg = ctx.line + ctx.val; |
| 11266 | val_end = val_beg + ctx.vlen; |
| 11267 | } |
| 11268 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11269 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11270 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11271 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11272 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11273 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11274 | &smp->data.u.str.str, |
| 11275 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11276 | cnt++; |
| 11277 | } |
| 11278 | } |
| 11279 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11280 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11281 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11282 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11283 | return 1; |
| 11284 | } |
| 11285 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11286 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11287 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11288 | */ |
| 11289 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11290 | smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11291 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11292 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11293 | |
| 11294 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11295 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11296 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11297 | } |
| 11298 | |
| 11299 | return ret; |
| 11300 | } |
| 11301 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11302 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11303 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11304 | /************************************************************************/ |
| 11305 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11306 | /* |
| 11307 | * Given a path string and its length, find the position of beginning of the |
| 11308 | * query string. Returns NULL if no query string is found in the path. |
| 11309 | * |
| 11310 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11311 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11312 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11313 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11314 | static inline char *find_param_list(char *path, size_t path_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11315 | { |
| 11316 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11317 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11318 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11319 | return p ? p + 1 : NULL; |
| 11320 | } |
| 11321 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11322 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11323 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11324 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11325 | } |
| 11326 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11327 | /* after increasing a pointer value, it can exceed the first buffer |
| 11328 | * size. This function transform the value of <ptr> according with |
| 11329 | * the expected position. <chunks> is an array of the one or two |
| 11330 | * avalaible chunks. The first value is the start of the first chunk, |
| 11331 | * the second value if the end+1 of the first chunks. The third value |
| 11332 | * is NULL or the start of the second chunk and the fourth value is |
| 11333 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11334 | * wrap, else returns 0. |
| 11335 | */ |
| 11336 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11337 | { |
| 11338 | if (*ptr < chunks[1]) |
| 11339 | return 0; |
| 11340 | if (!chunks[2]) |
| 11341 | return 0; |
| 11342 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11343 | return 1; |
| 11344 | } |
| 11345 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11346 | /* |
| 11347 | * Given a url parameter, find the starting position of the first occurence, |
| 11348 | * or NULL if the parameter is not found. |
| 11349 | * |
| 11350 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11351 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11352 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11353 | * Warning: this function returns a pointer that can point to the first chunk |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11354 | * or the second chunk. The caller must be check the position before using the |
| 11355 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11356 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11357 | static const char * |
| 11358 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11359 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11360 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11361 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11362 | const char *pos, *last, *equal; |
| 11363 | const char **bufs = chunks; |
| 11364 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11365 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11366 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11367 | pos = bufs[0]; |
| 11368 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11369 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11370 | /* Check the equal. */ |
| 11371 | equal = pos + url_param_name_l; |
| 11372 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11373 | if (equal >= chunks[3]) |
| 11374 | return NULL; |
| 11375 | } else { |
| 11376 | if (equal >= chunks[1]) |
| 11377 | return NULL; |
| 11378 | } |
| 11379 | if (*equal == '=') { |
| 11380 | if (pos + url_param_name_l > last) { |
| 11381 | /* process wrap case, we detect a wrap. In this case, the |
| 11382 | * comparison is performed in two parts. |
| 11383 | */ |
| 11384 | |
| 11385 | /* This is the end, we dont have any other chunk. */ |
| 11386 | if (bufs != chunks || !bufs[2]) |
| 11387 | return NULL; |
| 11388 | |
| 11389 | /* Compute the length of each part of the comparison. */ |
| 11390 | l1 = last - pos; |
| 11391 | l2 = url_param_name_l - l1; |
| 11392 | |
| 11393 | /* The second buffer is too short to contain the compared string. */ |
| 11394 | if (bufs[2] + l2 > bufs[3]) |
| 11395 | return NULL; |
| 11396 | |
| 11397 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11398 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11399 | return pos; |
| 11400 | |
| 11401 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11402 | bufs += 2; |
| 11403 | pos = bufs[0] + l2; |
| 11404 | last = bufs[1]; |
| 11405 | |
| 11406 | } else { |
| 11407 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11408 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11409 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11410 | pos += url_param_name_l + 1; |
| 11411 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11412 | last = bufs[2]; |
| 11413 | } |
| 11414 | } |
| 11415 | |
| 11416 | while (1) { |
| 11417 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11418 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11419 | pos++; |
| 11420 | if (pos < last) |
| 11421 | break; |
| 11422 | /* process buffer wrapping. */ |
| 11423 | if (bufs != chunks || !bufs[2]) |
| 11424 | return NULL; |
| 11425 | bufs += 2; |
| 11426 | pos = bufs[0]; |
| 11427 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11428 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11429 | pos++; |
| 11430 | } |
| 11431 | return NULL; |
| 11432 | } |
| 11433 | |
| 11434 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11435 | * Given a url parameter name and a query string, find the next value. |
| 11436 | * An empty url_param_name matches the first available parameter. |
| 11437 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11438 | * respectively provide a pointer to the value and its end. |
| 11439 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11440 | */ |
| 11441 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11442 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11443 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11444 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11445 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11446 | const char *arg_start, *qs_end; |
| 11447 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11448 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11449 | arg_start = chunks[0]; |
| 11450 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11451 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11452 | /* Looks for an argument name. */ |
| 11453 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11454 | url_param_name, url_param_name_l, |
| 11455 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11456 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11457 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11458 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11459 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11460 | if (!arg_start) |
| 11461 | return 0; |
| 11462 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11463 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11464 | while (1) { |
| 11465 | /* looks for the first argument. */ |
| 11466 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11467 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11468 | /* Check for wrapping. */ |
| 11469 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11470 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11471 | chunks[2]) { |
| 11472 | arg_start = chunks[2]; |
| 11473 | qs_end = chunks[3]; |
| 11474 | continue; |
| 11475 | } |
| 11476 | return 0; |
| 11477 | } |
| 11478 | break; |
| 11479 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11480 | value_start++; |
| 11481 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11482 | else { |
| 11483 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11484 | value_start = arg_start + url_param_name_l + 1; |
| 11485 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11486 | /* Check for pointer wrapping. */ |
| 11487 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11488 | /* Update the end pointer. */ |
| 11489 | qs_end = chunks[3]; |
| 11490 | |
| 11491 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11492 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11493 | return 0; |
| 11494 | } |
| 11495 | } |
| 11496 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11497 | value_end = value_start; |
| 11498 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11499 | while (1) { |
| 11500 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11501 | value_end++; |
| 11502 | if (value_end < qs_end) |
| 11503 | break; |
| 11504 | /* process buffer wrapping. */ |
| 11505 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11506 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11507 | chunks[2]) { |
| 11508 | value_end = chunks[2]; |
| 11509 | qs_end = chunks[3]; |
| 11510 | continue; |
| 11511 | } |
| 11512 | break; |
| 11513 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11514 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11515 | *vstart = value_start; |
| 11516 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11517 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11518 | } |
| 11519 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11520 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11521 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11522 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11523 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11524 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11525 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11526 | smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11527 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11528 | const char *vstart, *vend; |
| 11529 | struct chunk *temp; |
| 11530 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11531 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11532 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11533 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11534 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11535 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11536 | return 0; |
| 11537 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11538 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11539 | * if the value is wrapped, copy-it in a buffer. |
| 11540 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11541 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11542 | if (chunks[2] && |
| 11543 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11544 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11545 | /* Wrapped case. */ |
| 11546 | temp = get_trash_chunk(); |
| 11547 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11548 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11549 | smp->data.u.str.str = temp->str; |
| 11550 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11551 | } else { |
| 11552 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11553 | smp->data.u.str.str = (char *)vstart; |
| 11554 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11555 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11556 | } |
| 11557 | |
| 11558 | /* Update context, check wrapping. */ |
| 11559 | chunks[0] = vend; |
| 11560 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11561 | chunks[1] = chunks[3]; |
| 11562 | chunks[2] = NULL; |
| 11563 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11564 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11565 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11566 | smp->flags |= SMP_F_NOT_LAST; |
| 11567 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11568 | return 1; |
| 11569 | } |
| 11570 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11571 | /* This function iterates over each parameter of the query string. It uses |
| 11572 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11573 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11574 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11575 | * considered. It supports an optional delimiter argument for the beginning of |
| 11576 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11577 | */ |
| 11578 | static int |
| 11579 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11580 | { |
| 11581 | struct http_msg *msg; |
| 11582 | char delim = '?'; |
| 11583 | const char *name; |
| 11584 | int name_len; |
| 11585 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11586 | if (!args || |
| 11587 | (args[0].type && args[0].type != ARGT_STR) || |
| 11588 | (args[1].type && args[1].type != ARGT_STR)) |
| 11589 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11590 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11591 | name = ""; |
| 11592 | name_len = 0; |
| 11593 | if (args->type == ARGT_STR) { |
| 11594 | name = args->data.str.str; |
| 11595 | name_len = args->data.str.len; |
| 11596 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11597 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11598 | if (args[1].type) |
| 11599 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11600 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11601 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11602 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11603 | |
| 11604 | msg = &smp->strm->txn->req; |
| 11605 | |
| 11606 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11607 | msg->sl.rq.u_l, delim); |
| 11608 | if (!smp->ctx.a[0]) |
| 11609 | return 0; |
| 11610 | |
| 11611 | smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11612 | |
| 11613 | /* Assume that the context is filled with NULL pointer |
| 11614 | * before the first call. |
| 11615 | * smp->ctx.a[2] = NULL; |
| 11616 | * smp->ctx.a[3] = NULL; |
| 11617 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11618 | } |
| 11619 | |
| 11620 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11621 | } |
| 11622 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11623 | /* This function iterates over each parameter of the body. This requires |
| 11624 | * that the body has been waited for using http-buffer-request. It uses |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11625 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11626 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11627 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11628 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11629 | */ |
| 11630 | static int |
| 11631 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11632 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11633 | struct http_msg *msg; |
| 11634 | unsigned long len; |
| 11635 | unsigned long block1; |
| 11636 | char *body; |
| 11637 | const char *name; |
| 11638 | int name_len; |
| 11639 | |
| 11640 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11641 | return 0; |
| 11642 | |
| 11643 | name = ""; |
| 11644 | name_len = 0; |
| 11645 | if (args[0].type == ARGT_STR) { |
| 11646 | name = args[0].data.str.str; |
| 11647 | name_len = args[0].data.str.len; |
| 11648 | } |
| 11649 | |
| 11650 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11651 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11652 | |
| 11653 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11654 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11655 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11656 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11657 | |
| 11658 | len = http_body_bytes(msg); |
| 11659 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11660 | |
| 11661 | block1 = len; |
| 11662 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11663 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11664 | |
| 11665 | if (block1 == len) { |
| 11666 | /* buffer is not wrapped (or empty) */ |
| 11667 | smp->ctx.a[0] = body; |
| 11668 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11669 | |
| 11670 | /* Assume that the context is filled with NULL pointer |
| 11671 | * before the first call. |
| 11672 | * smp->ctx.a[2] = NULL; |
| 11673 | * smp->ctx.a[3] = NULL; |
| 11674 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11675 | } |
| 11676 | else { |
| 11677 | /* buffer is wrapped, we need to defragment it */ |
| 11678 | smp->ctx.a[0] = body; |
| 11679 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11680 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11681 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11682 | } |
| 11683 | } |
| 11684 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11685 | } |
| 11686 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11687 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11688 | * above). |
| 11689 | */ |
| 11690 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11691 | smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11692 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11693 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11694 | |
| 11695 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11696 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11697 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11698 | } |
| 11699 | |
| 11700 | return ret; |
| 11701 | } |
| 11702 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11703 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11704 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11705 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11706 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11707 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11708 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11709 | * high-traffic sites without having to store whole paths. |
| 11710 | * this differs from the base32 functions in that it includes the url parameters |
| 11711 | * as well as the path |
| 11712 | */ |
| 11713 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11714 | smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11715 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11716 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11717 | struct hdr_ctx ctx; |
| 11718 | unsigned int hash = 0; |
| 11719 | char *ptr, *beg, *end; |
| 11720 | int len; |
| 11721 | |
| 11722 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11723 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11724 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11725 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11726 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11727 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11728 | ptr = ctx.line + ctx.val; |
| 11729 | len = ctx.vlen; |
| 11730 | while (len--) |
| 11731 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11732 | } |
| 11733 | |
| 11734 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11735 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11736 | beg = http_get_path(txn); |
| 11737 | if (!beg) |
| 11738 | beg = end; |
| 11739 | |
| 11740 | for (ptr = beg; ptr < end ; ptr++); |
| 11741 | |
| 11742 | if (beg < ptr && *beg == '/') { |
| 11743 | while (beg < ptr) |
| 11744 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11745 | } |
| 11746 | hash = full_hash(hash); |
| 11747 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11748 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11749 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11750 | smp->flags = SMP_F_VOL_1ST; |
| 11751 | return 1; |
| 11752 | } |
| 11753 | |
| 11754 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11755 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11756 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11757 | * on the source address length. The URL hash is stored before the address so |
| 11758 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11759 | * 8 bytes would still work. |
| 11760 | */ |
| 11761 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11762 | smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11763 | { |
| 11764 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11765 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11766 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11767 | if (!cli_conn) |
| 11768 | return 0; |
| 11769 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11770 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11771 | return 0; |
| 11772 | |
| 11773 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11774 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11775 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11776 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11777 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11778 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11779 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11780 | temp->len += 4; |
| 11781 | break; |
| 11782 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11783 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11784 | temp->len += 16; |
| 11785 | break; |
| 11786 | default: |
| 11787 | return 0; |
| 11788 | } |
| 11789 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11790 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11791 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11792 | return 1; |
| 11793 | } |
| 11794 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11795 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11796 | * keyword. These keywords support an optional positive or negative occurrence |
| 11797 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11798 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11799 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11800 | * error message in case of error, that the caller is responsible for freeing. |
| 11801 | * The initial location must either be freeable or NULL. |
| 11802 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11803 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11804 | { |
| 11805 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 11806 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11807 | return 0; |
| 11808 | } |
| 11809 | return 1; |
| 11810 | } |
| 11811 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11812 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11813 | * adds an optional offset found in args[0] and emits a string representing |
| 11814 | * the date in RFC-1123/5322 format. |
| 11815 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11816 | static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11817 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11818 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11819 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11820 | struct chunk *temp; |
| 11821 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11822 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11823 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11824 | |
| 11825 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11826 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11827 | curr_date += args[0].data.sint; |
| 11828 | |
| 11829 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11830 | if (!tm) |
| 11831 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11832 | |
| 11833 | temp = get_trash_chunk(); |
| 11834 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11835 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11836 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11837 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11838 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11839 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11840 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11841 | return 1; |
| 11842 | } |
| 11843 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11844 | /* Match language range with language tag. RFC2616 14.4: |
| 11845 | * |
| 11846 | * A language-range matches a language-tag if it exactly equals |
| 11847 | * the tag, or if it exactly equals a prefix of the tag such |
| 11848 | * that the first tag character following the prefix is "-". |
| 11849 | * |
| 11850 | * Return 1 if the strings match, else return 0. |
| 11851 | */ |
| 11852 | static inline int language_range_match(const char *range, int range_len, |
| 11853 | const char *tag, int tag_len) |
| 11854 | { |
| 11855 | const char *end = range + range_len; |
| 11856 | const char *tend = tag + tag_len; |
| 11857 | while (range < end) { |
| 11858 | if (*range == '-' && tag == tend) |
| 11859 | return 1; |
| 11860 | if (*range != *tag || tag == tend) |
| 11861 | return 0; |
| 11862 | range++; |
| 11863 | tag++; |
| 11864 | } |
| 11865 | /* Return true only if the last char of the tag is matched. */ |
| 11866 | return tag == tend; |
| 11867 | } |
| 11868 | |
| 11869 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11870 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11871 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11872 | const char *al = smp->data.u.str.str; |
| 11873 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11874 | const char *token; |
| 11875 | int toklen; |
| 11876 | int qvalue; |
| 11877 | const char *str; |
| 11878 | const char *w; |
| 11879 | int best_q = 0; |
| 11880 | |
| 11881 | /* Set the constant to the sample, because the output of the |
| 11882 | * function will be peek in the constant configuration string. |
| 11883 | */ |
| 11884 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11885 | smp->data.u.str.size = 0; |
| 11886 | smp->data.u.str.str = ""; |
| 11887 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11888 | |
| 11889 | /* Parse the accept language */ |
| 11890 | while (1) { |
| 11891 | |
| 11892 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11893 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11894 | al++; |
| 11895 | if (al >= end) |
| 11896 | break; |
| 11897 | |
| 11898 | /* Start of the fisrt word. */ |
| 11899 | token = al; |
| 11900 | |
| 11901 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11902 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11903 | al++; |
| 11904 | if (al == token) |
| 11905 | goto expect_comma; |
| 11906 | |
| 11907 | /* Length of the token. */ |
| 11908 | toklen = al - token; |
| 11909 | qvalue = 1000; |
| 11910 | |
| 11911 | /* Check if the token exists in the list. If the token not exists, |
| 11912 | * jump to the next token. |
| 11913 | */ |
| 11914 | str = args[0].data.str.str; |
| 11915 | w = str; |
| 11916 | while (1) { |
| 11917 | if (*str == ';' || *str == '\0') { |
| 11918 | if (language_range_match(token, toklen, w, str-w)) |
| 11919 | goto look_for_q; |
| 11920 | if (*str == '\0') |
| 11921 | goto expect_comma; |
| 11922 | w = str + 1; |
| 11923 | } |
| 11924 | str++; |
| 11925 | } |
| 11926 | goto expect_comma; |
| 11927 | |
| 11928 | look_for_q: |
| 11929 | |
| 11930 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11931 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11932 | al++; |
| 11933 | if (al >= end) |
| 11934 | goto process_value; |
| 11935 | |
| 11936 | /* If ',' is found, process the result */ |
| 11937 | if (*al == ',') |
| 11938 | goto process_value; |
| 11939 | |
| 11940 | /* If the character is different from ';', look |
| 11941 | * for the end of the header part in best effort. |
| 11942 | */ |
| 11943 | if (*al != ';') |
| 11944 | goto expect_comma; |
| 11945 | |
| 11946 | /* Assumes that the char is ';', now expect "q=". */ |
| 11947 | al++; |
| 11948 | |
| 11949 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11950 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11951 | al++; |
| 11952 | if (al >= end) |
| 11953 | goto process_value; |
| 11954 | |
| 11955 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11956 | if (*al != 'q') |
| 11957 | goto process_value; |
| 11958 | al++; |
| 11959 | |
| 11960 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11961 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11962 | al++; |
| 11963 | if (al >= end) |
| 11964 | goto process_value; |
| 11965 | |
| 11966 | /* Expect '='. If no '=', continue in best effort */ |
| 11967 | if (*al != '=') |
| 11968 | goto process_value; |
| 11969 | al++; |
| 11970 | |
| 11971 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11972 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11973 | al++; |
| 11974 | if (al >= end) |
| 11975 | goto process_value; |
| 11976 | |
| 11977 | /* Parse the q value. */ |
| 11978 | qvalue = parse_qvalue(al, &al); |
| 11979 | |
| 11980 | process_value: |
| 11981 | |
| 11982 | /* If the new q value is the best q value, then store the associated |
| 11983 | * language in the response. If qvalue is the biggest value (1000), |
| 11984 | * break the process. |
| 11985 | */ |
| 11986 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11987 | smp->data.u.str.str = (char *)w; |
| 11988 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11989 | if (qvalue >= 1000) |
| 11990 | break; |
| 11991 | best_q = qvalue; |
| 11992 | } |
| 11993 | |
| 11994 | expect_comma: |
| 11995 | |
| 11996 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11997 | while (al < end && *al != ',') |
| 11998 | al++; |
| 11999 | if (al >= end) |
| 12000 | break; |
| 12001 | |
| 12002 | /* Comma is found, jump it and restart the analyzer. */ |
| 12003 | al++; |
| 12004 | } |
| 12005 | |
| 12006 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12007 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12008 | smp->data.u.str.str = args[1].data.str.str; |
| 12009 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12010 | } |
| 12011 | |
| 12012 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12013 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12014 | } |
| 12015 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12016 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12017 | static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private) |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12018 | { |
| 12019 | /* If the constant flag is set or if not size is avalaible at |
| 12020 | * the end of the buffer, copy the string in other buffer |
| 12021 | * before decoding. |
| 12022 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12023 | if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) { |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12024 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12025 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12026 | smp->data.u.str.str = str->str; |
| 12027 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12028 | smp->flags &= ~SMP_F_CONST; |
| 12029 | } |
| 12030 | |
| 12031 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12032 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12033 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12034 | return 1; |
| 12035 | } |
| 12036 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12037 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12038 | { |
| 12039 | struct proxy *fe = strm_fe(smp->strm); |
| 12040 | int idx, i; |
| 12041 | struct cap_hdr *hdr; |
| 12042 | int len; |
| 12043 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12044 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12045 | return 0; |
| 12046 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12047 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12048 | |
| 12049 | /* Check the availibity of the capture id. */ |
| 12050 | if (idx > fe->nb_req_cap - 1) |
| 12051 | return 0; |
| 12052 | |
| 12053 | /* Look for the original configuration. */ |
| 12054 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12055 | hdr != NULL && i != idx ; |
| 12056 | i--, hdr = hdr->next); |
| 12057 | if (!hdr) |
| 12058 | return 0; |
| 12059 | |
| 12060 | /* check for the memory allocation */ |
| 12061 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12062 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12063 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12064 | return 0; |
| 12065 | |
| 12066 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12067 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12068 | if (len > hdr->len) |
| 12069 | len = hdr->len; |
| 12070 | |
| 12071 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12072 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12073 | smp->strm->req_cap[idx][len] = '\0'; |
| 12074 | |
| 12075 | return 1; |
| 12076 | } |
| 12077 | |
| 12078 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12079 | { |
| 12080 | struct proxy *fe = strm_fe(smp->strm); |
| 12081 | int idx, i; |
| 12082 | struct cap_hdr *hdr; |
| 12083 | int len; |
| 12084 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12085 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12086 | return 0; |
| 12087 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12088 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12089 | |
| 12090 | /* Check the availibity of the capture id. */ |
| 12091 | if (idx > fe->nb_rsp_cap - 1) |
| 12092 | return 0; |
| 12093 | |
| 12094 | /* Look for the original configuration. */ |
| 12095 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12096 | hdr != NULL && i != idx ; |
| 12097 | i--, hdr = hdr->next); |
| 12098 | if (!hdr) |
| 12099 | return 0; |
| 12100 | |
| 12101 | /* check for the memory allocation */ |
| 12102 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12103 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12104 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12105 | return 0; |
| 12106 | |
| 12107 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12108 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12109 | if (len > hdr->len) |
| 12110 | len = hdr->len; |
| 12111 | |
| 12112 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12113 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12114 | smp->strm->res_cap[idx][len] = '\0'; |
| 12115 | |
| 12116 | return 1; |
| 12117 | } |
| 12118 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12119 | /* This function executes one of the set-{method,path,query,uri} actions. It |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12120 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12121 | * the relevant part of the request line accordingly. Then it updates various |
| 12122 | * pointers to the next elements which were moved, and the total buffer length. |
| 12123 | * It finds the action to be performed in p[2], previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12124 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12125 | * error, though this can be revisited when this code is finally exploited. |
| 12126 | * |
| 12127 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12128 | * query string and 3 to replace uri. |
| 12129 | * |
| 12130 | * In query string case, the mark question '?' must be set at the start of the |
| 12131 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12132 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12133 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12134 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12135 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12136 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12137 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12138 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12139 | int delta; |
| 12140 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12141 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12142 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12143 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12144 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12145 | |
| 12146 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12147 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12148 | txn->req.sl.rq.m_l += delta; |
| 12149 | txn->req.sl.rq.u += delta; |
| 12150 | txn->req.sl.rq.v += delta; |
| 12151 | break; |
| 12152 | |
| 12153 | case 1: // path |
| 12154 | cur_ptr = http_get_path(txn); |
| 12155 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12156 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12157 | |
| 12158 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12159 | while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12160 | cur_end++; |
| 12161 | |
| 12162 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12163 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12164 | txn->req.sl.rq.u_l += delta; |
| 12165 | txn->req.sl.rq.v += delta; |
| 12166 | break; |
| 12167 | |
| 12168 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12169 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12170 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12171 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12172 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12173 | cur_ptr++; |
| 12174 | |
| 12175 | /* skip the question mark or indicate that we must insert it |
| 12176 | * (but only if the format string is not empty then). |
| 12177 | */ |
| 12178 | if (cur_ptr < cur_end) |
| 12179 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12180 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12181 | offset = 0; |
| 12182 | |
| 12183 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12184 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12185 | txn->req.sl.rq.u_l += delta; |
| 12186 | txn->req.sl.rq.v += delta; |
| 12187 | break; |
| 12188 | |
| 12189 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12190 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12191 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12192 | |
| 12193 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12194 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12195 | txn->req.sl.rq.u_l += delta; |
| 12196 | txn->req.sl.rq.v += delta; |
| 12197 | break; |
| 12198 | |
| 12199 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12200 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12201 | } |
| 12202 | |
| 12203 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12204 | delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
Thierry FOURNIER | 7f6192c | 2015-04-26 18:01:40 +0200 | [diff] [blame] | 12205 | txn->req.sl.rq.l += delta; |
| 12206 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12207 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12208 | return 0; |
| 12209 | } |
| 12210 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12211 | /* This function replace the HTTP status code and the associated message. The |
| 12212 | * variable <status> contains the new status code. This function never fails. |
| 12213 | */ |
| 12214 | void http_set_status(unsigned int status, struct stream *s) |
| 12215 | { |
| 12216 | struct http_txn *txn = s->txn; |
| 12217 | char *cur_ptr, *cur_end; |
| 12218 | int delta; |
| 12219 | char *res; |
| 12220 | int c_l; |
| 12221 | const char *msg; |
| 12222 | int msg_len; |
| 12223 | |
| 12224 | chunk_reset(&trash); |
| 12225 | |
| 12226 | res = ultoa_o(status, trash.str, trash.size); |
| 12227 | c_l = res - trash.str; |
| 12228 | |
| 12229 | trash.str[c_l] = ' '; |
| 12230 | trash.len = c_l + 1; |
| 12231 | |
| 12232 | msg = get_reason(status); |
| 12233 | msg_len = strlen(msg); |
| 12234 | |
| 12235 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12236 | trash.len += msg_len; |
| 12237 | |
| 12238 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12239 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12240 | |
| 12241 | /* commit changes and adjust message */ |
| 12242 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12243 | |
| 12244 | /* adjust res line offsets and lengths */ |
| 12245 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12246 | txn->rsp.sl.st.c_l = c_l; |
| 12247 | txn->rsp.sl.st.r_l = msg_len; |
| 12248 | |
| 12249 | delta = trash.len - (cur_end - cur_ptr); |
| 12250 | txn->rsp.sl.st.l += delta; |
| 12251 | txn->hdr_idx.v[0].len += delta; |
| 12252 | http_msg_move_end(&txn->rsp, delta); |
| 12253 | } |
| 12254 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12255 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12256 | * builds a string in the trash from the specified format string. It finds |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12257 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12258 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12259 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12260 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12261 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12262 | enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12263 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12264 | { |
| 12265 | chunk_reset(&trash); |
| 12266 | |
| 12267 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12268 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12269 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12270 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12271 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12272 | http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12273 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12274 | } |
| 12275 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12276 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12277 | enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12278 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12279 | { |
| 12280 | http_set_status(rule->arg.status.code, s); |
| 12281 | return ACT_RET_CONT; |
| 12282 | } |
| 12283 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12284 | /* parse an http-request action among : |
| 12285 | * set-method |
| 12286 | * set-path |
| 12287 | * set-query |
| 12288 | * set-uri |
| 12289 | * |
| 12290 | * All of them accept a single argument of type string representing a log-format. |
| 12291 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12292 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12293 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12294 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12295 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12296 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12297 | { |
| 12298 | int cur_arg = *orig_arg; |
| 12299 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12300 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12301 | |
| 12302 | switch (args[0][4]) { |
| 12303 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12304 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12305 | rule->action_ptr = http_action_set_req_line; |
| 12306 | break; |
| 12307 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12308 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12309 | rule->action_ptr = http_action_set_req_line; |
| 12310 | break; |
| 12311 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12312 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12313 | rule->action_ptr = http_action_set_req_line; |
| 12314 | break; |
| 12315 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12316 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12317 | rule->action_ptr = http_action_set_req_line; |
| 12318 | break; |
| 12319 | default: |
| 12320 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12321 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12322 | } |
| 12323 | |
| 12324 | if (!*args[cur_arg] || |
| 12325 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12326 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12327 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12328 | } |
| 12329 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12330 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12331 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12332 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12333 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 12334 | proxy->conf.args.file, proxy->conf.args.line); |
| 12335 | |
| 12336 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12337 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12338 | } |
| 12339 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12340 | /* parse set-status action: |
| 12341 | * This action accepts a single argument of type int representing |
| 12342 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12343 | * ACT_RET_PRS_ERR on error. |
| 12344 | */ |
| 12345 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12346 | struct act_rule *rule, char **err) |
| 12347 | { |
| 12348 | char *error; |
| 12349 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12350 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12351 | rule->action_ptr = action_http_set_status; |
| 12352 | |
| 12353 | /* Check if an argument is available */ |
| 12354 | if (!*args[*orig_arg]) { |
| 12355 | memprintf(err, "expects exactly 1 argument <status>"); |
| 12356 | return ACT_RET_PRS_ERR; |
| 12357 | } |
| 12358 | |
| 12359 | /* convert status code as integer */ |
| 12360 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12361 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12362 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12363 | return ACT_RET_PRS_ERR; |
| 12364 | } |
| 12365 | |
| 12366 | (*orig_arg)++; |
| 12367 | return ACT_RET_PRS_OK; |
| 12368 | } |
| 12369 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12370 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12371 | * turns the result into a string and puts it in a capture slot. It always |
| 12372 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12373 | * processing continues. |
| 12374 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12375 | enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12376 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12377 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12378 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12379 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12380 | char **cap = s->req_cap; |
| 12381 | int len; |
| 12382 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12383 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12384 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12385 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12386 | |
| 12387 | if (cap[h->index] == NULL) |
| 12388 | cap[h->index] = pool_alloc2(h->pool); |
| 12389 | |
| 12390 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12391 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12392 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12393 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12394 | if (len > h->len) |
| 12395 | len = h->len; |
| 12396 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12397 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12398 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12399 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12400 | } |
| 12401 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12402 | /* This function executes the "capture" action and store the result in a |
| 12403 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12404 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12405 | * error occurs the action is cancelled, but the rule processing continues. |
| 12406 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12407 | enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12408 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12409 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12410 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12411 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12412 | char **cap = s->req_cap; |
| 12413 | struct proxy *fe = strm_fe(s); |
| 12414 | int len; |
| 12415 | int i; |
| 12416 | |
| 12417 | /* Look for the original configuration. */ |
| 12418 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12419 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12420 | i--, h = h->next); |
| 12421 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12422 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12423 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12424 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12425 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12426 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12427 | |
| 12428 | if (cap[h->index] == NULL) |
| 12429 | cap[h->index] = pool_alloc2(h->pool); |
| 12430 | |
| 12431 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12432 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12433 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12434 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12435 | if (len > h->len) |
| 12436 | len = h->len; |
| 12437 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12438 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12439 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12440 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12441 | } |
| 12442 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12443 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12444 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12445 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12446 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12447 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12448 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12449 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12450 | { |
| 12451 | struct sample_expr *expr; |
| 12452 | struct cap_hdr *hdr; |
| 12453 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12454 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12455 | |
| 12456 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12457 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12458 | strcmp(args[cur_arg], "unless") == 0) |
| 12459 | break; |
| 12460 | |
| 12461 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12462 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12463 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12464 | } |
| 12465 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12466 | cur_arg = *orig_arg; |
| 12467 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12468 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12469 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12470 | |
| 12471 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12472 | memprintf(err, |
| 12473 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12474 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12475 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12476 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12477 | } |
| 12478 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12479 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12480 | memprintf(err, "expects 'len or 'id'"); |
| 12481 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12482 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12483 | } |
| 12484 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12485 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12486 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12487 | |
| 12488 | if (!(px->cap & PR_CAP_FE)) { |
| 12489 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12490 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12491 | } |
| 12492 | |
| 12493 | proxy->conf.args.ctx = ARGC_CAP; |
| 12494 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12495 | if (!args[cur_arg]) { |
| 12496 | memprintf(err, "missing length value"); |
| 12497 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12498 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12499 | } |
| 12500 | /* we copy the table name for now, it will be resolved later */ |
| 12501 | len = atoi(args[cur_arg]); |
| 12502 | if (len <= 0) { |
| 12503 | memprintf(err, "length must be > 0"); |
| 12504 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12505 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12506 | } |
| 12507 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12508 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12509 | if (!len) { |
| 12510 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12511 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12512 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12513 | } |
| 12514 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12515 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12516 | hdr->next = px->req_cap; |
| 12517 | hdr->name = NULL; /* not a header capture */ |
| 12518 | hdr->namelen = 0; |
| 12519 | hdr->len = len; |
| 12520 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12521 | hdr->index = px->nb_req_cap++; |
| 12522 | |
| 12523 | px->req_cap = hdr; |
| 12524 | px->to_log |= LW_REQHDR; |
| 12525 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12526 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12527 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12528 | rule->arg.cap.expr = expr; |
| 12529 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12530 | } |
| 12531 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12532 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12533 | int id; |
| 12534 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12535 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12536 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12537 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12538 | if (!args[cur_arg]) { |
| 12539 | memprintf(err, "missing id value"); |
| 12540 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12541 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12542 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12543 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12544 | id = strtol(args[cur_arg], &error, 10); |
| 12545 | if (*error != '\0') { |
| 12546 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12547 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12548 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12549 | } |
| 12550 | cur_arg++; |
| 12551 | |
| 12552 | proxy->conf.args.ctx = ARGC_CAP; |
| 12553 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12554 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12555 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12556 | rule->arg.capid.expr = expr; |
| 12557 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12558 | } |
| 12559 | |
| 12560 | else { |
| 12561 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12562 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12563 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12564 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12565 | |
| 12566 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12567 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12568 | } |
| 12569 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12570 | /* This function executes the "capture" action and store the result in a |
| 12571 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12572 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12573 | * error occurs the action is cancelled, but the rule processing continues. |
| 12574 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12575 | enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12576 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12577 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12578 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12579 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12580 | char **cap = s->res_cap; |
| 12581 | struct proxy *fe = strm_fe(s); |
| 12582 | int len; |
| 12583 | int i; |
| 12584 | |
| 12585 | /* Look for the original configuration. */ |
| 12586 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12587 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12588 | i--, h = h->next); |
| 12589 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12590 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12591 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12592 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12593 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12594 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12595 | |
| 12596 | if (cap[h->index] == NULL) |
| 12597 | cap[h->index] = pool_alloc2(h->pool); |
| 12598 | |
| 12599 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12600 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12601 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12602 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12603 | if (len > h->len) |
| 12604 | len = h->len; |
| 12605 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12606 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12607 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12608 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12609 | } |
| 12610 | |
| 12611 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12612 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12613 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12614 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12615 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12616 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12617 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12618 | { |
| 12619 | struct sample_expr *expr; |
| 12620 | int cur_arg; |
| 12621 | int id; |
| 12622 | char *error; |
| 12623 | |
| 12624 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12625 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12626 | strcmp(args[cur_arg], "unless") == 0) |
| 12627 | break; |
| 12628 | |
| 12629 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12630 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12631 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12632 | } |
| 12633 | |
| 12634 | cur_arg = *orig_arg; |
| 12635 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12636 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12637 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12638 | |
| 12639 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12640 | memprintf(err, |
| 12641 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12642 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12643 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12644 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12645 | } |
| 12646 | |
| 12647 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12648 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12649 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12650 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12651 | } |
| 12652 | |
| 12653 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12654 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12655 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12656 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12657 | } |
| 12658 | |
| 12659 | cur_arg++; |
| 12660 | |
| 12661 | if (!args[cur_arg]) { |
| 12662 | memprintf(err, "missing id value"); |
| 12663 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12664 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12665 | } |
| 12666 | |
| 12667 | id = strtol(args[cur_arg], &error, 10); |
| 12668 | if (*error != '\0') { |
| 12669 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12670 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12671 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12672 | } |
| 12673 | cur_arg++; |
| 12674 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12675 | proxy->conf.args.ctx = ARGC_CAP; |
| 12676 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12677 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12678 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12679 | rule->arg.capid.expr = expr; |
| 12680 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12681 | |
| 12682 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12683 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12684 | } |
| 12685 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12686 | /* |
| 12687 | * Return the struct http_req_action_kw associated to a keyword. |
| 12688 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12689 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12690 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12691 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12692 | } |
| 12693 | |
| 12694 | /* |
| 12695 | * Return the struct http_res_action_kw associated to a keyword. |
| 12696 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12697 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12698 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12699 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12700 | } |
| 12701 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12702 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12703 | /* All supported ACL keywords must be declared here. */ |
| 12704 | /************************************************************************/ |
| 12705 | |
| 12706 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12707 | * Please take care of keeping this list alphabetically sorted. |
| 12708 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12709 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12710 | { "base", "base", PAT_MATCH_STR }, |
| 12711 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12712 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12713 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12714 | { "base_end", "base", PAT_MATCH_END }, |
| 12715 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12716 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12717 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12718 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12719 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12720 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12721 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12722 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12723 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12724 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12725 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12726 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12727 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12728 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12729 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12730 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12731 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12732 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12733 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12734 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12735 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12736 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12737 | /* these two declarations uses strings with list storage (in place |
| 12738 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12739 | * and delete functions are relative to the list management. The parse |
| 12740 | * and match method are related to the corresponding fetch methods. This |
| 12741 | * is very particular ACL declaration mode. |
| 12742 | */ |
| 12743 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12744 | { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12745 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12746 | { "path", "path", PAT_MATCH_STR }, |
| 12747 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12748 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12749 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12750 | { "path_end", "path", PAT_MATCH_END }, |
| 12751 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12752 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12753 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12754 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12755 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12756 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12757 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12758 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12759 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12760 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12761 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12762 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12763 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12764 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12765 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12766 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12767 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12768 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12769 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12770 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12771 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12772 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12773 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12774 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12775 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12776 | { "url", "url", PAT_MATCH_STR }, |
| 12777 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12778 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12779 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12780 | { "url_end", "url", PAT_MATCH_END }, |
| 12781 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12782 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12783 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12784 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12785 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12786 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12787 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12788 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12789 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12790 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12791 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12792 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12793 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12794 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12795 | }}; |
| 12796 | |
| 12797 | /************************************************************************/ |
| 12798 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12799 | /************************************************************************/ |
| 12800 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12801 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12802 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12803 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12804 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12805 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12806 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12807 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12808 | |
| 12809 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12810 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12811 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12812 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12813 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12814 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12815 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 12816 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12817 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12818 | * are only here to match the ACL's name, are request-only and are used |
| 12819 | * for ACL compatibility only. |
| 12820 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12821 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12822 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12823 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12824 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12825 | |
| 12826 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12827 | * only here to match the ACL's name, are request-only and are used for |
| 12828 | * ACL compatibility only. |
| 12829 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12830 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12831 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12832 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12833 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12834 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12835 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12836 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12837 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 12838 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12839 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12840 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12841 | |
| 12842 | /* HTTP protocol on the request path */ |
| 12843 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12844 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12845 | |
| 12846 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12847 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12848 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12849 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12850 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12851 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12852 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12853 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12854 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12855 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12856 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12857 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12858 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12859 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12860 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12861 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12862 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12863 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12864 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12865 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12866 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12867 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12868 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 12869 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12870 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12871 | |
| 12872 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12873 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12874 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12875 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12876 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12877 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12878 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12879 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12880 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12881 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 12882 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12883 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12884 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12885 | /* scook is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12886 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12887 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12888 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12889 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12890 | |
| 12891 | /* shdr is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12892 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12893 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12894 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12895 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12896 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12897 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12898 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12899 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12900 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12901 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12902 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12903 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 12904 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12905 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12906 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12907 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12908 | }}; |
| 12909 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12910 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12911 | /************************************************************************/ |
| 12912 | /* All supported converter keywords must be declared here. */ |
| 12913 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12914 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12915 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12916 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR}, |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12917 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12918 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12919 | { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12920 | { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12921 | { NULL, NULL, 0, 0, 0 }, |
| 12922 | }}; |
| 12923 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12924 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12925 | /************************************************************************/ |
| 12926 | /* All supported http-request action keywords must be declared here. */ |
| 12927 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12928 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12929 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12930 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12931 | { "set-method", parse_set_req_line }, |
| 12932 | { "set-path", parse_set_req_line }, |
| 12933 | { "set-query", parse_set_req_line }, |
| 12934 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12935 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12936 | } |
| 12937 | }; |
| 12938 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12939 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12940 | .kw = { |
| 12941 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12942 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12943 | { NULL, NULL } |
| 12944 | } |
| 12945 | }; |
| 12946 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12947 | __attribute__((constructor)) |
| 12948 | static void __http_protocol_init(void) |
| 12949 | { |
| 12950 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12951 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12952 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12953 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12954 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12955 | } |
| 12956 | |
| 12957 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12958 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12959 | * Local variables: |
| 12960 | * c-indent-level: 8 |
| 12961 | * c-basic-offset: 8 |
| 12962 | * End: |
| 12963 | */ |