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 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 278 | static int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 279 | static int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg); |
| 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); |
| 1588 | if (!p || p == h) |
| 1589 | return 0; |
| 1590 | |
| 1591 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1592 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1593 | |
| 1594 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1595 | |
| 1596 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1597 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1598 | |
| 1599 | if (len < 0) |
| 1600 | return 0; |
| 1601 | |
| 1602 | |
| 1603 | get_http_auth_buff[len] = '\0'; |
| 1604 | |
| 1605 | p = strchr(get_http_auth_buff, ':'); |
| 1606 | |
| 1607 | if (!p) |
| 1608 | return 0; |
| 1609 | |
| 1610 | txn->auth.user = get_http_auth_buff; |
| 1611 | *p = '\0'; |
| 1612 | txn->auth.pass = p+1; |
| 1613 | |
| 1614 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1615 | return 1; |
| 1616 | } |
| 1617 | |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1621 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1622 | /* |
| 1623 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1624 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1625 | * ensuring that the message does not wrap. The function can be preempted |
| 1626 | * everywhere when data are missing and recalled at the exact same location |
| 1627 | * with no information loss. The message may even be realigned between two |
| 1628 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1629 | * 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] | 1630 | * fields. Note that msg->sol will be initialized after completing the first |
| 1631 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1632 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1633 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1634 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1635 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1636 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1638 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1639 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1640 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1641 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1642 | ptr = buf->p + msg->next; |
| 1643 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | if (unlikely(ptr >= end)) |
| 1646 | goto http_msg_ood; |
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 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1649 | /* |
| 1650 | * First, states that are specific to the response only. |
| 1651 | * We check them first so that request and headers are |
| 1652 | * closer to each other (accessed more often). |
| 1653 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1654 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1655 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1656 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1657 | /* we have a start of message, but we have to check |
| 1658 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1659 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1660 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1661 | if (unlikely(ptr != buf->p)) { |
| 1662 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1663 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1664 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1665 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1666 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1667 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1668 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1669 | hdr_idx_init(idx); |
| 1670 | state = HTTP_MSG_RPVER; |
| 1671 | goto http_msg_rpver; |
| 1672 | } |
| 1673 | |
| 1674 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1675 | goto http_msg_invalid; |
| 1676 | |
| 1677 | if (unlikely(*ptr == '\n')) |
| 1678 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1679 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1680 | /* stop here */ |
| 1681 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1682 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1683 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1684 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1685 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1686 | /* stop here */ |
| 1687 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1688 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1689 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1690 | case HTTP_MSG_RPVER_SP: |
| 1691 | case HTTP_MSG_RPCODE: |
| 1692 | case HTTP_MSG_RPCODE_SP: |
| 1693 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1694 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1695 | state, ptr, end, |
| 1696 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1697 | if (unlikely(!ptr)) |
| 1698 | return; |
| 1699 | |
| 1700 | /* we have a full response and we know that we have either a CR |
| 1701 | * or an LF at <ptr>. |
| 1702 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1703 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1704 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1705 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1706 | if (likely(*ptr == '\r')) |
| 1707 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1708 | goto http_msg_rpline_end; |
| 1709 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1710 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1711 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1712 | /* msg->sol must point to the first of CR or LF. */ |
| 1713 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1714 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1715 | /* stop here */ |
| 1716 | |
| 1717 | /* |
| 1718 | * Second, states that are specific to the request only |
| 1719 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1720 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1721 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1723 | /* we have a start of message, but we have to check |
| 1724 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1725 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1726 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1727 | if (likely(ptr != buf->p)) { |
| 1728 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1729 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1730 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1731 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1732 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1733 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1734 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1735 | state = HTTP_MSG_RQMETH; |
| 1736 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1737 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1738 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1740 | goto http_msg_invalid; |
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(*ptr == '\n')) |
| 1743 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1744 | 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] | 1745 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1746 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1747 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1748 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1750 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1751 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1752 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1754 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | case HTTP_MSG_RQMETH_SP: |
| 1756 | case HTTP_MSG_RQURI: |
| 1757 | case HTTP_MSG_RQURI_SP: |
| 1758 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1759 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1760 | state, ptr, end, |
| 1761 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | if (unlikely(!ptr)) |
| 1763 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1764 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1765 | /* we have a full request and we know that we have either a CR |
| 1766 | * or an LF at <ptr>. |
| 1767 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1770 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | if (likely(*ptr == '\r')) |
| 1772 | 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] | 1773 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1776 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | /* check for HTTP/0.9 request : no version information available. |
| 1778 | * msg->sol must point to the first of CR or LF. |
| 1779 | */ |
| 1780 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1781 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1782 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1783 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1784 | 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] | 1785 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1786 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1787 | /* |
| 1788 | * Common states below |
| 1789 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1791 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1792 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1794 | goto http_msg_hdr_name; |
| 1795 | } |
| 1796 | |
| 1797 | if (likely(*ptr == '\r')) |
| 1798 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1799 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1800 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1802 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | /* assumes msg->sol points to the first char */ |
| 1804 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1805 | 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] | 1806 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1807 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | 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] | 1809 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1810 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1811 | goto http_msg_invalid; |
| 1812 | |
| 1813 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1814 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1815 | |
| 1816 | /* and we still accept this non-token character */ |
| 1817 | 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] | 1818 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1820 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1821 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1822 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1823 | 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] | 1824 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1826 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1829 | goto http_msg_hdr_val; |
| 1830 | } |
| 1831 | |
| 1832 | if (likely(*ptr == '\r')) |
| 1833 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1834 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1835 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1837 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1839 | 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] | 1840 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1841 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1842 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1844 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1845 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1846 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1847 | goto http_msg_hdr_l1_sp; |
| 1848 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1849 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1850 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1851 | goto http_msg_complete_header; |
| 1852 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1853 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1854 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1855 | /* assumes msg->sol points to the first char, and msg->sov |
| 1856 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1857 | */ |
| 1858 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1859 | 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] | 1860 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1861 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1862 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1863 | * real header end in case it ends with lots of LWS, but is this |
| 1864 | * really needed ? |
| 1865 | */ |
| 1866 | if (likely(*ptr == '\r')) |
| 1867 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1868 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1869 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1870 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1871 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1872 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1873 | 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] | 1874 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1875 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1876 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1877 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1878 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1879 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1880 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1881 | goto http_msg_hdr_val; |
| 1882 | } |
| 1883 | http_msg_complete_header: |
| 1884 | /* |
| 1885 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1886 | * Assumes msg->sol points to the first char, msg->sov points |
| 1887 | * to the first character of the value and msg->eol to the |
| 1888 | * first CR or LF so we know how the line ends. We insert last |
| 1889 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1891 | 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] | 1892 | idx, idx->tail) < 0)) |
| 1893 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1894 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1895 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1897 | goto http_msg_hdr_name; |
| 1898 | } |
| 1899 | |
| 1900 | if (likely(*ptr == '\r')) |
| 1901 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1902 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1903 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1904 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1905 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1906 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1907 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1908 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1909 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1910 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1911 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1912 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1913 | msg->eoh = msg->sol; |
| 1914 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1915 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1916 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1917 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1918 | |
| 1919 | case HTTP_MSG_ERROR: |
| 1920 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1921 | break; |
| 1922 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1924 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1925 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1926 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1927 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1928 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1929 | } |
| 1930 | http_msg_ood: |
| 1931 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1932 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1933 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1934 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1935 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1936 | http_msg_invalid: |
| 1937 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1938 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1939 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1940 | return; |
| 1941 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1942 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1943 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1944 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1945 | * nothing is done and 1 is returned. |
| 1946 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1947 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1948 | { |
| 1949 | int delta; |
| 1950 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1951 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1952 | |
| 1953 | if (msg->sl.rq.v_l != 0) |
| 1954 | return 1; |
| 1955 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1956 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1957 | if (txn->meth != HTTP_METH_GET) |
| 1958 | return 0; |
| 1959 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1960 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1961 | delta = 0; |
| 1962 | |
| 1963 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1964 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1965 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1966 | } |
| 1967 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1968 | 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] | 1969 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1970 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1971 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1972 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1973 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1974 | NULL, NULL); |
| 1975 | if (unlikely(!cur_end)) |
| 1976 | return 0; |
| 1977 | |
| 1978 | /* we have a full HTTP/1.0 request now and we know that |
| 1979 | * we have either a CR or an LF at <ptr>. |
| 1980 | */ |
| 1981 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1982 | return 1; |
| 1983 | } |
| 1984 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1985 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1986 | * and "keep-alive" values. If we already know that some headers may safely |
| 1987 | * 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] | 1988 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1989 | * - 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] | 1990 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1991 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1992 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1993 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1994 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1995 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1996 | 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] | 1997 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1998 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1999 | const char *hdr_val = "Connection"; |
| 2000 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2001 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2002 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2003 | return; |
| 2004 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2005 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2006 | hdr_val = "Proxy-Connection"; |
| 2007 | hdr_len = 16; |
| 2008 | } |
| 2009 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2010 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2011 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2012 | 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] | 2013 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2014 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2015 | if (to_del & 2) |
| 2016 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2017 | else |
| 2018 | txn->flags |= TX_CON_KAL_SET; |
| 2019 | } |
| 2020 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2021 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2022 | if (to_del & 1) |
| 2023 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2024 | else |
| 2025 | txn->flags |= TX_CON_CLO_SET; |
| 2026 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2027 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2028 | txn->flags |= TX_HDR_CONN_UPG; |
| 2029 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2030 | } |
| 2031 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2032 | txn->flags |= TX_HDR_CONN_PRS; |
| 2033 | return; |
| 2034 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2036 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2037 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2038 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2039 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2040 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2041 | 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] | 2042 | { |
| 2043 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2044 | const char *hdr_val = "Connection"; |
| 2045 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2046 | |
| 2047 | ctx.idx = 0; |
| 2048 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2049 | |
| 2050 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2051 | hdr_val = "Proxy-Connection"; |
| 2052 | hdr_len = 16; |
| 2053 | } |
| 2054 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2055 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2056 | 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] | 2057 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2058 | if (wanted & TX_CON_KAL_SET) |
| 2059 | txn->flags |= TX_CON_KAL_SET; |
| 2060 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2061 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2062 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2063 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2064 | if (wanted & TX_CON_CLO_SET) |
| 2065 | txn->flags |= TX_CON_CLO_SET; |
| 2066 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2067 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2068 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2069 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2070 | |
| 2071 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2072 | return; |
| 2073 | |
| 2074 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2075 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2076 | hdr_val = "Connection: close"; |
| 2077 | hdr_len = 17; |
| 2078 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2079 | hdr_val = "Proxy-Connection: close"; |
| 2080 | hdr_len = 23; |
| 2081 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2082 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2086 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2087 | hdr_val = "Connection: keep-alive"; |
| 2088 | hdr_len = 22; |
| 2089 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2090 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2091 | hdr_len = 28; |
| 2092 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2093 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2094 | } |
| 2095 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2096 | } |
| 2097 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2098 | /* Parse the chunk size at msg->next. Once done, caller should adjust ->next to |
| 2099 | * point to the first byte of data after the chunk size, so that we know we can |
| 2100 | * forward exactly msg->next bytes. msg->sol contains the exact number of bytes |
| 2101 | * forming the chunk size. That way it is always possible to differentiate |
| 2102 | * between the start of the body and the start of the data. Return the number |
| 2103 | * of byte parsed on success, 0 when some data is missing, <0 on error. Note: |
| 2104 | * 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] | 2105 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2106 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2107 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2108 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2109 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2110 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2111 | const char *end = buf->data + buf->size; |
| 2112 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2113 | unsigned int chunk = 0; |
| 2114 | |
| 2115 | /* The chunk size is in the following form, though we are only |
| 2116 | * interested in the size and CRLF : |
| 2117 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2118 | */ |
| 2119 | while (1) { |
| 2120 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2121 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2122 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2123 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2124 | if (c < 0) /* not a hex digit anymore */ |
| 2125 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2126 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2127 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2128 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2129 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2130 | chunk = (chunk << 4) + c; |
| 2131 | } |
| 2132 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2133 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2134 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2135 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2136 | |
| 2137 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2138 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2139 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2140 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2141 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2142 | } |
| 2143 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2144 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2145 | * for the end of chunk size. |
| 2146 | */ |
| 2147 | while (1) { |
| 2148 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2149 | /* we now have a CR or an LF at ptr */ |
| 2150 | if (likely(*ptr == '\r')) { |
| 2151 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2152 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2153 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2154 | return 0; |
| 2155 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2156 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2157 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2158 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2159 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2160 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2161 | /* done */ |
| 2162 | break; |
| 2163 | } |
| 2164 | else if (*ptr == ';') { |
| 2165 | /* chunk extension, ends at next CRLF */ |
| 2166 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2167 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2168 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2169 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2170 | |
| 2171 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2172 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2173 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2174 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2175 | return 0; |
| 2176 | } |
| 2177 | /* we have a CRLF now, loop above */ |
| 2178 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2179 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2180 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2181 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2184 | /* OK we found our CRLF and now <ptr> points to the next byte, which may |
| 2185 | * or may not be present. We save the number of bytes parsed into |
| 2186 | * msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2187 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2188 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2189 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2190 | msg->sol += buf->size; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2191 | msg->chunk_len = chunk; |
| 2192 | msg->body_len += chunk; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2193 | return msg->sol; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2194 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2195 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2196 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2197 | } |
| 2198 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2199 | /* This function skips trailers in the buffer associated with HTTP message |
| 2200 | * <msg>. The first visited position is msg->next. If the end of the trailers is |
| 2201 | * found, the function returns >0. So, the caller can automatically schedul it |
| 2202 | * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough |
| 2203 | * data are available, the function does not change anything except maybe |
| 2204 | * msg->sol if it could parse some lines, and returns zero. If a parse error |
| 2205 | * is encountered, the function returns < 0 and does not change anything except |
| 2206 | * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS |
| 2207 | * state before calling this function, which implies that all non-trailers data |
| 2208 | * have already been scheduled for forwarding, and that msg->next exactly |
| 2209 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2210 | * important to note that this function is designed to be able to parse wrapped |
| 2211 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2212 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2213 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2214 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2215 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2216 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2217 | /* we have msg->next which points to next line. Look for CRLF. But |
| 2218 | * first, we reset msg->sol */ |
| 2219 | msg->sol = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2220 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2221 | const char *p1 = NULL, *p2 = NULL; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2222 | const char *start = b_ptr(buf, msg->next + msg->sol); |
| 2223 | const char *stop = bi_end(buf); |
| 2224 | const char *ptr = start; |
| 2225 | int bytes = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2226 | |
| 2227 | /* scan current line and stop at LF or CRLF */ |
| 2228 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2229 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2230 | return 0; |
| 2231 | |
| 2232 | if (*ptr == '\n') { |
| 2233 | if (!p1) |
| 2234 | p1 = ptr; |
| 2235 | p2 = ptr; |
| 2236 | break; |
| 2237 | } |
| 2238 | |
| 2239 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2240 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2241 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2242 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2243 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2244 | p1 = ptr; |
| 2245 | } |
| 2246 | |
| 2247 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2248 | if (ptr >= buf->data + buf->size) |
| 2249 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /* after LF; point to beginning of next line */ |
| 2253 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2254 | if (p2 >= buf->data + buf->size) |
| 2255 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2256 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2257 | bytes = p2 - start; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2258 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2259 | bytes += buf->size; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2260 | msg->sol += bytes; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2261 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2262 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2263 | * Everything was scheduled for forwarding, there's nothing left |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2264 | * from this message. */ |
| 2265 | if (p1 == start) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2266 | return 1; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2267 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2268 | /* OK, next line then */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2272 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
| 2273 | * a possible LF alone at the end of a chunk. The caller should adjust msg->next |
| 2274 | * in order to include this part into the next forwarding phase. Note that the |
| 2275 | * caller must ensure that ->p points to the first byte to parse. It returns |
| 2276 | * the number of bytes parsed on success, so the caller can set msg_state to |
| 2277 | * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not |
| 2278 | * change anything and returns zero. If a parse error is encountered, the |
| 2279 | * function returns < 0. Note: this function is designed to parse wrapped CRLF |
| 2280 | * at the end of the buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2281 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2282 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2283 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2284 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2285 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2286 | int bytes; |
| 2287 | |
| 2288 | /* NB: we'll check data availabilty at the end. It's not a |
| 2289 | * problem because whatever we match first will be checked |
| 2290 | * against the correct length. |
| 2291 | */ |
| 2292 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2293 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2294 | if (*ptr == '\r') { |
| 2295 | bytes++; |
| 2296 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2297 | if (ptr >= buf->data + buf->size) |
| 2298 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2301 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2302 | return 0; |
| 2303 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2304 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2305 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2306 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2307 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2308 | return bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2309 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2310 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2311 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2312 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2313 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2314 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2315 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2316 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2317 | { |
| 2318 | int q = 1000; |
| 2319 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2320 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2321 | goto out; |
| 2322 | q = (*qvalue++ - '0') * 1000; |
| 2323 | |
| 2324 | if (*qvalue++ != '.') |
| 2325 | goto out; |
| 2326 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2327 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2328 | goto out; |
| 2329 | q += (*qvalue++ - '0') * 100; |
| 2330 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2331 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2332 | goto out; |
| 2333 | q += (*qvalue++ - '0') * 10; |
| 2334 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2335 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2336 | goto out; |
| 2337 | q += (*qvalue++ - '0') * 1; |
| 2338 | out: |
| 2339 | if (q > 1000) |
| 2340 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2341 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2342 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2343 | return q; |
| 2344 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2345 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2346 | 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] | 2347 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2348 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2349 | int tmp = TX_CON_WANT_KAL; |
| 2350 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2351 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2352 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2353 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2354 | tmp = TX_CON_WANT_TUN; |
| 2355 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2356 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2357 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2358 | tmp = TX_CON_WANT_TUN; |
| 2359 | } |
| 2360 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2361 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2362 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2363 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2364 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2365 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2366 | tmp = TX_CON_WANT_CLO; |
| 2367 | else |
| 2368 | tmp = TX_CON_WANT_SCL; |
| 2369 | } |
| 2370 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2371 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2372 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2373 | tmp = TX_CON_WANT_CLO; |
| 2374 | |
| 2375 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2376 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2377 | |
| 2378 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2379 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2380 | /* parse the Connection header and possibly clean it */ |
| 2381 | int to_del = 0; |
| 2382 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2383 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2384 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2385 | to_del |= 2; /* remove "keep-alive" */ |
| 2386 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2387 | to_del |= 1; /* remove "close" */ |
| 2388 | http_parse_connection_header(txn, msg, to_del); |
| 2389 | } |
| 2390 | |
| 2391 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2392 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2393 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2394 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2395 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2396 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2397 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2398 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2399 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2400 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2401 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2402 | * processing can continue on next analysers, or zero if it either needs more |
| 2403 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2404 | * 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] | 2405 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2406 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2407 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2408 | 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] | 2409 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2410 | /* |
| 2411 | * We will parse the partial (or complete) lines. |
| 2412 | * We will check the request syntax, and also join multi-line |
| 2413 | * headers. An index of all the lines will be elaborated while |
| 2414 | * parsing. |
| 2415 | * |
| 2416 | * For the parsing, we use a 28 states FSM. |
| 2417 | * |
| 2418 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2419 | * req->buf->p = beginning of request |
| 2420 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2421 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2422 | * msg->eol = end of current header or line (LF or CRLF) |
| 2423 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2424 | * |
| 2425 | * 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] | 2426 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2427 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2428 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2429 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2430 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2432 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2433 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2434 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2435 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2436 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2437 | 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] | 2438 | now_ms, __FUNCTION__, |
| 2439 | s, |
| 2440 | req, |
| 2441 | req->rex, req->wex, |
| 2442 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2443 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2444 | req->analysers); |
| 2445 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2446 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2447 | s->srv_error = http_return_srv_error; |
| 2448 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2449 | /* There's a protected area at the end of the buffer for rewriting |
| 2450 | * purposes. We don't want to start to parse the request if the |
| 2451 | * protected area is affected, because we may have to move processed |
| 2452 | * data later, which is much more complicated. |
| 2453 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2454 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2455 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2456 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2457 | 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] | 2458 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2459 | /* 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] | 2460 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2461 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2462 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2463 | return 0; |
| 2464 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2465 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2466 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2467 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2468 | } |
| 2469 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2470 | /* Note that we have the same problem with the response ; we |
| 2471 | * may want to send a redirect, error or anything which requires |
| 2472 | * some spare space. So we'll ensure that we have at least |
| 2473 | * maxrewrite bytes available in the response buffer before |
| 2474 | * processing that one. This will only affect pipelined |
| 2475 | * keep-alive requests. |
| 2476 | */ |
| 2477 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2478 | unlikely(!channel_is_rewritable(&s->res) || |
| 2479 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2480 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2481 | if (s->res.buf->o) { |
| 2482 | 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] | 2483 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2484 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2485 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2486 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2487 | s->res.flags |= CF_WAKE_WRITE; |
| 2488 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2489 | return 0; |
| 2490 | } |
| 2491 | } |
| 2492 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2493 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2494 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2495 | } |
| 2496 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2497 | /* 1: we might have to print this header in debug mode */ |
| 2498 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2499 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2500 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2501 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2502 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2503 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2504 | /* this is a bit complex : in case of error on the request line, |
| 2505 | * we know that rq.l is still zero, so we display only the part |
| 2506 | * up to the end of the line (truncated by debug_hdr). |
| 2507 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2508 | 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] | 2509 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2510 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2511 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2512 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2513 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | while (cur_idx) { |
| 2515 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2516 | debug_hdr("clihdr", s, sol, eol); |
| 2517 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2518 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2519 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2520 | } |
| 2521 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | /* |
| 2524 | * Now we quickly check if we have found a full valid request. |
| 2525 | * If not so, we check the FD and buffer states before leaving. |
| 2526 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2527 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2528 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2529 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2530 | * 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] | 2531 | * 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] | 2532 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2533 | * Last, we may increase some tracked counters' http request errors on |
| 2534 | * the cases that are deliberately the client's fault. For instance, |
| 2535 | * a timeout or connection reset is not counted as an error. However |
| 2536 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2537 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2538 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2539 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2540 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2541 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2542 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2543 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2544 | stream_inc_http_req_ctr(s); |
| 2545 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2546 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2547 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2548 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | /* 1: Since we are in header mode, if there's no space |
| 2551 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2552 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2553 | * must terminate it now. |
| 2554 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2555 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2556 | /* FIXME: check if URI is set and return Status |
| 2557 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2558 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2559 | stream_inc_http_req_ctr(s); |
| 2560 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2561 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2562 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2563 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2564 | goto return_bad_req; |
| 2565 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2566 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2567 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2568 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2569 | if (!(s->flags & SF_ERR_MASK)) |
| 2570 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2571 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2572 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2573 | goto failed_keep_alive; |
| 2574 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2575 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2576 | goto failed_keep_alive; |
| 2577 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2578 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2579 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2580 | 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] | 2581 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2582 | } |
| 2583 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2584 | txn->status = 400; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2585 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2586 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2587 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2588 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2589 | proxy_inc_fe_req_ctr(sess->fe); |
| 2590 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2591 | if (sess->listener->counters) |
| 2592 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2593 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2594 | if (!(s->flags & SF_FINST_MASK)) |
| 2595 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2596 | return 0; |
| 2597 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2598 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2600 | 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] | 2601 | if (!(s->flags & SF_ERR_MASK)) |
| 2602 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2603 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2604 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2605 | goto failed_keep_alive; |
| 2606 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2607 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2608 | goto failed_keep_alive; |
| 2609 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2610 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2611 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2612 | 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] | 2613 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2614 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2615 | txn->status = 408; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2616 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2617 | 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] | 2618 | req->analysers &= AN_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2619 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2620 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2621 | proxy_inc_fe_req_ctr(sess->fe); |
| 2622 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2623 | if (sess->listener->counters) |
| 2624 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2625 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2626 | if (!(s->flags & SF_FINST_MASK)) |
| 2627 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2628 | return 0; |
| 2629 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2630 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2631 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2632 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2633 | if (!(s->flags & SF_ERR_MASK)) |
| 2634 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2635 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2636 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2637 | goto failed_keep_alive; |
| 2638 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2639 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2640 | goto failed_keep_alive; |
| 2641 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2642 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2643 | 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] | 2644 | txn->status = 400; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2645 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2646 | 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] | 2647 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2648 | stream_inc_http_err_ctr(s); |
| 2649 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2650 | proxy_inc_fe_req_ctr(sess->fe); |
| 2651 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2652 | if (sess->listener->counters) |
| 2653 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2654 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2655 | if (!(s->flags & SF_FINST_MASK)) |
| 2656 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2657 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2658 | } |
| 2659 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2660 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2661 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2662 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2663 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2664 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2665 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2666 | /* We need more data, we have to re-enable quick-ack in case we |
| 2667 | * previously disabled it, otherwise we might cause the client |
| 2668 | * to delay next data. |
| 2669 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2670 | 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] | 2671 | } |
| 2672 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2673 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2674 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2675 | /* If the client starts to talk, let's fall back to |
| 2676 | * request timeout processing. |
| 2677 | */ |
| 2678 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2679 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2680 | } |
| 2681 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2682 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2683 | if (!tick_isset(req->analyse_exp)) { |
| 2684 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2685 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2686 | tick_isset(s->be->timeout.httpka)) |
| 2687 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2688 | else |
| 2689 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2690 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2691 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2692 | /* we're not ready yet */ |
| 2693 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2694 | |
| 2695 | failed_keep_alive: |
| 2696 | /* Here we process low-level errors for keep-alive requests. In |
| 2697 | * short, if the request is not the first one and it experiences |
| 2698 | * a timeout, read error or shutdown, we just silently close so |
| 2699 | * that the client can try again. |
| 2700 | */ |
| 2701 | txn->status = 0; |
| 2702 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2703 | req->analysers &= AN_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2704 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2705 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2706 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2707 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2708 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2709 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2710 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2711 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2712 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2713 | * 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] | 2714 | * 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] | 2715 | * 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] | 2716 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2717 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2718 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2719 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2720 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2721 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2722 | 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] | 2723 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2724 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2725 | /* kill the pending keep-alive timeout */ |
| 2726 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2727 | req->analyse_exp = TICK_ETERNITY; |
| 2728 | } |
| 2729 | |
| 2730 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2731 | /* Maybe we found in invalid header name while we were configured not |
| 2732 | * to block on that, so we have to capture it now. |
| 2733 | */ |
| 2734 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2735 | 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] | 2736 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2737 | /* |
| 2738 | * 1: identify the method |
| 2739 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2740 | 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] | 2741 | |
| 2742 | /* we can make use of server redirect on GET and HEAD */ |
| 2743 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2744 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2745 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2746 | /* |
| 2747 | * 2: check if the URI matches the monitor_uri. |
| 2748 | * We have to do this for every request which gets in, because |
| 2749 | * the monitor-uri is defined by the frontend. |
| 2750 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2751 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2752 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2753 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2754 | sess->fe->monitor_uri, |
| 2755 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2756 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2757 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2758 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2759 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2760 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2761 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2762 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2763 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2764 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2765 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2766 | 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] | 2767 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2768 | ret = acl_pass(ret); |
| 2769 | if (cond->pol == ACL_COND_UNLESS) |
| 2770 | ret = !ret; |
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 | if (ret) { |
| 2773 | /* we fail this request, let's return 503 service unavail */ |
| 2774 | txn->status = 503; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2775 | 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] | 2776 | if (!(s->flags & SF_ERR_MASK)) |
| 2777 | 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] | 2778 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2779 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2780 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2781 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2782 | /* nothing to fail, let's reply normaly */ |
| 2783 | txn->status = 200; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2784 | 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] | 2785 | if (!(s->flags & SF_ERR_MASK)) |
| 2786 | 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] | 2787 | goto return_prx_cond; |
| 2788 | } |
| 2789 | |
| 2790 | /* |
| 2791 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2792 | * Note: we cannot log anymore if the request has been |
| 2793 | * classified as invalid. |
| 2794 | */ |
| 2795 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2796 | /* we have a complete HTTP request that we must log */ |
| 2797 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2798 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2799 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | if (urilen >= REQURI_LEN) |
| 2801 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2802 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2803 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2804 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2805 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2806 | s->do_log(s); |
| 2807 | } else { |
| 2808 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2809 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2810 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2811 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2812 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2813 | * exactly one digit "." one digit. This check may be disabled using |
| 2814 | * option accept-invalid-http-request. |
| 2815 | */ |
| 2816 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2817 | if (msg->sl.rq.v_l != 8) { |
| 2818 | msg->err_pos = msg->sl.rq.v; |
| 2819 | goto return_bad_req; |
| 2820 | } |
| 2821 | |
| 2822 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2823 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2824 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2825 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2826 | msg->err_pos = msg->sl.rq.v + 4; |
| 2827 | goto return_bad_req; |
| 2828 | } |
| 2829 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2830 | else { |
| 2831 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2832 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2833 | goto return_bad_req; |
| 2834 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2835 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2836 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2837 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2838 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2839 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2840 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2841 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2842 | |
| 2843 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2844 | 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] | 2845 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2846 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2847 | * we have what looks like a proxied connection instead of a connection, |
| 2848 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2849 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2850 | * happen to do that despite being non-standard :-( |
| 2851 | * We consider that a request not beginning with either '/' or '*' is |
| 2852 | * a proxied connection, which covers both "scheme://location" and |
| 2853 | * CONNECT ip:port. |
| 2854 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2855 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2856 | 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] | 2857 | txn->flags |= TX_USE_PX_CONN; |
| 2858 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2859 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2860 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2861 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2862 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2863 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2864 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2865 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2866 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2867 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2868 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2869 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2870 | * The length of a message body is determined by one of the following |
| 2871 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2872 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2873 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2874 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2875 | * code is always terminated by the first empty line after the |
| 2876 | * header fields, regardless of the header fields present in the |
| 2877 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2878 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2879 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2880 | * the connection will become a tunnel immediately after the empty |
| 2881 | * line that concludes the header fields. A client MUST ignore any |
| 2882 | * Content-Length or Transfer-Encoding header fields received in |
| 2883 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2884 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2885 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2886 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2887 | * body length is determined by reading and decoding the chunked |
| 2888 | * data until the transfer coding indicates the data is complete. |
| 2889 | * |
| 2890 | * If a Transfer-Encoding header field is present in a response and |
| 2891 | * the chunked transfer coding is not the final encoding, the |
| 2892 | * message body length is determined by reading the connection until |
| 2893 | * it is closed by the server. If a Transfer-Encoding header field |
| 2894 | * is present in a request and the chunked transfer coding is not |
| 2895 | * the final encoding, the message body length cannot be determined |
| 2896 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2897 | * status code and then close the connection. |
| 2898 | * |
| 2899 | * If a message is received with both a Transfer-Encoding and a |
| 2900 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2901 | * Content-Length. Such a message might indicate an attempt to |
| 2902 | * perform request smuggling (Section 9.5) or response splitting |
| 2903 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2904 | * remove the received Content-Length field prior to forwarding such |
| 2905 | * a message downstream. |
| 2906 | * |
| 2907 | * 4. If a message is received without Transfer-Encoding and with |
| 2908 | * either multiple Content-Length header fields having differing |
| 2909 | * field-values or a single Content-Length header field having an |
| 2910 | * invalid value, then the message framing is invalid and the |
| 2911 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2912 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2913 | * status code and then close the connection. If this is a response |
| 2914 | * message received by a proxy, the proxy MUST close the connection |
| 2915 | * to the server, discard the received response, and send a 502 (Bad |
| 2916 | * Gateway) response to the client. If this is a response message |
| 2917 | * received by a user agent, the user agent MUST close the |
| 2918 | * connection to the server and discard the received response. |
| 2919 | * |
| 2920 | * 5. If a valid Content-Length header field is present without |
| 2921 | * Transfer-Encoding, its decimal value defines the expected message |
| 2922 | * body length in octets. If the sender closes the connection or |
| 2923 | * the recipient times out before the indicated number of octets are |
| 2924 | * received, the recipient MUST consider the message to be |
| 2925 | * incomplete and close the connection. |
| 2926 | * |
| 2927 | * 6. If this is a request message and none of the above are true, then |
| 2928 | * the message body length is zero (no message body is present). |
| 2929 | * |
| 2930 | * 7. Otherwise, this is a response message without a declared message |
| 2931 | * body length, so the message body length is determined by the |
| 2932 | * number of octets received prior to the server closing the |
| 2933 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2934 | */ |
| 2935 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2936 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2937 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2938 | 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] | 2939 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2940 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2941 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2942 | /* chunked not last, return badreq */ |
| 2943 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2944 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2947 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2948 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2949 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2950 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2951 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2952 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2953 | 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] | 2954 | signed long long cl; |
| 2955 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2956 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2957 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2958 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2959 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2960 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2961 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2962 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2963 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2964 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2965 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2966 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2967 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2968 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2969 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2971 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2972 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2973 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2974 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2975 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2976 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2977 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2978 | } |
| 2979 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2980 | /* even bodyless requests have a known length */ |
| 2981 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2982 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2983 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2984 | * only change if both the request and the config reference something else. |
| 2985 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2986 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2987 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2988 | * if FE and BE have the same settings (common). The method consists in |
| 2989 | * checking if options changed between the two calls (implying that either |
| 2990 | * one is non-null, or one of them is non-null and we are there for the first |
| 2991 | * time. |
| 2992 | */ |
| 2993 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2994 | ((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] | 2995 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2996 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 2997 | /* we may have to wait for the request's body */ |
| 2998 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 2999 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3000 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3001 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3002 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3003 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3004 | req->analyse_exp = TICK_ETERNITY; |
| 3005 | return 1; |
| 3006 | |
| 3007 | return_bad_req: |
| 3008 | /* We centralize bad requests processing here */ |
| 3009 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3010 | /* we detected a parsing error. We want to archive this request |
| 3011 | * in the dedicated proxy area for later troubleshooting. |
| 3012 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3013 | 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] | 3014 | } |
| 3015 | |
| 3016 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3017 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3018 | 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] | 3019 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3020 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3021 | if (sess->listener->counters) |
| 3022 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3023 | |
| 3024 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3025 | if (!(s->flags & SF_ERR_MASK)) |
| 3026 | s->flags |= SF_ERR_PRXCOND; |
| 3027 | if (!(s->flags & SF_FINST_MASK)) |
| 3028 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3029 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3030 | req->analysers &= AN_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3031 | req->analyse_exp = TICK_ETERNITY; |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3035 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3036 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3037 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3038 | * and program a response message if something was unexpected. It cannot fail |
| 3039 | * 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] | 3040 | * 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] | 3041 | * 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] | 3042 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3043 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3044 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3045 | { |
| 3046 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3047 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3048 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3049 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3050 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3051 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3052 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3053 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3054 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3055 | appctx = si_appctx(si); |
| 3056 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3057 | appctx->st1 = appctx->st2 = 0; |
| 3058 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3059 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3060 | 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] | 3061 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3062 | |
| 3063 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3064 | lookup = uri + uri_auth->uri_len; |
| 3065 | |
| 3066 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3067 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3068 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3069 | break; |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | if (uri_auth->refresh) { |
| 3074 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3075 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3076 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3077 | break; |
| 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3083 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3084 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3085 | break; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3090 | if (memcmp(h, ";st=", 4) == 0) { |
| 3091 | int i; |
| 3092 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3093 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3094 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3095 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3096 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3097 | break; |
| 3098 | } |
| 3099 | } |
| 3100 | break; |
| 3101 | } |
| 3102 | } |
| 3103 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3104 | appctx->ctx.stats.scope_str = 0; |
| 3105 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3106 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3107 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3108 | int itx = 0; |
| 3109 | const char *h2; |
| 3110 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3111 | const char *err; |
| 3112 | |
| 3113 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3114 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3115 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3116 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3117 | itx++; |
| 3118 | h++; |
| 3119 | } |
| 3120 | |
| 3121 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3122 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3123 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3124 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3125 | /* 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] | 3126 | memcpy(scope_txt, h2, itx); |
| 3127 | scope_txt[itx] = '\0'; |
| 3128 | err = invalid_char(scope_txt); |
| 3129 | if (err) { |
| 3130 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3131 | appctx->ctx.stats.scope_str = 0; |
| 3132 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3133 | } |
| 3134 | break; |
| 3135 | } |
| 3136 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3137 | |
| 3138 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3139 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3140 | int ret = 1; |
| 3141 | |
| 3142 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3143 | 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] | 3144 | ret = acl_pass(ret); |
| 3145 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3146 | ret = !ret; |
| 3147 | } |
| 3148 | |
| 3149 | if (ret) { |
| 3150 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3151 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3152 | break; |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3157 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3158 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3159 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3160 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3161 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3162 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3163 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3164 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3165 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3166 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3167 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3168 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3169 | else { |
| 3170 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3171 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3172 | } |
| 3173 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3174 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3175 | return 1; |
| 3176 | } |
| 3177 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3178 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3179 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3180 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3181 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3182 | { |
| 3183 | #ifdef IP_TOS |
| 3184 | if (from.ss_family == AF_INET) |
| 3185 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3186 | #endif |
| 3187 | #ifdef IPV6_TCLASS |
| 3188 | if (from.ss_family == AF_INET6) { |
| 3189 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3190 | /* v4-mapped addresses need IP_TOS */ |
| 3191 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3192 | else |
| 3193 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3194 | } |
| 3195 | #endif |
| 3196 | } |
| 3197 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3198 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3199 | const char* name, unsigned int name_len, |
| 3200 | const char *str, struct my_regex *re, |
| 3201 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3202 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3203 | struct hdr_ctx ctx; |
| 3204 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3205 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3206 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3207 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3208 | struct chunk *output = get_trash_chunk(); |
| 3209 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3210 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3211 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3212 | /* Choose the header browsing function. */ |
| 3213 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3214 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3215 | http_find_hdr_func = http_find_header2; |
| 3216 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3217 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3218 | http_find_hdr_func = http_find_full_header2; |
| 3219 | break; |
| 3220 | default: /* impossible */ |
| 3221 | return -1; |
| 3222 | } |
| 3223 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3224 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3225 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3226 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3227 | char *val = ctx.line + ctx.val; |
| 3228 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3229 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3230 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3231 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3232 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3233 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3234 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3235 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3236 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3237 | 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] | 3238 | |
| 3239 | hdr->len += delta; |
| 3240 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3241 | |
| 3242 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3243 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | return 0; |
| 3247 | } |
| 3248 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3249 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3250 | const char* name, unsigned int name_len, |
| 3251 | struct list *fmt, struct my_regex *re, |
| 3252 | int action) |
| 3253 | { |
| 3254 | struct chunk *replace = get_trash_chunk(); |
| 3255 | |
| 3256 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3257 | if (replace->len >= replace->size - 1) |
| 3258 | return -1; |
| 3259 | |
| 3260 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3261 | } |
| 3262 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3263 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3264 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3265 | * further processing of the request (auth, deny, ...), and defaults to |
| 3266 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3267 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3268 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3269 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3270 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3271 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3272 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3273 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3274 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3275 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3276 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3277 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3278 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3279 | int act_flags = 0; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3280 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3281 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3282 | * resume condition. If a resume is needed it is always in the action |
| 3283 | * and never in the ACL or converters. In this case, we initialise the |
| 3284 | * current rule, and go to the action execution point. |
| 3285 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3286 | if (s->current_rule) { |
| 3287 | rule = s->current_rule; |
| 3288 | s->current_rule = NULL; |
| 3289 | if (s->current_rule_list == rules) |
| 3290 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3291 | } |
| 3292 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3293 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3294 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3295 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3296 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3297 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3298 | int ret; |
| 3299 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3300 | 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] | 3301 | ret = acl_pass(ret); |
| 3302 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3303 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3304 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3305 | |
| 3306 | if (!ret) /* condition not matched */ |
| 3307 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3308 | } |
| 3309 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3310 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3311 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3312 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3313 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3314 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3315 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3316 | case ACT_ACTION_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3317 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3318 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3319 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3320 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3321 | txn->flags |= TX_CLTARPIT; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3322 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3323 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3324 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3325 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3326 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3327 | auth_realm = rule->arg.auth.realm; |
| 3328 | if (!auth_realm) { |
| 3329 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3330 | auth_realm = STATS_DEFAULT_REALM; |
| 3331 | else |
| 3332 | auth_realm = px->id; |
| 3333 | } |
| 3334 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3335 | * count one error, because normal browsing won't significantly |
| 3336 | * increase the counter but brute force attempts will. |
| 3337 | */ |
| 3338 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3339 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3340 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3341 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3342 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3343 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3344 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3345 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3346 | return HTTP_RULE_RES_BADREQ; |
| 3347 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3348 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3349 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3350 | s->task->nice = rule->arg.nice; |
| 3351 | break; |
| 3352 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3353 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3354 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3355 | 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] | 3356 | break; |
| 3357 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3358 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3359 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3360 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3361 | 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] | 3362 | #endif |
| 3363 | break; |
| 3364 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3365 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3366 | s->logs.level = rule->arg.loglevel; |
| 3367 | break; |
| 3368 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3369 | case ACT_HTTP_REPLACE_HDR: |
| 3370 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3371 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3372 | rule->arg.hdr_add.name_len, |
| 3373 | &rule->arg.hdr_add.fmt, |
| 3374 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3375 | return HTTP_RULE_RES_BADREQ; |
| 3376 | break; |
| 3377 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3378 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3379 | ctx.idx = 0; |
| 3380 | /* remove all occurrences of the header */ |
| 3381 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3382 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3383 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3384 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3385 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3386 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3387 | case ACT_HTTP_SET_HDR: |
| 3388 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3389 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3390 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3391 | trash.len = rule->arg.hdr_add.name_len; |
| 3392 | trash.str[trash.len++] = ':'; |
| 3393 | trash.str[trash.len++] = ' '; |
| 3394 | 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] | 3395 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3396 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3397 | /* remove all occurrences of the header */ |
| 3398 | ctx.idx = 0; |
| 3399 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3400 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3401 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3402 | } |
| 3403 | } |
| 3404 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3405 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3406 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3407 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3408 | case ACT_HTTP_DEL_ACL: |
| 3409 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3410 | struct pat_ref *ref; |
| 3411 | char *key; |
| 3412 | int len; |
| 3413 | |
| 3414 | /* collect reference */ |
| 3415 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3416 | if (!ref) |
| 3417 | continue; |
| 3418 | |
| 3419 | /* collect key */ |
| 3420 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3421 | key = trash.str; |
| 3422 | key[len] = '\0'; |
| 3423 | |
| 3424 | /* perform update */ |
| 3425 | /* returned code: 1=ok, 0=ko */ |
| 3426 | pat_ref_delete(ref, key); |
| 3427 | |
| 3428 | break; |
| 3429 | } |
| 3430 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3431 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3432 | struct pat_ref *ref; |
| 3433 | char *key; |
| 3434 | struct chunk *trash_key; |
| 3435 | int len; |
| 3436 | |
| 3437 | trash_key = get_trash_chunk(); |
| 3438 | |
| 3439 | /* collect reference */ |
| 3440 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3441 | if (!ref) |
| 3442 | continue; |
| 3443 | |
| 3444 | /* collect key */ |
| 3445 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3446 | key = trash_key->str; |
| 3447 | key[len] = '\0'; |
| 3448 | |
| 3449 | /* perform update */ |
| 3450 | /* add entry only if it does not already exist */ |
| 3451 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3452 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3453 | |
| 3454 | break; |
| 3455 | } |
| 3456 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3457 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3458 | struct pat_ref *ref; |
| 3459 | char *key, *value; |
| 3460 | struct chunk *trash_key, *trash_value; |
| 3461 | int len; |
| 3462 | |
| 3463 | trash_key = get_trash_chunk(); |
| 3464 | trash_value = 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 | /* collect value */ |
| 3477 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3478 | value = trash_value->str; |
| 3479 | value[len] = '\0'; |
| 3480 | |
| 3481 | /* perform update */ |
| 3482 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3483 | /* update entry if it exists */ |
| 3484 | pat_ref_set(ref, key, value, NULL); |
| 3485 | else |
| 3486 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3487 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3488 | |
| 3489 | break; |
| 3490 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3491 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3492 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3493 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3494 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3495 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3496 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3497 | case ACT_RET_ERR: |
| 3498 | case ACT_RET_CONT: |
| 3499 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3500 | case ACT_RET_STOP: |
| 3501 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3502 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3503 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3504 | return HTTP_RULE_RES_YIELD; |
| 3505 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3506 | break; |
| 3507 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3508 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3509 | /* Note: only the first valid tracking parameter of each |
| 3510 | * applies. |
| 3511 | */ |
| 3512 | |
| 3513 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3514 | struct stktable *t; |
| 3515 | struct stksess *ts; |
| 3516 | struct stktable_key *key; |
| 3517 | void *ptr; |
| 3518 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3519 | t = rule->arg.trk_ctr.table.t; |
| 3520 | 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] | 3521 | |
| 3522 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3523 | stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3524 | |
| 3525 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3526 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3527 | if (ptr) |
| 3528 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3529 | |
| 3530 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3531 | if (ptr) |
| 3532 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3533 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3534 | |
| 3535 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3536 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3537 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3538 | } |
| 3539 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3540 | break; |
| 3541 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3542 | case ACT_HTTP_REQ_SET_SRC: |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3543 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) { |
| 3544 | struct sample *smp; |
| 3545 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 3546 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR); |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3547 | |
| 3548 | if (smp) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3549 | if (smp->data.type == SMP_T_IPV4) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3550 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3551 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3552 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3553 | } else if (smp->data.type == SMP_T_IPV6) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3554 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3555 | memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr)); |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3556 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0; |
| 3557 | } |
| 3558 | } |
| 3559 | } |
| 3560 | break; |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3561 | |
| 3562 | /* other flags exists, but normaly, they never be matched. */ |
| 3563 | default: |
| 3564 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3565 | } |
| 3566 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3567 | |
| 3568 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3569 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3570 | } |
| 3571 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3572 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3573 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3574 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3575 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3576 | * is returned, the process can continue the evaluation of next rule list. If |
| 3577 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3578 | * is returned, it means the operation could not be processed and a server error |
| 3579 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3580 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3581 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3582 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3583 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3584 | 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] | 3585 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3586 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3587 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3588 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3589 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3590 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3591 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3592 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3593 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3594 | * resume condition. If a resume is needed it is always in the action |
| 3595 | * and never in the ACL or converters. In this case, we initialise the |
| 3596 | * current rule, and go to the action execution point. |
| 3597 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3598 | if (s->current_rule) { |
| 3599 | rule = s->current_rule; |
| 3600 | s->current_rule = NULL; |
| 3601 | if (s->current_rule_list == rules) |
| 3602 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3603 | } |
| 3604 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3605 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3606 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3607 | |
| 3608 | /* check optional condition */ |
| 3609 | if (rule->cond) { |
| 3610 | int ret; |
| 3611 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3612 | 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] | 3613 | ret = acl_pass(ret); |
| 3614 | |
| 3615 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3616 | ret = !ret; |
| 3617 | |
| 3618 | if (!ret) /* condition not matched */ |
| 3619 | continue; |
| 3620 | } |
| 3621 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3622 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3623 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3624 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3625 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3626 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3627 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3628 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3629 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3630 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3631 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3632 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3633 | s->task->nice = rule->arg.nice; |
| 3634 | break; |
| 3635 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3636 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3637 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3638 | 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] | 3639 | break; |
| 3640 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3641 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3642 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3643 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3644 | 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] | 3645 | #endif |
| 3646 | break; |
| 3647 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3648 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3649 | s->logs.level = rule->arg.loglevel; |
| 3650 | break; |
| 3651 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3652 | case ACT_HTTP_REPLACE_HDR: |
| 3653 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3654 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3655 | rule->arg.hdr_add.name_len, |
| 3656 | &rule->arg.hdr_add.fmt, |
| 3657 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3658 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3659 | break; |
| 3660 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3661 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3662 | ctx.idx = 0; |
| 3663 | /* remove all occurrences of the header */ |
| 3664 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3665 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3666 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3667 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3668 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3669 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3670 | case ACT_HTTP_SET_HDR: |
| 3671 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3672 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3673 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3674 | trash.len = rule->arg.hdr_add.name_len; |
| 3675 | trash.str[trash.len++] = ':'; |
| 3676 | trash.str[trash.len++] = ' '; |
| 3677 | 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] | 3678 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3679 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3680 | /* remove all occurrences of the header */ |
| 3681 | ctx.idx = 0; |
| 3682 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3683 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3684 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3685 | } |
| 3686 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3687 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3688 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3689 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3690 | case ACT_HTTP_DEL_ACL: |
| 3691 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3692 | struct pat_ref *ref; |
| 3693 | char *key; |
| 3694 | int len; |
| 3695 | |
| 3696 | /* collect reference */ |
| 3697 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3698 | if (!ref) |
| 3699 | continue; |
| 3700 | |
| 3701 | /* collect key */ |
| 3702 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3703 | key = trash.str; |
| 3704 | key[len] = '\0'; |
| 3705 | |
| 3706 | /* perform update */ |
| 3707 | /* returned code: 1=ok, 0=ko */ |
| 3708 | pat_ref_delete(ref, key); |
| 3709 | |
| 3710 | break; |
| 3711 | } |
| 3712 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3713 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3714 | struct pat_ref *ref; |
| 3715 | char *key; |
| 3716 | struct chunk *trash_key; |
| 3717 | int len; |
| 3718 | |
| 3719 | trash_key = get_trash_chunk(); |
| 3720 | |
| 3721 | /* collect reference */ |
| 3722 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3723 | if (!ref) |
| 3724 | continue; |
| 3725 | |
| 3726 | /* collect key */ |
| 3727 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3728 | key = trash_key->str; |
| 3729 | key[len] = '\0'; |
| 3730 | |
| 3731 | /* perform update */ |
| 3732 | /* check if the entry already exists */ |
| 3733 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3734 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3735 | |
| 3736 | break; |
| 3737 | } |
| 3738 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3739 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3740 | struct pat_ref *ref; |
| 3741 | char *key, *value; |
| 3742 | struct chunk *trash_key, *trash_value; |
| 3743 | int len; |
| 3744 | |
| 3745 | trash_key = get_trash_chunk(); |
| 3746 | trash_value = get_trash_chunk(); |
| 3747 | |
| 3748 | /* collect reference */ |
| 3749 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3750 | if (!ref) |
| 3751 | continue; |
| 3752 | |
| 3753 | /* collect key */ |
| 3754 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3755 | key = trash_key->str; |
| 3756 | key[len] = '\0'; |
| 3757 | |
| 3758 | /* collect value */ |
| 3759 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3760 | value = trash_value->str; |
| 3761 | value[len] = '\0'; |
| 3762 | |
| 3763 | /* perform update */ |
| 3764 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3765 | /* update entry if it exists */ |
| 3766 | pat_ref_set(ref, key, value, NULL); |
| 3767 | else |
| 3768 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3769 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3770 | |
| 3771 | break; |
| 3772 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3773 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3774 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3775 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3776 | return HTTP_RULE_RES_BADREQ; |
| 3777 | return HTTP_RULE_RES_DONE; |
| 3778 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3779 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3780 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3781 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3782 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3783 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3784 | case ACT_RET_ERR: |
| 3785 | case ACT_RET_CONT: |
| 3786 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3787 | case ACT_RET_STOP: |
| 3788 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3789 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3790 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3791 | return HTTP_RULE_RES_YIELD; |
| 3792 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3793 | break; |
| 3794 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3795 | /* other flags exists, but normaly, they never be matched. */ |
| 3796 | default: |
| 3797 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3802 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3806 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3807 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3808 | * as too large a request to build a valid response. |
| 3809 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3810 | 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] | 3811 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3812 | struct http_msg *req = &txn->req; |
| 3813 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3814 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3815 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3816 | |
| 3817 | /* build redirect message */ |
| 3818 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3819 | case 308: |
| 3820 | msg_fmt = HTTP_308; |
| 3821 | break; |
| 3822 | case 307: |
| 3823 | msg_fmt = HTTP_307; |
| 3824 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3825 | case 303: |
| 3826 | msg_fmt = HTTP_303; |
| 3827 | break; |
| 3828 | case 301: |
| 3829 | msg_fmt = HTTP_301; |
| 3830 | break; |
| 3831 | case 302: |
| 3832 | default: |
| 3833 | msg_fmt = HTTP_302; |
| 3834 | break; |
| 3835 | } |
| 3836 | |
| 3837 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3838 | return 0; |
| 3839 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3840 | location = trash.str + trash.len; |
| 3841 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3842 | switch(rule->type) { |
| 3843 | case REDIRECT_TYPE_SCHEME: { |
| 3844 | const char *path; |
| 3845 | const char *host; |
| 3846 | struct hdr_ctx ctx; |
| 3847 | int pathlen; |
| 3848 | int hostlen; |
| 3849 | |
| 3850 | host = ""; |
| 3851 | hostlen = 0; |
| 3852 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3853 | 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] | 3854 | host = ctx.line + ctx.val; |
| 3855 | hostlen = ctx.vlen; |
| 3856 | } |
| 3857 | |
| 3858 | path = http_get_path(txn); |
| 3859 | /* build message using path */ |
| 3860 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3861 | 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] | 3862 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3863 | int qs = 0; |
| 3864 | while (qs < pathlen) { |
| 3865 | if (path[qs] == '?') { |
| 3866 | pathlen = qs; |
| 3867 | break; |
| 3868 | } |
| 3869 | qs++; |
| 3870 | } |
| 3871 | } |
| 3872 | } else { |
| 3873 | path = "/"; |
| 3874 | pathlen = 1; |
| 3875 | } |
| 3876 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3877 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3878 | /* check if we can add scheme + "://" + host + path */ |
| 3879 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3880 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3881 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3882 | /* add scheme */ |
| 3883 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3884 | trash.len += rule->rdr_len; |
| 3885 | } |
| 3886 | else { |
| 3887 | /* add scheme with executing log format */ |
| 3888 | 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] | 3889 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3890 | /* check if we can add scheme + "://" + host + path */ |
| 3891 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3892 | return 0; |
| 3893 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3894 | /* add "://" */ |
| 3895 | memcpy(trash.str + trash.len, "://", 3); |
| 3896 | trash.len += 3; |
| 3897 | |
| 3898 | /* add host */ |
| 3899 | memcpy(trash.str + trash.len, host, hostlen); |
| 3900 | trash.len += hostlen; |
| 3901 | |
| 3902 | /* add path */ |
| 3903 | memcpy(trash.str + trash.len, path, pathlen); |
| 3904 | trash.len += pathlen; |
| 3905 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3906 | /* 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] | 3907 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3908 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3909 | if (trash.len > trash.size - 5) |
| 3910 | return 0; |
| 3911 | trash.str[trash.len] = '/'; |
| 3912 | trash.len++; |
| 3913 | } |
| 3914 | |
| 3915 | break; |
| 3916 | } |
| 3917 | case REDIRECT_TYPE_PREFIX: { |
| 3918 | const char *path; |
| 3919 | int pathlen; |
| 3920 | |
| 3921 | path = http_get_path(txn); |
| 3922 | /* build message using path */ |
| 3923 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3924 | 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] | 3925 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3926 | int qs = 0; |
| 3927 | while (qs < pathlen) { |
| 3928 | if (path[qs] == '?') { |
| 3929 | pathlen = qs; |
| 3930 | break; |
| 3931 | } |
| 3932 | qs++; |
| 3933 | } |
| 3934 | } |
| 3935 | } else { |
| 3936 | path = "/"; |
| 3937 | pathlen = 1; |
| 3938 | } |
| 3939 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3940 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3941 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3942 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3943 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3944 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3945 | * add anything, otherwise it makes it hard for the user to |
| 3946 | * configure a self-redirection. |
| 3947 | */ |
| 3948 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3949 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3950 | trash.len += rule->rdr_len; |
| 3951 | } |
| 3952 | } |
| 3953 | else { |
| 3954 | /* add prefix with executing log format */ |
| 3955 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3956 | |
| 3957 | /* Check length */ |
| 3958 | if (trash.len + pathlen > trash.size - 4) |
| 3959 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | /* add path */ |
| 3963 | memcpy(trash.str + trash.len, path, pathlen); |
| 3964 | trash.len += pathlen; |
| 3965 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3966 | /* 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] | 3967 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3968 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3969 | if (trash.len > trash.size - 5) |
| 3970 | return 0; |
| 3971 | trash.str[trash.len] = '/'; |
| 3972 | trash.len++; |
| 3973 | } |
| 3974 | |
| 3975 | break; |
| 3976 | } |
| 3977 | case REDIRECT_TYPE_LOCATION: |
| 3978 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3979 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3980 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3981 | return 0; |
| 3982 | |
| 3983 | /* add location */ |
| 3984 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3985 | trash.len += rule->rdr_len; |
| 3986 | } |
| 3987 | else { |
| 3988 | /* add location with executing log format */ |
| 3989 | 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] | 3990 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3991 | /* Check left length */ |
| 3992 | if (trash.len > trash.size - 4) |
| 3993 | return 0; |
| 3994 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3995 | break; |
| 3996 | } |
| 3997 | |
| 3998 | if (rule->cookie_len) { |
| 3999 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4000 | trash.len += 14; |
| 4001 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4002 | trash.len += rule->cookie_len; |
| 4003 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4004 | trash.len += 2; |
| 4005 | } |
| 4006 | |
| 4007 | /* add end of headers and the keep-alive/close status. |
| 4008 | * We may choose to set keep-alive if the Location begins |
| 4009 | * with a slash, because the client will come back to the |
| 4010 | * same server. |
| 4011 | */ |
| 4012 | txn->status = rule->code; |
| 4013 | /* let's log the request time */ |
| 4014 | s->logs.tv_request = now; |
| 4015 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4016 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4017 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4018 | ((!(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] | 4019 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4020 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4021 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4022 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4023 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4024 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4025 | trash.len += 30; |
| 4026 | } else { |
| 4027 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4028 | trash.len += 24; |
| 4029 | } |
| 4030 | } |
| 4031 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4032 | trash.len += 4; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4033 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash)); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4034 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4035 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4036 | bi_fast_delete(req->chn->buf, req->sov); |
| 4037 | req->next -= req->sov; |
| 4038 | req->sov = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4039 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END); |
| 4040 | 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] | 4041 | req->msg_state = HTTP_MSG_CLOSED; |
| 4042 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4043 | /* Trim any possible response */ |
| 4044 | res->chn->buf->i = 0; |
| 4045 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4046 | } else { |
| 4047 | /* keep-alive not possible */ |
| 4048 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4049 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4050 | trash.len += 29; |
| 4051 | } else { |
| 4052 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4053 | trash.len += 23; |
| 4054 | } |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4055 | http_reply_and_close(s, txn->status, &trash); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4056 | req->chn->analysers &= AN_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4059 | if (!(s->flags & SF_ERR_MASK)) |
| 4060 | s->flags |= SF_ERR_LOCAL; |
| 4061 | if (!(s->flags & SF_FINST_MASK)) |
| 4062 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4063 | |
| 4064 | return 1; |
| 4065 | } |
| 4066 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4067 | /* This stream analyser runs all HTTP request processing which is common to |
| 4068 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4069 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4070 | * 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] | 4071 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4072 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4073 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4074 | 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] | 4075 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4076 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4077 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4078 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4079 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4080 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4081 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4082 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4083 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4084 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4085 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4086 | } |
| 4087 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4088 | 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] | 4089 | now_ms, __FUNCTION__, |
| 4090 | s, |
| 4091 | req, |
| 4092 | req->rex, req->wex, |
| 4093 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4094 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4095 | req->analysers); |
| 4096 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4097 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4098 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4099 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4100 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4101 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4102 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4103 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4104 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4105 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4106 | goto return_prx_yield; |
| 4107 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4108 | case HTTP_RULE_RES_CONT: |
| 4109 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4110 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4111 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4112 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4113 | if (txn->flags & TX_CLTARPIT) |
| 4114 | goto tarpit; |
| 4115 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4116 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4117 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4118 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4119 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4120 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4121 | goto done; |
| 4122 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4123 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4124 | goto return_bad_req; |
| 4125 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4128 | /* OK at this stage, we know that the request was accepted according to |
| 4129 | * the http-request rules, we can check for the stats. Note that the |
| 4130 | * URI is detected *before* the req* rules in order not to be affected |
| 4131 | * by a possible reqrep, while they are processed *after* so that a |
| 4132 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4133 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4134 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4135 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4136 | 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] | 4137 | txn->status = 500; |
| 4138 | s->logs.tv_request = now; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4139 | 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] | 4140 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4141 | if (!(s->flags & SF_ERR_MASK)) |
| 4142 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4143 | goto return_prx_cond; |
| 4144 | } |
| 4145 | |
| 4146 | /* parse the whole stats request and extract the relevant information */ |
| 4147 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4148 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4149 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4150 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4151 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4152 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4153 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4154 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4155 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4156 | } |
| 4157 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4158 | /* evaluate the req* rules except reqadd */ |
| 4159 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4160 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4161 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4162 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4163 | if (txn->flags & TX_CLDENY) |
| 4164 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4165 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4166 | if (txn->flags & TX_CLTARPIT) |
| 4167 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4168 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4169 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4170 | /* add request headers from the rule sets in the same order */ |
| 4171 | list_for_each_entry(wl, &px->req_add, list) { |
| 4172 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4173 | 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] | 4174 | ret = acl_pass(ret); |
| 4175 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4176 | ret = !ret; |
| 4177 | if (!ret) |
| 4178 | continue; |
| 4179 | } |
| 4180 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4181 | 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] | 4182 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4183 | } |
| 4184 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4185 | |
| 4186 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4187 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4188 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4189 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4190 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4191 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4192 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4193 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4194 | if (!(s->flags & SF_FINST_MASK)) |
| 4195 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4196 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4197 | /* 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] | 4198 | 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] | 4199 | req->analysers &= ~AN_FLT_XFER_DATA; |
| 4200 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4201 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4202 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4203 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4204 | /* check whether we have some ACLs set to redirect this request */ |
| 4205 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4206 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4207 | int ret; |
| 4208 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4209 | 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] | 4210 | ret = acl_pass(ret); |
| 4211 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4212 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4213 | if (!ret) |
| 4214 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4215 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4216 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4217 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4218 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4219 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4220 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4221 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4222 | * If this happens, then the data will not come immediately, so we must |
| 4223 | * send all what we have without waiting. Note that due to the small gain |
| 4224 | * 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] | 4225 | * 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] | 4226 | * itself once used. |
| 4227 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4228 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4229 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4230 | done: /* done with this analyser, continue with next ones that the calling |
| 4231 | * points will have set, if any. |
| 4232 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4233 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4234 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4235 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4236 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4237 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4238 | tarpit: |
| 4239 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4240 | * which may be the same as the connect timeout if unspecified. |
| 4241 | * If unset, then set it to zero because we really want it to |
| 4242 | * eventually expire. We build the tarpit as an analyser. |
| 4243 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4244 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4245 | |
| 4246 | /* wipe the request out so that we can drop the connection early |
| 4247 | * if the client closes first. |
| 4248 | */ |
| 4249 | channel_dont_connect(req); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4250 | req->analysers &= AN_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4251 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4252 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4253 | if (!req->analyse_exp) |
| 4254 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4255 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4256 | sess->fe->fe_counters.denied_req++; |
| 4257 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4258 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4259 | if (sess->listener->counters) |
| 4260 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4261 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4262 | |
| 4263 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4264 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4265 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4266 | s->logs.tv_request = now; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4267 | http_reply_and_close(s, txn->status, http_error_message(s, txn->rule_deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4268 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4269 | sess->fe->fe_counters.denied_req++; |
| 4270 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4271 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4272 | if (sess->listener->counters) |
| 4273 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4274 | goto return_prx_cond; |
| 4275 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4276 | return_bad_req: |
| 4277 | /* We centralize bad requests processing here */ |
| 4278 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4279 | /* we detected a parsing error. We want to archive this request |
| 4280 | * in the dedicated proxy area for later troubleshooting. |
| 4281 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4282 | 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] | 4283 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4284 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4285 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4286 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4287 | 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] | 4288 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4289 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4290 | if (sess->listener->counters) |
| 4291 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4292 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4293 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4294 | if (!(s->flags & SF_ERR_MASK)) |
| 4295 | s->flags |= SF_ERR_PRXCOND; |
| 4296 | if (!(s->flags & SF_FINST_MASK)) |
| 4297 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4298 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4299 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4300 | req->analyse_exp = TICK_ETERNITY; |
| 4301 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4302 | |
| 4303 | return_prx_yield: |
| 4304 | channel_dont_connect(req); |
| 4305 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4306 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4307 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4308 | /* This function performs all the processing enabled for the current request. |
| 4309 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4310 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4311 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4312 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4313 | 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] | 4314 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4315 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4316 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4317 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4318 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4319 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4320 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4321 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4322 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4323 | return 0; |
| 4324 | } |
| 4325 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4326 | 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] | 4327 | now_ms, __FUNCTION__, |
| 4328 | s, |
| 4329 | req, |
| 4330 | req->rex, req->wex, |
| 4331 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4332 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4333 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4334 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4335 | /* |
| 4336 | * Right now, we know that we have processed the entire headers |
| 4337 | * and that unwanted requests have been filtered out. We can do |
| 4338 | * whatever we want with the remaining request. Also, now we |
| 4339 | * may have separate values for ->fe, ->be. |
| 4340 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4341 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4342 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4343 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4344 | * incoming request. Note that this requires that a connection is |
| 4345 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4346 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4347 | 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] | 4348 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4349 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4350 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4351 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4352 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4353 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4354 | txn->status = 500; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4355 | req->analysers &= AN_FLT_END; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4356 | 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] | 4357 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4358 | if (!(s->flags & SF_ERR_MASK)) |
| 4359 | s->flags |= SF_ERR_RESOURCE; |
| 4360 | if (!(s->flags & SF_FINST_MASK)) |
| 4361 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4362 | |
| 4363 | return 0; |
| 4364 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4365 | |
| 4366 | path = http_get_path(txn); |
| 4367 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4368 | 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] | 4369 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4370 | /* if the path was found, we have to remove everything between |
| 4371 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4372 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4373 | * u_l characters by a single "/". |
| 4374 | */ |
| 4375 | if (path) { |
| 4376 | char *cur_ptr = req->buf->p; |
| 4377 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4378 | int delta; |
| 4379 | |
| 4380 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4381 | http_msg_move_end(&txn->req, delta); |
| 4382 | cur_end += delta; |
| 4383 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4384 | goto return_bad_req; |
| 4385 | } |
| 4386 | else { |
| 4387 | char *cur_ptr = req->buf->p; |
| 4388 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4389 | int delta; |
| 4390 | |
| 4391 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4392 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4393 | http_msg_move_end(&txn->req, delta); |
| 4394 | cur_end += delta; |
| 4395 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4396 | goto return_bad_req; |
| 4397 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4398 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4399 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4400 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4401 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4402 | * Note that doing so might move headers in the request, but |
| 4403 | * the fields will stay coherent and the URI will not move. |
| 4404 | * This should only be performed in the backend. |
| 4405 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 4406 | if ((s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4407 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4408 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4409 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4410 | /* add unique-id if "header-unique-id" is specified */ |
| 4411 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4412 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4413 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4414 | goto return_bad_req; |
| 4415 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4416 | 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] | 4417 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4418 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4419 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4420 | 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] | 4421 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4422 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4423 | 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] | 4424 | goto return_bad_req; |
| 4425 | } |
| 4426 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4427 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4428 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4429 | * asks for it. |
| 4430 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4431 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4432 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4433 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4434 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4435 | 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] | 4436 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4437 | /* The header is set to be added only if none is present |
| 4438 | * and we found it, so don't do anything. |
| 4439 | */ |
| 4440 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4441 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4442 | /* Add an X-Forwarded-For header unless the source IP is |
| 4443 | * in the 'except' network range. |
| 4444 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4445 | if ((!sess->fe->except_mask.s_addr || |
| 4446 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4447 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4448 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4449 | (((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] | 4450 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4451 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4452 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4453 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4454 | |
| 4455 | /* Note: we rely on the backend to get the header name to be used for |
| 4456 | * x-forwarded-for, because the header is really meant for the backends. |
| 4457 | * However, if the backend did not specify any option, we have to rely |
| 4458 | * on the frontend's header name. |
| 4459 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4460 | if (s->be->fwdfor_hdr_len) { |
| 4461 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4462 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4463 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4464 | len = sess->fe->fwdfor_hdr_len; |
| 4465 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4466 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4467 | 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] | 4468 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4469 | 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] | 4470 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4471 | } |
| 4472 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4473 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4474 | /* FIXME: for the sake of completeness, we should also support |
| 4475 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4476 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4477 | int len; |
| 4478 | char pn[INET6_ADDRSTRLEN]; |
| 4479 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4480 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4481 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4482 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4483 | /* Note: we rely on the backend to get the header name to be used for |
| 4484 | * x-forwarded-for, because the header is really meant for the backends. |
| 4485 | * However, if the backend did not specify any option, we have to rely |
| 4486 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4487 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4488 | if (s->be->fwdfor_hdr_len) { |
| 4489 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4490 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4491 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4492 | len = sess->fe->fwdfor_hdr_len; |
| 4493 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4494 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4495 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4496 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4497 | 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] | 4498 | goto return_bad_req; |
| 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4503 | * 10: add X-Original-To if either the frontend or the backend |
| 4504 | * asks for it. |
| 4505 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4506 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4507 | |
| 4508 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4509 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4510 | /* Add an X-Original-To header unless the destination IP is |
| 4511 | * in the 'except' network range. |
| 4512 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4513 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4514 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4515 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4516 | ((!sess->fe->except_mask_to.s_addr || |
| 4517 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4518 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4519 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4520 | (((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] | 4521 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4522 | int len; |
| 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.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4525 | |
| 4526 | /* Note: we rely on the backend to get the header name to be used for |
| 4527 | * x-original-to, 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 | */ |
| 4531 | if (s->be->orgto_hdr_len) { |
| 4532 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4533 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4534 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4535 | len = sess->fe->orgto_hdr_len; |
| 4536 | memcpy(trash.str, sess->fe->orgto_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]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [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)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4541 | goto return_bad_req; |
| 4542 | } |
| 4543 | } |
| 4544 | } |
| 4545 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4546 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4547 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4548 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4549 | * "Upgrade" is present in the Connection header. |
| 4550 | */ |
| 4551 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4552 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4553 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4554 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4555 | unsigned int want_flags = 0; |
| 4556 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4557 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4558 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4559 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4560 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4561 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4562 | want_flags |= TX_CON_CLO_SET; |
| 4563 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4564 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4565 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4566 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4567 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4568 | want_flags |= TX_CON_KAL_SET; |
| 4569 | } |
| 4570 | |
| 4571 | 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] | 4572 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4573 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4574 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4575 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4576 | /* If we have no server assigned yet and we're balancing on url_param |
| 4577 | * with a POST request, we may be interested in checking the body for |
| 4578 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4579 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4580 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4581 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4582 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4583 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4584 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4585 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4586 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4587 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4588 | req->analysers &= ~AN_FLT_XFER_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4589 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4590 | #ifdef TCP_QUICKACK |
| 4591 | /* We expect some data from the client. Unless we know for sure |
| 4592 | * we already have a full request, we have to re-enable quick-ack |
| 4593 | * in case we previously disabled it, otherwise we might cause |
| 4594 | * the client to delay further data. |
| 4595 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4596 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4597 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4598 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4599 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4600 | 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] | 4601 | #endif |
| 4602 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4604 | /************************************************************* |
| 4605 | * OK, that's finished for the headers. We have done what we * |
| 4606 | * could. Let's switch to the DATA state. * |
| 4607 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4608 | req->analyse_exp = TICK_ETERNITY; |
| 4609 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4610 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4611 | /* if the server closes the connection, we want to immediately react |
| 4612 | * and close the socket to save packets and syscalls. |
| 4613 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4614 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4615 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4616 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4617 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4618 | /* OK let's go on with the BODY now */ |
| 4619 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4620 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4621 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4622 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4623 | /* we detected a parsing error. We want to archive this request |
| 4624 | * in the dedicated proxy area for later troubleshooting. |
| 4625 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4626 | 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] | 4627 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4628 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4629 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4630 | txn->status = 400; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4631 | req->analysers &= AN_FLT_END; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4632 | 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] | 4633 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4634 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4635 | if (sess->listener->counters) |
| 4636 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4637 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4638 | if (!(s->flags & SF_ERR_MASK)) |
| 4639 | s->flags |= SF_ERR_PRXCOND; |
| 4640 | if (!(s->flags & SF_FINST_MASK)) |
| 4641 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4642 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4643 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4644 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4645 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4646 | * returns zero, at the beginning because it prevents any other processing |
| 4647 | * from occurring, and at the end because it terminates the request. |
| 4648 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4649 | 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] | 4650 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4651 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4652 | |
| 4653 | /* This connection is being tarpitted. The CLIENT side has |
| 4654 | * already set the connect expiration date to the right |
| 4655 | * timeout. We just have to check that the client is still |
| 4656 | * there and that the timeout has not expired. |
| 4657 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4658 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4659 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4660 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4661 | return 0; |
| 4662 | |
| 4663 | /* We will set the queue timer to the time spent, just for |
| 4664 | * logging purposes. We fake a 500 server error, so that the |
| 4665 | * attacker will not suspect his connection has been tarpitted. |
| 4666 | * It will not cause trouble to the logs because we can exclude |
| 4667 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4668 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4669 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4670 | |
| 4671 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4672 | if (!(req->flags & CF_READ_ERROR)) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4673 | 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] | 4674 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4675 | req->analysers &= AN_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4676 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4677 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4678 | if (!(s->flags & SF_ERR_MASK)) |
| 4679 | s->flags |= SF_ERR_PRXCOND; |
| 4680 | if (!(s->flags & SF_FINST_MASK)) |
| 4681 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4682 | return 0; |
| 4683 | } |
| 4684 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4685 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4686 | * for either the buffer to be full, or the full advertised contents to have |
| 4687 | * reached the buffer. It must only be called after the standard HTTP request |
| 4688 | * processing has occurred, because it expects the request to be parsed and will |
| 4689 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4690 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4691 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4692 | * 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] | 4693 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4694 | 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] | 4695 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4696 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4697 | struct http_txn *txn = s->txn; |
| 4698 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4699 | |
| 4700 | /* We have to parse the HTTP request body to find any required data. |
| 4701 | * "balance url_param check_post" should have been the only way to get |
| 4702 | * into this. We were brought here after HTTP header analysis, so all |
| 4703 | * related structures are ready. |
| 4704 | */ |
| 4705 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4706 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4707 | /* This is the first call */ |
| 4708 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4709 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4710 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4711 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4712 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4713 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4714 | */ |
| 4715 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4716 | struct hdr_ctx ctx; |
| 4717 | ctx.idx = 0; |
| 4718 | /* Expect is allowed in 1.1, look for it */ |
| 4719 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4720 | 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] | 4721 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4722 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4723 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4724 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4725 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4726 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4727 | /* 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] | 4728 | * req->buf->p still points to the beginning of the message. We |
| 4729 | * must save the body in msg->next because it survives buffer |
| 4730 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4731 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4732 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4733 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4734 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4735 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4736 | else |
| 4737 | msg->msg_state = HTTP_MSG_DATA; |
| 4738 | } |
| 4739 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4740 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4741 | /* 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] | 4742 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4743 | goto missing_data; |
| 4744 | |
| 4745 | /* OK we have everything we need now */ |
| 4746 | goto http_end; |
| 4747 | } |
| 4748 | |
| 4749 | /* OK here we're parsing a chunked-encoded message */ |
| 4750 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4751 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4752 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4753 | * 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] | 4754 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4755 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4756 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4757 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4758 | if (!ret) |
| 4759 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4760 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4761 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4762 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4763 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4764 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4765 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4766 | } |
| 4767 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4768 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4769 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4770 | * for at least a whole chunk or the whole content length bytes after |
| 4771 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4772 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4773 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4774 | goto http_end; |
| 4775 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4776 | 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] | 4777 | goto http_end; |
| 4778 | |
| 4779 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4780 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4781 | * we have the maximum we can expect. |
| 4782 | */ |
| 4783 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4784 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4785 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4786 | 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] | 4787 | txn->status = 408; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4788 | 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] | 4789 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4790 | if (!(s->flags & SF_ERR_MASK)) |
| 4791 | s->flags |= SF_ERR_CLITO; |
| 4792 | if (!(s->flags & SF_FINST_MASK)) |
| 4793 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4794 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4795 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4796 | |
| 4797 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4798 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4799 | /* Not enough data. We'll re-use the http-request |
| 4800 | * timeout here. Ideally, we should set the timeout |
| 4801 | * relative to the accept() date. We just set the |
| 4802 | * request timeout once at the beginning of the |
| 4803 | * request. |
| 4804 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4805 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4806 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4807 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4808 | return 0; |
| 4809 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4810 | |
| 4811 | http_end: |
| 4812 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4813 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4814 | req->analysers &= ~an_bit; |
| 4815 | req->analyse_exp = TICK_ETERNITY; |
| 4816 | return 1; |
| 4817 | |
| 4818 | return_bad_req: /* let's centralize all bad requests */ |
| 4819 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4820 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4821 | 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] | 4822 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4823 | if (!(s->flags & SF_ERR_MASK)) |
| 4824 | s->flags |= SF_ERR_PRXCOND; |
| 4825 | if (!(s->flags & SF_FINST_MASK)) |
| 4826 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4827 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4828 | return_err_msg: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4829 | req->analysers &= AN_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4830 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4831 | if (sess->listener->counters) |
| 4832 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4833 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4834 | } |
| 4835 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4836 | /* send a server's name with an outgoing request over an established connection. |
| 4837 | * Note: this function is designed to be called once the request has been scheduled |
| 4838 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4839 | * proceeding. |
| 4840 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4841 | 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] | 4842 | |
| 4843 | struct hdr_ctx ctx; |
| 4844 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4845 | char *hdr_name = be->server_id_hdr_name; |
| 4846 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4847 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4848 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4849 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4850 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4851 | ctx.idx = 0; |
| 4852 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4853 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4854 | if (old_o) { |
| 4855 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4856 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4857 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4858 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4859 | } |
| 4860 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4861 | old_i = chn->buf->i; |
| 4862 | 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] | 4863 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4864 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4868 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4869 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4870 | hdr_val += hdr_name_len; |
| 4871 | *hdr_val++ = ':'; |
| 4872 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4873 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4874 | 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] | 4875 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4876 | if (old_o) { |
| 4877 | /* If this was a forwarded request, we must readjust the amount of |
| 4878 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4879 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4880 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4881 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4882 | old_o += chn->buf->i - old_i; |
| 4883 | b_adv(chn->buf, old_o); |
| 4884 | txn->req.next -= old_o; |
| 4885 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4886 | } |
| 4887 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4888 | return 0; |
| 4889 | } |
| 4890 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4891 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4892 | * right now but it works. |
| 4893 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4894 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4895 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4896 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4897 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4898 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4899 | struct connection *srv_conn; |
| 4900 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4901 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4902 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4903 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4904 | * server's connections. We need a safer way to reinitialize buffer |
| 4905 | * flags. We also need a more accurate method for computing per-request |
| 4906 | * data. |
| 4907 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4908 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4909 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4910 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4911 | * to the server. |
| 4912 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4913 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4914 | !si_conn_ready(&s->si[1])) { |
| 4915 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4916 | si_shutr(&s->si[1]); |
| 4917 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4918 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4919 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4920 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4921 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4922 | if (unlikely(s->srv_conn)) |
| 4923 | sess_change_server(s, NULL); |
| 4924 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4925 | |
| 4926 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4927 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4928 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4929 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4930 | int n; |
| 4931 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4932 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4933 | if (n < 1 || n > 5) |
| 4934 | n = 0; |
| 4935 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4936 | if (fe->mode == PR_MODE_HTTP) { |
| 4937 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4938 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4939 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4940 | (be->mode == PR_MODE_HTTP)) { |
| 4941 | be->be_counters.p.http.rsp[n]++; |
| 4942 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4943 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4947 | s->logs.bytes_in -= s->req.buf->i; |
| 4948 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4949 | |
| 4950 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4951 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4952 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4953 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4954 | s->do_log(s); |
| 4955 | } |
| 4956 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4957 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4958 | stream_stop_content_counters(s); |
| 4959 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4960 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4961 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4962 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4963 | tv_zero(&s->logs.tv_request); |
| 4964 | s->logs.t_queue = -1; |
| 4965 | s->logs.t_connect = -1; |
| 4966 | s->logs.t_data = -1; |
| 4967 | s->logs.t_close = 0; |
| 4968 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4969 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4970 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4971 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4972 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4973 | |
| 4974 | if (s->pend_pos) |
| 4975 | pendconn_free(s->pend_pos); |
| 4976 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4977 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4978 | if (s->flags & SF_CURR_SESS) { |
| 4979 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4980 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4981 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4982 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4983 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4986 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4987 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4988 | /* only release our endpoint if we don't intend to reuse the |
| 4989 | * connection. |
| 4990 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4991 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4992 | !si_conn_ready(&s->si[1])) { |
| 4993 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4994 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4995 | } |
| 4996 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4997 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4998 | s->si[1].err_type = SI_ET_NONE; |
| 4999 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5000 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5001 | 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] | 5002 | 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); |
| 5003 | 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] | 5004 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5005 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5006 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5007 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5008 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5009 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5010 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5011 | |
| 5012 | if (prev_status == 401 || prev_status == 407) { |
| 5013 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5014 | * a chance of being able to send the visitor again to the same |
| 5015 | * server over the same connection. This is required by some |
| 5016 | * broken protocols such as NTLM, and anyway whenever there is |
| 5017 | * an opportunity for sending the challenge to the proper place, |
| 5018 | * it's better to do it (at least it helps with debugging). |
| 5019 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5020 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5021 | if (srv_conn) |
| 5022 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5023 | } |
| 5024 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5025 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 5026 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 5027 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5028 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5029 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5030 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5031 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5032 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5033 | s->req.flags |= CF_NEVER_WAIT; |
| 5034 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5035 | } |
| 5036 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5037 | /* if the request buffer is not empty, it means we're |
| 5038 | * about to process another request, so send pending |
| 5039 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5040 | * Just don't do this if the buffer is close to be full, |
| 5041 | * because the request will wait for it to flush a little |
| 5042 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5043 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5044 | if (s->req.buf->i) { |
| 5045 | if (s->res.buf->o && |
| 5046 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5047 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5048 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5049 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5050 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 5051 | /* we're removing the analysers, we MUST re-enable events detection. |
| 5052 | * We don't enable close on the response channel since it's either |
| 5053 | * already closed, or in keep-alive with an idle connection handler. |
| 5054 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5055 | channel_auto_read(&s->req); |
| 5056 | channel_auto_close(&s->req); |
| 5057 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5058 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5059 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5060 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5061 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5062 | if (!srv) |
| 5063 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5064 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5065 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5066 | else if (prev_flags & TX_NOT_FIRST) |
| 5067 | /* note: we check the request, not the connection, but |
| 5068 | * this is valid for strategies SAFE and AGGR, and in |
| 5069 | * case of ALWS, we don't care anyway. |
| 5070 | */ |
| 5071 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5072 | else |
| 5073 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5074 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5075 | |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5076 | if (HAS_FILTERS(s)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5077 | s->req.analysers &= AN_FLT_END; |
| 5078 | s->res.analysers &= AN_FLT_END; |
| 5079 | } |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5080 | else { |
| 5081 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 5082 | s->res.analysers = 0; |
| 5083 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | |
| 5087 | /* This function updates the request state machine according to the response |
| 5088 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5089 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5090 | * it is only used to find when a request/response couple is complete. Both |
| 5091 | * this function and its equivalent should loop until both return zero. It |
| 5092 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5093 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5094 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5095 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5096 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5097 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5098 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5099 | unsigned int old_state = txn->req.msg_state; |
| 5100 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5101 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5102 | return 0; |
| 5103 | |
| 5104 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5105 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5106 | * We can shut the read side unless we want to abort_on_close, |
| 5107 | * or we have a POST request. The issue with POST requests is |
| 5108 | * that some browsers still send a CRLF after the request, and |
| 5109 | * this CRLF must be read so that it does not remain in the kernel |
| 5110 | * buffers, otherwise a close could cause an RST on some systems |
| 5111 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5112 | * Note that if we're using keep-alive on the client side, we'd |
| 5113 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5114 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5115 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5116 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5117 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5118 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5119 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5120 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5121 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5122 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5123 | /* if the server closes the connection, we want to immediately react |
| 5124 | * and close the socket to save packets and syscalls. |
| 5125 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5126 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5127 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5128 | /* In any case we've finished parsing the request so we must |
| 5129 | * disable Nagle when sending data because 1) we're not going |
| 5130 | * to shut this side, and 2) the server is waiting for us to |
| 5131 | * send pending data. |
| 5132 | */ |
| 5133 | chn->flags |= CF_NEVER_WAIT; |
| 5134 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5135 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5136 | goto wait_other_side; |
| 5137 | |
| 5138 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5139 | /* The server has not finished to respond, so we |
| 5140 | * don't want to move in order not to upset it. |
| 5141 | */ |
| 5142 | goto wait_other_side; |
| 5143 | } |
| 5144 | |
| 5145 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5146 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5147 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5148 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5149 | goto wait_other_side; |
| 5150 | } |
| 5151 | |
| 5152 | /* When we get here, it means that both the request and the |
| 5153 | * response have finished receiving. Depending on the connection |
| 5154 | * mode, we'll have to wait for the last bytes to leave in either |
| 5155 | * direction, and sometimes for a close to be effective. |
| 5156 | */ |
| 5157 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5158 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5159 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5160 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5161 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5162 | } |
| 5163 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5164 | /* Option forceclose is set, or either side wants to close, |
| 5165 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5166 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5167 | * once both states are CLOSED. |
| 5168 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5169 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5170 | channel_shutr_now(chn); |
| 5171 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5172 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5173 | } |
| 5174 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5175 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5176 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5177 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5178 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5179 | channel_auto_read(chn); |
| 5180 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5181 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5182 | } |
| 5183 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5184 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5185 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5186 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5187 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5188 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5189 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5190 | goto http_msg_closing; |
| 5191 | } |
| 5192 | else { |
| 5193 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5194 | goto http_msg_closed; |
| 5195 | } |
| 5196 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5197 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5201 | http_msg_closing: |
| 5202 | /* nothing else to forward, just waiting for the output buffer |
| 5203 | * to be empty and for the shutw_now to take effect. |
| 5204 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5205 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5206 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5207 | goto http_msg_closed; |
| 5208 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5209 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5210 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5211 | goto wait_other_side; |
| 5212 | } |
| 5213 | } |
| 5214 | |
| 5215 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5216 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5217 | /* see above in MSG_DONE why we only do this in these states */ |
| 5218 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5219 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5220 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5221 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5222 | goto wait_other_side; |
| 5223 | } |
| 5224 | |
| 5225 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5226 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | |
| 5230 | /* This function updates the response state machine according to the request |
| 5231 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5232 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5233 | * it is only used to find when a request/response couple is complete. Both |
| 5234 | * this function and its equivalent should loop until both return zero. It |
| 5235 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5236 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5237 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5238 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5239 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5240 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5241 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5242 | unsigned int old_state = txn->rsp.msg_state; |
| 5243 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5244 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5245 | return 0; |
| 5246 | |
| 5247 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5248 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5249 | * still monitor the server connection for a possible close |
| 5250 | * while the request is being uploaded, so we don't disable |
| 5251 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5252 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5253 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5254 | |
| 5255 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5256 | goto wait_other_side; |
| 5257 | |
| 5258 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5259 | /* The client seems to still be sending data, probably |
| 5260 | * because we got an error response during an upload. |
| 5261 | * We have the choice of either breaking the connection |
| 5262 | * or letting it pass through. Let's do the later. |
| 5263 | */ |
| 5264 | goto wait_other_side; |
| 5265 | } |
| 5266 | |
| 5267 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5268 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5269 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5270 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5271 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5272 | goto wait_other_side; |
| 5273 | } |
| 5274 | |
| 5275 | /* When we get here, it means that both the request and the |
| 5276 | * response have finished receiving. Depending on the connection |
| 5277 | * mode, we'll have to wait for the last bytes to leave in either |
| 5278 | * direction, and sometimes for a close to be effective. |
| 5279 | */ |
| 5280 | |
| 5281 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5282 | /* Server-close mode : shut read and wait for the request |
| 5283 | * side to close its output buffer. The caller will detect |
| 5284 | * when we're in DONE and the other is in CLOSED and will |
| 5285 | * catch that for the final cleanup. |
| 5286 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5287 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5288 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5289 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5290 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5291 | /* Option forceclose is set, or either side wants to close, |
| 5292 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5293 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5294 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5295 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5296 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5297 | channel_shutr_now(chn); |
| 5298 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5299 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5300 | } |
| 5301 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5302 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5303 | * need to forward remaining data. Keep-alive will need to monitor |
| 5304 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5305 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5306 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5307 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5308 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5309 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5310 | } |
| 5311 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5312 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5313 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5314 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5315 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5316 | goto http_msg_closing; |
| 5317 | } |
| 5318 | else { |
| 5319 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5320 | goto http_msg_closed; |
| 5321 | } |
| 5322 | } |
| 5323 | goto wait_other_side; |
| 5324 | } |
| 5325 | |
| 5326 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5327 | http_msg_closing: |
| 5328 | /* nothing else to forward, just waiting for the output buffer |
| 5329 | * to be empty and for the shutw_now to take effect. |
| 5330 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5331 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5332 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5333 | goto http_msg_closed; |
| 5334 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5335 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5336 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5337 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5338 | if (objt_server(s->target)) |
| 5339 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5340 | goto wait_other_side; |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5345 | http_msg_closed: |
| 5346 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5347 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5348 | channel_auto_close(chn); |
| 5349 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5350 | goto wait_other_side; |
| 5351 | } |
| 5352 | |
| 5353 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5354 | /* We force the response to leave immediately if we're waiting for the |
| 5355 | * other side, since there is no pending shutdown to push it out. |
| 5356 | */ |
| 5357 | if (!channel_is_empty(chn)) |
| 5358 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5359 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5360 | } |
| 5361 | |
| 5362 | |
| 5363 | /* Resync the request and response state machines. Return 1 if either state |
| 5364 | * changes. |
| 5365 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5366 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5367 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5368 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5369 | int old_req_state = txn->req.msg_state; |
| 5370 | int old_res_state = txn->rsp.msg_state; |
| 5371 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5372 | http_sync_req_state(s); |
| 5373 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5374 | if (!http_sync_res_state(s)) |
| 5375 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5376 | if (!http_sync_req_state(s)) |
| 5377 | break; |
| 5378 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5379 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5380 | /* OK, both state machines agree on a compatible state. |
| 5381 | * There are a few cases we're interested in : |
| 5382 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5383 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5384 | * directions, so let's simply disable both analysers. |
| 5385 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5386 | * request. |
| 5387 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5388 | * with server-close mode means we've completed one request and we |
| 5389 | * must re-initialize the server connection. |
| 5390 | */ |
| 5391 | |
| 5392 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5393 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5394 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5395 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5396 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5397 | channel_auto_close(&s->req); |
| 5398 | channel_auto_read(&s->req); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5399 | s->res.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5400 | channel_auto_close(&s->res); |
| 5401 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5402 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5403 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5404 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5405 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5406 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5407 | s->res.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5408 | channel_auto_close(&s->res); |
| 5409 | channel_auto_read(&s->res); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5410 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5411 | channel_abort(&s->req); |
| 5412 | channel_auto_close(&s->req); |
| 5413 | channel_auto_read(&s->req); |
| 5414 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5415 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5416 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5417 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5418 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5419 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5420 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5421 | /* server-close/keep-alive: terminate this transaction, |
| 5422 | * possibly killing the server connection and reinitialize |
| 5423 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5424 | */ |
| 5425 | http_end_txn_clean_session(s); |
| 5426 | } |
| 5427 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5428 | return txn->req.msg_state != old_req_state || |
| 5429 | txn->rsp.msg_state != old_res_state; |
| 5430 | } |
| 5431 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5432 | /* This function is an analyser which forwards request body (including chunk |
| 5433 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5434 | * zero byte. The only situation where it must not be called is when we're in |
| 5435 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5436 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5437 | * 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] | 5438 | * 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] | 5439 | * 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] | 5440 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5441 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5442 | 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] | 5443 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5444 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5445 | struct http_txn *txn = s->txn; |
| 5446 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5447 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5448 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5449 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5450 | return 0; |
| 5451 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5452 | 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] | 5453 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5454 | /* Output closed while we were sending data. We must abort and |
| 5455 | * wake the other side up. |
| 5456 | */ |
| 5457 | msg->msg_state = HTTP_MSG_ERROR; |
| 5458 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5459 | return 1; |
| 5460 | } |
| 5461 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5462 | /* Note that we don't have to send 100-continue back because we don't |
| 5463 | * need the data to complete our job, and it's up to the server to |
| 5464 | * decide whether to return 100, 417 or anything else in return of |
| 5465 | * an "Expect: 100-continue" header. |
| 5466 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5467 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5468 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5469 | ? HTTP_MSG_CHUNK_SIZE |
| 5470 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5471 | |
| 5472 | /* TODO/filters: when http-buffer-request option is set or if a |
| 5473 | * rule on url_param exists, the first chunk size could be |
| 5474 | * already parsed. In that case, msg->next is after the chunk |
| 5475 | * size (including the CRLF after the size). So this case should |
| 5476 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5477 | } |
| 5478 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5479 | /* Some post-connect processing might want us to refrain from starting to |
| 5480 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5481 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5482 | */ |
| 5483 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5484 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5485 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5486 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5487 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5488 | } |
| 5489 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5490 | } |
| 5491 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5492 | /* in most states, we should abort in case of early close */ |
| 5493 | channel_auto_close(req); |
| 5494 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5495 | if (req->to_forward) { |
| 5496 | /* We can't process the buffer's contents yet */ |
| 5497 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5498 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5499 | } |
| 5500 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5501 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5502 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5503 | ? http_msg_forward_chunked_body(s, msg) |
| 5504 | : http_msg_forward_body(s, msg)); |
| 5505 | if (!ret) |
| 5506 | goto missing_data_or_waiting; |
| 5507 | if (ret < 0) |
| 5508 | goto return_bad_req; |
| 5509 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5510 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5511 | /* other states, DONE...TUNNEL */ |
| 5512 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 5513 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5514 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5515 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5516 | ret = msg->msg_state; |
| 5517 | if (http_resync_states(s)) { |
| 5518 | /* some state changes occurred, maybe the analyser |
| 5519 | * was disabled too. */ |
| 5520 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5521 | if (req->flags & CF_SHUTW) { |
| 5522 | /* request errors are most likely due to the |
| 5523 | * server aborting the transfer. */ |
| 5524 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5525 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5526 | if (msg->err_pos >= 0) |
| 5527 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be); |
| 5528 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5529 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5530 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5531 | } |
| 5532 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5533 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 5534 | * the client's connection and forward any shutdown notification to the |
| 5535 | * server, which will decide whether to close or to go on processing the |
| 5536 | * request. We only do that in tunnel mode, and not in other modes since |
| 5537 | * it can be abused to exhaust source ports. */ |
| 5538 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 5539 | channel_auto_read(req); |
| 5540 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5541 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5542 | s->si[1].flags |= SI_FL_NOLINGER; |
| 5543 | channel_auto_close(req); |
| 5544 | } |
| 5545 | else if (s->txn->meth == HTTP_METH_POST) { |
| 5546 | /* POST requests may require to read extra CRLF sent by broken |
| 5547 | * browsers and which could cause an RST to be sent upon close |
| 5548 | * on some systems (eg: Linux). */ |
| 5549 | channel_auto_read(req); |
| 5550 | } |
| 5551 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5552 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5553 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5554 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5555 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5556 | if (!(s->flags & SF_ERR_MASK)) |
| 5557 | s->flags |= SF_ERR_CLICL; |
| 5558 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5559 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5560 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5561 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5562 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5563 | } |
| 5564 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5565 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5566 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5567 | if (objt_server(s->target)) |
| 5568 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5569 | |
| 5570 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5571 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5572 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5573 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5574 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5575 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5576 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5577 | /* 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] | 5578 | * 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] | 5579 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5580 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5581 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5582 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5583 | /* 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] | 5584 | * 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] | 5585 | * 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] | 5586 | * modes are already handled by the stream sock layer. We must not do |
| 5587 | * this in content-length mode because it could present the MSG_MORE |
| 5588 | * flag with the last block of forwarded data, which would cause an |
| 5589 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5590 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5591 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5592 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5593 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5594 | return 0; |
| 5595 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5596 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5597 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5598 | if (sess->listener->counters) |
| 5599 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5600 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5601 | return_bad_req_stats_ok: |
| 5602 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5603 | if (txn->status) { |
| 5604 | /* 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] | 5605 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5606 | } else { |
| 5607 | txn->status = 400; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5608 | 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] | 5609 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5610 | req->analysers &= AN_FLT_END; |
| 5611 | 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] | 5612 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5613 | if (!(s->flags & SF_ERR_MASK)) |
| 5614 | s->flags |= SF_ERR_PRXCOND; |
| 5615 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5616 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5617 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5618 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5619 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5620 | } |
| 5621 | return 0; |
| 5622 | |
| 5623 | aborted_xfer: |
| 5624 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5625 | if (txn->status) { |
| 5626 | /* 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] | 5627 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5628 | } else { |
| 5629 | txn->status = 502; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5630 | 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] | 5631 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5632 | req->analysers &= AN_FLT_END; |
| 5633 | 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] | 5634 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5635 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5636 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5637 | if (objt_server(s->target)) |
| 5638 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5639 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5640 | if (!(s->flags & SF_ERR_MASK)) |
| 5641 | s->flags |= SF_ERR_SRVCL; |
| 5642 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5643 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5644 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5645 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5646 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5647 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5648 | return 0; |
| 5649 | } |
| 5650 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5651 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5652 | * processing can continue on next analysers, or zero if it either needs more |
| 5653 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5654 | * 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] | 5655 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5656 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5657 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5658 | 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] | 5659 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5660 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5661 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5662 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5663 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5664 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5665 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5666 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5667 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5668 | 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] | 5669 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5670 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5671 | rep, |
| 5672 | rep->rex, rep->wex, |
| 5673 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5674 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5675 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5676 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5677 | /* |
| 5678 | * Now parse the partial (or complete) lines. |
| 5679 | * We will check the response syntax, and also join multi-line |
| 5680 | * headers. An index of all the lines will be elaborated while |
| 5681 | * parsing. |
| 5682 | * |
| 5683 | * For the parsing, we use a 28 states FSM. |
| 5684 | * |
| 5685 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5686 | * rep->buf->p = beginning of response |
| 5687 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5688 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5689 | * msg->eol = end of current header or line (LF or CRLF) |
| 5690 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5691 | */ |
| 5692 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5693 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5694 | /* There's a protected area at the end of the buffer for rewriting |
| 5695 | * purposes. We don't want to start to parse the request if the |
| 5696 | * protected area is affected, because we may have to move processed |
| 5697 | * data later, which is much more complicated. |
| 5698 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5699 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5700 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5701 | /* some data has still not left the buffer, wake us once that's done */ |
| 5702 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5703 | goto abort_response; |
| 5704 | channel_dont_close(rep); |
| 5705 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5706 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5707 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5708 | } |
| 5709 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5710 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5711 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5712 | buffer_slow_realign(rep->buf); |
| 5713 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5714 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5715 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5716 | } |
| 5717 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5718 | /* 1: we might have to print this header in debug mode */ |
| 5719 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5720 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5721 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5722 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5723 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5724 | sol = rep->buf->p; |
| 5725 | 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] | 5726 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5727 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5728 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5729 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5730 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5731 | while (cur_idx) { |
| 5732 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5733 | debug_hdr("srvhdr", s, sol, eol); |
| 5734 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5735 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5736 | } |
| 5737 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5738 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5739 | /* |
| 5740 | * Now we quickly check if we have found a full valid response. |
| 5741 | * If not so, we check the FD and buffer states before leaving. |
| 5742 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5743 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5744 | * responses are checked first. |
| 5745 | * |
| 5746 | * Depending on whether the client is still there or not, we |
| 5747 | * may send an error response back or not. Note that normally |
| 5748 | * we should only check for HTTP status there, and check I/O |
| 5749 | * errors somewhere else. |
| 5750 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5751 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5752 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5753 | /* Invalid response */ |
| 5754 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5755 | /* we detected a parsing error. We want to archive this response |
| 5756 | * in the dedicated proxy area for later troubleshooting. |
| 5757 | */ |
| 5758 | hdr_response_bad: |
| 5759 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5760 | 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] | 5761 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5762 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5763 | if (objt_server(s->target)) { |
| 5764 | objt_server(s->target)->counters.failed_resp++; |
| 5765 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5766 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5767 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5768 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5769 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5770 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5771 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5772 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5773 | 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] | 5774 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5775 | if (!(s->flags & SF_ERR_MASK)) |
| 5776 | s->flags |= SF_ERR_PRXCOND; |
| 5777 | if (!(s->flags & SF_FINST_MASK)) |
| 5778 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5779 | |
| 5780 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5781 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5782 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5783 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5784 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5785 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5786 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5787 | goto hdr_response_bad; |
| 5788 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5789 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5790 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5791 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5792 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5793 | 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] | 5794 | else if (txn->flags & TX_NOT_FIRST) |
| 5795 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5796 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5797 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5798 | if (objt_server(s->target)) { |
| 5799 | objt_server(s->target)->counters.failed_resp++; |
| 5800 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5801 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5802 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5803 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5804 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5805 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5806 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5807 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5808 | 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] | 5809 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5810 | if (!(s->flags & SF_ERR_MASK)) |
| 5811 | s->flags |= SF_ERR_SRVCL; |
| 5812 | if (!(s->flags & SF_FINST_MASK)) |
| 5813 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5814 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5815 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5816 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5817 | /* read timeout : return a 504 to the client. */ |
| 5818 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5819 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5820 | 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] | 5821 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5822 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5823 | if (objt_server(s->target)) { |
| 5824 | objt_server(s->target)->counters.failed_resp++; |
| 5825 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5826 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5827 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5828 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5829 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5830 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5831 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5832 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5833 | 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] | 5834 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5835 | if (!(s->flags & SF_ERR_MASK)) |
| 5836 | s->flags |= SF_ERR_SRVTO; |
| 5837 | if (!(s->flags & SF_FINST_MASK)) |
| 5838 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5839 | return 0; |
| 5840 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5841 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5842 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5843 | 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] | 5844 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5845 | s->be->be_counters.cli_aborts++; |
| 5846 | if (objt_server(s->target)) |
| 5847 | objt_server(s->target)->counters.cli_aborts++; |
| 5848 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5849 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5850 | channel_auto_close(rep); |
| 5851 | |
| 5852 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5853 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5854 | 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] | 5855 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5856 | if (!(s->flags & SF_ERR_MASK)) |
| 5857 | s->flags |= SF_ERR_CLICL; |
| 5858 | if (!(s->flags & SF_FINST_MASK)) |
| 5859 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5860 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5861 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5862 | return 0; |
| 5863 | } |
| 5864 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5865 | /* 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] | 5866 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5867 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5868 | 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] | 5869 | else if (txn->flags & TX_NOT_FIRST) |
| 5870 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5871 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5872 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5873 | if (objt_server(s->target)) { |
| 5874 | objt_server(s->target)->counters.failed_resp++; |
| 5875 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5876 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5877 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5878 | channel_auto_close(rep); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5879 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5880 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5881 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5882 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5883 | 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] | 5884 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5885 | if (!(s->flags & SF_ERR_MASK)) |
| 5886 | s->flags |= SF_ERR_SRVCL; |
| 5887 | if (!(s->flags & SF_FINST_MASK)) |
| 5888 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5889 | return 0; |
| 5890 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5891 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5892 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5893 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5894 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5895 | 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] | 5896 | else if (txn->flags & TX_NOT_FIRST) |
| 5897 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5898 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5899 | s->be->be_counters.failed_resp++; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5900 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5901 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5902 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5903 | if (!(s->flags & SF_ERR_MASK)) |
| 5904 | s->flags |= SF_ERR_CLICL; |
| 5905 | if (!(s->flags & SF_FINST_MASK)) |
| 5906 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5907 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5908 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5909 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5910 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5911 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5912 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5913 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5914 | return 0; |
| 5915 | } |
| 5916 | |
| 5917 | /* More interesting part now : we know that we have a complete |
| 5918 | * response which at least looks like HTTP. We have an indicator |
| 5919 | * of each header's length, so we can parse them quickly. |
| 5920 | */ |
| 5921 | |
| 5922 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5923 | 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] | 5924 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5925 | /* |
| 5926 | * 1: get the status code |
| 5927 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5928 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5929 | if (n < 1 || n > 5) |
| 5930 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5931 | /* when the client triggers a 4xx from the server, it's most often due |
| 5932 | * to a missing object or permission. These events should be tracked |
| 5933 | * because if they happen often, it may indicate a brute force or a |
| 5934 | * vulnerability scan. |
| 5935 | */ |
| 5936 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5937 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5938 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5939 | if (objt_server(s->target)) |
| 5940 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5941 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5942 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5943 | * exactly one digit "." one digit. This check may be disabled using |
| 5944 | * option accept-invalid-http-response. |
| 5945 | */ |
| 5946 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5947 | if (msg->sl.st.v_l != 8) { |
| 5948 | msg->err_pos = 0; |
| 5949 | goto hdr_response_bad; |
| 5950 | } |
| 5951 | |
| 5952 | if (rep->buf->p[4] != '/' || |
| 5953 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5954 | rep->buf->p[6] != '.' || |
| 5955 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5956 | msg->err_pos = 4; |
| 5957 | goto hdr_response_bad; |
| 5958 | } |
| 5959 | } |
| 5960 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5961 | /* check if the response is HTTP/1.1 or above */ |
| 5962 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5963 | ((rep->buf->p[5] > '1') || |
| 5964 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5965 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5966 | |
| 5967 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5968 | 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] | 5969 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5970 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5971 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5972 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5973 | 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] | 5974 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5975 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5976 | * and 505 are triggered on demand by client request, so we must not |
| 5977 | * count them as server failures. |
| 5978 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5979 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5980 | 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] | 5981 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5982 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5983 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5984 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5985 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5986 | /* |
| 5987 | * 2: check for cacheability. |
| 5988 | */ |
| 5989 | |
| 5990 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5991 | case 100: |
| 5992 | /* |
| 5993 | * We may be facing a 100-continue response, in which case this |
| 5994 | * is not the right response, and we're waiting for the next one. |
| 5995 | * Let's allow this response to go to the client and wait for the |
| 5996 | * next one. |
| 5997 | */ |
| 5998 | hdr_idx_init(&txn->hdr_idx); |
| 5999 | msg->next -= channel_forward(rep, msg->next); |
| 6000 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6001 | txn->status = 0; |
| 6002 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6003 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6004 | goto next_one; |
| 6005 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6006 | case 200: |
| 6007 | case 203: |
| 6008 | case 206: |
| 6009 | case 300: |
| 6010 | case 301: |
| 6011 | case 410: |
| 6012 | /* RFC2616 @13.4: |
| 6013 | * "A response received with a status code of |
| 6014 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6015 | * by a cache (...) unless a cache-control |
| 6016 | * directive prohibits caching." |
| 6017 | * |
| 6018 | * RFC2616 @9.5: POST method : |
| 6019 | * "Responses to this method are not cacheable, |
| 6020 | * unless the response includes appropriate |
| 6021 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6022 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6023 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6024 | ((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] | 6025 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6026 | break; |
| 6027 | default: |
| 6028 | break; |
| 6029 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6030 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6031 | /* |
| 6032 | * 3: we may need to capture headers |
| 6033 | */ |
| 6034 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6035 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6036 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6037 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6038 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6039 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6040 | * by RFC7230#3.3.3 : |
| 6041 | * |
| 6042 | * The length of a message body is determined by one of the following |
| 6043 | * (in order of precedence): |
| 6044 | * |
| 6045 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6046 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6047 | * code is always terminated by the first empty line after the |
| 6048 | * header fields, regardless of the header fields present in the |
| 6049 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6050 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6051 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6052 | * the connection will become a tunnel immediately after the empty |
| 6053 | * line that concludes the header fields. A client MUST ignore any |
| 6054 | * Content-Length or Transfer-Encoding header fields received in |
| 6055 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6056 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6057 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6058 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6059 | * body length is determined by reading and decoding the chunked |
| 6060 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6061 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6062 | * If a Transfer-Encoding header field is present in a response and |
| 6063 | * the chunked transfer coding is not the final encoding, the |
| 6064 | * message body length is determined by reading the connection until |
| 6065 | * it is closed by the server. If a Transfer-Encoding header field |
| 6066 | * is present in a request and the chunked transfer coding is not |
| 6067 | * the final encoding, the message body length cannot be determined |
| 6068 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6069 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6070 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6071 | * If a message is received with both a Transfer-Encoding and a |
| 6072 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6073 | * Content-Length. Such a message might indicate an attempt to |
| 6074 | * perform request smuggling (Section 9.5) or response splitting |
| 6075 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6076 | * remove the received Content-Length field prior to forwarding such |
| 6077 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6078 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6079 | * 4. If a message is received without Transfer-Encoding and with |
| 6080 | * either multiple Content-Length header fields having differing |
| 6081 | * field-values or a single Content-Length header field having an |
| 6082 | * invalid value, then the message framing is invalid and the |
| 6083 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6084 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6085 | * status code and then close the connection. If this is a response |
| 6086 | * message received by a proxy, the proxy MUST close the connection |
| 6087 | * to the server, discard the received response, and send a 502 (Bad |
| 6088 | * Gateway) response to the client. If this is a response message |
| 6089 | * received by a user agent, the user agent MUST close the |
| 6090 | * connection to the server and discard the received response. |
| 6091 | * |
| 6092 | * 5. If a valid Content-Length header field is present without |
| 6093 | * Transfer-Encoding, its decimal value defines the expected message |
| 6094 | * body length in octets. If the sender closes the connection or |
| 6095 | * the recipient times out before the indicated number of octets are |
| 6096 | * received, the recipient MUST consider the message to be |
| 6097 | * incomplete and close the connection. |
| 6098 | * |
| 6099 | * 6. If this is a request message and none of the above are true, then |
| 6100 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6101 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6102 | * 7. Otherwise, this is a response message without a declared message |
| 6103 | * body length, so the message body length is determined by the |
| 6104 | * number of octets received prior to the server closing the |
| 6105 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6106 | */ |
| 6107 | |
| 6108 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6109 | * 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] | 6110 | * 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] | 6111 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6112 | */ |
| 6113 | if (txn->meth == HTTP_METH_HEAD || |
| 6114 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6115 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6116 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6117 | goto skip_content_length; |
| 6118 | } |
| 6119 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6120 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6121 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6122 | 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] | 6123 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6124 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6125 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6126 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6127 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6128 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6129 | break; |
| 6130 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6131 | } |
| 6132 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6133 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6134 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6135 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6136 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6137 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6138 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6139 | 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] | 6140 | signed long long cl; |
| 6141 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6142 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6143 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6144 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6145 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6146 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6147 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6148 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6149 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6150 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6151 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6152 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6153 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6154 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6155 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6156 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6157 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6158 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6159 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6160 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6161 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6162 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6163 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6164 | } |
| 6165 | |
| 6166 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6167 | /* Now we have to check if we need to modify the Connection header. |
| 6168 | * This is more difficult on the response than it is on the request, |
| 6169 | * because we can have two different HTTP versions and we don't know |
| 6170 | * how the client will interprete a response. For instance, let's say |
| 6171 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6172 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6173 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6174 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6175 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6176 | * indicating how a request MAY be understood by the client. In case |
| 6177 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6178 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6179 | * will fall back to explicit close. Note that we won't take risks with |
| 6180 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6181 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6182 | */ |
| 6183 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6184 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6185 | txn->status == 101)) { |
| 6186 | /* Either we've established an explicit tunnel, or we're |
| 6187 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6188 | * to understand the next protocols. We have to switch to tunnel |
| 6189 | * mode, so that we transfer the request and responses then let |
| 6190 | * this protocol pass unmodified. When we later implement specific |
| 6191 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6192 | * header which contains information about that protocol for |
| 6193 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6194 | */ |
| 6195 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6196 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6197 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6198 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6199 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6200 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6201 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6202 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6203 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6204 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6205 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6206 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6207 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6208 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6209 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6210 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6211 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6212 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6213 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6214 | /* now adjust header transformations depending on current state */ |
| 6215 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6216 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6217 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6218 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6219 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6220 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6221 | else { /* SCL / KAL */ |
| 6222 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6223 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6224 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6225 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6227 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6228 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6229 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6230 | /* Some keep-alive responses are converted to Server-close if |
| 6231 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6232 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6233 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6234 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6235 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6236 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6237 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6238 | } |
| 6239 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6240 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6241 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6242 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6243 | /* end of job, return OK */ |
| 6244 | rep->analysers &= ~an_bit; |
| 6245 | rep->analyse_exp = TICK_ETERNITY; |
| 6246 | channel_auto_close(rep); |
| 6247 | return 1; |
| 6248 | |
| 6249 | abort_keep_alive: |
| 6250 | /* A keep-alive request to the server failed on a network error. |
| 6251 | * The client is required to retry. We need to close without returning |
| 6252 | * any other information so that the client retries. |
| 6253 | */ |
| 6254 | txn->status = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6255 | rep->analysers &= AN_FLT_END; |
| 6256 | s->req.analysers &= AN_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6257 | channel_auto_close(rep); |
| 6258 | s->logs.logwait = 0; |
| 6259 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6260 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6261 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6262 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6263 | return 0; |
| 6264 | } |
| 6265 | |
| 6266 | /* This function performs all the processing enabled for the current response. |
| 6267 | * 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] | 6268 | * 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] | 6269 | * other functions. It works like process_request (see indications above). |
| 6270 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6271 | 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] | 6272 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6273 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6274 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6275 | struct http_msg *msg = &txn->rsp; |
| 6276 | struct proxy *cur_proxy; |
| 6277 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6278 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6279 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6280 | 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] | 6281 | now_ms, __FUNCTION__, |
| 6282 | s, |
| 6283 | rep, |
| 6284 | rep->rex, rep->wex, |
| 6285 | rep->flags, |
| 6286 | rep->buf->i, |
| 6287 | rep->analysers); |
| 6288 | |
| 6289 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6290 | return 0; |
| 6291 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6292 | /* The stats applet needs to adjust the Connection header but we don't |
| 6293 | * apply any filter there. |
| 6294 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6295 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6296 | rep->analysers &= ~an_bit; |
| 6297 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6298 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6299 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6300 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6301 | /* |
| 6302 | * We will have to evaluate the filters. |
| 6303 | * As opposed to version 1.2, now they will be evaluated in the |
| 6304 | * filters order and not in the header order. This means that |
| 6305 | * each filter has to be validated among all headers. |
| 6306 | * |
| 6307 | * Filters are tried with ->be first, then with ->fe if it is |
| 6308 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6309 | * |
| 6310 | * Maybe we are in resume condiion. In this case I choose the |
| 6311 | * "struct proxy" which contains the rule list matching the resume |
| 6312 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6313 | * the process with the first one. |
| 6314 | * |
| 6315 | * In fact, I check only correspondance betwwen the current list |
| 6316 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6317 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6318 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6319 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6320 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6321 | else |
| 6322 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6323 | while (1) { |
| 6324 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6325 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6326 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6327 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6328 | 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] | 6329 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6330 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6331 | goto return_srv_prx_502; |
| 6332 | |
| 6333 | if (ret == HTTP_RULE_RES_DONE) { |
| 6334 | rep->analysers &= ~an_bit; |
| 6335 | rep->analyse_exp = TICK_ETERNITY; |
| 6336 | return 1; |
| 6337 | } |
| 6338 | } |
| 6339 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6340 | /* we need to be called again. */ |
| 6341 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6342 | channel_dont_close(rep); |
| 6343 | return 0; |
| 6344 | } |
| 6345 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6346 | /* try headers filters */ |
| 6347 | if (rule_set->rsp_exp != NULL) { |
| 6348 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6349 | return_bad_resp: |
| 6350 | if (objt_server(s->target)) { |
| 6351 | objt_server(s->target)->counters.failed_resp++; |
| 6352 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6353 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6354 | s->be->be_counters.failed_resp++; |
| 6355 | return_srv_prx_502: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6356 | rep->analysers &= AN_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6357 | txn->status = 502; |
| 6358 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6359 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6360 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6361 | 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] | 6362 | if (!(s->flags & SF_ERR_MASK)) |
| 6363 | s->flags |= SF_ERR_PRXCOND; |
| 6364 | if (!(s->flags & SF_FINST_MASK)) |
| 6365 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6366 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6367 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6368 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6369 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6370 | /* has the response been denied ? */ |
| 6371 | if (txn->flags & TX_SVDENY) { |
| 6372 | if (objt_server(s->target)) |
| 6373 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6374 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6375 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6376 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6377 | if (sess->listener->counters) |
| 6378 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6379 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6380 | goto return_srv_prx_502; |
| 6381 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6382 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6383 | /* add response headers from the rule sets in the same order */ |
| 6384 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6385 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6386 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6387 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6388 | 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] | 6389 | ret = acl_pass(ret); |
| 6390 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6391 | ret = !ret; |
| 6392 | if (!ret) |
| 6393 | continue; |
| 6394 | } |
| 6395 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6396 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6397 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6398 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6399 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6400 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6401 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6402 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6403 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6404 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6405 | /* After this point, this anayzer can't return yield, so we can |
| 6406 | * remove the bit corresponding to this analyzer from the list. |
| 6407 | * |
| 6408 | * Note that the intermediate returns and goto found previously |
| 6409 | * reset the analyzers. |
| 6410 | */ |
| 6411 | rep->analysers &= ~an_bit; |
| 6412 | rep->analyse_exp = TICK_ETERNITY; |
| 6413 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6414 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6415 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6416 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6417 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6418 | /* |
| 6419 | * Now check for a server cookie. |
| 6420 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6421 | 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] | 6422 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6423 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6424 | /* |
| 6425 | * Check for cache-control or pragma headers if required. |
| 6426 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6427 | 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] | 6428 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6429 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6430 | /* |
| 6431 | * Add server cookie in the response if needed |
| 6432 | */ |
| 6433 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6434 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6435 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6436 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6437 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6438 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6439 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6440 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6441 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6442 | /* the server is known, it's not the one the client requested, or the |
| 6443 | * cookie's last seen date needs to be refreshed. We have to |
| 6444 | * insert a set-cookie here, except if we want to insert only on POST |
| 6445 | * requests and this one isn't. Note that servers which don't have cookies |
| 6446 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6447 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6448 | if (!objt_server(s->target)->cookie) { |
| 6449 | chunk_printf(&trash, |
| 6450 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6451 | s->be->cookie_name); |
| 6452 | } |
| 6453 | else { |
| 6454 | 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] | 6455 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6456 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6457 | /* emit last_date, which is mandatory */ |
| 6458 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6459 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6460 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6461 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6462 | if (s->be->cookie_maxlife) { |
| 6463 | /* emit first_date, which is either the original one or |
| 6464 | * the current date. |
| 6465 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6466 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6467 | s30tob64(txn->cookie_first_date ? |
| 6468 | txn->cookie_first_date >> 2 : |
| 6469 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6470 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6471 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6472 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6473 | chunk_appendf(&trash, "; path=/"); |
| 6474 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6475 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6476 | if (s->be->cookie_domain) |
| 6477 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6478 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6479 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6480 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6481 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6482 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6483 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6484 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6485 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6486 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6487 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6488 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6489 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6490 | /* the server did not change, only the date was updated */ |
| 6491 | txn->flags |= TX_SCK_UPDATED; |
| 6492 | else |
| 6493 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6494 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6495 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6496 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6497 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6498 | * 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] | 6499 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6500 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6501 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6502 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6503 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6504 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6505 | "Cache-control: private", 22) < 0)) |
| 6506 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6507 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6508 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6509 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6510 | /* |
| 6511 | * Check if result will be cacheable with a cookie. |
| 6512 | * We'll block the response if security checks have caught |
| 6513 | * nasty things such as a cacheable cookie. |
| 6514 | */ |
| 6515 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6516 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6517 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6518 | /* we're in presence of a cacheable response containing |
| 6519 | * a set-cookie header. We'll block it as requested by |
| 6520 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6521 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6522 | if (objt_server(s->target)) |
| 6523 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6524 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6525 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6526 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6527 | if (sess->listener->counters) |
| 6528 | sess->listener->counters->denied_resp++; |
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 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6531 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6532 | send_log(s->be, LOG_ALERT, |
| 6533 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6534 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6535 | goto return_srv_prx_502; |
| 6536 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6537 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6538 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6539 | /* |
| 6540 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6541 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6542 | * 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] | 6543 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6544 | * want to touch any 101 response either since it's switching to another |
| 6545 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6546 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6547 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6548 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6549 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6550 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6551 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6552 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6553 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6554 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6555 | /* we want a keep-alive response here. Keep-alive header |
| 6556 | * required if either side is not 1.1. |
| 6557 | */ |
| 6558 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6559 | want_flags |= TX_CON_KAL_SET; |
| 6560 | } |
| 6561 | else { |
| 6562 | /* we want a close response here. Close header required if |
| 6563 | * the server is 1.1, regardless of the client. |
| 6564 | */ |
| 6565 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6566 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6567 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6568 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6569 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6570 | http_change_connection_header(txn, msg, want_flags); |
| 6571 | } |
| 6572 | |
| 6573 | skip_header_mangling: |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6574 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6575 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 6576 | rep->analysers &= ~AN_FLT_XFER_DATA; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6577 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6578 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6579 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6580 | /* if the user wants to log as soon as possible, without counting |
| 6581 | * bytes from the server, then this is the right moment. We have |
| 6582 | * to temporarily assign bytes_out to log what we currently have. |
| 6583 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6584 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6585 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6586 | s->logs.bytes_out = txn->rsp.eoh; |
| 6587 | s->do_log(s); |
| 6588 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6589 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6590 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6591 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6592 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6593 | /* This function is an analyser which forwards response body (including chunk |
| 6594 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6595 | * zero byte. The only situation where it must not be called is when we're in |
| 6596 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6597 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6598 | * 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] | 6599 | * 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] | 6600 | * |
| 6601 | * It is capable of compressing response data both in content-length mode and |
| 6602 | * in chunked mode. The state machines follows different flows depending on |
| 6603 | * whether content-length and chunked modes are used, since there are no |
| 6604 | * trailers in content-length : |
| 6605 | * |
| 6606 | * chk-mode cl-mode |
| 6607 | * ,----- BODY -----. |
| 6608 | * / \ |
| 6609 | * V size > 0 V chk-mode |
| 6610 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6611 | * | | size == 0 | last byte | |
| 6612 | * | v final crlf v inspected | |
| 6613 | * | TRAILERS -----------> DONE | |
| 6614 | * | | |
| 6615 | * `----------------------------------------------' |
| 6616 | * |
| 6617 | * Compression only happens in the DATA state, and must be flushed in final |
| 6618 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6619 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6620 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6621 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6622 | 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] | 6623 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6624 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6625 | struct http_txn *txn = s->txn; |
| 6626 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6627 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6628 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6629 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6630 | return 0; |
| 6631 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6632 | 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] | 6633 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6634 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6635 | /* Output closed while we were sending data. We must abort and |
| 6636 | * wake the other side up. |
| 6637 | */ |
| 6638 | msg->msg_state = HTTP_MSG_ERROR; |
| 6639 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6640 | return 1; |
| 6641 | } |
| 6642 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6643 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6644 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6645 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6646 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6647 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6648 | ? HTTP_MSG_CHUNK_SIZE |
| 6649 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6650 | } |
| 6651 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6652 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6653 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6654 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6655 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6656 | } |
| 6657 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6658 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6659 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6660 | ? http_msg_forward_chunked_body(s, msg) |
| 6661 | : http_msg_forward_body(s, msg)); |
| 6662 | if (!ret) |
| 6663 | goto missing_data_or_waiting; |
| 6664 | if (ret < 0) |
| 6665 | goto return_bad_res; |
| 6666 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6667 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6668 | /* other states, DONE...TUNNEL */ |
| 6669 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6670 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6671 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6672 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6673 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6674 | ret = msg->msg_state; |
| 6675 | if (http_resync_states(s)) { |
| 6676 | /* some state changes occurred, maybe the analyser was disabled |
| 6677 | * too. */ |
| 6678 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6679 | if (res->flags & CF_SHUTW) { |
| 6680 | /* response errors are most likely due to the |
| 6681 | * client aborting the transfer. */ |
| 6682 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6683 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6684 | if (msg->err_pos >= 0) |
| 6685 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s)); |
| 6686 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6687 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6688 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6689 | } |
| 6690 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6691 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6692 | if (res->flags & CF_SHUTW) |
| 6693 | goto aborted_xfer; |
| 6694 | |
| 6695 | /* stop waiting for data if the input is closed before the end. If the |
| 6696 | * client side was already closed, it means that the client has aborted, |
| 6697 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6698 | * server abort. |
| 6699 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6700 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6701 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6702 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6703 | /* If we have some pending data, we continue the processing */ |
| 6704 | if (!buffer_pending(res->buf)) { |
| 6705 | if (!(s->flags & SF_ERR_MASK)) |
| 6706 | s->flags |= SF_ERR_SRVCL; |
| 6707 | s->be->be_counters.srv_aborts++; |
| 6708 | if (objt_server(s->target)) |
| 6709 | objt_server(s->target)->counters.srv_aborts++; |
| 6710 | goto return_bad_res_stats_ok; |
| 6711 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6712 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6713 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6714 | /* 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] | 6715 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6716 | goto return_bad_res; |
| 6717 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6718 | /* When TE: chunked is used, we need to get there again to parse |
| 6719 | * remaining chunks even if the server has closed, so we don't want to |
| 6720 | * set CF_DONTCLOSE. Similarly, if the body length is undefined, if |
| 6721 | * keep-alive is set on the client side or if there are filters |
| 6722 | * registered on the stream, we don't want to forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6723 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6724 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len || |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6725 | HAS_FILTERS(s) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6726 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6727 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6728 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6729 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6730 | /* 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] | 6731 | * 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] | 6732 | * 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] | 6733 | * modes are already handled by the stream sock layer. We must not do |
| 6734 | * this in content-length mode because it could present the MSG_MORE |
| 6735 | * flag with the last block of forwarded data, which would cause an |
| 6736 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6737 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6738 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6739 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6740 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6741 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6742 | return 0; |
| 6743 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6744 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6745 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6746 | if (objt_server(s->target)) |
| 6747 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6748 | |
| 6749 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6750 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6751 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6752 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6753 | res->analysers &= AN_FLT_END; |
| 6754 | 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] | 6755 | if (objt_server(s->target)) |
| 6756 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6757 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6758 | if (!(s->flags & SF_ERR_MASK)) |
| 6759 | s->flags |= SF_ERR_PRXCOND; |
| 6760 | if (!(s->flags & SF_FINST_MASK)) |
| 6761 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6762 | return 0; |
| 6763 | |
| 6764 | aborted_xfer: |
| 6765 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6766 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6767 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6768 | res->analysers &= AN_FLT_END; |
| 6769 | 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] | 6770 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6771 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6772 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6773 | if (objt_server(s->target)) |
| 6774 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6775 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6776 | if (!(s->flags & SF_ERR_MASK)) |
| 6777 | s->flags |= SF_ERR_CLICL; |
| 6778 | if (!(s->flags & SF_FINST_MASK)) |
| 6779 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6780 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | |
| 6784 | static inline int |
| 6785 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6786 | { |
| 6787 | struct channel *chn = msg->chn; |
| 6788 | int ret; |
| 6789 | |
| 6790 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6791 | |
| 6792 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6793 | goto ending; |
| 6794 | |
| 6795 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6796 | * read the body until the server connection is closed. In that case, we |
| 6797 | * eat data as they come. Of course, this happens for response only. */ |
| 6798 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6799 | unsigned long long len = (chn->buf->i - msg->next); |
| 6800 | msg->chunk_len += len; |
| 6801 | msg->body_len += len; |
| 6802 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6803 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6804 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6805 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6806 | msg->next += ret; |
| 6807 | msg->chunk_len -= ret; |
| 6808 | if (msg->chunk_len) { |
| 6809 | /* input empty or output full */ |
| 6810 | if (chn->buf->i > msg->next) |
| 6811 | chn->flags |= CF_WAKE_WRITE; |
| 6812 | goto missing_data_or_waiting; |
| 6813 | } |
| 6814 | |
| 6815 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) { |
| 6816 | /* The server still sending data */ |
| 6817 | goto missing_data_or_waiting; |
| 6818 | } |
| 6819 | msg->msg_state = HTTP_MSG_ENDING; |
| 6820 | |
| 6821 | ending: |
| 6822 | /* we may have some pending data starting at res->buf->p such as a last |
| 6823 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6824 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6825 | /* default_ret */ msg->next, |
| 6826 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6827 | b_adv(chn->buf, ret); |
| 6828 | msg->next -= ret; |
| 6829 | if (msg->next) |
| 6830 | goto missing_data_or_waiting; |
| 6831 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6832 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6833 | /* default_ret */ 1, |
| 6834 | /* on_error */ goto error, |
| 6835 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6836 | msg->msg_state = HTTP_MSG_DONE; |
| 6837 | return 1; |
| 6838 | |
| 6839 | missing_data_or_waiting: |
| 6840 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6841 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6842 | /* default_ret */ msg->next, |
| 6843 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6844 | b_adv(chn->buf, ret); |
| 6845 | msg->next -= ret; |
| 6846 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6847 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6848 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6849 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
| 6850 | waiting: |
| 6851 | return 0; |
| 6852 | error: |
| 6853 | return -1; |
| 6854 | } |
| 6855 | |
| 6856 | static inline int |
| 6857 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6858 | { |
| 6859 | struct channel *chn = msg->chn; |
| 6860 | int ret; |
| 6861 | |
| 6862 | /* Here we have the guarantee to be in one of the following state: |
| 6863 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6864 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6865 | |
| 6866 | switch_states: |
| 6867 | switch (msg->msg_state) { |
| 6868 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6869 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6870 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6871 | /* on_error */ goto error); |
| 6872 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6873 | msg->chunk_len -= ret; |
| 6874 | if (msg->chunk_len) { |
| 6875 | /* input empty or output full */ |
| 6876 | if (chn->buf->i > msg->next) |
| 6877 | chn->flags |= CF_WAKE_WRITE; |
| 6878 | goto missing_data_or_waiting; |
| 6879 | } |
| 6880 | |
| 6881 | /* nothing left to forward for this chunk*/ |
| 6882 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6883 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6884 | |
| 6885 | case HTTP_MSG_CHUNK_CRLF: |
| 6886 | /* we want the CRLF after the data */ |
| 6887 | ret = http_skip_chunk_crlf(msg); |
| 6888 | if (ret == 0) |
| 6889 | goto missing_data_or_waiting; |
| 6890 | if (ret < 0) |
| 6891 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6892 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6893 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6894 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6895 | |
| 6896 | case HTTP_MSG_CHUNK_SIZE: |
| 6897 | /* read the chunk size and assign it to ->chunk_len, |
| 6898 | * then set ->next to point to the body and switch to |
| 6899 | * DATA or TRAILERS state. |
| 6900 | */ |
| 6901 | ret = http_parse_chunk_size(msg); |
| 6902 | if (ret == 0) |
| 6903 | goto missing_data_or_waiting; |
| 6904 | if (ret < 0) |
| 6905 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6906 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6907 | if (msg->chunk_len) { |
| 6908 | msg->msg_state = HTTP_MSG_DATA; |
| 6909 | goto switch_states; |
| 6910 | } |
| 6911 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6912 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6913 | |
| 6914 | case HTTP_MSG_TRAILERS: |
| 6915 | ret = http_forward_trailers(msg); |
| 6916 | if (ret < 0) |
| 6917 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6918 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6919 | /* default_ret */ 1, |
| 6920 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6921 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6922 | if (!ret) |
| 6923 | goto missing_data_or_waiting; |
| 6924 | break; |
| 6925 | |
| 6926 | case HTTP_MSG_ENDING: |
| 6927 | goto ending; |
| 6928 | |
| 6929 | default: |
| 6930 | /* This should no happen in this function */ |
| 6931 | goto error; |
| 6932 | } |
| 6933 | |
| 6934 | msg->msg_state = HTTP_MSG_ENDING; |
| 6935 | ending: |
| 6936 | /* we may have some pending data starting at res->buf->p such as a last |
| 6937 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6938 | 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] | 6939 | /* default_ret */ msg->next, |
| 6940 | /* on_error */ goto error); |
| 6941 | b_adv(chn->buf, ret); |
| 6942 | msg->next -= ret; |
| 6943 | if (msg->next) |
| 6944 | goto missing_data_or_waiting; |
| 6945 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6946 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6947 | /* default_ret */ 1, |
| 6948 | /* on_error */ goto error, |
| 6949 | /* on_wait */ goto waiting); |
| 6950 | msg->msg_state = HTTP_MSG_DONE; |
| 6951 | return 1; |
| 6952 | |
| 6953 | missing_data_or_waiting: |
| 6954 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6955 | 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] | 6956 | /* default_ret */ msg->next, |
| 6957 | /* on_error */ goto error); |
| 6958 | b_adv(chn->buf, ret); |
| 6959 | msg->next -= ret; |
| 6960 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6961 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6962 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6963 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
| 6964 | waiting: |
| 6965 | return 0; |
| 6966 | |
| 6967 | chunk_parsing_error: |
| 6968 | if (msg->err_pos >= 0) { |
| 6969 | if (chn->flags & CF_ISRESP) |
| 6970 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 6971 | msg->msg_state, strm_fe(s)); |
| 6972 | else |
| 6973 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 6974 | msg, msg->msg_state, s->be); |
| 6975 | } |
| 6976 | error: |
| 6977 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6978 | } |
| 6979 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6980 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6981 | /* Iterate the same filter through all request headers. |
| 6982 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6983 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6984 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6985 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6986 | 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] | 6987 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6988 | char *cur_ptr, *cur_end, *cur_next; |
| 6989 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6990 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6991 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6992 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6993 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6994 | last_hdr = 0; |
| 6995 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6996 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6997 | old_idx = 0; |
| 6998 | |
| 6999 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7000 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7001 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7002 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7003 | (exp->action == ACT_ALLOW || |
| 7004 | exp->action == ACT_DENY || |
| 7005 | exp->action == ACT_TARPIT)) |
| 7006 | return 0; |
| 7007 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7008 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7009 | if (!cur_idx) |
| 7010 | break; |
| 7011 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7012 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7013 | cur_ptr = cur_next; |
| 7014 | cur_end = cur_ptr + cur_hdr->len; |
| 7015 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7016 | |
| 7017 | /* Now we have one header between cur_ptr and cur_end, |
| 7018 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7019 | */ |
| 7020 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7021 | 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] | 7022 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7023 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7024 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7025 | last_hdr = 1; |
| 7026 | break; |
| 7027 | |
| 7028 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7029 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7030 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7031 | break; |
| 7032 | |
| 7033 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7034 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7035 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7036 | break; |
| 7037 | |
| 7038 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7039 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7040 | if (trash.len < 0) |
| 7041 | return -1; |
| 7042 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7043 | 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] | 7044 | /* FIXME: if the user adds a newline in the replacement, the |
| 7045 | * index will not be recalculated for now, and the new line |
| 7046 | * will not be counted as a new header. |
| 7047 | */ |
| 7048 | |
| 7049 | cur_end += delta; |
| 7050 | cur_next += delta; |
| 7051 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7052 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7053 | break; |
| 7054 | |
| 7055 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7056 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7057 | cur_next += delta; |
| 7058 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7059 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7060 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7061 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7062 | cur_hdr->len = 0; |
| 7063 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7064 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7065 | break; |
| 7066 | |
| 7067 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7068 | } |
| 7069 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7070 | /* keep the link from this header to next one in case of later |
| 7071 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7073 | old_idx = cur_idx; |
| 7074 | } |
| 7075 | return 0; |
| 7076 | } |
| 7077 | |
| 7078 | |
| 7079 | /* Apply the filter to the request line. |
| 7080 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7081 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7082 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7083 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7084 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7085 | 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] | 7086 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7087 | char *cur_ptr, *cur_end; |
| 7088 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7089 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7090 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7091 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7092 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7093 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7094 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7095 | (exp->action == ACT_ALLOW || |
| 7096 | exp->action == ACT_DENY || |
| 7097 | exp->action == ACT_TARPIT)) |
| 7098 | return 0; |
| 7099 | else if (exp->action == ACT_REMOVE) |
| 7100 | return 0; |
| 7101 | |
| 7102 | done = 0; |
| 7103 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7104 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7105 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7106 | |
| 7107 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7108 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7109 | 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] | 7110 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7111 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7112 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7113 | done = 1; |
| 7114 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7115 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7116 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7117 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7118 | done = 1; |
| 7119 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7120 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7121 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7122 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7123 | done = 1; |
| 7124 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7125 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7126 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7127 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7128 | if (trash.len < 0) |
| 7129 | return -1; |
| 7130 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7131 | 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] | 7132 | /* FIXME: if the user adds a newline in the replacement, the |
| 7133 | * index will not be recalculated for now, and the new line |
| 7134 | * will not be counted as a new header. |
| 7135 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7136 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7137 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7138 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7139 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7140 | HTTP_MSG_RQMETH, |
| 7141 | cur_ptr, cur_end + 1, |
| 7142 | NULL, NULL); |
| 7143 | if (unlikely(!cur_end)) |
| 7144 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7145 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7146 | /* we have a full request and we know that we have either a CR |
| 7147 | * or an LF at <ptr>. |
| 7148 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7149 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7150 | 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] | 7151 | /* there is no point trying this regex on headers */ |
| 7152 | return 1; |
| 7153 | } |
| 7154 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7155 | return done; |
| 7156 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7157 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7158 | |
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 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7161 | * 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] | 7162 | * 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] | 7163 | * unparsable request. Since it can manage the switch to another backend, it |
| 7164 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7165 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7166 | 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] | 7167 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7168 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7169 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7170 | struct hdr_exp *exp; |
| 7171 | |
| 7172 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7173 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7174 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7175 | /* |
| 7176 | * The interleaving of transformations and verdicts |
| 7177 | * makes it difficult to decide to continue or stop |
| 7178 | * the evaluation. |
| 7179 | */ |
| 7180 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7181 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7182 | break; |
| 7183 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7184 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7185 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7186 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7187 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7188 | |
| 7189 | /* if this filter had a condition, evaluate it now and skip to |
| 7190 | * next filter if the condition does not match. |
| 7191 | */ |
| 7192 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7193 | 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] | 7194 | ret = acl_pass(ret); |
| 7195 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7196 | ret = !ret; |
| 7197 | |
| 7198 | if (!ret) |
| 7199 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7203 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7204 | if (unlikely(ret < 0)) |
| 7205 | return -1; |
| 7206 | |
| 7207 | if (likely(ret == 0)) { |
| 7208 | /* The filter did not match the request, it can be |
| 7209 | * iterated through all headers. |
| 7210 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7211 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7212 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7213 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7214 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7215 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7216 | } |
| 7217 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7218 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7219 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7220 | * same way as with headers above except that the semi-colon also ends a token. |
| 7221 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7222 | * return a valid result. |
| 7223 | */ |
| 7224 | char *find_cookie_value_end(char *s, const char *e) |
| 7225 | { |
| 7226 | int quoted, qdpair; |
| 7227 | |
| 7228 | quoted = qdpair = 0; |
| 7229 | for (; s < e; s++) { |
| 7230 | if (qdpair) qdpair = 0; |
| 7231 | else if (quoted) { |
| 7232 | if (*s == '\\') qdpair = 1; |
| 7233 | else if (*s == '"') quoted = 0; |
| 7234 | } |
| 7235 | else if (*s == '"') quoted = 1; |
| 7236 | else if (*s == ',' || *s == ';') return s; |
| 7237 | } |
| 7238 | return s; |
| 7239 | } |
| 7240 | |
| 7241 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7242 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7243 | * the first delimiter is updated if required. The function tries as much as |
| 7244 | * possible to respect the following principles : |
| 7245 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7246 | * colon, in which case <next> is simply removed |
| 7247 | * - set exactly one space character after the new first delimiter, unless |
| 7248 | * there are not enough characters in the block being moved to do so. |
| 7249 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7250 | * one. |
| 7251 | * |
| 7252 | * It is the caller's responsibility to ensure that : |
| 7253 | * - <from> points to a valid delimiter or the colon ; |
| 7254 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7255 | * - there are non-space chars before <from> ; |
| 7256 | * - there is a CR/LF at or after <next>. |
| 7257 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7258 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7259 | { |
| 7260 | char *prev = *from; |
| 7261 | |
| 7262 | if (*prev == ':') { |
| 7263 | /* We're removing the first value, preserve the colon and add a |
| 7264 | * space if possible. |
| 7265 | */ |
| 7266 | if (!http_is_crlf[(unsigned char)*next]) |
| 7267 | next++; |
| 7268 | prev++; |
| 7269 | if (prev < next) |
| 7270 | *prev++ = ' '; |
| 7271 | |
| 7272 | while (http_is_spht[(unsigned char)*next]) |
| 7273 | next++; |
| 7274 | } else { |
| 7275 | /* Remove useless spaces before the old delimiter. */ |
| 7276 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7277 | prev--; |
| 7278 | *from = prev; |
| 7279 | |
| 7280 | /* copy the delimiter and if possible a space if we're |
| 7281 | * not at the end of the line. |
| 7282 | */ |
| 7283 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7284 | *prev++ = *next++; |
| 7285 | if (prev + 1 < next) |
| 7286 | *prev++ = ' '; |
| 7287 | while (http_is_spht[(unsigned char)*next]) |
| 7288 | next++; |
| 7289 | } |
| 7290 | } |
| 7291 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7292 | } |
| 7293 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7294 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7295 | * 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] | 7296 | * desirable to call it only when needed. This code is quite complex because |
| 7297 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7298 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7299 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7300 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7301 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7302 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7303 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7304 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7305 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7306 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7307 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7308 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7309 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7310 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7311 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7312 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7313 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7314 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7315 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7316 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7317 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7318 | hdr_beg = hdr_next; |
| 7319 | hdr_end = hdr_beg + cur_hdr->len; |
| 7320 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7321 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7322 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7323 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7324 | * "Cookie:" headers. |
| 7325 | */ |
| 7326 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7327 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7328 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7329 | old_idx = cur_idx; |
| 7330 | continue; |
| 7331 | } |
| 7332 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7333 | del_from = NULL; /* nothing to be deleted */ |
| 7334 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7335 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7336 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7337 | * attributes whose name begin with a '$', and associate them with |
| 7338 | * the right cookie, if we want to delete this cookie. |
| 7339 | * So there are 3 cases for each cookie read : |
| 7340 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7341 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7342 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7343 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7344 | * "special" cookie. |
| 7345 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7346 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7347 | * *MUST* delete it. |
| 7348 | * |
| 7349 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7350 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7351 | * the RFC explicitly allows spaces before it, and not within the |
| 7352 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7353 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7354 | * after the equal sign too, resulting in some (rare) buggy |
| 7355 | * implementations trying to do that. So let's do what servers do. |
| 7356 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7357 | * allowed quoted strings in values, with any possible character |
| 7358 | * after a backslash, including control chars and delimitors, which |
| 7359 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7360 | * within values even without quotes. |
| 7361 | * |
| 7362 | * We have to keep multiple pointers in order to support cookie |
| 7363 | * removal at the beginning, middle or end of header without |
| 7364 | * corrupting the header. All of these headers are valid : |
| 7365 | * |
| 7366 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7367 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7368 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7369 | * | | | | | | | | | |
| 7370 | * | | | | | | | | hdr_end <--+ |
| 7371 | * | | | | | | | +--> next |
| 7372 | * | | | | | | +----> val_end |
| 7373 | * | | | | | +-----------> val_beg |
| 7374 | * | | | | +--------------> equal |
| 7375 | * | | | +----------------> att_end |
| 7376 | * | | +---------------------> att_beg |
| 7377 | * | +--------------------------> prev |
| 7378 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7379 | */ |
| 7380 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7381 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7382 | /* Iterate through all cookies on this line */ |
| 7383 | |
| 7384 | /* find att_beg */ |
| 7385 | att_beg = prev + 1; |
| 7386 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7387 | att_beg++; |
| 7388 | |
| 7389 | /* find att_end : this is the first character after the last non |
| 7390 | * space before the equal. It may be equal to hdr_end. |
| 7391 | */ |
| 7392 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7393 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7394 | while (equal < hdr_end) { |
| 7395 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7396 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7397 | if (http_is_spht[(unsigned char)*equal++]) |
| 7398 | continue; |
| 7399 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7400 | } |
| 7401 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7402 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7403 | * is between <att_beg> and <equal>, both may be identical. |
| 7404 | */ |
| 7405 | |
| 7406 | /* look for end of cookie if there is an equal sign */ |
| 7407 | if (equal < hdr_end && *equal == '=') { |
| 7408 | /* look for the beginning of the value */ |
| 7409 | val_beg = equal + 1; |
| 7410 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7411 | val_beg++; |
| 7412 | |
| 7413 | /* find the end of the value, respecting quotes */ |
| 7414 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7415 | |
| 7416 | /* make val_end point to the first white space or delimitor after the value */ |
| 7417 | val_end = next; |
| 7418 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7419 | val_end--; |
| 7420 | } else { |
| 7421 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7422 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7423 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7424 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7425 | * they will automatically be removed if a header before them is removed, |
| 7426 | * since they're supposed to be linked together. |
| 7427 | */ |
| 7428 | if (*att_beg == '$') |
| 7429 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7430 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7431 | /* Ignore cookies with no equal sign */ |
| 7432 | if (equal == next) { |
| 7433 | /* This is not our cookie, so we must preserve it. But if we already |
| 7434 | * scheduled another cookie for removal, we cannot remove the |
| 7435 | * complete header, but we can remove the previous block itself. |
| 7436 | */ |
| 7437 | preserve_hdr = 1; |
| 7438 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7439 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7440 | val_end += delta; |
| 7441 | next += delta; |
| 7442 | hdr_end += delta; |
| 7443 | hdr_next += delta; |
| 7444 | cur_hdr->len += delta; |
| 7445 | http_msg_move_end(&txn->req, delta); |
| 7446 | prev = del_from; |
| 7447 | del_from = NULL; |
| 7448 | } |
| 7449 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7450 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7451 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7452 | /* if there are spaces around the equal sign, we need to |
| 7453 | * strip them otherwise we'll get trouble for cookie captures, |
| 7454 | * or even for rewrites. Since this happens extremely rarely, |
| 7455 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7456 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7457 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7458 | int stripped_before = 0; |
| 7459 | int stripped_after = 0; |
| 7460 | |
| 7461 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7462 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7463 | equal += stripped_before; |
| 7464 | val_beg += stripped_before; |
| 7465 | } |
| 7466 | |
| 7467 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7468 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7469 | val_beg += stripped_after; |
| 7470 | stripped_before += stripped_after; |
| 7471 | } |
| 7472 | |
| 7473 | val_end += stripped_before; |
| 7474 | next += stripped_before; |
| 7475 | hdr_end += stripped_before; |
| 7476 | hdr_next += stripped_before; |
| 7477 | cur_hdr->len += stripped_before; |
| 7478 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7479 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7480 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7481 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7482 | /* First, let's see if we want to capture this cookie. We check |
| 7483 | * that we don't already have a client side cookie, because we |
| 7484 | * can only capture one. Also as an optimisation, we ignore |
| 7485 | * cookies shorter than the declared name. |
| 7486 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7487 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7488 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7489 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7490 | int log_len = val_end - att_beg; |
| 7491 | |
| 7492 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7493 | Alert("HTTP logging : out of memory.\n"); |
| 7494 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7495 | if (log_len > sess->fe->capture_len) |
| 7496 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7497 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7498 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7499 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7500 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7501 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7502 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7503 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7504 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7505 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7506 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7507 | * For cookies in prefix mode, the form is : |
| 7508 | * |
| 7509 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7510 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7511 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7512 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7513 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7514 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7515 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7516 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7517 | * have the server ID between val_beg and delim, and the original cookie between |
| 7518 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7519 | * |
| 7520 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7521 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7522 | * | || || | |+-> next |
| 7523 | * | || || | +--> val_end |
| 7524 | * | || || +---------> delim |
| 7525 | * | || |+------------> val_beg |
| 7526 | * | || +-------------> att_end = equal |
| 7527 | * | |+-----------------> att_beg |
| 7528 | * | +------------------> prev |
| 7529 | * +-------------------------> hdr_beg |
| 7530 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7531 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7532 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7533 | for (delim = val_beg; delim < val_end; delim++) |
| 7534 | if (*delim == COOKIE_DELIM) |
| 7535 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7536 | } else { |
| 7537 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7538 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7539 | /* Now check if the cookie contains a date field, which would |
| 7540 | * appear after a vertical bar ('|') just after the server name |
| 7541 | * and before the delimiter. |
| 7542 | */ |
| 7543 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7544 | if (vbar1) { |
| 7545 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7546 | * right is the last seen date. It is a base64 encoded |
| 7547 | * 30-bit value representing the UNIX date since the |
| 7548 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7549 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7550 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7551 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7552 | if (val_end - vbar1 >= 5) { |
| 7553 | val = b64tos30(vbar1); |
| 7554 | if (val > 0) |
| 7555 | txn->cookie_last_date = val << 2; |
| 7556 | } |
| 7557 | /* look for a second vertical bar */ |
| 7558 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7559 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7560 | val = b64tos30(vbar1 + 1); |
| 7561 | if (val > 0) |
| 7562 | txn->cookie_first_date = val << 2; |
| 7563 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7564 | } |
| 7565 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7566 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7567 | /* if the cookie has an expiration date and the proxy wants to check |
| 7568 | * it, then we do that now. We first check if the cookie is too old, |
| 7569 | * then only if it has expired. We detect strict overflow because the |
| 7570 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7571 | * in the future are ignored if their offset is beyond one day. This |
| 7572 | * allows an admin to fix timezone issues without expiring everyone |
| 7573 | * and at the same time avoids keeping unwanted side effects for too |
| 7574 | * long. |
| 7575 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7576 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7577 | (((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] | 7578 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7579 | txn->flags &= ~TX_CK_MASK; |
| 7580 | txn->flags |= TX_CK_OLD; |
| 7581 | delim = val_beg; // let's pretend we have not found the cookie |
| 7582 | txn->cookie_first_date = 0; |
| 7583 | txn->cookie_last_date = 0; |
| 7584 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7585 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7586 | (((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] | 7587 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7588 | txn->flags &= ~TX_CK_MASK; |
| 7589 | txn->flags |= TX_CK_EXPIRED; |
| 7590 | delim = val_beg; // let's pretend we have not found the cookie |
| 7591 | txn->cookie_first_date = 0; |
| 7592 | txn->cookie_last_date = 0; |
| 7593 | } |
| 7594 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7595 | /* Here, we'll look for the first running server which supports the cookie. |
| 7596 | * This allows to share a same cookie between several servers, for example |
| 7597 | * to dedicate backup servers to specific servers only. |
| 7598 | * However, to prevent clients from sticking to cookie-less backup server |
| 7599 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7600 | * empty cookies and mark them as invalid. |
| 7601 | * The same behaviour is applied when persistence must be ignored. |
| 7602 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7603 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7604 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7605 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7606 | while (srv) { |
| 7607 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7608 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7609 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7610 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7611 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7612 | /* we found the server and we can use it */ |
| 7613 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7614 | 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] | 7615 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7616 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7617 | break; |
| 7618 | } else { |
| 7619 | /* we found a server, but it's down, |
| 7620 | * mark it as such and go on in case |
| 7621 | * another one is available. |
| 7622 | */ |
| 7623 | txn->flags &= ~TX_CK_MASK; |
| 7624 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7625 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7626 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7627 | srv = srv->next; |
| 7628 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7629 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7630 | 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] | 7631 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7632 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7633 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7634 | txn->flags |= TX_CK_UNUSED; |
| 7635 | else |
| 7636 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7637 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7638 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7639 | /* depending on the cookie mode, we may have to either : |
| 7640 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7641 | * the server never sees it ; |
| 7642 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7643 | * application cookie so that it does not get accidentely removed later, |
| 7644 | * if we're in cookie prefix mode |
| 7645 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7646 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7647 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7648 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7649 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7650 | val_end += delta; |
| 7651 | next += delta; |
| 7652 | hdr_end += delta; |
| 7653 | hdr_next += delta; |
| 7654 | cur_hdr->len += delta; |
| 7655 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7656 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7657 | del_from = NULL; |
| 7658 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7659 | } |
| 7660 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7661 | (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] | 7662 | del_from = prev; |
| 7663 | } |
| 7664 | } else { |
| 7665 | /* This is not our cookie, so we must preserve it. But if we already |
| 7666 | * scheduled another cookie for removal, we cannot remove the |
| 7667 | * complete header, but we can remove the previous block itself. |
| 7668 | */ |
| 7669 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7670 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7671 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7672 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7673 | if (att_beg >= del_from) |
| 7674 | att_beg += delta; |
| 7675 | if (att_end >= del_from) |
| 7676 | att_end += delta; |
| 7677 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7678 | val_end += delta; |
| 7679 | next += delta; |
| 7680 | hdr_end += delta; |
| 7681 | hdr_next += delta; |
| 7682 | cur_hdr->len += delta; |
| 7683 | http_msg_move_end(&txn->req, delta); |
| 7684 | prev = del_from; |
| 7685 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7686 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7687 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7688 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7689 | /* continue with next cookie on this header line */ |
| 7690 | att_beg = next; |
| 7691 | } /* for each cookie */ |
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 | /* There are no more cookies on this line. |
| 7694 | * We may still have one (or several) marked for deletion at the |
| 7695 | * end of the line. We must do this now in two ways : |
| 7696 | * - if some cookies must be preserved, we only delete from the |
| 7697 | * mark to the end of line ; |
| 7698 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7699 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7700 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7701 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7702 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7703 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7704 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7705 | cur_hdr->len += delta; |
| 7706 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7707 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7708 | |
| 7709 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7710 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7711 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7712 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7713 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7714 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7715 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7716 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7717 | } |
| 7718 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7719 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7720 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7721 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7722 | } |
| 7723 | |
| 7724 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7725 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7726 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7727 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7728 | 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] | 7729 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7730 | char *cur_ptr, *cur_end, *cur_next; |
| 7731 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7732 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7733 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7734 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7735 | |
| 7736 | last_hdr = 0; |
| 7737 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7738 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7739 | old_idx = 0; |
| 7740 | |
| 7741 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7742 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7743 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7744 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7745 | (exp->action == ACT_ALLOW || |
| 7746 | exp->action == ACT_DENY)) |
| 7747 | return 0; |
| 7748 | |
| 7749 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7750 | if (!cur_idx) |
| 7751 | break; |
| 7752 | |
| 7753 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7754 | cur_ptr = cur_next; |
| 7755 | cur_end = cur_ptr + cur_hdr->len; |
| 7756 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7757 | |
| 7758 | /* Now we have one header between cur_ptr and cur_end, |
| 7759 | * and the next header starts at cur_next. |
| 7760 | */ |
| 7761 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7762 | 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] | 7763 | switch (exp->action) { |
| 7764 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7765 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7766 | last_hdr = 1; |
| 7767 | break; |
| 7768 | |
| 7769 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7770 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7771 | last_hdr = 1; |
| 7772 | break; |
| 7773 | |
| 7774 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7775 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7776 | if (trash.len < 0) |
| 7777 | return -1; |
| 7778 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7779 | 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] | 7780 | /* FIXME: if the user adds a newline in the replacement, the |
| 7781 | * index will not be recalculated for now, and the new line |
| 7782 | * will not be counted as a new header. |
| 7783 | */ |
| 7784 | |
| 7785 | cur_end += delta; |
| 7786 | cur_next += delta; |
| 7787 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7788 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7789 | break; |
| 7790 | |
| 7791 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7792 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7793 | cur_next += delta; |
| 7794 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7795 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7796 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7797 | txn->hdr_idx.used--; |
| 7798 | cur_hdr->len = 0; |
| 7799 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7800 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7801 | break; |
| 7802 | |
| 7803 | } |
| 7804 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7805 | |
| 7806 | /* keep the link from this header to next one in case of later |
| 7807 | * removal of next header. |
| 7808 | */ |
| 7809 | old_idx = cur_idx; |
| 7810 | } |
| 7811 | return 0; |
| 7812 | } |
| 7813 | |
| 7814 | |
| 7815 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7816 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7817 | * or -1 if a replacement resulted in an invalid status line. |
| 7818 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7819 | 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] | 7820 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7821 | char *cur_ptr, *cur_end; |
| 7822 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7823 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7824 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7825 | |
| 7826 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7827 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7828 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7829 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7830 | (exp->action == ACT_ALLOW || |
| 7831 | exp->action == ACT_DENY)) |
| 7832 | return 0; |
| 7833 | else if (exp->action == ACT_REMOVE) |
| 7834 | return 0; |
| 7835 | |
| 7836 | done = 0; |
| 7837 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7838 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7839 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | |
| 7841 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7842 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7843 | 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] | 7844 | switch (exp->action) { |
| 7845 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7846 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7847 | done = 1; |
| 7848 | break; |
| 7849 | |
| 7850 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7851 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7852 | done = 1; |
| 7853 | break; |
| 7854 | |
| 7855 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7856 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7857 | if (trash.len < 0) |
| 7858 | return -1; |
| 7859 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7860 | 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] | 7861 | /* FIXME: if the user adds a newline in the replacement, the |
| 7862 | * index will not be recalculated for now, and the new line |
| 7863 | * will not be counted as a new header. |
| 7864 | */ |
| 7865 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7866 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7867 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7868 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7869 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7870 | cur_ptr, cur_end + 1, |
| 7871 | NULL, NULL); |
| 7872 | if (unlikely(!cur_end)) |
| 7873 | return -1; |
| 7874 | |
| 7875 | /* we have a full respnse and we know that we have either a CR |
| 7876 | * or an LF at <ptr>. |
| 7877 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7878 | 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] | 7879 | 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] | 7880 | /* there is no point trying this regex on headers */ |
| 7881 | return 1; |
| 7882 | } |
| 7883 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7884 | return done; |
| 7885 | } |
| 7886 | |
| 7887 | |
| 7888 | |
| 7889 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7890 | * 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] | 7891 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7892 | * unparsable response. |
| 7893 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7894 | 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] | 7895 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7896 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7897 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7898 | struct hdr_exp *exp; |
| 7899 | |
| 7900 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7901 | int ret; |
| 7902 | |
| 7903 | /* |
| 7904 | * The interleaving of transformations and verdicts |
| 7905 | * makes it difficult to decide to continue or stop |
| 7906 | * the evaluation. |
| 7907 | */ |
| 7908 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7909 | if (txn->flags & TX_SVDENY) |
| 7910 | break; |
| 7911 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7912 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7913 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7914 | exp->action == ACT_PASS)) { |
| 7915 | exp = exp->next; |
| 7916 | continue; |
| 7917 | } |
| 7918 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7919 | /* if this filter had a condition, evaluate it now and skip to |
| 7920 | * next filter if the condition does not match. |
| 7921 | */ |
| 7922 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7923 | 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] | 7924 | ret = acl_pass(ret); |
| 7925 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7926 | ret = !ret; |
| 7927 | if (!ret) |
| 7928 | continue; |
| 7929 | } |
| 7930 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7931 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7932 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7933 | if (unlikely(ret < 0)) |
| 7934 | return -1; |
| 7935 | |
| 7936 | if (likely(ret == 0)) { |
| 7937 | /* The filter did not match the response, it can be |
| 7938 | * iterated through all headers. |
| 7939 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7940 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7941 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7942 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7943 | } |
| 7944 | return 0; |
| 7945 | } |
| 7946 | |
| 7947 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7948 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7949 | * 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] | 7950 | * desirable to call it only when needed. This function is also used when we |
| 7951 | * 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] | 7952 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7953 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7954 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7955 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7956 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7957 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7958 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7959 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7960 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7961 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7962 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7963 | /* Iterate through the headers. |
| 7964 | * we start with the start line. |
| 7965 | */ |
| 7966 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7967 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7968 | |
| 7969 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7970 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7971 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7972 | |
| 7973 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7974 | hdr_beg = hdr_next; |
| 7975 | hdr_end = hdr_beg + cur_hdr->len; |
| 7976 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7977 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7978 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7979 | * next header starts at hdr_next. We're only interested in |
| 7980 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7981 | */ |
| 7982 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7983 | is_cookie2 = 0; |
| 7984 | prev = hdr_beg + 10; |
| 7985 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7986 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7987 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7988 | if (!val) { |
| 7989 | old_idx = cur_idx; |
| 7990 | continue; |
| 7991 | } |
| 7992 | is_cookie2 = 1; |
| 7993 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7994 | } |
| 7995 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7996 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7997 | * <prev> points to the colon. |
| 7998 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7999 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8000 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8001 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8002 | * check-cache is enabled) and we are not interested in checking |
| 8003 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8004 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8005 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8006 | return; |
| 8007 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8008 | /* OK so now we know we have to process this response cookie. |
| 8009 | * The format of the Set-Cookie header is slightly different |
| 8010 | * from the format of the Cookie header in that it does not |
| 8011 | * support the comma as a cookie delimiter (thus the header |
| 8012 | * cannot be folded) because the Expires attribute described in |
| 8013 | * the original Netscape's spec may contain an unquoted date |
| 8014 | * with a comma inside. We have to live with this because |
| 8015 | * many browsers don't support Max-Age and some browsers don't |
| 8016 | * support quoted strings. However the Set-Cookie2 header is |
| 8017 | * clean. |
| 8018 | * |
| 8019 | * We have to keep multiple pointers in order to support cookie |
| 8020 | * removal at the beginning, middle or end of header without |
| 8021 | * corrupting the header (in case of set-cookie2). A special |
| 8022 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8023 | * fields after the first semi-colon. The <next> pointer points |
| 8024 | * either to the end of line (set-cookie) or next unquoted comma |
| 8025 | * (set-cookie2). All of these headers are valid : |
| 8026 | * |
| 8027 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8028 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8029 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8030 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8031 | * | | | | | | | | | | |
| 8032 | * | | | | | | | | +-> next hdr_end <--+ |
| 8033 | * | | | | | | | +------------> scav |
| 8034 | * | | | | | | +--------------> val_end |
| 8035 | * | | | | | +--------------------> val_beg |
| 8036 | * | | | | +----------------------> equal |
| 8037 | * | | | +------------------------> att_end |
| 8038 | * | | +----------------------------> att_beg |
| 8039 | * | +------------------------------> prev |
| 8040 | * +-----------------------------------------> hdr_beg |
| 8041 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8042 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8043 | for (; prev < hdr_end; prev = next) { |
| 8044 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8045 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8046 | /* find att_beg */ |
| 8047 | att_beg = prev + 1; |
| 8048 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8049 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8051 | /* find att_end : this is the first character after the last non |
| 8052 | * space before the equal. It may be equal to hdr_end. |
| 8053 | */ |
| 8054 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8055 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8056 | while (equal < hdr_end) { |
| 8057 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8058 | break; |
| 8059 | if (http_is_spht[(unsigned char)*equal++]) |
| 8060 | continue; |
| 8061 | att_end = equal; |
| 8062 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8063 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8064 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8065 | * is between <att_beg> and <equal>, both may be identical. |
| 8066 | */ |
| 8067 | |
| 8068 | /* look for end of cookie if there is an equal sign */ |
| 8069 | if (equal < hdr_end && *equal == '=') { |
| 8070 | /* look for the beginning of the value */ |
| 8071 | val_beg = equal + 1; |
| 8072 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8073 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8074 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8075 | /* find the end of the value, respecting quotes */ |
| 8076 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8077 | |
| 8078 | /* make val_end point to the first white space or delimitor after the value */ |
| 8079 | val_end = next; |
| 8080 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8081 | val_end--; |
| 8082 | } else { |
| 8083 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8084 | val_beg = val_end = next = equal; |
| 8085 | } |
| 8086 | |
| 8087 | if (next < hdr_end) { |
| 8088 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8089 | * a colon or semi-colon before the end. So skip all attr-value |
| 8090 | * pairs and look for the next comma. For Set-Cookie, since |
| 8091 | * commas are permitted in values, skip to the end. |
| 8092 | */ |
| 8093 | if (is_cookie2) |
| 8094 | next = find_hdr_value_end(next, hdr_end); |
| 8095 | else |
| 8096 | next = hdr_end; |
| 8097 | } |
| 8098 | |
| 8099 | /* Now everything is as on the diagram above */ |
| 8100 | |
| 8101 | /* Ignore cookies with no equal sign */ |
| 8102 | if (equal == val_end) |
| 8103 | continue; |
| 8104 | |
| 8105 | /* If there are spaces around the equal sign, we need to |
| 8106 | * strip them otherwise we'll get trouble for cookie captures, |
| 8107 | * or even for rewrites. Since this happens extremely rarely, |
| 8108 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8109 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8110 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8111 | int stripped_before = 0; |
| 8112 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8113 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8114 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8115 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8116 | equal += stripped_before; |
| 8117 | val_beg += stripped_before; |
| 8118 | } |
| 8119 | |
| 8120 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8121 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8122 | val_beg += stripped_after; |
| 8123 | stripped_before += stripped_after; |
| 8124 | } |
| 8125 | |
| 8126 | val_end += stripped_before; |
| 8127 | next += stripped_before; |
| 8128 | hdr_end += stripped_before; |
| 8129 | hdr_next += stripped_before; |
| 8130 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8131 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8132 | } |
| 8133 | |
| 8134 | /* First, let's see if we want to capture this cookie. We check |
| 8135 | * that we don't already have a server side cookie, because we |
| 8136 | * can only capture one. Also as an optimisation, we ignore |
| 8137 | * cookies shorter than the declared name. |
| 8138 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8139 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8140 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8141 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8142 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8143 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8144 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8145 | Alert("HTTP logging : out of memory.\n"); |
| 8146 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8147 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8148 | if (log_len > sess->fe->capture_len) |
| 8149 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8150 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8151 | txn->srv_cookie[log_len] = 0; |
| 8152 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8153 | } |
| 8154 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8155 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8156 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8157 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8158 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8159 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8160 | /* assume passive cookie by default */ |
| 8161 | txn->flags &= ~TX_SCK_MASK; |
| 8162 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8163 | |
| 8164 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8165 | * this occurrence because we'll insert another one later. |
| 8166 | * 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] | 8167 | * a direct access. |
| 8168 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8169 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8170 | /* The "preserve" flag was set, we don't want to touch the |
| 8171 | * server's cookie. |
| 8172 | */ |
| 8173 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8174 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8175 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8176 | /* this cookie must be deleted */ |
| 8177 | if (*prev == ':' && next == hdr_end) { |
| 8178 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8179 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8180 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8181 | txn->hdr_idx.used--; |
| 8182 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8183 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8184 | hdr_next += delta; |
| 8185 | http_msg_move_end(&txn->rsp, delta); |
| 8186 | /* note: while both invalid now, <next> and <hdr_end> |
| 8187 | * are still equal, so the for() will stop as expected. |
| 8188 | */ |
| 8189 | } else { |
| 8190 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8191 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8192 | next = prev; |
| 8193 | hdr_end += delta; |
| 8194 | hdr_next += delta; |
| 8195 | cur_hdr->len += delta; |
| 8196 | http_msg_move_end(&txn->rsp, delta); |
| 8197 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8198 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8199 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8200 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8201 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8202 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8203 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8204 | * with this server since we know it. |
| 8205 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8206 | 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] | 8207 | next += delta; |
| 8208 | hdr_end += delta; |
| 8209 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8210 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8211 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8212 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8213 | txn->flags &= ~TX_SCK_MASK; |
| 8214 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8215 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8216 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8217 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8218 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8219 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8220 | 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] | 8221 | next += delta; |
| 8222 | hdr_end += delta; |
| 8223 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8224 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8225 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8226 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8227 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8228 | txn->flags &= ~TX_SCK_MASK; |
| 8229 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8230 | } |
| 8231 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8232 | /* that's done for this cookie, check the next one on the same |
| 8233 | * line when next != hdr_end (only if is_cookie2). |
| 8234 | */ |
| 8235 | } |
| 8236 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8237 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8238 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8239 | } |
| 8240 | |
| 8241 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8242 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8243 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8244 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8245 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8246 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8247 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8248 | char *p1, *p2; |
| 8249 | |
| 8250 | char *cur_ptr, *cur_end, *cur_next; |
| 8251 | int cur_idx; |
| 8252 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8253 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8254 | return; |
| 8255 | |
| 8256 | /* Iterate through the headers. |
| 8257 | * we start with the start line. |
| 8258 | */ |
| 8259 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8260 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8261 | |
| 8262 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8263 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8264 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8265 | |
| 8266 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8267 | cur_ptr = cur_next; |
| 8268 | cur_end = cur_ptr + cur_hdr->len; |
| 8269 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8270 | |
| 8271 | /* We have one full header between cur_ptr and cur_end, and the |
| 8272 | * next header starts at cur_next. We're only interested in |
| 8273 | * "Cookie:" headers. |
| 8274 | */ |
| 8275 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8276 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8277 | if (val) { |
| 8278 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8279 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8280 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8281 | return; |
| 8282 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8283 | } |
| 8284 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8285 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8286 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8287 | continue; |
| 8288 | |
| 8289 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8290 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8291 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8292 | |
| 8293 | if (p1 >= cur_end) /* no more info */ |
| 8294 | continue; |
| 8295 | |
| 8296 | /* p1 is at the beginning of the value */ |
| 8297 | p2 = p1; |
| 8298 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8299 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8300 | p2++; |
| 8301 | |
| 8302 | /* we have a complete value between p1 and p2 */ |
| 8303 | if (p2 < cur_end && *p2 == '=') { |
| 8304 | /* we have something of the form no-cache="set-cookie" */ |
| 8305 | if ((cur_end - p1 >= 21) && |
| 8306 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8307 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8308 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8309 | continue; |
| 8310 | } |
| 8311 | |
| 8312 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8313 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8314 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8315 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8316 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8317 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8318 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8319 | return; |
| 8320 | } |
| 8321 | |
| 8322 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8323 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8324 | continue; |
| 8325 | } |
| 8326 | } |
| 8327 | } |
| 8328 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8329 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8330 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8331 | * 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] | 8332 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8333 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8334 | * 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] | 8335 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8336 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8337 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8338 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8339 | 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] | 8340 | { |
| 8341 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8342 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8343 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8344 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8345 | if (!uri_auth) |
| 8346 | return 0; |
| 8347 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8348 | 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] | 8349 | return 0; |
| 8350 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8351 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8352 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8353 | return 0; |
| 8354 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8355 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8356 | return 0; |
| 8357 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8358 | return 1; |
| 8359 | } |
| 8360 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8361 | /* |
| 8362 | * 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] | 8363 | * By default it tries to report the error position as msg->err_pos. However if |
| 8364 | * this one is not set, it will then report msg->next, which is the last known |
| 8365 | * 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] | 8366 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8367 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8368 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8369 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8370 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8371 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8372 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8373 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8374 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8375 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8376 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8377 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8378 | len1 = MIN(len1, es->len); |
| 8379 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8380 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8381 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8382 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8383 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8384 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8385 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8386 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8387 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8388 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8389 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8390 | es->when = date; // user-visible date |
| 8391 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8392 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8393 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8394 | if (objt_conn(sess->origin)) |
| 8395 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8396 | else |
| 8397 | memset(&es->src, 0, sizeof(es->src)); |
| 8398 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8399 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8400 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8401 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8402 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8403 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8404 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8405 | es->b_out = chn->buf->o; |
| 8406 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8407 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8408 | es->m_clen = msg->chunk_len; |
| 8409 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8410 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8411 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8412 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8413 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8414 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8415 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8416 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8417 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8418 | * 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] | 8419 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8420 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8421 | * 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] | 8422 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8423 | 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] | 8424 | struct hdr_idx *idx, int occ, |
| 8425 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8426 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8427 | struct hdr_ctx local_ctx; |
| 8428 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8429 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8430 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8431 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8432 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8433 | if (!ctx) { |
| 8434 | local_ctx.idx = 0; |
| 8435 | ctx = &local_ctx; |
| 8436 | } |
| 8437 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8438 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8439 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8440 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8441 | occ--; |
| 8442 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8443 | *vptr = ctx->line + ctx->val; |
| 8444 | *vlen = ctx->vlen; |
| 8445 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8446 | } |
| 8447 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8448 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8449 | } |
| 8450 | |
| 8451 | /* negative occurrence, we scan all the list then walk back */ |
| 8452 | if (-occ > MAX_HDR_HISTORY) |
| 8453 | return 0; |
| 8454 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8455 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8456 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8457 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8458 | len_hist[hist_ptr] = ctx->vlen; |
| 8459 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8460 | hist_ptr = 0; |
| 8461 | found++; |
| 8462 | } |
| 8463 | if (-occ > found) |
| 8464 | return 0; |
| 8465 | /* 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] | 8466 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8467 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8468 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8469 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8470 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8471 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8472 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8473 | *vptr = ptr_hist[hist_ptr]; |
| 8474 | *vlen = len_hist[hist_ptr]; |
| 8475 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8476 | } |
| 8477 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8478 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8479 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8480 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8481 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8482 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8483 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8484 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8485 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8486 | * line header values and does not stop at commas. |
| 8487 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8488 | */ |
| 8489 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8490 | struct hdr_idx *idx, int occ, |
| 8491 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8492 | { |
| 8493 | struct hdr_ctx local_ctx; |
| 8494 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8495 | int len_hist[MAX_HDR_HISTORY]; |
| 8496 | unsigned int hist_ptr; |
| 8497 | int found; |
| 8498 | |
| 8499 | if (!ctx) { |
| 8500 | local_ctx.idx = 0; |
| 8501 | ctx = &local_ctx; |
| 8502 | } |
| 8503 | |
| 8504 | if (occ >= 0) { |
| 8505 | /* search from the beginning */ |
| 8506 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8507 | occ--; |
| 8508 | if (occ <= 0) { |
| 8509 | *vptr = ctx->line + ctx->val; |
| 8510 | *vlen = ctx->vlen; |
| 8511 | return 1; |
| 8512 | } |
| 8513 | } |
| 8514 | return 0; |
| 8515 | } |
| 8516 | |
| 8517 | /* negative occurrence, we scan all the list then walk back */ |
| 8518 | if (-occ > MAX_HDR_HISTORY) |
| 8519 | return 0; |
| 8520 | |
| 8521 | found = hist_ptr = 0; |
| 8522 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8523 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8524 | len_hist[hist_ptr] = ctx->vlen; |
| 8525 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8526 | hist_ptr = 0; |
| 8527 | found++; |
| 8528 | } |
| 8529 | if (-occ > found) |
| 8530 | return 0; |
| 8531 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8532 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8533 | */ |
| 8534 | hist_ptr += occ; |
| 8535 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8536 | hist_ptr -= MAX_HDR_HISTORY; |
| 8537 | *vptr = ptr_hist[hist_ptr]; |
| 8538 | *vlen = len_hist[hist_ptr]; |
| 8539 | return 1; |
| 8540 | } |
| 8541 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8542 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8543 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8544 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8545 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8546 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8547 | 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] | 8548 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8549 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8550 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8551 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8552 | 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] | 8553 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8554 | 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] | 8555 | 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] | 8556 | |
| 8557 | for (max = 0; start + max < end; max++) |
| 8558 | if (start[max] == '\r' || start[max] == '\n') |
| 8559 | break; |
| 8560 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8561 | UBOUND(max, trash.size - trash.len - 3); |
| 8562 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8563 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8564 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8565 | } |
| 8566 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8567 | |
| 8568 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8569 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8570 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8571 | * assigned to the stream and returned. |
| 8572 | */ |
| 8573 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8574 | { |
| 8575 | struct http_txn *txn = s->txn; |
| 8576 | |
| 8577 | if (txn) |
| 8578 | return txn; |
| 8579 | |
| 8580 | txn = pool_alloc2(pool2_http_txn); |
| 8581 | if (!txn) |
| 8582 | return txn; |
| 8583 | |
| 8584 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8585 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8586 | if (!txn->hdr_idx.v) { |
| 8587 | pool_free2(pool2_http_txn, txn); |
| 8588 | return NULL; |
| 8589 | } |
| 8590 | |
| 8591 | s->txn = txn; |
| 8592 | return txn; |
| 8593 | } |
| 8594 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8595 | void http_txn_reset_req(struct http_txn *txn) |
| 8596 | { |
| 8597 | txn->req.flags = 0; |
| 8598 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8599 | txn->req.next = 0; |
| 8600 | txn->req.chunk_len = 0LL; |
| 8601 | txn->req.body_len = 0LL; |
| 8602 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8603 | } |
| 8604 | |
| 8605 | void http_txn_reset_res(struct http_txn *txn) |
| 8606 | { |
| 8607 | txn->rsp.flags = 0; |
| 8608 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8609 | txn->rsp.next = 0; |
| 8610 | txn->rsp.chunk_len = 0LL; |
| 8611 | txn->rsp.body_len = 0LL; |
| 8612 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8613 | } |
| 8614 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8615 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8616 | * 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] | 8617 | * the required fields are properly allocated and that we only need to (re)init |
| 8618 | * them. This should be used before processing any new request. |
| 8619 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8620 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8621 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8622 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8623 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8624 | |
| 8625 | txn->flags = 0; |
| 8626 | txn->status = -1; |
| 8627 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8628 | txn->cookie_first_date = 0; |
| 8629 | txn->cookie_last_date = 0; |
| 8630 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8631 | txn->srv_cookie = NULL; |
| 8632 | txn->cli_cookie = NULL; |
| 8633 | txn->uri = NULL; |
| 8634 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8635 | http_txn_reset_req(txn); |
| 8636 | http_txn_reset_res(txn); |
| 8637 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8638 | txn->req.chn = &s->req; |
| 8639 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8640 | |
| 8641 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8642 | |
| 8643 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8644 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8645 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8646 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8647 | if (txn->hdr_idx.v) |
| 8648 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8649 | |
| 8650 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8651 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8652 | } |
| 8653 | |
| 8654 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8655 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8656 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8657 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8658 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8659 | |
| 8660 | /* these ones will have been dynamically allocated */ |
| 8661 | pool_free2(pool2_requri, txn->uri); |
| 8662 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8663 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8664 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8665 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8666 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8667 | txn->uri = NULL; |
| 8668 | txn->srv_cookie = NULL; |
| 8669 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8670 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8671 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8672 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8673 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8674 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8675 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8676 | } |
| 8677 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8678 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8679 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8680 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8681 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8682 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8683 | } |
| 8684 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8685 | vars_prune(&s->vars_txn, s->sess, s); |
| 8686 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8687 | } |
| 8688 | |
| 8689 | /* 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] | 8690 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8691 | { |
| 8692 | http_end_txn(s); |
| 8693 | http_init_txn(s); |
| 8694 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8695 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8696 | * any rulelist match the NULL pointer. |
| 8697 | */ |
| 8698 | s->current_rule_list = NULL; |
| 8699 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8700 | s->be = strm_fe(s); |
| 8701 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8702 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8703 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8704 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8705 | /* re-init store persistence */ |
| 8706 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8707 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8708 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8709 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8710 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8711 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8712 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8713 | /* We must trim any excess data from the response buffer, because we |
| 8714 | * may have blocked an invalid response from a server that we don't |
| 8715 | * want to accidentely forward once we disable the analysers, nor do |
| 8716 | * we want those data to come along with next response. A typical |
| 8717 | * example of such data would be from a buggy server responding to |
| 8718 | * a HEAD with some data, or sending more than the advertised |
| 8719 | * content-length. |
| 8720 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8721 | if (unlikely(s->res.buf->i)) |
| 8722 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8723 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8724 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8725 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8726 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8727 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8728 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8729 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8730 | s->req.rex = TICK_ETERNITY; |
| 8731 | s->req.wex = TICK_ETERNITY; |
| 8732 | s->req.analyse_exp = TICK_ETERNITY; |
| 8733 | s->res.rex = TICK_ETERNITY; |
| 8734 | s->res.wex = TICK_ETERNITY; |
| 8735 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8736 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8737 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8738 | void free_http_res_rules(struct list *r) |
| 8739 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8740 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8741 | |
| 8742 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8743 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8744 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8745 | free(pr); |
| 8746 | } |
| 8747 | } |
| 8748 | |
| 8749 | void free_http_req_rules(struct list *r) |
| 8750 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8751 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8752 | |
| 8753 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8754 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8755 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8756 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8757 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8758 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8759 | free(pr); |
| 8760 | } |
| 8761 | } |
| 8762 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8763 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8764 | 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] | 8765 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8766 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8767 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8768 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8769 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8770 | |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8771 | rule = (struct act_rule*)calloc(1, sizeof(struct act_rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8772 | if (!rule) { |
| 8773 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8774 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8775 | } |
| 8776 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8777 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8778 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8779 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8780 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 8781 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8782 | int code; |
| 8783 | int hc; |
| 8784 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8785 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8786 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8787 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8788 | cur_arg++; |
| 8789 | if (!args[cur_arg]) { |
| 8790 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8791 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8792 | goto out_err; |
| 8793 | } |
| 8794 | |
| 8795 | code = atol(args[cur_arg]); |
| 8796 | cur_arg++; |
| 8797 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8798 | if (http_err_codes[hc] == code) { |
| 8799 | rule->deny_status = hc; |
| 8800 | break; |
| 8801 | } |
| 8802 | } |
| 8803 | |
| 8804 | if (hc >= HTTP_ERR_SIZE) { |
| 8805 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 8806 | file, linenum, code); |
| 8807 | } |
| 8808 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8809 | } else if (!strcmp(args[0], "tarpit")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8810 | rule->action = ACT_HTTP_REQ_TARPIT; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8811 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8812 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8813 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8814 | cur_arg = 1; |
| 8815 | |
| 8816 | while(*args[cur_arg]) { |
| 8817 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8818 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8819 | cur_arg+=2; |
| 8820 | continue; |
| 8821 | } else |
| 8822 | break; |
| 8823 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8824 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8825 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8826 | cur_arg = 1; |
| 8827 | |
| 8828 | if (!*args[cur_arg] || |
| 8829 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8830 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8831 | file, linenum, args[0]); |
| 8832 | goto out_err; |
| 8833 | } |
| 8834 | rule->arg.nice = atoi(args[cur_arg]); |
| 8835 | if (rule->arg.nice < -1024) |
| 8836 | rule->arg.nice = -1024; |
| 8837 | else if (rule->arg.nice > 1024) |
| 8838 | rule->arg.nice = 1024; |
| 8839 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8840 | } else if (!strcmp(args[0], "set-tos")) { |
| 8841 | #ifdef IP_TOS |
| 8842 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8843 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8844 | cur_arg = 1; |
| 8845 | |
| 8846 | if (!*args[cur_arg] || |
| 8847 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8848 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8849 | file, linenum, args[0]); |
| 8850 | goto out_err; |
| 8851 | } |
| 8852 | |
| 8853 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8854 | if (err && *err != '\0') { |
| 8855 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8856 | file, linenum, err, args[0]); |
| 8857 | goto out_err; |
| 8858 | } |
| 8859 | cur_arg++; |
| 8860 | #else |
| 8861 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8862 | goto out_err; |
| 8863 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8864 | } else if (!strcmp(args[0], "set-mark")) { |
| 8865 | #ifdef SO_MARK |
| 8866 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8867 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8868 | cur_arg = 1; |
| 8869 | |
| 8870 | if (!*args[cur_arg] || |
| 8871 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8872 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8873 | file, linenum, args[0]); |
| 8874 | goto out_err; |
| 8875 | } |
| 8876 | |
| 8877 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8878 | if (err && *err != '\0') { |
| 8879 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8880 | file, linenum, err, args[0]); |
| 8881 | goto out_err; |
| 8882 | } |
| 8883 | cur_arg++; |
| 8884 | global.last_checks |= LSTCHK_NETADM; |
| 8885 | #else |
| 8886 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8887 | goto out_err; |
| 8888 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8889 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8890 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8891 | cur_arg = 1; |
| 8892 | |
| 8893 | if (!*args[cur_arg] || |
| 8894 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8895 | bad_log_level: |
| 8896 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8897 | file, linenum, args[0]); |
| 8898 | goto out_err; |
| 8899 | } |
| 8900 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8901 | rule->arg.loglevel = -1; |
| 8902 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8903 | goto bad_log_level; |
| 8904 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8905 | } 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] | 8906 | 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] | 8907 | cur_arg = 1; |
| 8908 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8909 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8910 | (*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] | 8911 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8912 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8913 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8914 | } |
| 8915 | |
| 8916 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8917 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8918 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8919 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8920 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8921 | 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] | 8922 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8923 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8924 | free(proxy->conf.lfs_file); |
| 8925 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8926 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8927 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8928 | } 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] | 8929 | 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] | 8930 | cur_arg = 1; |
| 8931 | |
| 8932 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8933 | (*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] | 8934 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8935 | file, linenum, args[0]); |
| 8936 | goto out_err; |
| 8937 | } |
| 8938 | |
| 8939 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8940 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8941 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8942 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8943 | error = NULL; |
| 8944 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 8945 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8946 | args[cur_arg + 1], error); |
| 8947 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8948 | goto out_err; |
| 8949 | } |
| 8950 | |
| 8951 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8952 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 8953 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8954 | file, linenum); |
| 8955 | |
| 8956 | free(proxy->conf.lfs_file); |
| 8957 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8958 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8959 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8960 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8961 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8962 | cur_arg = 1; |
| 8963 | |
| 8964 | if (!*args[cur_arg] || |
| 8965 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8966 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8967 | file, linenum, args[0]); |
| 8968 | goto out_err; |
| 8969 | } |
| 8970 | |
| 8971 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8972 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8973 | |
| 8974 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8975 | free(proxy->conf.lfs_file); |
| 8976 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8977 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8978 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8979 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 8980 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 8981 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8982 | struct sample_expr *expr; |
| 8983 | unsigned int where; |
| 8984 | char *err = NULL; |
| 8985 | |
| 8986 | cur_arg = 1; |
| 8987 | proxy->conf.args.ctx = ARGC_TRK; |
| 8988 | |
| 8989 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8990 | if (!expr) { |
| 8991 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8992 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8993 | free(err); |
| 8994 | goto out_err; |
| 8995 | } |
| 8996 | |
| 8997 | where = 0; |
| 8998 | if (proxy->cap & PR_CAP_FE) |
| 8999 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9000 | if (proxy->cap & PR_CAP_BE) |
| 9001 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9002 | |
| 9003 | if (!(expr->fetch->val & where)) { |
| 9004 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9005 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9006 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9007 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9008 | free(expr); |
| 9009 | goto out_err; |
| 9010 | } |
| 9011 | |
| 9012 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9013 | cur_arg++; |
| 9014 | if (!args[cur_arg]) { |
| 9015 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9016 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9017 | free(expr); |
| 9018 | goto out_err; |
| 9019 | } |
| 9020 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9021 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9022 | cur_arg++; |
| 9023 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9024 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9025 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9026 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9027 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9028 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9029 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9030 | 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] | 9031 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9032 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9033 | goto out_err; |
| 9034 | } |
| 9035 | |
| 9036 | /* this redirect rule might already contain a parsed condition which |
| 9037 | * we'll pass to the http-request rule. |
| 9038 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9039 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9040 | rule->arg.redir = redir; |
| 9041 | rule->cond = redir->cond; |
| 9042 | redir->cond = NULL; |
| 9043 | cur_arg = 2; |
| 9044 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9045 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9046 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9047 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9048 | /* |
| 9049 | * '+ 8' for 'add-acl(' |
| 9050 | * '- 9' for 'add-acl(' + trailing ')' |
| 9051 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9052 | 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] | 9053 | |
| 9054 | cur_arg = 1; |
| 9055 | |
| 9056 | if (!*args[cur_arg] || |
| 9057 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9058 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9059 | file, linenum, args[0]); |
| 9060 | goto out_err; |
| 9061 | } |
| 9062 | |
| 9063 | LIST_INIT(&rule->arg.map.key); |
| 9064 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9065 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9066 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9067 | file, linenum); |
| 9068 | free(proxy->conf.lfs_file); |
| 9069 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9070 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9071 | cur_arg += 1; |
| 9072 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9073 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9074 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9075 | /* |
| 9076 | * '+ 8' for 'del-acl(' |
| 9077 | * '- 9' for 'del-acl(' + trailing ')' |
| 9078 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9079 | 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] | 9080 | |
| 9081 | cur_arg = 1; |
| 9082 | |
| 9083 | if (!*args[cur_arg] || |
| 9084 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9085 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9086 | file, linenum, args[0]); |
| 9087 | goto out_err; |
| 9088 | } |
| 9089 | |
| 9090 | LIST_INIT(&rule->arg.map.key); |
| 9091 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9092 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9093 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9094 | file, linenum); |
| 9095 | free(proxy->conf.lfs_file); |
| 9096 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9097 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9098 | cur_arg += 1; |
| 9099 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9100 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9101 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9102 | /* |
| 9103 | * '+ 8' for 'del-map(' |
| 9104 | * '- 9' for 'del-map(' + trailing ')' |
| 9105 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9106 | 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] | 9107 | |
| 9108 | cur_arg = 1; |
| 9109 | |
| 9110 | if (!*args[cur_arg] || |
| 9111 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9112 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9113 | file, linenum, args[0]); |
| 9114 | goto out_err; |
| 9115 | } |
| 9116 | |
| 9117 | LIST_INIT(&rule->arg.map.key); |
| 9118 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9119 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9120 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9121 | file, linenum); |
| 9122 | free(proxy->conf.lfs_file); |
| 9123 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9124 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9125 | cur_arg += 1; |
| 9126 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9127 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9128 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9129 | /* |
| 9130 | * '+ 8' for 'set-map(' |
| 9131 | * '- 9' for 'set-map(' + trailing ')' |
| 9132 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9133 | 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] | 9134 | |
| 9135 | cur_arg = 1; |
| 9136 | |
| 9137 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9138 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9139 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9140 | file, linenum, args[0]); |
| 9141 | goto out_err; |
| 9142 | } |
| 9143 | |
| 9144 | LIST_INIT(&rule->arg.map.key); |
| 9145 | LIST_INIT(&rule->arg.map.value); |
| 9146 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9147 | |
| 9148 | /* key pattern */ |
| 9149 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9150 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9151 | file, linenum); |
| 9152 | |
| 9153 | /* value pattern */ |
| 9154 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9155 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9156 | file, linenum); |
| 9157 | free(proxy->conf.lfs_file); |
| 9158 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9159 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9160 | |
| 9161 | cur_arg += 2; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9162 | } else if (strncmp(args[0], "set-src", 7) == 0) { |
| 9163 | struct sample_expr *expr; |
| 9164 | unsigned int where; |
| 9165 | char *err = NULL; |
| 9166 | |
| 9167 | cur_arg = 1; |
| 9168 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9169 | |
| 9170 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9171 | if (!expr) { |
| 9172 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9173 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9174 | free(err); |
| 9175 | goto out_err; |
| 9176 | } |
| 9177 | |
| 9178 | where = 0; |
| 9179 | if (proxy->cap & PR_CAP_FE) |
| 9180 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9181 | if (proxy->cap & PR_CAP_BE) |
| 9182 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9183 | |
| 9184 | if (!(expr->fetch->val & where)) { |
| 9185 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9186 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9187 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9188 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9189 | free(expr); |
| 9190 | goto out_err; |
| 9191 | } |
| 9192 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 9193 | rule->arg.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9194 | rule->action = ACT_HTTP_REQ_SET_SRC; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9195 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9196 | char *errmsg = NULL; |
| 9197 | cur_arg = 1; |
| 9198 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9199 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9200 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9201 | 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] | 9202 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9203 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9204 | free(errmsg); |
| 9205 | goto out_err; |
| 9206 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9207 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9208 | action_build_list(&http_req_keywords.list, &trash); |
| 9209 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9210 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9211 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', " |
| 9212 | "'set-src'%s%s, but got '%s'%s.\n", |
| 9213 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9214 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9218 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9219 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9220 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9221 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9222 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9223 | file, linenum, args[0], errmsg); |
| 9224 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9225 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9226 | } |
| 9227 | rule->cond = cond; |
| 9228 | } |
| 9229 | else if (*args[cur_arg]) { |
| 9230 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9231 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9232 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9233 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9237 | out_err: |
| 9238 | free(rule); |
| 9239 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9240 | } |
| 9241 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9242 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9243 | 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] | 9244 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9245 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9246 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9247 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9248 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9249 | |
| 9250 | rule = calloc(1, sizeof(*rule)); |
| 9251 | if (!rule) { |
| 9252 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9253 | goto out_err; |
| 9254 | } |
| 9255 | |
| 9256 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9257 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9258 | cur_arg = 1; |
| 9259 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9260 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9261 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9262 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9263 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9264 | cur_arg = 1; |
| 9265 | |
| 9266 | if (!*args[cur_arg] || |
| 9267 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9268 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9269 | file, linenum, args[0]); |
| 9270 | goto out_err; |
| 9271 | } |
| 9272 | rule->arg.nice = atoi(args[cur_arg]); |
| 9273 | if (rule->arg.nice < -1024) |
| 9274 | rule->arg.nice = -1024; |
| 9275 | else if (rule->arg.nice > 1024) |
| 9276 | rule->arg.nice = 1024; |
| 9277 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9278 | } else if (!strcmp(args[0], "set-tos")) { |
| 9279 | #ifdef IP_TOS |
| 9280 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9281 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9282 | cur_arg = 1; |
| 9283 | |
| 9284 | if (!*args[cur_arg] || |
| 9285 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9286 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9287 | file, linenum, args[0]); |
| 9288 | goto out_err; |
| 9289 | } |
| 9290 | |
| 9291 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9292 | if (err && *err != '\0') { |
| 9293 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9294 | file, linenum, err, args[0]); |
| 9295 | goto out_err; |
| 9296 | } |
| 9297 | cur_arg++; |
| 9298 | #else |
| 9299 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9300 | goto out_err; |
| 9301 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9302 | } else if (!strcmp(args[0], "set-mark")) { |
| 9303 | #ifdef SO_MARK |
| 9304 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9305 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9306 | cur_arg = 1; |
| 9307 | |
| 9308 | if (!*args[cur_arg] || |
| 9309 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9310 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9311 | file, linenum, args[0]); |
| 9312 | goto out_err; |
| 9313 | } |
| 9314 | |
| 9315 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9316 | if (err && *err != '\0') { |
| 9317 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9318 | file, linenum, err, args[0]); |
| 9319 | goto out_err; |
| 9320 | } |
| 9321 | cur_arg++; |
| 9322 | global.last_checks |= LSTCHK_NETADM; |
| 9323 | #else |
| 9324 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9325 | goto out_err; |
| 9326 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9327 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9328 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9329 | cur_arg = 1; |
| 9330 | |
| 9331 | if (!*args[cur_arg] || |
| 9332 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9333 | bad_log_level: |
| 9334 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9335 | file, linenum, args[0]); |
| 9336 | goto out_err; |
| 9337 | } |
| 9338 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9339 | rule->arg.loglevel = -1; |
| 9340 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9341 | goto bad_log_level; |
| 9342 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9343 | } 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] | 9344 | 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] | 9345 | cur_arg = 1; |
| 9346 | |
| 9347 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9348 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9349 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9350 | file, linenum, args[0]); |
| 9351 | goto out_err; |
| 9352 | } |
| 9353 | |
| 9354 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9355 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9356 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9357 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9358 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9359 | 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] | 9360 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9361 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9362 | free(proxy->conf.lfs_file); |
| 9363 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9364 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9365 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9366 | } 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] | 9367 | 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] | 9368 | cur_arg = 1; |
| 9369 | |
| 9370 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9371 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9372 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9373 | file, linenum, args[0]); |
| 9374 | goto out_err; |
| 9375 | } |
| 9376 | |
| 9377 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9378 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9379 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9380 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9381 | error = NULL; |
| 9382 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9383 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9384 | args[cur_arg + 1], error); |
| 9385 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9386 | goto out_err; |
| 9387 | } |
| 9388 | |
| 9389 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9390 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9391 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9392 | file, linenum); |
| 9393 | |
| 9394 | free(proxy->conf.lfs_file); |
| 9395 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9396 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9397 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9398 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9399 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9400 | cur_arg = 1; |
| 9401 | |
| 9402 | if (!*args[cur_arg] || |
| 9403 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9404 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9405 | file, linenum, args[0]); |
| 9406 | goto out_err; |
| 9407 | } |
| 9408 | |
| 9409 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9410 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9411 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9412 | proxy->conf.args.ctx = ARGC_HRS; |
| 9413 | free(proxy->conf.lfs_file); |
| 9414 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9415 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9416 | cur_arg += 1; |
| 9417 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9418 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9419 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9420 | /* |
| 9421 | * '+ 8' for 'add-acl(' |
| 9422 | * '- 9' for 'add-acl(' + trailing ')' |
| 9423 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9424 | 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] | 9425 | |
| 9426 | cur_arg = 1; |
| 9427 | |
| 9428 | if (!*args[cur_arg] || |
| 9429 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9430 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9431 | file, linenum, args[0]); |
| 9432 | goto out_err; |
| 9433 | } |
| 9434 | |
| 9435 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9436 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9437 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9438 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9439 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9440 | free(proxy->conf.lfs_file); |
| 9441 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9442 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9443 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9444 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9445 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9446 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9447 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9448 | /* |
| 9449 | * '+ 8' for 'del-acl(' |
| 9450 | * '- 9' for 'del-acl(' + trailing ')' |
| 9451 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9452 | 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] | 9453 | |
| 9454 | cur_arg = 1; |
| 9455 | |
| 9456 | if (!*args[cur_arg] || |
| 9457 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9458 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9459 | file, linenum, args[0]); |
| 9460 | goto out_err; |
| 9461 | } |
| 9462 | |
| 9463 | LIST_INIT(&rule->arg.map.key); |
| 9464 | proxy->conf.args.ctx = ARGC_HRS; |
| 9465 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9466 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9467 | file, linenum); |
| 9468 | free(proxy->conf.lfs_file); |
| 9469 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9470 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9471 | cur_arg += 1; |
| 9472 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9473 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9474 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9475 | /* |
| 9476 | * '+ 8' for 'del-map(' |
| 9477 | * '- 9' for 'del-map(' + trailing ')' |
| 9478 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9479 | 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] | 9480 | |
| 9481 | cur_arg = 1; |
| 9482 | |
| 9483 | if (!*args[cur_arg] || |
| 9484 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9485 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9486 | file, linenum, args[0]); |
| 9487 | goto out_err; |
| 9488 | } |
| 9489 | |
| 9490 | LIST_INIT(&rule->arg.map.key); |
| 9491 | proxy->conf.args.ctx = ARGC_HRS; |
| 9492 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9493 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9494 | file, linenum); |
| 9495 | free(proxy->conf.lfs_file); |
| 9496 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9497 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9498 | cur_arg += 1; |
| 9499 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9500 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9501 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9502 | /* |
| 9503 | * '+ 8' for 'set-map(' |
| 9504 | * '- 9' for 'set-map(' + trailing ')' |
| 9505 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9506 | 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] | 9507 | |
| 9508 | cur_arg = 1; |
| 9509 | |
| 9510 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9511 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9512 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9513 | file, linenum, args[0]); |
| 9514 | goto out_err; |
| 9515 | } |
| 9516 | |
| 9517 | LIST_INIT(&rule->arg.map.key); |
| 9518 | LIST_INIT(&rule->arg.map.value); |
| 9519 | |
| 9520 | proxy->conf.args.ctx = ARGC_HRS; |
| 9521 | |
| 9522 | /* key pattern */ |
| 9523 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9524 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9525 | file, linenum); |
| 9526 | |
| 9527 | /* value pattern */ |
| 9528 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9529 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9530 | file, linenum); |
| 9531 | |
| 9532 | free(proxy->conf.lfs_file); |
| 9533 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9534 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9535 | |
| 9536 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9537 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9538 | struct redirect_rule *redir; |
| 9539 | char *errmsg = NULL; |
| 9540 | |
| 9541 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9542 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9543 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9544 | goto out_err; |
| 9545 | } |
| 9546 | |
| 9547 | /* this redirect rule might already contain a parsed condition which |
| 9548 | * we'll pass to the http-request rule. |
| 9549 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9550 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9551 | rule->arg.redir = redir; |
| 9552 | rule->cond = redir->cond; |
| 9553 | redir->cond = NULL; |
| 9554 | cur_arg = 2; |
| 9555 | return rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9556 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9557 | char *errmsg = NULL; |
| 9558 | cur_arg = 1; |
| 9559 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9560 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9561 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9562 | 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] | 9563 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9564 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9565 | free(errmsg); |
| 9566 | goto out_err; |
| 9567 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9568 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9569 | action_build_list(&http_res_keywords.list, &trash); |
| 9570 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9571 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9572 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', " |
| 9573 | "'set-src'%s%s, but got '%s'%s.\n", |
| 9574 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9575 | goto out_err; |
| 9576 | } |
| 9577 | |
| 9578 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9579 | struct acl_cond *cond; |
| 9580 | char *errmsg = NULL; |
| 9581 | |
| 9582 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9583 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9584 | file, linenum, args[0], errmsg); |
| 9585 | free(errmsg); |
| 9586 | goto out_err; |
| 9587 | } |
| 9588 | rule->cond = cond; |
| 9589 | } |
| 9590 | else if (*args[cur_arg]) { |
| 9591 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9592 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9593 | file, linenum, args[0], args[cur_arg]); |
| 9594 | goto out_err; |
| 9595 | } |
| 9596 | |
| 9597 | return rule; |
| 9598 | out_err: |
| 9599 | free(rule); |
| 9600 | return NULL; |
| 9601 | } |
| 9602 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9603 | /* 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] | 9604 | * 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] | 9605 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9606 | * 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] | 9607 | */ |
| 9608 | 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] | 9609 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9610 | { |
| 9611 | struct redirect_rule *rule; |
| 9612 | int cur_arg; |
| 9613 | int type = REDIRECT_TYPE_NONE; |
| 9614 | int code = 302; |
| 9615 | const char *destination = NULL; |
| 9616 | const char *cookie = NULL; |
| 9617 | int cookie_set = 0; |
| 9618 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9619 | struct acl_cond *cond = NULL; |
| 9620 | |
| 9621 | cur_arg = 0; |
| 9622 | while (*(args[cur_arg])) { |
| 9623 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9624 | if (!*args[cur_arg + 1]) |
| 9625 | goto missing_arg; |
| 9626 | |
| 9627 | type = REDIRECT_TYPE_LOCATION; |
| 9628 | cur_arg++; |
| 9629 | destination = args[cur_arg]; |
| 9630 | } |
| 9631 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9632 | if (!*args[cur_arg + 1]) |
| 9633 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9634 | type = REDIRECT_TYPE_PREFIX; |
| 9635 | cur_arg++; |
| 9636 | destination = args[cur_arg]; |
| 9637 | } |
| 9638 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9639 | if (!*args[cur_arg + 1]) |
| 9640 | goto missing_arg; |
| 9641 | |
| 9642 | type = REDIRECT_TYPE_SCHEME; |
| 9643 | cur_arg++; |
| 9644 | destination = args[cur_arg]; |
| 9645 | } |
| 9646 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9647 | if (!*args[cur_arg + 1]) |
| 9648 | goto missing_arg; |
| 9649 | |
| 9650 | cur_arg++; |
| 9651 | cookie = args[cur_arg]; |
| 9652 | cookie_set = 1; |
| 9653 | } |
| 9654 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9655 | if (!*args[cur_arg + 1]) |
| 9656 | goto missing_arg; |
| 9657 | |
| 9658 | cur_arg++; |
| 9659 | cookie = args[cur_arg]; |
| 9660 | cookie_set = 0; |
| 9661 | } |
| 9662 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9663 | if (!*args[cur_arg + 1]) |
| 9664 | goto missing_arg; |
| 9665 | |
| 9666 | cur_arg++; |
| 9667 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9668 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9669 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9670 | "'%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] | 9671 | args[cur_arg - 1], args[cur_arg]); |
| 9672 | return NULL; |
| 9673 | } |
| 9674 | } |
| 9675 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9676 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9677 | } |
| 9678 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9679 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9680 | } |
| 9681 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9682 | strcmp(args[cur_arg], "unless") == 0) { |
| 9683 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9684 | if (!cond) { |
| 9685 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9686 | return NULL; |
| 9687 | } |
| 9688 | break; |
| 9689 | } |
| 9690 | else { |
| 9691 | memprintf(errmsg, |
| 9692 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9693 | args[cur_arg]); |
| 9694 | return NULL; |
| 9695 | } |
| 9696 | cur_arg++; |
| 9697 | } |
| 9698 | |
| 9699 | if (type == REDIRECT_TYPE_NONE) { |
| 9700 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9701 | return NULL; |
| 9702 | } |
| 9703 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9704 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9705 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9706 | return NULL; |
| 9707 | } |
| 9708 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9709 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9710 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9711 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9712 | |
| 9713 | if (!use_fmt) { |
| 9714 | /* old-style static redirect rule */ |
| 9715 | rule->rdr_str = strdup(destination); |
| 9716 | rule->rdr_len = strlen(destination); |
| 9717 | } |
| 9718 | else { |
| 9719 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9720 | |
| 9721 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9722 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9723 | * makes it hard for the user to configure a self-redirection. |
| 9724 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9725 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9726 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9727 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9728 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 9729 | : (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] | 9730 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9731 | free(curproxy->conf.lfs_file); |
| 9732 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9733 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9734 | } |
| 9735 | } |
| 9736 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9737 | if (cookie) { |
| 9738 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9739 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9740 | */ |
| 9741 | rule->cookie_len = strlen(cookie); |
| 9742 | if (cookie_set) { |
| 9743 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9744 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9745 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9746 | rule->cookie_len += 9; |
| 9747 | } else { |
| 9748 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9749 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9750 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9751 | rule->cookie_len += 20; |
| 9752 | } |
| 9753 | } |
| 9754 | rule->type = type; |
| 9755 | rule->code = code; |
| 9756 | rule->flags = flags; |
| 9757 | LIST_INIT(&rule->list); |
| 9758 | return rule; |
| 9759 | |
| 9760 | missing_arg: |
| 9761 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9762 | return NULL; |
| 9763 | } |
| 9764 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9765 | /************************************************************************/ |
| 9766 | /* The code below is dedicated to ACL parsing and matching */ |
| 9767 | /************************************************************************/ |
| 9768 | |
| 9769 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9770 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9771 | * which means that a request or response is ready. If some data is missing, |
| 9772 | * 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] | 9773 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9774 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9775 | * |
| 9776 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9777 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9778 | * decide whether or not an HTTP message is present ; |
| 9779 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9780 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9781 | * 1 if an HTTP message is ready |
| 9782 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9783 | 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] | 9784 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9785 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9786 | struct http_txn *txn; |
| 9787 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9788 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9789 | /* Note: it is possible that <s> is NULL when called before stream |
| 9790 | * initialization (eg: tcp-request connection), so this function is the |
| 9791 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9792 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9793 | if (!s) |
| 9794 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9795 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9796 | if (!s->txn) { |
| 9797 | if (unlikely(!http_alloc_txn(s))) |
| 9798 | return 0; /* not enough memory */ |
| 9799 | http_init_txn(s); |
| 9800 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9801 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9802 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9803 | |
| 9804 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9805 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9806 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9807 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9808 | /* If the buffer does not leave enough free space at the end, |
| 9809 | * we must first realign it. |
| 9810 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9811 | if (s->req.buf->p > s->req.buf->data && |
| 9812 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9813 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9814 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9815 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9816 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9817 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9818 | |
| 9819 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9820 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9821 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9822 | |
| 9823 | /* Still no valid request ? */ |
| 9824 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9825 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9826 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9827 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9828 | } |
| 9829 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9830 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9831 | return 0; |
| 9832 | } |
| 9833 | |
| 9834 | /* OK we just got a valid HTTP request. We have some minor |
| 9835 | * preparation to perform so that further checks can rely |
| 9836 | * on HTTP tests. |
| 9837 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9838 | |
| 9839 | /* If the request was parsed but was too large, we must absolutely |
| 9840 | * return an error so that it is not processed. At the moment this |
| 9841 | * cannot happen, but if the parsers are to change in the future, |
| 9842 | * we want this check to be maintained. |
| 9843 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9844 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9845 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9846 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9847 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9848 | return 1; |
| 9849 | } |
| 9850 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9851 | 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] | 9852 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9853 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9854 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9855 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9856 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9857 | } |
| 9858 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9859 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9860 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9861 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9862 | |
| 9863 | /* otherwise everything's ready for the request */ |
| 9864 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9865 | else { |
| 9866 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9867 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9868 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9869 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9870 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9871 | } |
| 9872 | |
| 9873 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9874 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9875 | return 1; |
| 9876 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9877 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9878 | /* 1. Check on METHOD |
| 9879 | * We use the pre-parsed method if it is known, and store its number as an |
| 9880 | * integer. If it is unknown, we use the pointer and the length. |
| 9881 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9882 | 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] | 9883 | { |
| 9884 | int len, meth; |
| 9885 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9886 | len = strlen(text); |
| 9887 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9888 | |
| 9889 | pattern->val.i = meth; |
| 9890 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9891 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9892 | pattern->len = len; |
| 9893 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9894 | else { |
| 9895 | pattern->ptr.str = NULL; |
| 9896 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9897 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9898 | return 1; |
| 9899 | } |
| 9900 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9901 | /* This function fetches the method of current HTTP request and stores |
| 9902 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9903 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9904 | * in <len> and <ptr> is NULL ; |
| 9905 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9906 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9907 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9908 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9909 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9910 | 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] | 9911 | { |
| 9912 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9913 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9914 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9915 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9916 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9917 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9918 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9919 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9920 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9921 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9922 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9923 | /* ensure the indexes are not affected */ |
| 9924 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9925 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9926 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9927 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9928 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9929 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9930 | return 1; |
| 9931 | } |
| 9932 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9933 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9934 | 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] | 9935 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9936 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9937 | struct pattern_list *lst; |
| 9938 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9939 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9940 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9941 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9942 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9943 | /* well-known method */ |
| 9944 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9945 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9946 | return pattern; |
| 9947 | else |
| 9948 | continue; |
| 9949 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9950 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9951 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9952 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9953 | continue; |
| 9954 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9955 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9956 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9957 | (!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] | 9958 | return pattern; |
| 9959 | } |
| 9960 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9961 | } |
| 9962 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9963 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9964 | 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] | 9965 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9966 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9967 | char *ptr; |
| 9968 | int len; |
| 9969 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9970 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9971 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9972 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9973 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9974 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9975 | |
| 9976 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9977 | if (len <= 0) |
| 9978 | return 0; |
| 9979 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9980 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9981 | smp->data.u.str.str = ptr; |
| 9982 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9983 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9984 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9985 | return 1; |
| 9986 | } |
| 9987 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9988 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9989 | 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] | 9990 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9991 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9992 | char *ptr; |
| 9993 | int len; |
| 9994 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9995 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9996 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9997 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9998 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9999 | return 0; |
| 10000 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10001 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10002 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10003 | |
| 10004 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10005 | if (len <= 0) |
| 10006 | return 0; |
| 10007 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10008 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10009 | smp->data.u.str.str = ptr; |
| 10010 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10011 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10012 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10013 | return 1; |
| 10014 | } |
| 10015 | |
| 10016 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10017 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10018 | 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] | 10019 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10020 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10021 | char *ptr; |
| 10022 | int len; |
| 10023 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10024 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10025 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10026 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10027 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10028 | return 0; |
| 10029 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10030 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10031 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10032 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10033 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10034 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10035 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10036 | return 1; |
| 10037 | } |
| 10038 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10039 | /* returns the longest available part of the body. This requires that the body |
| 10040 | * has been waited for using http-buffer-request. |
| 10041 | */ |
| 10042 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10043 | 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] | 10044 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10045 | struct http_msg *msg; |
| 10046 | unsigned long len; |
| 10047 | unsigned long block1; |
| 10048 | char *body; |
| 10049 | struct chunk *temp; |
| 10050 | |
| 10051 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10052 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10053 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10054 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10055 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10056 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10057 | |
| 10058 | len = http_body_bytes(msg); |
| 10059 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10060 | |
| 10061 | block1 = len; |
| 10062 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10063 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10064 | |
| 10065 | if (block1 == len) { |
| 10066 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10067 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10068 | smp->data.u.str.str = body; |
| 10069 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10070 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10071 | } |
| 10072 | else { |
| 10073 | /* buffer is wrapped, we need to defragment it */ |
| 10074 | temp = get_trash_chunk(); |
| 10075 | memcpy(temp->str, body, block1); |
| 10076 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10077 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10078 | smp->data.u.str.str = temp->str; |
| 10079 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10080 | smp->flags = SMP_F_VOL_TEST; |
| 10081 | } |
| 10082 | return 1; |
| 10083 | } |
| 10084 | |
| 10085 | |
| 10086 | /* returns the available length of the body. This requires that the body |
| 10087 | * has been waited for using http-buffer-request. |
| 10088 | */ |
| 10089 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10090 | 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] | 10091 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10092 | struct http_msg *msg; |
| 10093 | |
| 10094 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10095 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10096 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10097 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10098 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10099 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10100 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10101 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10102 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10103 | |
| 10104 | smp->flags = SMP_F_VOL_TEST; |
| 10105 | return 1; |
| 10106 | } |
| 10107 | |
| 10108 | |
| 10109 | /* returns the advertised length of the body, or the advertised size of the |
| 10110 | * chunks available in the buffer. This requires that the body has been waited |
| 10111 | * for using http-buffer-request. |
| 10112 | */ |
| 10113 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10114 | 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] | 10115 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10116 | struct http_msg *msg; |
| 10117 | |
| 10118 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10119 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10120 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10121 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10122 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10123 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10124 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10125 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10126 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10127 | |
| 10128 | smp->flags = SMP_F_VOL_TEST; |
| 10129 | return 1; |
| 10130 | } |
| 10131 | |
| 10132 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10133 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10134 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10135 | 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] | 10136 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10137 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10138 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10139 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10140 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10141 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10142 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10143 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10144 | 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] | 10145 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10146 | return 1; |
| 10147 | } |
| 10148 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10149 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10150 | 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] | 10151 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10152 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10153 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10154 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10155 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10156 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10157 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10158 | 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] | 10159 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10160 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10161 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10162 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10163 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10164 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10165 | return 1; |
| 10166 | } |
| 10167 | |
| 10168 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10169 | 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] | 10170 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10171 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10172 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10173 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10174 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10175 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10176 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10177 | 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] | 10178 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10179 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10180 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10181 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10182 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10183 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10184 | return 1; |
| 10185 | } |
| 10186 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10187 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10188 | * Accepts an optional argument of type string containing the header field name, |
| 10189 | * and an optional argument of type signed or unsigned integer to request an |
| 10190 | * 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] | 10191 | * headers are considered from the first one. It does not stop on commas and |
| 10192 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10193 | */ |
| 10194 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10195 | 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] | 10196 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10197 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10198 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10199 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10200 | int occ = 0; |
| 10201 | const char *name_str = NULL; |
| 10202 | int name_len = 0; |
| 10203 | |
| 10204 | if (!ctx) { |
| 10205 | /* first call */ |
| 10206 | ctx = &static_hdr_ctx; |
| 10207 | ctx->idx = 0; |
| 10208 | smp->ctx.a[0] = ctx; |
| 10209 | } |
| 10210 | |
| 10211 | if (args) { |
| 10212 | if (args[0].type != ARGT_STR) |
| 10213 | return 0; |
| 10214 | name_str = args[0].data.str.str; |
| 10215 | name_len = args[0].data.str.len; |
| 10216 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10217 | if (args[1].type == ARGT_SINT) |
| 10218 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10219 | } |
| 10220 | |
| 10221 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10222 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10223 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10224 | 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] | 10225 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10226 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10227 | /* search for header from the beginning */ |
| 10228 | ctx->idx = 0; |
| 10229 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10230 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10231 | /* no explicit occurrence and single fetch => last header by default */ |
| 10232 | occ = -1; |
| 10233 | |
| 10234 | if (!occ) |
| 10235 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10236 | smp->flags |= SMP_F_NOT_LAST; |
| 10237 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10238 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10239 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10240 | 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] | 10241 | return 1; |
| 10242 | |
| 10243 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10244 | return 0; |
| 10245 | } |
| 10246 | |
| 10247 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10248 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10249 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10250 | */ |
| 10251 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10252 | 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] | 10253 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10254 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10255 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10256 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10257 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10258 | const char *name = NULL; |
| 10259 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10260 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10261 | if (args && args->type == ARGT_STR) { |
| 10262 | name = args->data.str.str; |
| 10263 | len = args->data.str.len; |
| 10264 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10265 | |
| 10266 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10267 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10268 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10269 | 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] | 10270 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10271 | ctx.idx = 0; |
| 10272 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10273 | 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] | 10274 | cnt++; |
| 10275 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10276 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10277 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10278 | smp->flags = SMP_F_VOL_HDR; |
| 10279 | return 1; |
| 10280 | } |
| 10281 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10282 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10283 | 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] | 10284 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10285 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10286 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10287 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10288 | struct chunk *temp; |
| 10289 | char del = ','; |
| 10290 | |
| 10291 | if (args && args->type == ARGT_STR) |
| 10292 | del = *args[0].data.str.str; |
| 10293 | |
| 10294 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10295 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10296 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10297 | 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] | 10298 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10299 | temp = get_trash_chunk(); |
| 10300 | |
| 10301 | ctx.idx = 0; |
| 10302 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10303 | if (temp->len) |
| 10304 | temp->str[temp->len++] = del; |
| 10305 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10306 | temp->len += ctx.del; |
| 10307 | } |
| 10308 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10309 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10310 | smp->data.u.str.str = temp->str; |
| 10311 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10312 | smp->flags = SMP_F_VOL_HDR; |
| 10313 | return 1; |
| 10314 | } |
| 10315 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +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 | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10320 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10321 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10322 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10323 | 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] | 10324 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10325 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10326 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10327 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10328 | int occ = 0; |
| 10329 | const char *name_str = NULL; |
| 10330 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10331 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10332 | if (!ctx) { |
| 10333 | /* first call */ |
| 10334 | ctx = &static_hdr_ctx; |
| 10335 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10336 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10337 | } |
| 10338 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10339 | if (args) { |
| 10340 | if (args[0].type != ARGT_STR) |
| 10341 | return 0; |
| 10342 | name_str = args[0].data.str.str; |
| 10343 | name_len = args[0].data.str.len; |
| 10344 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10345 | if (args[1].type == ARGT_SINT) |
| 10346 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10347 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10348 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10349 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10350 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10351 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10352 | 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] | 10353 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10354 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10355 | /* search for header from the beginning */ |
| 10356 | ctx->idx = 0; |
| 10357 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10358 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10359 | /* no explicit occurrence and single fetch => last header by default */ |
| 10360 | occ = -1; |
| 10361 | |
| 10362 | if (!occ) |
| 10363 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10364 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10365 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10366 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10367 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10368 | 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] | 10369 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10370 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10371 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10372 | return 0; |
| 10373 | } |
| 10374 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10375 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10376 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10377 | */ |
| 10378 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10379 | 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] | 10380 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10381 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10382 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10383 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10384 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10385 | const char *name = NULL; |
| 10386 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10387 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10388 | if (args && args->type == ARGT_STR) { |
| 10389 | name = args->data.str.str; |
| 10390 | len = args->data.str.len; |
| 10391 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10392 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10393 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10394 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10395 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10396 | 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] | 10397 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10398 | ctx.idx = 0; |
| 10399 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10400 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10401 | cnt++; |
| 10402 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10403 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10404 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10405 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10406 | return 1; |
| 10407 | } |
| 10408 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10409 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10410 | * takes a mandatory argument of type string and an optional one of type int |
| 10411 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10412 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10413 | */ |
| 10414 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10415 | 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] | 10416 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10417 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10418 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10419 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10420 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10421 | 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] | 10422 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10423 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10424 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10425 | } |
| 10426 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10427 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10428 | * and an optional one of type int to designate a specific occurrence. |
| 10429 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10430 | */ |
| 10431 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10432 | 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] | 10433 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10434 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10435 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10436 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10437 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10438 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10439 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10440 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10441 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10442 | if (smp->data.u.str.len < temp->size - 1) { |
| 10443 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10444 | temp->str[smp->data.u.str.len] = '\0'; |
| 10445 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10446 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10447 | break; |
| 10448 | } |
| 10449 | } |
| 10450 | } |
| 10451 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10452 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10453 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10454 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10455 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10456 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10457 | } |
| 10458 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10459 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10460 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10461 | */ |
| 10462 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10463 | 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] | 10464 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10465 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10466 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10467 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10468 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10469 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10470 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10471 | 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] | 10472 | ptr = http_get_path(txn); |
| 10473 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10474 | return 0; |
| 10475 | |
| 10476 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10477 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10478 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10479 | |
| 10480 | while (ptr < end && *ptr != '?') |
| 10481 | ptr++; |
| 10482 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10483 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10484 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10485 | return 1; |
| 10486 | } |
| 10487 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10488 | /* This produces a concatenation of the first occurrence of the Host header |
| 10489 | * followed by the path component if it begins with a slash ('/'). This means |
| 10490 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10491 | * If no Host header is found, then the path is returned as-is. The returned |
| 10492 | * 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] | 10493 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10494 | */ |
| 10495 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10496 | 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] | 10497 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10498 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10499 | char *ptr, *end, *beg; |
| 10500 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10501 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10502 | |
| 10503 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10504 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10505 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10506 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10507 | 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] | 10508 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10509 | |
| 10510 | /* 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] | 10511 | temp = get_trash_chunk(); |
| 10512 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10513 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10514 | smp->data.u.str.str = temp->str; |
| 10515 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10516 | |
| 10517 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10518 | 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] | 10519 | beg = http_get_path(txn); |
| 10520 | if (!beg) |
| 10521 | beg = end; |
| 10522 | |
| 10523 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10524 | |
| 10525 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10526 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10527 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10528 | } |
| 10529 | |
| 10530 | smp->flags = SMP_F_VOL_1ST; |
| 10531 | return 1; |
| 10532 | } |
| 10533 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10534 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10535 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10536 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10537 | * 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] | 10538 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10539 | * 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] | 10540 | * high-traffic sites without having to store whole paths. |
| 10541 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10542 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10543 | 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] | 10544 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10545 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10546 | struct hdr_ctx ctx; |
| 10547 | unsigned int hash = 0; |
| 10548 | char *ptr, *beg, *end; |
| 10549 | int len; |
| 10550 | |
| 10551 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10552 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10553 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10554 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10555 | 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] | 10556 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10557 | ptr = ctx.line + ctx.val; |
| 10558 | len = ctx.vlen; |
| 10559 | while (len--) |
| 10560 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10561 | } |
| 10562 | |
| 10563 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10564 | 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] | 10565 | beg = http_get_path(txn); |
| 10566 | if (!beg) |
| 10567 | beg = end; |
| 10568 | |
| 10569 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10570 | |
| 10571 | if (beg < ptr && *beg == '/') { |
| 10572 | while (beg < ptr) |
| 10573 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10574 | } |
| 10575 | hash = full_hash(hash); |
| 10576 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10577 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10578 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10579 | smp->flags = SMP_F_VOL_1ST; |
| 10580 | return 1; |
| 10581 | } |
| 10582 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10583 | /* 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] | 10584 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10585 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10586 | * 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] | 10587 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10588 | * 8 bytes would still work. |
| 10589 | */ |
| 10590 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10591 | 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] | 10592 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10593 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10594 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10595 | |
| 10596 | if (!cli_conn) |
| 10597 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10598 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10599 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10600 | return 0; |
| 10601 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10602 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10603 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10604 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10605 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10606 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10607 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10608 | 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] | 10609 | temp->len += 4; |
| 10610 | break; |
| 10611 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10612 | 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] | 10613 | temp->len += 16; |
| 10614 | break; |
| 10615 | default: |
| 10616 | return 0; |
| 10617 | } |
| 10618 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10619 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10620 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10621 | return 1; |
| 10622 | } |
| 10623 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10624 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10625 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10626 | * of type string carrying the whole query string. |
| 10627 | */ |
| 10628 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10629 | 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] | 10630 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10631 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10632 | char *ptr, *end; |
| 10633 | |
| 10634 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10635 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10636 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10637 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10638 | end = ptr + txn->req.sl.rq.u_l; |
| 10639 | |
| 10640 | /* look up the '?' */ |
| 10641 | do { |
| 10642 | if (ptr == end) |
| 10643 | return 0; |
| 10644 | } while (*ptr++ != '?'); |
| 10645 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10646 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10647 | smp->data.u.str.str = ptr; |
| 10648 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10649 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10650 | return 1; |
| 10651 | } |
| 10652 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10653 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10654 | 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] | 10655 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10656 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10657 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10658 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10659 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10660 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10661 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10662 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10663 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10664 | return 1; |
| 10665 | } |
| 10666 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10667 | /* return a valid test if the current request is the first one on the connection */ |
| 10668 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10669 | 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] | 10670 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10671 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10672 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10673 | return 1; |
| 10674 | } |
| 10675 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10676 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10677 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10678 | 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] | 10679 | { |
| 10680 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10681 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10682 | return 0; |
| 10683 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10684 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10685 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10686 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10687 | return 0; |
| 10688 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10689 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10690 | 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] | 10691 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10692 | return 1; |
| 10693 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10694 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10695 | /* Accepts exactly 1 argument of type userlist */ |
| 10696 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10697 | 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] | 10698 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10699 | if (!args || args->type != ARGT_USR) |
| 10700 | return 0; |
| 10701 | |
| 10702 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10703 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10704 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10705 | return 0; |
| 10706 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10707 | /* if the user does not belong to the userlist or has a wrong password, |
| 10708 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10709 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10710 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10711 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10712 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10713 | return 0; |
| 10714 | |
| 10715 | /* pat_match_auth() will need the user list */ |
| 10716 | smp->ctx.a[0] = args->data.usr; |
| 10717 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10718 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10719 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10720 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10721 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10722 | |
| 10723 | return 1; |
| 10724 | } |
| 10725 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10726 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10727 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10728 | * the cookie value is returned into *value and *value_l, and the function |
| 10729 | * returns a pointer to the next pointer to search from if the value was found. |
| 10730 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10731 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10732 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10733 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10734 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10735 | * list is expected. |
| 10736 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10737 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10738 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10739 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10740 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10741 | { |
| 10742 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10743 | char *next; |
| 10744 | |
| 10745 | /* we search at least a cookie name followed by an equal, and more |
| 10746 | * generally something like this : |
| 10747 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10748 | */ |
| 10749 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10750 | /* Iterate through all cookies on this line */ |
| 10751 | |
| 10752 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10753 | att_beg++; |
| 10754 | |
| 10755 | /* find att_end : this is the first character after the last non |
| 10756 | * space before the equal. It may be equal to hdr_end. |
| 10757 | */ |
| 10758 | equal = att_end = att_beg; |
| 10759 | |
| 10760 | while (equal < hdr_end) { |
| 10761 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10762 | break; |
| 10763 | if (http_is_spht[(unsigned char)*equal++]) |
| 10764 | continue; |
| 10765 | att_end = equal; |
| 10766 | } |
| 10767 | |
| 10768 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10769 | * is between <att_beg> and <equal>, both may be identical. |
| 10770 | */ |
| 10771 | |
| 10772 | /* look for end of cookie if there is an equal sign */ |
| 10773 | if (equal < hdr_end && *equal == '=') { |
| 10774 | /* look for the beginning of the value */ |
| 10775 | val_beg = equal + 1; |
| 10776 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10777 | val_beg++; |
| 10778 | |
| 10779 | /* find the end of the value, respecting quotes */ |
| 10780 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10781 | |
| 10782 | /* make val_end point to the first white space or delimitor after the value */ |
| 10783 | val_end = next; |
| 10784 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10785 | val_end--; |
| 10786 | } else { |
| 10787 | val_beg = val_end = next = equal; |
| 10788 | } |
| 10789 | |
| 10790 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10791 | * they will automatically be removed if a header before them is removed, |
| 10792 | * since they're supposed to be linked together. |
| 10793 | */ |
| 10794 | if (*att_beg == '$') |
| 10795 | continue; |
| 10796 | |
| 10797 | /* Ignore cookies with no equal sign */ |
| 10798 | if (equal == next) |
| 10799 | continue; |
| 10800 | |
| 10801 | /* Now we have the cookie name between att_beg and att_end, and |
| 10802 | * its value between val_beg and val_end. |
| 10803 | */ |
| 10804 | |
| 10805 | if (att_end - att_beg == cookie_name_l && |
| 10806 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10807 | /* let's return this value and indicate where to go on from */ |
| 10808 | *value = val_beg; |
| 10809 | *value_l = val_end - val_beg; |
| 10810 | return next + 1; |
| 10811 | } |
| 10812 | |
| 10813 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10814 | if (!list) |
| 10815 | break; |
| 10816 | } |
| 10817 | |
| 10818 | return NULL; |
| 10819 | } |
| 10820 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10821 | /* Fetch a captured HTTP request header. The index is the position of |
| 10822 | * the "capture" option in the configuration file |
| 10823 | */ |
| 10824 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10825 | 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] | 10826 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10827 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10828 | int idx; |
| 10829 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10830 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10831 | return 0; |
| 10832 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10833 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10834 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10835 | 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] | 10836 | return 0; |
| 10837 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10838 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10839 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10840 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10841 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10842 | |
| 10843 | return 1; |
| 10844 | } |
| 10845 | |
| 10846 | /* Fetch a captured HTTP response header. The index is the position of |
| 10847 | * the "capture" option in the configuration file |
| 10848 | */ |
| 10849 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10850 | 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] | 10851 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10852 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10853 | int idx; |
| 10854 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10855 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10856 | return 0; |
| 10857 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10858 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10859 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10860 | 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] | 10861 | return 0; |
| 10862 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10863 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10864 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10865 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10866 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10867 | |
| 10868 | return 1; |
| 10869 | } |
| 10870 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10871 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10872 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10873 | 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] | 10874 | { |
| 10875 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10876 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10877 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10878 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10879 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10880 | return 0; |
| 10881 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10882 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10883 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10884 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10885 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10886 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10887 | temp = get_trash_chunk(); |
| 10888 | temp->str = txn->uri; |
| 10889 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10890 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10891 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10892 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10893 | |
| 10894 | return 1; |
| 10895 | |
| 10896 | } |
| 10897 | |
| 10898 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10899 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10900 | 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] | 10901 | { |
| 10902 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10903 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10904 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10905 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10906 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10907 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10908 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10909 | ptr = txn->uri; |
| 10910 | |
| 10911 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10912 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10913 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10914 | if (!*ptr) |
| 10915 | return 0; |
| 10916 | |
| 10917 | ptr++; /* skip the space */ |
| 10918 | |
| 10919 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10920 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10921 | if (!ptr) |
| 10922 | return 0; |
| 10923 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10924 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10925 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10926 | smp->data.u.str = *temp; |
| 10927 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10928 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10929 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10930 | |
| 10931 | return 1; |
| 10932 | } |
| 10933 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10934 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10935 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10936 | */ |
| 10937 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10938 | 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] | 10939 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10940 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10941 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10942 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10943 | return 0; |
| 10944 | |
| 10945 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10946 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10947 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10948 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10949 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10950 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10951 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10952 | smp->flags = SMP_F_CONST; |
| 10953 | return 1; |
| 10954 | |
| 10955 | } |
| 10956 | |
| 10957 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10958 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10959 | */ |
| 10960 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10961 | 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] | 10962 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10963 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10964 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10965 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10966 | return 0; |
| 10967 | |
| 10968 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10969 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10970 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10971 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10972 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10973 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10974 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10975 | smp->flags = SMP_F_CONST; |
| 10976 | return 1; |
| 10977 | |
| 10978 | } |
| 10979 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10980 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10981 | /* 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] | 10982 | * 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] | 10983 | * 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] | 10984 | * 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] | 10985 | * 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] | 10986 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10987 | * 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] | 10988 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10989 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10990 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10991 | 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] | 10992 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10993 | struct http_txn *txn; |
| 10994 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10995 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10996 | const struct http_msg *msg; |
| 10997 | const char *hdr_name; |
| 10998 | int hdr_name_len; |
| 10999 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11000 | int occ = 0; |
| 11001 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11002 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11003 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11004 | return 0; |
| 11005 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11006 | if (!ctx) { |
| 11007 | /* first call */ |
| 11008 | ctx = &static_hdr_ctx; |
| 11009 | ctx->idx = 0; |
| 11010 | smp->ctx.a[2] = ctx; |
| 11011 | } |
| 11012 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11013 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11014 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11015 | txn = smp->strm->txn; |
| 11016 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11017 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11018 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11019 | msg = &txn->req; |
| 11020 | hdr_name = "Cookie"; |
| 11021 | hdr_name_len = 6; |
| 11022 | } else { |
| 11023 | msg = &txn->rsp; |
| 11024 | hdr_name = "Set-Cookie"; |
| 11025 | hdr_name_len = 10; |
| 11026 | } |
| 11027 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11028 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11029 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11030 | occ = -1; |
| 11031 | |
| 11032 | /* OK so basically here, either we want only one value and it's the |
| 11033 | * last one, or we want to iterate over all of them and we fetch the |
| 11034 | * next one. |
| 11035 | */ |
| 11036 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11037 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11038 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11039 | /* search for the header from the beginning, we must first initialize |
| 11040 | * the search parameters. |
| 11041 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11042 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11043 | ctx->idx = 0; |
| 11044 | } |
| 11045 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11046 | smp->flags |= SMP_F_VOL_HDR; |
| 11047 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11048 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11049 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11050 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11051 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11052 | goto out; |
| 11053 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11054 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11055 | continue; |
| 11056 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11057 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11058 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11059 | } |
| 11060 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11061 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11062 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11063 | 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] | 11064 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11065 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11066 | &smp->data.u.str.str, |
| 11067 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11068 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11069 | found = 1; |
| 11070 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11071 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11072 | smp->flags |= SMP_F_NOT_LAST; |
| 11073 | return 1; |
| 11074 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11075 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11076 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11077 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11078 | /* all cookie headers and values were scanned. If we're looking for the |
| 11079 | * last occurrence, we may return it now. |
| 11080 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11081 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11082 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11083 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11084 | } |
| 11085 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11086 | /* 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] | 11087 | * 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] | 11088 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11089 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11090 | */ |
| 11091 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11092 | 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] | 11093 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11094 | struct http_txn *txn; |
| 11095 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11096 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11097 | const struct http_msg *msg; |
| 11098 | const char *hdr_name; |
| 11099 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11100 | int cnt; |
| 11101 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11102 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11103 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11104 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11105 | return 0; |
| 11106 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11107 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11108 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11109 | txn = smp->strm->txn; |
| 11110 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11111 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11112 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11113 | msg = &txn->req; |
| 11114 | hdr_name = "Cookie"; |
| 11115 | hdr_name_len = 6; |
| 11116 | } else { |
| 11117 | msg = &txn->rsp; |
| 11118 | hdr_name = "Set-Cookie"; |
| 11119 | hdr_name_len = 10; |
| 11120 | } |
| 11121 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11122 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11123 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11124 | ctx.idx = 0; |
| 11125 | cnt = 0; |
| 11126 | |
| 11127 | while (1) { |
| 11128 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11129 | if (!val_beg) { |
| 11130 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11131 | break; |
| 11132 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11133 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11134 | continue; |
| 11135 | |
| 11136 | val_beg = ctx.line + ctx.val; |
| 11137 | val_end = val_beg + ctx.vlen; |
| 11138 | } |
| 11139 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11140 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11141 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11142 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11143 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11144 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11145 | &smp->data.u.str.str, |
| 11146 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11147 | cnt++; |
| 11148 | } |
| 11149 | } |
| 11150 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11151 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11152 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11153 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11154 | return 1; |
| 11155 | } |
| 11156 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11157 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11158 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11159 | */ |
| 11160 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11161 | 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] | 11162 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11163 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11164 | |
| 11165 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11166 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11167 | 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] | 11168 | } |
| 11169 | |
| 11170 | return ret; |
| 11171 | } |
| 11172 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11173 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11174 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11175 | /************************************************************************/ |
| 11176 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11177 | /* |
| 11178 | * Given a path string and its length, find the position of beginning of the |
| 11179 | * query string. Returns NULL if no query string is found in the path. |
| 11180 | * |
| 11181 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11182 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11183 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11184 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11185 | 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] | 11186 | { |
| 11187 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11188 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11189 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11190 | return p ? p + 1 : NULL; |
| 11191 | } |
| 11192 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11193 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11194 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11195 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11196 | } |
| 11197 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11198 | /* after increasing a pointer value, it can exceed the first buffer |
| 11199 | * size. This function transform the value of <ptr> according with |
| 11200 | * the expected position. <chunks> is an array of the one or two |
| 11201 | * avalaible chunks. The first value is the start of the first chunk, |
| 11202 | * the second value if the end+1 of the first chunks. The third value |
| 11203 | * is NULL or the start of the second chunk and the fourth value is |
| 11204 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11205 | * wrap, else returns 0. |
| 11206 | */ |
| 11207 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11208 | { |
| 11209 | if (*ptr < chunks[1]) |
| 11210 | return 0; |
| 11211 | if (!chunks[2]) |
| 11212 | return 0; |
| 11213 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11214 | return 1; |
| 11215 | } |
| 11216 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11217 | /* |
| 11218 | * Given a url parameter, find the starting position of the first occurence, |
| 11219 | * or NULL if the parameter is not found. |
| 11220 | * |
| 11221 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11222 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11223 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11224 | * 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] | 11225 | * or the second chunk. The caller must be check the position before using the |
| 11226 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11227 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11228 | static const char * |
| 11229 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11230 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11231 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11232 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11233 | const char *pos, *last, *equal; |
| 11234 | const char **bufs = chunks; |
| 11235 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11236 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11237 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11238 | pos = bufs[0]; |
| 11239 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11240 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11241 | /* Check the equal. */ |
| 11242 | equal = pos + url_param_name_l; |
| 11243 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11244 | if (equal >= chunks[3]) |
| 11245 | return NULL; |
| 11246 | } else { |
| 11247 | if (equal >= chunks[1]) |
| 11248 | return NULL; |
| 11249 | } |
| 11250 | if (*equal == '=') { |
| 11251 | if (pos + url_param_name_l > last) { |
| 11252 | /* process wrap case, we detect a wrap. In this case, the |
| 11253 | * comparison is performed in two parts. |
| 11254 | */ |
| 11255 | |
| 11256 | /* This is the end, we dont have any other chunk. */ |
| 11257 | if (bufs != chunks || !bufs[2]) |
| 11258 | return NULL; |
| 11259 | |
| 11260 | /* Compute the length of each part of the comparison. */ |
| 11261 | l1 = last - pos; |
| 11262 | l2 = url_param_name_l - l1; |
| 11263 | |
| 11264 | /* The second buffer is too short to contain the compared string. */ |
| 11265 | if (bufs[2] + l2 > bufs[3]) |
| 11266 | return NULL; |
| 11267 | |
| 11268 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11269 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11270 | return pos; |
| 11271 | |
| 11272 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11273 | bufs += 2; |
| 11274 | pos = bufs[0] + l2; |
| 11275 | last = bufs[1]; |
| 11276 | |
| 11277 | } else { |
| 11278 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11279 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11280 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11281 | pos += url_param_name_l + 1; |
| 11282 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11283 | last = bufs[2]; |
| 11284 | } |
| 11285 | } |
| 11286 | |
| 11287 | while (1) { |
| 11288 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11289 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11290 | pos++; |
| 11291 | if (pos < last) |
| 11292 | break; |
| 11293 | /* process buffer wrapping. */ |
| 11294 | if (bufs != chunks || !bufs[2]) |
| 11295 | return NULL; |
| 11296 | bufs += 2; |
| 11297 | pos = bufs[0]; |
| 11298 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11299 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11300 | pos++; |
| 11301 | } |
| 11302 | return NULL; |
| 11303 | } |
| 11304 | |
| 11305 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11306 | * Given a url parameter name and a query string, find the next value. |
| 11307 | * An empty url_param_name matches the first available parameter. |
| 11308 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11309 | * respectively provide a pointer to the value and its end. |
| 11310 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11311 | */ |
| 11312 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11313 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11314 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11315 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11316 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11317 | const char *arg_start, *qs_end; |
| 11318 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11319 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11320 | arg_start = chunks[0]; |
| 11321 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11322 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11323 | /* Looks for an argument name. */ |
| 11324 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11325 | url_param_name, url_param_name_l, |
| 11326 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11327 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11328 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11329 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11330 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11331 | if (!arg_start) |
| 11332 | return 0; |
| 11333 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11334 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11335 | while (1) { |
| 11336 | /* looks for the first argument. */ |
| 11337 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11338 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11339 | /* Check for wrapping. */ |
| 11340 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11341 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11342 | chunks[2]) { |
| 11343 | arg_start = chunks[2]; |
| 11344 | qs_end = chunks[3]; |
| 11345 | continue; |
| 11346 | } |
| 11347 | return 0; |
| 11348 | } |
| 11349 | break; |
| 11350 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11351 | value_start++; |
| 11352 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11353 | else { |
| 11354 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11355 | value_start = arg_start + url_param_name_l + 1; |
| 11356 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11357 | /* Check for pointer wrapping. */ |
| 11358 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11359 | /* Update the end pointer. */ |
| 11360 | qs_end = chunks[3]; |
| 11361 | |
| 11362 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11363 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11364 | return 0; |
| 11365 | } |
| 11366 | } |
| 11367 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11368 | value_end = value_start; |
| 11369 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11370 | while (1) { |
| 11371 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11372 | value_end++; |
| 11373 | if (value_end < qs_end) |
| 11374 | break; |
| 11375 | /* process buffer wrapping. */ |
| 11376 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11377 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11378 | chunks[2]) { |
| 11379 | value_end = chunks[2]; |
| 11380 | qs_end = chunks[3]; |
| 11381 | continue; |
| 11382 | } |
| 11383 | break; |
| 11384 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11385 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11386 | *vstart = value_start; |
| 11387 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11388 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11389 | } |
| 11390 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11391 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11392 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11393 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11394 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11395 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11396 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11397 | 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] | 11398 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11399 | const char *vstart, *vend; |
| 11400 | struct chunk *temp; |
| 11401 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11402 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11403 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11404 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11405 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11406 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11407 | return 0; |
| 11408 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11409 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11410 | * if the value is wrapped, copy-it in a buffer. |
| 11411 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11412 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11413 | if (chunks[2] && |
| 11414 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11415 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11416 | /* Wrapped case. */ |
| 11417 | temp = get_trash_chunk(); |
| 11418 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11419 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11420 | smp->data.u.str.str = temp->str; |
| 11421 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11422 | } else { |
| 11423 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11424 | smp->data.u.str.str = (char *)vstart; |
| 11425 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11426 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11427 | } |
| 11428 | |
| 11429 | /* Update context, check wrapping. */ |
| 11430 | chunks[0] = vend; |
| 11431 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11432 | chunks[1] = chunks[3]; |
| 11433 | chunks[2] = NULL; |
| 11434 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11435 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11436 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11437 | smp->flags |= SMP_F_NOT_LAST; |
| 11438 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11439 | return 1; |
| 11440 | } |
| 11441 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11442 | /* This function iterates over each parameter of the query string. It uses |
| 11443 | * 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] | 11444 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11445 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11446 | * considered. It supports an optional delimiter argument for the beginning of |
| 11447 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11448 | */ |
| 11449 | static int |
| 11450 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11451 | { |
| 11452 | struct http_msg *msg; |
| 11453 | char delim = '?'; |
| 11454 | const char *name; |
| 11455 | int name_len; |
| 11456 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11457 | if (!args || |
| 11458 | (args[0].type && args[0].type != ARGT_STR) || |
| 11459 | (args[1].type && args[1].type != ARGT_STR)) |
| 11460 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11461 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11462 | name = ""; |
| 11463 | name_len = 0; |
| 11464 | if (args->type == ARGT_STR) { |
| 11465 | name = args->data.str.str; |
| 11466 | name_len = args->data.str.len; |
| 11467 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11468 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11469 | if (args[1].type) |
| 11470 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11471 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11472 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11473 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11474 | |
| 11475 | msg = &smp->strm->txn->req; |
| 11476 | |
| 11477 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11478 | msg->sl.rq.u_l, delim); |
| 11479 | if (!smp->ctx.a[0]) |
| 11480 | return 0; |
| 11481 | |
| 11482 | 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] | 11483 | |
| 11484 | /* Assume that the context is filled with NULL pointer |
| 11485 | * before the first call. |
| 11486 | * smp->ctx.a[2] = NULL; |
| 11487 | * smp->ctx.a[3] = NULL; |
| 11488 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11489 | } |
| 11490 | |
| 11491 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11492 | } |
| 11493 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11494 | /* This function iterates over each parameter of the body. This requires |
| 11495 | * 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] | 11496 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11497 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11498 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11499 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11500 | */ |
| 11501 | static int |
| 11502 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11503 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11504 | struct http_msg *msg; |
| 11505 | unsigned long len; |
| 11506 | unsigned long block1; |
| 11507 | char *body; |
| 11508 | const char *name; |
| 11509 | int name_len; |
| 11510 | |
| 11511 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11512 | return 0; |
| 11513 | |
| 11514 | name = ""; |
| 11515 | name_len = 0; |
| 11516 | if (args[0].type == ARGT_STR) { |
| 11517 | name = args[0].data.str.str; |
| 11518 | name_len = args[0].data.str.len; |
| 11519 | } |
| 11520 | |
| 11521 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11522 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11523 | |
| 11524 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11525 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11526 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11527 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11528 | |
| 11529 | len = http_body_bytes(msg); |
| 11530 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11531 | |
| 11532 | block1 = len; |
| 11533 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11534 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11535 | |
| 11536 | if (block1 == len) { |
| 11537 | /* buffer is not wrapped (or empty) */ |
| 11538 | smp->ctx.a[0] = body; |
| 11539 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11540 | |
| 11541 | /* Assume that the context is filled with NULL pointer |
| 11542 | * before the first call. |
| 11543 | * smp->ctx.a[2] = NULL; |
| 11544 | * smp->ctx.a[3] = NULL; |
| 11545 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11546 | } |
| 11547 | else { |
| 11548 | /* buffer is wrapped, we need to defragment it */ |
| 11549 | smp->ctx.a[0] = body; |
| 11550 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11551 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11552 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11553 | } |
| 11554 | } |
| 11555 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11556 | } |
| 11557 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11558 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11559 | * above). |
| 11560 | */ |
| 11561 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11562 | 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] | 11563 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11564 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11565 | |
| 11566 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11567 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11568 | 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] | 11569 | } |
| 11570 | |
| 11571 | return ret; |
| 11572 | } |
| 11573 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11574 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11575 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11576 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11577 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11578 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11579 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11580 | * high-traffic sites without having to store whole paths. |
| 11581 | * this differs from the base32 functions in that it includes the url parameters |
| 11582 | * as well as the path |
| 11583 | */ |
| 11584 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11585 | 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] | 11586 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11587 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11588 | struct hdr_ctx ctx; |
| 11589 | unsigned int hash = 0; |
| 11590 | char *ptr, *beg, *end; |
| 11591 | int len; |
| 11592 | |
| 11593 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11594 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11595 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11596 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11597 | 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] | 11598 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11599 | ptr = ctx.line + ctx.val; |
| 11600 | len = ctx.vlen; |
| 11601 | while (len--) |
| 11602 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11603 | } |
| 11604 | |
| 11605 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11606 | 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] | 11607 | beg = http_get_path(txn); |
| 11608 | if (!beg) |
| 11609 | beg = end; |
| 11610 | |
| 11611 | for (ptr = beg; ptr < end ; ptr++); |
| 11612 | |
| 11613 | if (beg < ptr && *beg == '/') { |
| 11614 | while (beg < ptr) |
| 11615 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11616 | } |
| 11617 | hash = full_hash(hash); |
| 11618 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11619 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11620 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11621 | smp->flags = SMP_F_VOL_1ST; |
| 11622 | return 1; |
| 11623 | } |
| 11624 | |
| 11625 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11626 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11627 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11628 | * on the source address length. The URL hash is stored before the address so |
| 11629 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11630 | * 8 bytes would still work. |
| 11631 | */ |
| 11632 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11633 | 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] | 11634 | { |
| 11635 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11636 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11637 | unsigned int hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11638 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11639 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11640 | return 0; |
| 11641 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11642 | /* The returned hash is a 32 bytes integer. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11643 | hash = smp->data.u.sint; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11644 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11645 | temp = get_trash_chunk(); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11646 | memcpy(temp->str + temp->len, &hash, sizeof(hash)); |
| 11647 | temp->len += sizeof(hash); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11648 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11649 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11650 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11651 | 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] | 11652 | temp->len += 4; |
| 11653 | break; |
| 11654 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11655 | 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] | 11656 | temp->len += 16; |
| 11657 | break; |
| 11658 | default: |
| 11659 | return 0; |
| 11660 | } |
| 11661 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11662 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11663 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11664 | return 1; |
| 11665 | } |
| 11666 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11667 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11668 | * keyword. These keywords support an optional positive or negative occurrence |
| 11669 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11670 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11671 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11672 | * error message in case of error, that the caller is responsible for freeing. |
| 11673 | * The initial location must either be freeable or NULL. |
| 11674 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11675 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11676 | { |
| 11677 | 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] | 11678 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11679 | return 0; |
| 11680 | } |
| 11681 | return 1; |
| 11682 | } |
| 11683 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11684 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11685 | * adds an optional offset found in args[0] and emits a string representing |
| 11686 | * the date in RFC-1123/5322 format. |
| 11687 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11688 | 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] | 11689 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11690 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11691 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11692 | struct chunk *temp; |
| 11693 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11694 | /* 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] | 11695 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11696 | |
| 11697 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11698 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11699 | curr_date += args[0].data.sint; |
| 11700 | |
| 11701 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11702 | if (!tm) |
| 11703 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11704 | |
| 11705 | temp = get_trash_chunk(); |
| 11706 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11707 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11708 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11709 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11710 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11711 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11712 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11713 | return 1; |
| 11714 | } |
| 11715 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11716 | /* Match language range with language tag. RFC2616 14.4: |
| 11717 | * |
| 11718 | * A language-range matches a language-tag if it exactly equals |
| 11719 | * the tag, or if it exactly equals a prefix of the tag such |
| 11720 | * that the first tag character following the prefix is "-". |
| 11721 | * |
| 11722 | * Return 1 if the strings match, else return 0. |
| 11723 | */ |
| 11724 | static inline int language_range_match(const char *range, int range_len, |
| 11725 | const char *tag, int tag_len) |
| 11726 | { |
| 11727 | const char *end = range + range_len; |
| 11728 | const char *tend = tag + tag_len; |
| 11729 | while (range < end) { |
| 11730 | if (*range == '-' && tag == tend) |
| 11731 | return 1; |
| 11732 | if (*range != *tag || tag == tend) |
| 11733 | return 0; |
| 11734 | range++; |
| 11735 | tag++; |
| 11736 | } |
| 11737 | /* Return true only if the last char of the tag is matched. */ |
| 11738 | return tag == tend; |
| 11739 | } |
| 11740 | |
| 11741 | /* 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] | 11742 | 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] | 11743 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11744 | const char *al = smp->data.u.str.str; |
| 11745 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11746 | const char *token; |
| 11747 | int toklen; |
| 11748 | int qvalue; |
| 11749 | const char *str; |
| 11750 | const char *w; |
| 11751 | int best_q = 0; |
| 11752 | |
| 11753 | /* Set the constant to the sample, because the output of the |
| 11754 | * function will be peek in the constant configuration string. |
| 11755 | */ |
| 11756 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11757 | smp->data.u.str.size = 0; |
| 11758 | smp->data.u.str.str = ""; |
| 11759 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11760 | |
| 11761 | /* Parse the accept language */ |
| 11762 | while (1) { |
| 11763 | |
| 11764 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11765 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11766 | al++; |
| 11767 | if (al >= end) |
| 11768 | break; |
| 11769 | |
| 11770 | /* Start of the fisrt word. */ |
| 11771 | token = al; |
| 11772 | |
| 11773 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11774 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11775 | al++; |
| 11776 | if (al == token) |
| 11777 | goto expect_comma; |
| 11778 | |
| 11779 | /* Length of the token. */ |
| 11780 | toklen = al - token; |
| 11781 | qvalue = 1000; |
| 11782 | |
| 11783 | /* Check if the token exists in the list. If the token not exists, |
| 11784 | * jump to the next token. |
| 11785 | */ |
| 11786 | str = args[0].data.str.str; |
| 11787 | w = str; |
| 11788 | while (1) { |
| 11789 | if (*str == ';' || *str == '\0') { |
| 11790 | if (language_range_match(token, toklen, w, str-w)) |
| 11791 | goto look_for_q; |
| 11792 | if (*str == '\0') |
| 11793 | goto expect_comma; |
| 11794 | w = str + 1; |
| 11795 | } |
| 11796 | str++; |
| 11797 | } |
| 11798 | goto expect_comma; |
| 11799 | |
| 11800 | look_for_q: |
| 11801 | |
| 11802 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11803 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11804 | al++; |
| 11805 | if (al >= end) |
| 11806 | goto process_value; |
| 11807 | |
| 11808 | /* If ',' is found, process the result */ |
| 11809 | if (*al == ',') |
| 11810 | goto process_value; |
| 11811 | |
| 11812 | /* If the character is different from ';', look |
| 11813 | * for the end of the header part in best effort. |
| 11814 | */ |
| 11815 | if (*al != ';') |
| 11816 | goto expect_comma; |
| 11817 | |
| 11818 | /* Assumes that the char is ';', now expect "q=". */ |
| 11819 | al++; |
| 11820 | |
| 11821 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11822 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11823 | al++; |
| 11824 | if (al >= end) |
| 11825 | goto process_value; |
| 11826 | |
| 11827 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11828 | if (*al != 'q') |
| 11829 | goto process_value; |
| 11830 | al++; |
| 11831 | |
| 11832 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11833 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11834 | al++; |
| 11835 | if (al >= end) |
| 11836 | goto process_value; |
| 11837 | |
| 11838 | /* Expect '='. If no '=', continue in best effort */ |
| 11839 | if (*al != '=') |
| 11840 | goto process_value; |
| 11841 | al++; |
| 11842 | |
| 11843 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11844 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11845 | al++; |
| 11846 | if (al >= end) |
| 11847 | goto process_value; |
| 11848 | |
| 11849 | /* Parse the q value. */ |
| 11850 | qvalue = parse_qvalue(al, &al); |
| 11851 | |
| 11852 | process_value: |
| 11853 | |
| 11854 | /* If the new q value is the best q value, then store the associated |
| 11855 | * language in the response. If qvalue is the biggest value (1000), |
| 11856 | * break the process. |
| 11857 | */ |
| 11858 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11859 | smp->data.u.str.str = (char *)w; |
| 11860 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11861 | if (qvalue >= 1000) |
| 11862 | break; |
| 11863 | best_q = qvalue; |
| 11864 | } |
| 11865 | |
| 11866 | expect_comma: |
| 11867 | |
| 11868 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11869 | while (al < end && *al != ',') |
| 11870 | al++; |
| 11871 | if (al >= end) |
| 11872 | break; |
| 11873 | |
| 11874 | /* Comma is found, jump it and restart the analyzer. */ |
| 11875 | al++; |
| 11876 | } |
| 11877 | |
| 11878 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11879 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11880 | smp->data.u.str.str = args[1].data.str.str; |
| 11881 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11882 | } |
| 11883 | |
| 11884 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11885 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11886 | } |
| 11887 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11888 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11889 | 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] | 11890 | { |
| 11891 | /* If the constant flag is set or if not size is avalaible at |
| 11892 | * the end of the buffer, copy the string in other buffer |
| 11893 | * before decoding. |
| 11894 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11895 | 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] | 11896 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11897 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11898 | smp->data.u.str.str = str->str; |
| 11899 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11900 | smp->flags &= ~SMP_F_CONST; |
| 11901 | } |
| 11902 | |
| 11903 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11904 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11905 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11906 | return 1; |
| 11907 | } |
| 11908 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11909 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11910 | { |
| 11911 | struct proxy *fe = strm_fe(smp->strm); |
| 11912 | int idx, i; |
| 11913 | struct cap_hdr *hdr; |
| 11914 | int len; |
| 11915 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11916 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11917 | return 0; |
| 11918 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11919 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11920 | |
| 11921 | /* Check the availibity of the capture id. */ |
| 11922 | if (idx > fe->nb_req_cap - 1) |
| 11923 | return 0; |
| 11924 | |
| 11925 | /* Look for the original configuration. */ |
| 11926 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11927 | hdr != NULL && i != idx ; |
| 11928 | i--, hdr = hdr->next); |
| 11929 | if (!hdr) |
| 11930 | return 0; |
| 11931 | |
| 11932 | /* check for the memory allocation */ |
| 11933 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11934 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 11935 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11936 | return 0; |
| 11937 | |
| 11938 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11939 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11940 | if (len > hdr->len) |
| 11941 | len = hdr->len; |
| 11942 | |
| 11943 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11944 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11945 | smp->strm->req_cap[idx][len] = '\0'; |
| 11946 | |
| 11947 | return 1; |
| 11948 | } |
| 11949 | |
| 11950 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11951 | { |
| 11952 | struct proxy *fe = strm_fe(smp->strm); |
| 11953 | int idx, i; |
| 11954 | struct cap_hdr *hdr; |
| 11955 | int len; |
| 11956 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11957 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11958 | return 0; |
| 11959 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11960 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11961 | |
| 11962 | /* Check the availibity of the capture id. */ |
| 11963 | if (idx > fe->nb_rsp_cap - 1) |
| 11964 | return 0; |
| 11965 | |
| 11966 | /* Look for the original configuration. */ |
| 11967 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11968 | hdr != NULL && i != idx ; |
| 11969 | i--, hdr = hdr->next); |
| 11970 | if (!hdr) |
| 11971 | return 0; |
| 11972 | |
| 11973 | /* check for the memory allocation */ |
| 11974 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11975 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 11976 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11977 | return 0; |
| 11978 | |
| 11979 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11980 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11981 | if (len > hdr->len) |
| 11982 | len = hdr->len; |
| 11983 | |
| 11984 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11985 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11986 | smp->strm->res_cap[idx][len] = '\0'; |
| 11987 | |
| 11988 | return 1; |
| 11989 | } |
| 11990 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11991 | /* 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] | 11992 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11993 | * the relevant part of the request line accordingly. Then it updates various |
| 11994 | * pointers to the next elements which were moved, and the total buffer length. |
| 11995 | * 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] | 11996 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11997 | * error, though this can be revisited when this code is finally exploited. |
| 11998 | * |
| 11999 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12000 | * query string and 3 to replace uri. |
| 12001 | * |
| 12002 | * In query string case, the mark question '?' must be set at the start of the |
| 12003 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12004 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12005 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12006 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12007 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12008 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12009 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12010 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12011 | int delta; |
| 12012 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12013 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12014 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12015 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12016 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12017 | |
| 12018 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12019 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12020 | txn->req.sl.rq.m_l += delta; |
| 12021 | txn->req.sl.rq.u += delta; |
| 12022 | txn->req.sl.rq.v += delta; |
| 12023 | break; |
| 12024 | |
| 12025 | case 1: // path |
| 12026 | cur_ptr = http_get_path(txn); |
| 12027 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12028 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12029 | |
| 12030 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12031 | 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] | 12032 | cur_end++; |
| 12033 | |
| 12034 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12035 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12036 | txn->req.sl.rq.u_l += delta; |
| 12037 | txn->req.sl.rq.v += delta; |
| 12038 | break; |
| 12039 | |
| 12040 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12041 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12042 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12043 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12044 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12045 | cur_ptr++; |
| 12046 | |
| 12047 | /* skip the question mark or indicate that we must insert it |
| 12048 | * (but only if the format string is not empty then). |
| 12049 | */ |
| 12050 | if (cur_ptr < cur_end) |
| 12051 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12052 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12053 | offset = 0; |
| 12054 | |
| 12055 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12056 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12057 | txn->req.sl.rq.u_l += delta; |
| 12058 | txn->req.sl.rq.v += delta; |
| 12059 | break; |
| 12060 | |
| 12061 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12062 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12063 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12064 | |
| 12065 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12066 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12067 | txn->req.sl.rq.u_l += delta; |
| 12068 | txn->req.sl.rq.v += delta; |
| 12069 | break; |
| 12070 | |
| 12071 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12072 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12073 | } |
| 12074 | |
| 12075 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12076 | 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] | 12077 | txn->req.sl.rq.l += delta; |
| 12078 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12079 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12080 | return 0; |
| 12081 | } |
| 12082 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12083 | /* This function replace the HTTP status code and the associated message. The |
| 12084 | * variable <status> contains the new status code. This function never fails. |
| 12085 | */ |
| 12086 | void http_set_status(unsigned int status, struct stream *s) |
| 12087 | { |
| 12088 | struct http_txn *txn = s->txn; |
| 12089 | char *cur_ptr, *cur_end; |
| 12090 | int delta; |
| 12091 | char *res; |
| 12092 | int c_l; |
| 12093 | const char *msg; |
| 12094 | int msg_len; |
| 12095 | |
| 12096 | chunk_reset(&trash); |
| 12097 | |
| 12098 | res = ultoa_o(status, trash.str, trash.size); |
| 12099 | c_l = res - trash.str; |
| 12100 | |
| 12101 | trash.str[c_l] = ' '; |
| 12102 | trash.len = c_l + 1; |
| 12103 | |
| 12104 | msg = get_reason(status); |
| 12105 | msg_len = strlen(msg); |
| 12106 | |
| 12107 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12108 | trash.len += msg_len; |
| 12109 | |
| 12110 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12111 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12112 | |
| 12113 | /* commit changes and adjust message */ |
| 12114 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12115 | |
| 12116 | /* adjust res line offsets and lengths */ |
| 12117 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12118 | txn->rsp.sl.st.c_l = c_l; |
| 12119 | txn->rsp.sl.st.r_l = msg_len; |
| 12120 | |
| 12121 | delta = trash.len - (cur_end - cur_ptr); |
| 12122 | txn->rsp.sl.st.l += delta; |
| 12123 | txn->hdr_idx.v[0].len += delta; |
| 12124 | http_msg_move_end(&txn->rsp, delta); |
| 12125 | } |
| 12126 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12127 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12128 | * 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] | 12129 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12130 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12131 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12132 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12133 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12134 | 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] | 12135 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12136 | { |
| 12137 | chunk_reset(&trash); |
| 12138 | |
| 12139 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12140 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12141 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12142 | 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] | 12143 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12144 | 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] | 12145 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12146 | } |
| 12147 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12148 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12149 | 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] | 12150 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12151 | { |
| 12152 | http_set_status(rule->arg.status.code, s); |
| 12153 | return ACT_RET_CONT; |
| 12154 | } |
| 12155 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12156 | /* parse an http-request action among : |
| 12157 | * set-method |
| 12158 | * set-path |
| 12159 | * set-query |
| 12160 | * set-uri |
| 12161 | * |
| 12162 | * All of them accept a single argument of type string representing a log-format. |
| 12163 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12164 | * 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] | 12165 | * 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] | 12166 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12167 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12168 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12169 | { |
| 12170 | int cur_arg = *orig_arg; |
| 12171 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12172 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12173 | |
| 12174 | switch (args[0][4]) { |
| 12175 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12176 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12177 | rule->action_ptr = http_action_set_req_line; |
| 12178 | break; |
| 12179 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12180 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12181 | rule->action_ptr = http_action_set_req_line; |
| 12182 | break; |
| 12183 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12184 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12185 | rule->action_ptr = http_action_set_req_line; |
| 12186 | break; |
| 12187 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12188 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12189 | rule->action_ptr = http_action_set_req_line; |
| 12190 | break; |
| 12191 | default: |
| 12192 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12193 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12194 | } |
| 12195 | |
| 12196 | if (!*args[cur_arg] || |
| 12197 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12198 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12199 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12200 | } |
| 12201 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12202 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12203 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12204 | 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] | 12205 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 12206 | proxy->conf.args.file, proxy->conf.args.line); |
| 12207 | |
| 12208 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12209 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12210 | } |
| 12211 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12212 | /* parse set-status action: |
| 12213 | * This action accepts a single argument of type int representing |
| 12214 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12215 | * ACT_RET_PRS_ERR on error. |
| 12216 | */ |
| 12217 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12218 | struct act_rule *rule, char **err) |
| 12219 | { |
| 12220 | char *error; |
| 12221 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12222 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12223 | rule->action_ptr = action_http_set_status; |
| 12224 | |
| 12225 | /* Check if an argument is available */ |
| 12226 | if (!*args[*orig_arg]) { |
| 12227 | memprintf(err, "expects exactly 1 argument <status>"); |
| 12228 | return ACT_RET_PRS_ERR; |
| 12229 | } |
| 12230 | |
| 12231 | /* convert status code as integer */ |
| 12232 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12233 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12234 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12235 | return ACT_RET_PRS_ERR; |
| 12236 | } |
| 12237 | |
| 12238 | (*orig_arg)++; |
| 12239 | return ACT_RET_PRS_OK; |
| 12240 | } |
| 12241 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12242 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12243 | * turns the result into a string and puts it in a capture slot. It always |
| 12244 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12245 | * processing continues. |
| 12246 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12247 | 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] | 12248 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12249 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12250 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12251 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12252 | char **cap = s->req_cap; |
| 12253 | int len; |
| 12254 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12255 | 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] | 12256 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12257 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12258 | |
| 12259 | if (cap[h->index] == NULL) |
| 12260 | cap[h->index] = pool_alloc2(h->pool); |
| 12261 | |
| 12262 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12263 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12264 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12265 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12266 | if (len > h->len) |
| 12267 | len = h->len; |
| 12268 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12269 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12270 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12271 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12272 | } |
| 12273 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12274 | /* This function executes the "capture" action and store the result in a |
| 12275 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12276 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12277 | * error occurs the action is cancelled, but the rule processing continues. |
| 12278 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12279 | 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] | 12280 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12281 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12282 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12283 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12284 | char **cap = s->req_cap; |
| 12285 | struct proxy *fe = strm_fe(s); |
| 12286 | int len; |
| 12287 | int i; |
| 12288 | |
| 12289 | /* Look for the original configuration. */ |
| 12290 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12291 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12292 | i--, h = h->next); |
| 12293 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12294 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12295 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12296 | 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] | 12297 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12298 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12299 | |
| 12300 | if (cap[h->index] == NULL) |
| 12301 | cap[h->index] = pool_alloc2(h->pool); |
| 12302 | |
| 12303 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12304 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12305 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12306 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12307 | if (len > h->len) |
| 12308 | len = h->len; |
| 12309 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12310 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12311 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12312 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12313 | } |
| 12314 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12315 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12316 | * 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] | 12317 | * 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] | 12318 | * 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] | 12319 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12320 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12321 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12322 | { |
| 12323 | struct sample_expr *expr; |
| 12324 | struct cap_hdr *hdr; |
| 12325 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12326 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12327 | |
| 12328 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12329 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12330 | strcmp(args[cur_arg], "unless") == 0) |
| 12331 | break; |
| 12332 | |
| 12333 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12334 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12335 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12336 | } |
| 12337 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12338 | cur_arg = *orig_arg; |
| 12339 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12340 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12341 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12342 | |
| 12343 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12344 | memprintf(err, |
| 12345 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12346 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12347 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12348 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12349 | } |
| 12350 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12351 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12352 | memprintf(err, "expects 'len or 'id'"); |
| 12353 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12354 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12355 | } |
| 12356 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12357 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12358 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12359 | |
| 12360 | if (!(px->cap & PR_CAP_FE)) { |
| 12361 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12362 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12363 | } |
| 12364 | |
| 12365 | proxy->conf.args.ctx = ARGC_CAP; |
| 12366 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12367 | if (!args[cur_arg]) { |
| 12368 | memprintf(err, "missing length value"); |
| 12369 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12370 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12371 | } |
| 12372 | /* we copy the table name for now, it will be resolved later */ |
| 12373 | len = atoi(args[cur_arg]); |
| 12374 | if (len <= 0) { |
| 12375 | memprintf(err, "length must be > 0"); |
| 12376 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12377 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12378 | } |
| 12379 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12380 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12381 | if (!len) { |
| 12382 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12383 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12384 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12385 | } |
| 12386 | |
| 12387 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 12388 | hdr->next = px->req_cap; |
| 12389 | hdr->name = NULL; /* not a header capture */ |
| 12390 | hdr->namelen = 0; |
| 12391 | hdr->len = len; |
| 12392 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12393 | hdr->index = px->nb_req_cap++; |
| 12394 | |
| 12395 | px->req_cap = hdr; |
| 12396 | px->to_log |= LW_REQHDR; |
| 12397 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12398 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12399 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12400 | rule->arg.cap.expr = expr; |
| 12401 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12402 | } |
| 12403 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12404 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12405 | int id; |
| 12406 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12407 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12408 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12409 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12410 | if (!args[cur_arg]) { |
| 12411 | memprintf(err, "missing id value"); |
| 12412 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12413 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12414 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12415 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12416 | id = strtol(args[cur_arg], &error, 10); |
| 12417 | if (*error != '\0') { |
| 12418 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12419 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12420 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12421 | } |
| 12422 | cur_arg++; |
| 12423 | |
| 12424 | proxy->conf.args.ctx = ARGC_CAP; |
| 12425 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12426 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12427 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12428 | rule->arg.capid.expr = expr; |
| 12429 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12430 | } |
| 12431 | |
| 12432 | else { |
| 12433 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12434 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12435 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12436 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12437 | |
| 12438 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12439 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12440 | } |
| 12441 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12442 | /* This function executes the "capture" action and store the result in a |
| 12443 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12444 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12445 | * error occurs the action is cancelled, but the rule processing continues. |
| 12446 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12447 | 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] | 12448 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12449 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12450 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12451 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12452 | char **cap = s->res_cap; |
| 12453 | struct proxy *fe = strm_fe(s); |
| 12454 | int len; |
| 12455 | int i; |
| 12456 | |
| 12457 | /* Look for the original configuration. */ |
| 12458 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12459 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12460 | i--, h = h->next); |
| 12461 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12462 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12463 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12464 | 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] | 12465 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12466 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12467 | |
| 12468 | if (cap[h->index] == NULL) |
| 12469 | cap[h->index] = pool_alloc2(h->pool); |
| 12470 | |
| 12471 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12472 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12473 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12474 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12475 | if (len > h->len) |
| 12476 | len = h->len; |
| 12477 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12478 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12479 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12480 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12481 | } |
| 12482 | |
| 12483 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12484 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12485 | * 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] | 12486 | * 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] | 12487 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12488 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12489 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12490 | { |
| 12491 | struct sample_expr *expr; |
| 12492 | int cur_arg; |
| 12493 | int id; |
| 12494 | char *error; |
| 12495 | |
| 12496 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12497 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12498 | strcmp(args[cur_arg], "unless") == 0) |
| 12499 | break; |
| 12500 | |
| 12501 | if (cur_arg < *orig_arg + 3) { |
| 12502 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12503 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12504 | } |
| 12505 | |
| 12506 | cur_arg = *orig_arg; |
| 12507 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12508 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12509 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12510 | |
| 12511 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12512 | memprintf(err, |
| 12513 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12514 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12515 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12516 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12517 | } |
| 12518 | |
| 12519 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12520 | memprintf(err, "expects 'len or 'id'"); |
| 12521 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12522 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12523 | } |
| 12524 | |
| 12525 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12526 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12527 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12528 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12529 | } |
| 12530 | |
| 12531 | cur_arg++; |
| 12532 | |
| 12533 | if (!args[cur_arg]) { |
| 12534 | memprintf(err, "missing id value"); |
| 12535 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12536 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12537 | } |
| 12538 | |
| 12539 | id = strtol(args[cur_arg], &error, 10); |
| 12540 | if (*error != '\0') { |
| 12541 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12542 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12543 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12544 | } |
| 12545 | cur_arg++; |
| 12546 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12547 | proxy->conf.args.ctx = ARGC_CAP; |
| 12548 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12549 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12550 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12551 | rule->arg.capid.expr = expr; |
| 12552 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12553 | |
| 12554 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12555 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12556 | } |
| 12557 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12558 | /* |
| 12559 | * Return the struct http_req_action_kw associated to a keyword. |
| 12560 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12561 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12562 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12563 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12564 | } |
| 12565 | |
| 12566 | /* |
| 12567 | * Return the struct http_res_action_kw associated to a keyword. |
| 12568 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12569 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12570 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12571 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12572 | } |
| 12573 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12574 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12575 | /* All supported ACL keywords must be declared here. */ |
| 12576 | /************************************************************************/ |
| 12577 | |
| 12578 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12579 | * Please take care of keeping this list alphabetically sorted. |
| 12580 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12581 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12582 | { "base", "base", PAT_MATCH_STR }, |
| 12583 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12584 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12585 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12586 | { "base_end", "base", PAT_MATCH_END }, |
| 12587 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12588 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12589 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12590 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12591 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12592 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12593 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12594 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12595 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12596 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12597 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12598 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12599 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12600 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12601 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12602 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12603 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12604 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12605 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12606 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12607 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12608 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12609 | /* these two declarations uses strings with list storage (in place |
| 12610 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12611 | * and delete functions are relative to the list management. The parse |
| 12612 | * and match method are related to the corresponding fetch methods. This |
| 12613 | * is very particular ACL declaration mode. |
| 12614 | */ |
| 12615 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12616 | { "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] | 12617 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12618 | { "path", "path", PAT_MATCH_STR }, |
| 12619 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12620 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12621 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12622 | { "path_end", "path", PAT_MATCH_END }, |
| 12623 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12624 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12625 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12626 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12627 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12628 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12629 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12630 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12631 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12632 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12633 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12634 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12635 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12636 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12637 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12638 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12639 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12640 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12641 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12642 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12643 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12644 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12645 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12646 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12647 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12648 | { "url", "url", PAT_MATCH_STR }, |
| 12649 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12650 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12651 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12652 | { "url_end", "url", PAT_MATCH_END }, |
| 12653 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12654 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12655 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12656 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12657 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12658 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12659 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12660 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12661 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12662 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12663 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12664 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12665 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12666 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12667 | }}; |
| 12668 | |
| 12669 | /************************************************************************/ |
| 12670 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12671 | /************************************************************************/ |
| 12672 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12673 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12674 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12675 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12676 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12677 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12678 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12679 | { "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] | 12680 | |
| 12681 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12682 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12683 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12684 | { "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] | 12685 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12686 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12687 | { "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] | 12688 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12689 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12690 | * are only here to match the ACL's name, are request-only and are used |
| 12691 | * for ACL compatibility only. |
| 12692 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12693 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12694 | { "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] | 12695 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12696 | { "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] | 12697 | |
| 12698 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12699 | * only here to match the ACL's name, are request-only and are used for |
| 12700 | * ACL compatibility only. |
| 12701 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12702 | { "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] | 12703 | { "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] | 12704 | { "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] | 12705 | { "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] | 12706 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12707 | { "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] | 12708 | { "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] | 12709 | { "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] | 12710 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12711 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12712 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12713 | |
| 12714 | /* HTTP protocol on the request path */ |
| 12715 | { "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] | 12716 | { "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] | 12717 | |
| 12718 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12719 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12720 | { "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] | 12721 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12722 | { "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] | 12723 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12724 | { "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] | 12725 | { "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] | 12726 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12727 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12728 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12729 | { "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] | 12730 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12731 | /* 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] | 12732 | { "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] | 12733 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12734 | { "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] | 12735 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12736 | { "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] | 12737 | { "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] | 12738 | { "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] | 12739 | { "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] | 12740 | { "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] | 12741 | { "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] | 12742 | { "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] | 12743 | |
| 12744 | /* 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] | 12745 | { "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] | 12746 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12747 | { "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] | 12748 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12749 | { "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] | 12750 | { "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] | 12751 | { "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] | 12752 | { "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] | 12753 | { "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] | 12754 | { "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] | 12755 | { "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] | 12756 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12757 | /* 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] | 12758 | { "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] | 12759 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12760 | { "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] | 12761 | { "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] | 12762 | |
| 12763 | /* 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] | 12764 | { "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] | 12765 | { "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] | 12766 | { "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] | 12767 | { "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] | 12768 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12769 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12770 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12771 | { "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] | 12772 | { "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] | 12773 | { "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] | 12774 | { "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] | 12775 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12776 | { "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] | 12777 | { "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] | 12778 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12779 | }}; |
| 12780 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12781 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12782 | /************************************************************************/ |
| 12783 | /* All supported converter keywords must be declared here. */ |
| 12784 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12785 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12786 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12787 | { "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] | 12788 | { "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] | 12789 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12790 | { "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] | 12791 | { "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] | 12792 | { NULL, NULL, 0, 0, 0 }, |
| 12793 | }}; |
| 12794 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12795 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12796 | /************************************************************************/ |
| 12797 | /* All supported http-request action keywords must be declared here. */ |
| 12798 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12799 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12800 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12801 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12802 | { "set-method", parse_set_req_line }, |
| 12803 | { "set-path", parse_set_req_line }, |
| 12804 | { "set-query", parse_set_req_line }, |
| 12805 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12806 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12807 | } |
| 12808 | }; |
| 12809 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12810 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12811 | .kw = { |
| 12812 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12813 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12814 | { NULL, NULL } |
| 12815 | } |
| 12816 | }; |
| 12817 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12818 | __attribute__((constructor)) |
| 12819 | static void __http_protocol_init(void) |
| 12820 | { |
| 12821 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12822 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12823 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12824 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12825 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12826 | } |
| 12827 | |
| 12828 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12829 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12830 | * Local variables: |
| 12831 | * c-indent-level: 8 |
| 12832 | * c-basic-offset: 8 |
| 12833 | * End: |
| 12834 | */ |