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> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 42 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <types/global.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 45 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 47 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 48 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 49 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 50 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 52 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 53 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 54 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 55 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 56 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 58 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 59 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 61 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 62 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 63 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 65 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 67 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 68 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 69 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 70 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 72 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 73 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 75 | const char HTTP_100[] = |
| 76 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 77 | |
| 78 | const struct chunk http_100_chunk = { |
| 79 | .str = (char *)&HTTP_100, |
| 80 | .len = sizeof(HTTP_100)-1 |
| 81 | }; |
| 82 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 83 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 84 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 96 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 97 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 98 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 102 | |
| 103 | /* same as 302 except that the browser MUST retry with the same method */ |
| 104 | const char *HTTP_307 = |
| 105 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 106 | "Cache-Control: no-cache\r\n" |
| 107 | "Content-length: 0\r\n" |
| 108 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 109 | |
| 110 | /* same as 301 except that the browser MUST retry with the same method */ |
| 111 | const char *HTTP_308 = |
| 112 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 113 | "Content-length: 0\r\n" |
| 114 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 115 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 117 | const char *HTTP_401_fmt = |
| 118 | "HTTP/1.0 401 Unauthorized\r\n" |
| 119 | "Cache-Control: no-cache\r\n" |
| 120 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 121 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 123 | "\r\n" |
| 124 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 125 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 126 | const char *HTTP_407_fmt = |
| 127 | "HTTP/1.0 407 Unauthorized\r\n" |
| 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 132 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 133 | "<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] | 134 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | |
| 136 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 137 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | [HTTP_ERR_400] = 400, |
| 139 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 140 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 141 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 142 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 143 | [HTTP_ERR_500] = 500, |
| 144 | [HTTP_ERR_502] = 502, |
| 145 | [HTTP_ERR_503] = 503, |
| 146 | [HTTP_ERR_504] = 504, |
| 147 | }; |
| 148 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 149 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 150 | [HTTP_ERR_200] = |
| 151 | "HTTP/1.0 200 OK\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 157 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 158 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 159 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_403] = |
| 167 | "HTTP/1.0 403 Forbidden\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 173 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 174 | [HTTP_ERR_405] = |
| 175 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n", |
| 181 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 182 | [HTTP_ERR_408] = |
| 183 | "HTTP/1.0 408 Request Time-out\r\n" |
| 184 | "Cache-Control: no-cache\r\n" |
| 185 | "Connection: close\r\n" |
| 186 | "Content-Type: text/html\r\n" |
| 187 | "\r\n" |
| 188 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 189 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 190 | [HTTP_ERR_429] = |
| 191 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 192 | "Cache-Control: no-cache\r\n" |
| 193 | "Connection: close\r\n" |
| 194 | "Content-Type: text/html\r\n" |
| 195 | "\r\n" |
| 196 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 197 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 198 | [HTTP_ERR_500] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 199 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 200 | "Cache-Control: no-cache\r\n" |
| 201 | "Connection: close\r\n" |
| 202 | "Content-Type: text/html\r\n" |
| 203 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 204 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 205 | |
| 206 | [HTTP_ERR_502] = |
| 207 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 208 | "Cache-Control: no-cache\r\n" |
| 209 | "Connection: close\r\n" |
| 210 | "Content-Type: text/html\r\n" |
| 211 | "\r\n" |
| 212 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 213 | |
| 214 | [HTTP_ERR_503] = |
| 215 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 216 | "Cache-Control: no-cache\r\n" |
| 217 | "Connection: close\r\n" |
| 218 | "Content-Type: text/html\r\n" |
| 219 | "\r\n" |
| 220 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 221 | |
| 222 | [HTTP_ERR_504] = |
| 223 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 224 | "Cache-Control: no-cache\r\n" |
| 225 | "Connection: close\r\n" |
| 226 | "Content-Type: text/html\r\n" |
| 227 | "\r\n" |
| 228 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 229 | |
| 230 | }; |
| 231 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 232 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 233 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 234 | [STAT_STATUS_DENY] = "DENY", |
| 235 | [STAT_STATUS_DONE] = "DONE", |
| 236 | [STAT_STATUS_ERRP] = "ERRP", |
| 237 | [STAT_STATUS_EXCD] = "EXCD", |
| 238 | [STAT_STATUS_NONE] = "NONE", |
| 239 | [STAT_STATUS_PART] = "PART", |
| 240 | [STAT_STATUS_UNKN] = "UNKN", |
| 241 | }; |
| 242 | |
| 243 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 244 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 245 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 246 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 247 | }; |
| 248 | |
| 249 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 250 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 251 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 252 | }; |
| 253 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 254 | /* We must put the messages here since GCC cannot initialize consts depending |
| 255 | * on strlen(). |
| 256 | */ |
| 257 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 258 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 259 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 260 | static struct hdr_ctx static_hdr_ctx; |
| 261 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 262 | #define FD_SETS_ARE_BITFIELDS |
| 263 | #ifdef FD_SETS_ARE_BITFIELDS |
| 264 | /* |
| 265 | * This map is used with all the FD_* macros to check whether a particular bit |
| 266 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 267 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 268 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 269 | * exclusively to the macros. |
| 270 | */ |
| 271 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 272 | 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] | 273 | 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] | 274 | |
| 275 | #else |
| 276 | #error "Check if your OS uses bitfields for fd_sets" |
| 277 | #endif |
| 278 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 279 | 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] | 280 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 281 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 282 | static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 283 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 284 | /* This function returns a reason associated with the HTTP status. |
| 285 | * This function never fails, a message is always returned. |
| 286 | */ |
| 287 | const char *get_reason(unsigned int status) |
| 288 | { |
| 289 | switch (status) { |
| 290 | case 100: return "Continue"; |
| 291 | case 101: return "Switching Protocols"; |
| 292 | case 102: return "Processing"; |
| 293 | case 200: return "OK"; |
| 294 | case 201: return "Created"; |
| 295 | case 202: return "Accepted"; |
| 296 | case 203: return "Non-Authoritative Information"; |
| 297 | case 204: return "No Content"; |
| 298 | case 205: return "Reset Content"; |
| 299 | case 206: return "Partial Content"; |
| 300 | case 207: return "Multi-Status"; |
| 301 | case 210: return "Content Different"; |
| 302 | case 226: return "IM Used"; |
| 303 | case 300: return "Multiple Choices"; |
| 304 | case 301: return "Moved Permanently"; |
| 305 | case 302: return "Moved Temporarily"; |
| 306 | case 303: return "See Other"; |
| 307 | case 304: return "Not Modified"; |
| 308 | case 305: return "Use Proxy"; |
| 309 | case 307: return "Temporary Redirect"; |
| 310 | case 308: return "Permanent Redirect"; |
| 311 | case 310: return "Too many Redirects"; |
| 312 | case 400: return "Bad Request"; |
| 313 | case 401: return "Unauthorized"; |
| 314 | case 402: return "Payment Required"; |
| 315 | case 403: return "Forbidden"; |
| 316 | case 404: return "Not Found"; |
| 317 | case 405: return "Method Not Allowed"; |
| 318 | case 406: return "Not Acceptable"; |
| 319 | case 407: return "Proxy Authentication Required"; |
| 320 | case 408: return "Request Time-out"; |
| 321 | case 409: return "Conflict"; |
| 322 | case 410: return "Gone"; |
| 323 | case 411: return "Length Required"; |
| 324 | case 412: return "Precondition Failed"; |
| 325 | case 413: return "Request Entity Too Large"; |
| 326 | case 414: return "Request-URI Too Long"; |
| 327 | case 415: return "Unsupported Media Type"; |
| 328 | case 416: return "Requested range unsatisfiable"; |
| 329 | case 417: return "Expectation failed"; |
| 330 | case 418: return "I'm a teapot"; |
| 331 | case 422: return "Unprocessable entity"; |
| 332 | case 423: return "Locked"; |
| 333 | case 424: return "Method failure"; |
| 334 | case 425: return "Unordered Collection"; |
| 335 | case 426: return "Upgrade Required"; |
| 336 | case 428: return "Precondition Required"; |
| 337 | case 429: return "Too Many Requests"; |
| 338 | case 431: return "Request Header Fields Too Large"; |
| 339 | case 449: return "Retry With"; |
| 340 | case 450: return "Blocked by Windows Parental Controls"; |
| 341 | case 451: return "Unavailable For Legal Reasons"; |
| 342 | case 456: return "Unrecoverable Error"; |
| 343 | case 499: return "client has closed connection"; |
| 344 | case 500: return "Internal Server Error"; |
| 345 | case 501: return "Not Implemented"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 346 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 347 | case 503: return "Service Unavailable"; |
| 348 | case 504: return "Gateway Time-out"; |
| 349 | case 505: return "HTTP Version not supported"; |
| 350 | case 506: return "Variant also negociate"; |
| 351 | case 507: return "Insufficient storage"; |
| 352 | case 508: return "Loop detected"; |
| 353 | case 509: return "Bandwidth Limit Exceeded"; |
| 354 | case 510: return "Not extended"; |
| 355 | case 511: return "Network authentication required"; |
| 356 | case 520: return "Web server is returning an unknown error"; |
| 357 | default: |
| 358 | switch (status) { |
| 359 | case 100 ... 199: return "Informational"; |
| 360 | case 200 ... 299: return "Success"; |
| 361 | case 300 ... 399: return "Redirection"; |
| 362 | case 400 ... 499: return "Client Error"; |
| 363 | case 500 ... 599: return "Server Error"; |
| 364 | default: return "Other"; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 369 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 370 | * Returned value should match codes from http_err_codes. |
| 371 | */ |
| 372 | static const int http_get_status_idx(unsigned int status) |
| 373 | { |
| 374 | switch (status) { |
| 375 | case 200: return HTTP_ERR_200; |
| 376 | case 400: return HTTP_ERR_400; |
| 377 | case 403: return HTTP_ERR_403; |
| 378 | case 405: return HTTP_ERR_405; |
| 379 | case 408: return HTTP_ERR_408; |
| 380 | case 429: return HTTP_ERR_429; |
| 381 | case 500: return HTTP_ERR_500; |
| 382 | case 502: return HTTP_ERR_502; |
| 383 | case 503: return HTTP_ERR_503; |
| 384 | case 504: return HTTP_ERR_504; |
| 385 | default: return HTTP_ERR_500; |
| 386 | } |
| 387 | } |
| 388 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 389 | void init_proto_http() |
| 390 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 391 | int i; |
| 392 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 393 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 394 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 395 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 396 | if (!http_err_msgs[msg]) { |
| 397 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 398 | abort(); |
| 399 | } |
| 400 | |
| 401 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 402 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 403 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 404 | |
| 405 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 406 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 407 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 408 | * printable characters above. |
| 409 | */ |
| 410 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 411 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 412 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 413 | for (i = 0; i < 32; i++) { |
| 414 | FD_SET(i, hdr_encode_map); |
| 415 | FD_SET(i, url_encode_map); |
| 416 | } |
| 417 | for (i = 127; i < 256; i++) { |
| 418 | FD_SET(i, hdr_encode_map); |
| 419 | FD_SET(i, url_encode_map); |
| 420 | } |
| 421 | |
| 422 | tmp = "\"#{|}"; |
| 423 | while (*tmp) { |
| 424 | FD_SET(*tmp, hdr_encode_map); |
| 425 | tmp++; |
| 426 | } |
| 427 | |
| 428 | tmp = "\"#"; |
| 429 | while (*tmp) { |
| 430 | FD_SET(*tmp, url_encode_map); |
| 431 | tmp++; |
| 432 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 433 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 434 | /* initialize the http header encoding map. The draft httpbis define the |
| 435 | * header content as: |
| 436 | * |
| 437 | * HTTP-message = start-line |
| 438 | * *( header-field CRLF ) |
| 439 | * CRLF |
| 440 | * [ message-body ] |
| 441 | * header-field = field-name ":" OWS field-value OWS |
| 442 | * field-value = *( field-content / obs-fold ) |
| 443 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 444 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 445 | * field-vchar = VCHAR / obs-text |
| 446 | * VCHAR = %x21-7E |
| 447 | * obs-text = %x80-FF |
| 448 | * |
| 449 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 450 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 451 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 452 | */ |
| 453 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 454 | for (i = 0x00; i <= 0x08; i++) |
| 455 | FD_SET(i, http_encode_map); |
| 456 | for (i = 0x0a; i <= 0x1f; i++) |
| 457 | FD_SET(i, http_encode_map); |
| 458 | FD_SET(0x7f, http_encode_map); |
| 459 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 460 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 461 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 462 | pool2_requri = create_pool("requri", global.tune.requri_len , MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 463 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 464 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 465 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 466 | /* |
| 467 | * We have 26 list of methods (1 per first letter), each of which can have |
| 468 | * up to 3 entries (2 valid, 1 null). |
| 469 | */ |
| 470 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 471 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 472 | int len; |
| 473 | const char text[8]; |
| 474 | }; |
| 475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 476 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 477 | ['C' - 'A'] = { |
| 478 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 479 | }, |
| 480 | ['D' - 'A'] = { |
| 481 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 482 | }, |
| 483 | ['G' - 'A'] = { |
| 484 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 485 | }, |
| 486 | ['H' - 'A'] = { |
| 487 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 488 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 489 | ['O' - 'A'] = { |
| 490 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 491 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 492 | ['P' - 'A'] = { |
| 493 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 494 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 495 | }, |
| 496 | ['T' - 'A'] = { |
| 497 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 498 | }, |
| 499 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 500 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 501 | */ |
| 502 | }; |
| 503 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 504 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 505 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 506 | [HTTP_METH_GET] = { "GET", 3 }, |
| 507 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 508 | [HTTP_METH_POST] = { "POST", 4 }, |
| 509 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 510 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 511 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 512 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 513 | }; |
| 514 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 515 | /* 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] | 516 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 517 | * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is |
| 518 | * neither a separator nor a CTL char. An http ver_token is any ASCII which can |
| 519 | * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any |
| 520 | * digit. Note: please do not overwrite values in assignment since gcc-2.95 |
| 521 | * will not handle them correctly. It's worth noting that chars 128..255 are |
| 522 | * nothing, not even control chars. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 523 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 524 | const unsigned char http_char_classes[256] = { |
| 525 | [ 0] = HTTP_FLG_CTL, |
| 526 | [ 1] = HTTP_FLG_CTL, |
| 527 | [ 2] = HTTP_FLG_CTL, |
| 528 | [ 3] = HTTP_FLG_CTL, |
| 529 | [ 4] = HTTP_FLG_CTL, |
| 530 | [ 5] = HTTP_FLG_CTL, |
| 531 | [ 6] = HTTP_FLG_CTL, |
| 532 | [ 7] = HTTP_FLG_CTL, |
| 533 | [ 8] = HTTP_FLG_CTL, |
| 534 | [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL, |
| 535 | [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 536 | [ 11] = HTTP_FLG_CTL, |
| 537 | [ 12] = HTTP_FLG_CTL, |
| 538 | [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL, |
| 539 | [ 14] = HTTP_FLG_CTL, |
| 540 | [ 15] = HTTP_FLG_CTL, |
| 541 | [ 16] = HTTP_FLG_CTL, |
| 542 | [ 17] = HTTP_FLG_CTL, |
| 543 | [ 18] = HTTP_FLG_CTL, |
| 544 | [ 19] = HTTP_FLG_CTL, |
| 545 | [ 20] = HTTP_FLG_CTL, |
| 546 | [ 21] = HTTP_FLG_CTL, |
| 547 | [ 22] = HTTP_FLG_CTL, |
| 548 | [ 23] = HTTP_FLG_CTL, |
| 549 | [ 24] = HTTP_FLG_CTL, |
| 550 | [ 25] = HTTP_FLG_CTL, |
| 551 | [ 26] = HTTP_FLG_CTL, |
| 552 | [ 27] = HTTP_FLG_CTL, |
| 553 | [ 28] = HTTP_FLG_CTL, |
| 554 | [ 29] = HTTP_FLG_CTL, |
| 555 | [ 30] = HTTP_FLG_CTL, |
| 556 | [ 31] = HTTP_FLG_CTL, |
| 557 | [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP, |
| 558 | ['!'] = HTTP_FLG_TOK, |
| 559 | ['"'] = HTTP_FLG_SEP, |
| 560 | ['#'] = HTTP_FLG_TOK, |
| 561 | ['$'] = HTTP_FLG_TOK, |
| 562 | ['%'] = HTTP_FLG_TOK, |
| 563 | ['&'] = HTTP_FLG_TOK, |
| 564 | [ 39] = HTTP_FLG_TOK, |
| 565 | ['('] = HTTP_FLG_SEP, |
| 566 | [')'] = HTTP_FLG_SEP, |
| 567 | ['*'] = HTTP_FLG_TOK, |
| 568 | ['+'] = HTTP_FLG_TOK, |
| 569 | [','] = HTTP_FLG_SEP, |
| 570 | ['-'] = HTTP_FLG_TOK, |
| 571 | ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 572 | ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER, |
| 573 | ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 574 | ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 575 | ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 576 | ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 577 | ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 578 | ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 579 | ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 580 | ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 581 | ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 582 | ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 583 | [':'] = HTTP_FLG_SEP, |
| 584 | [';'] = HTTP_FLG_SEP, |
| 585 | ['<'] = HTTP_FLG_SEP, |
| 586 | ['='] = HTTP_FLG_SEP, |
| 587 | ['>'] = HTTP_FLG_SEP, |
| 588 | ['?'] = HTTP_FLG_SEP, |
| 589 | ['@'] = HTTP_FLG_SEP, |
| 590 | ['A'] = HTTP_FLG_TOK, |
| 591 | ['B'] = HTTP_FLG_TOK, |
| 592 | ['C'] = HTTP_FLG_TOK, |
| 593 | ['D'] = HTTP_FLG_TOK, |
| 594 | ['E'] = HTTP_FLG_TOK, |
| 595 | ['F'] = HTTP_FLG_TOK, |
| 596 | ['G'] = HTTP_FLG_TOK, |
| 597 | ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 598 | ['I'] = HTTP_FLG_TOK, |
| 599 | ['J'] = HTTP_FLG_TOK, |
| 600 | ['K'] = HTTP_FLG_TOK, |
| 601 | ['L'] = HTTP_FLG_TOK, |
| 602 | ['M'] = HTTP_FLG_TOK, |
| 603 | ['N'] = HTTP_FLG_TOK, |
| 604 | ['O'] = HTTP_FLG_TOK, |
| 605 | ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 606 | ['Q'] = HTTP_FLG_TOK, |
| 607 | ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 608 | ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 609 | ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER, |
| 610 | ['U'] = HTTP_FLG_TOK, |
| 611 | ['V'] = HTTP_FLG_TOK, |
| 612 | ['W'] = HTTP_FLG_TOK, |
| 613 | ['X'] = HTTP_FLG_TOK, |
| 614 | ['Y'] = HTTP_FLG_TOK, |
| 615 | ['Z'] = HTTP_FLG_TOK, |
| 616 | ['['] = HTTP_FLG_SEP, |
| 617 | [ 92] = HTTP_FLG_SEP, |
| 618 | [']'] = HTTP_FLG_SEP, |
| 619 | ['^'] = HTTP_FLG_TOK, |
| 620 | ['_'] = HTTP_FLG_TOK, |
| 621 | ['`'] = HTTP_FLG_TOK, |
| 622 | ['a'] = HTTP_FLG_TOK, |
| 623 | ['b'] = HTTP_FLG_TOK, |
| 624 | ['c'] = HTTP_FLG_TOK, |
| 625 | ['d'] = HTTP_FLG_TOK, |
| 626 | ['e'] = HTTP_FLG_TOK, |
| 627 | ['f'] = HTTP_FLG_TOK, |
| 628 | ['g'] = HTTP_FLG_TOK, |
| 629 | ['h'] = HTTP_FLG_TOK, |
| 630 | ['i'] = HTTP_FLG_TOK, |
| 631 | ['j'] = HTTP_FLG_TOK, |
| 632 | ['k'] = HTTP_FLG_TOK, |
| 633 | ['l'] = HTTP_FLG_TOK, |
| 634 | ['m'] = HTTP_FLG_TOK, |
| 635 | ['n'] = HTTP_FLG_TOK, |
| 636 | ['o'] = HTTP_FLG_TOK, |
| 637 | ['p'] = HTTP_FLG_TOK, |
| 638 | ['q'] = HTTP_FLG_TOK, |
| 639 | ['r'] = HTTP_FLG_TOK, |
| 640 | ['s'] = HTTP_FLG_TOK, |
| 641 | ['t'] = HTTP_FLG_TOK, |
| 642 | ['u'] = HTTP_FLG_TOK, |
| 643 | ['v'] = HTTP_FLG_TOK, |
| 644 | ['w'] = HTTP_FLG_TOK, |
| 645 | ['x'] = HTTP_FLG_TOK, |
| 646 | ['y'] = HTTP_FLG_TOK, |
| 647 | ['z'] = HTTP_FLG_TOK, |
| 648 | ['{'] = HTTP_FLG_SEP, |
| 649 | ['|'] = HTTP_FLG_TOK, |
| 650 | ['}'] = HTTP_FLG_SEP, |
| 651 | ['~'] = HTTP_FLG_TOK, |
| 652 | [127] = HTTP_FLG_CTL, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 653 | }; |
| 654 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 655 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 656 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 657 | * 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] | 658 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 659 | * of headers is automatically adjusted. The number of bytes added is returned |
| 660 | * on success, otherwise <0 is returned indicating an error. |
| 661 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 662 | 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] | 663 | { |
| 664 | int bytes, len; |
| 665 | |
| 666 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 667 | 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] | 668 | if (!bytes) |
| 669 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 670 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 671 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 672 | } |
| 673 | |
| 674 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 675 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 676 | * 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] | 677 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 678 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 679 | * of headers is automatically adjusted. The number of bytes added is returned |
| 680 | * on success, otherwise <0 is returned indicating an error. |
| 681 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 682 | int http_header_add_tail2(struct http_msg *msg, |
| 683 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 684 | { |
| 685 | int bytes; |
| 686 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 687 | 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] | 688 | if (!bytes) |
| 689 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 690 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 691 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 692 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 693 | |
| 694 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 695 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 696 | * If so, returns the position of the first non-space character relative to |
| 697 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 698 | * to return a pointer to the place after the first space. Returns 0 if the |
| 699 | * header name does not match. Checks are case-insensitive. |
| 700 | */ |
| 701 | int http_header_match2(const char *hdr, const char *end, |
| 702 | const char *name, int len) |
| 703 | { |
| 704 | const char *val; |
| 705 | |
| 706 | if (hdr + len >= end) |
| 707 | return 0; |
| 708 | if (hdr[len] != ':') |
| 709 | return 0; |
| 710 | if (strncasecmp(hdr, name, len) != 0) |
| 711 | return 0; |
| 712 | val = hdr + len + 1; |
| 713 | while (val < end && HTTP_IS_SPHT(*val)) |
| 714 | val++; |
| 715 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 716 | return len + 2; /* we may replace starting from second space */ |
| 717 | return val - hdr; |
| 718 | } |
| 719 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 720 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 721 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 722 | * structure holds everything necessary to use the header and find next |
| 723 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 724 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 725 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 726 | * http_find_header2() except that it is designed to work with full-line headers |
| 727 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 728 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 729 | * header is rescanned. This allows rescanning after a header deletion. |
| 730 | */ |
| 731 | int http_find_full_header2(const char *name, int len, |
| 732 | char *sol, struct hdr_idx *idx, |
| 733 | struct hdr_ctx *ctx) |
| 734 | { |
| 735 | char *eol, *sov; |
| 736 | int cur_idx, old_idx; |
| 737 | |
| 738 | cur_idx = ctx->idx; |
| 739 | if (cur_idx) { |
| 740 | /* We have previously returned a header, let's search another one */ |
| 741 | sol = ctx->line; |
| 742 | eol = sol + idx->v[cur_idx].len; |
| 743 | goto next_hdr; |
| 744 | } |
| 745 | |
| 746 | /* first request for this header */ |
| 747 | sol += hdr_idx_first_pos(idx); |
| 748 | old_idx = 0; |
| 749 | cur_idx = hdr_idx_first_idx(idx); |
| 750 | while (cur_idx) { |
| 751 | eol = sol + idx->v[cur_idx].len; |
| 752 | |
| 753 | if (len == 0) { |
| 754 | /* No argument was passed, we want any header. |
| 755 | * To achieve this, we simply build a fake request. */ |
| 756 | while (sol + len < eol && sol[len] != ':') |
| 757 | len++; |
| 758 | name = sol; |
| 759 | } |
| 760 | |
| 761 | if ((len < eol - sol) && |
| 762 | (sol[len] == ':') && |
| 763 | (strncasecmp(sol, name, len) == 0)) { |
| 764 | ctx->del = len; |
| 765 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 766 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 767 | sov++; |
| 768 | |
| 769 | ctx->line = sol; |
| 770 | ctx->prev = old_idx; |
| 771 | ctx->idx = cur_idx; |
| 772 | ctx->val = sov - sol; |
| 773 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 774 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 775 | eol--; |
| 776 | ctx->tws++; |
| 777 | } |
| 778 | ctx->vlen = eol - sov; |
| 779 | return 1; |
| 780 | } |
| 781 | next_hdr: |
| 782 | sol = eol + idx->v[cur_idx].cr + 1; |
| 783 | old_idx = cur_idx; |
| 784 | cur_idx = idx->v[cur_idx].next; |
| 785 | } |
| 786 | return 0; |
| 787 | } |
| 788 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 789 | /* Find the first or next header field in message buffer <sol> using headers |
| 790 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 791 | * everything necessary to use the header and find next occurrence. If its |
| 792 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 793 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 794 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 795 | * no header name. |
| 796 | */ |
| 797 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 798 | { |
| 799 | char *eol, *sov; |
| 800 | int cur_idx, old_idx; |
| 801 | int len; |
| 802 | |
| 803 | cur_idx = ctx->idx; |
| 804 | if (cur_idx) { |
| 805 | /* We have previously returned a header, let's search another one */ |
| 806 | sol = ctx->line; |
| 807 | eol = sol + idx->v[cur_idx].len; |
| 808 | goto next_hdr; |
| 809 | } |
| 810 | |
| 811 | /* first request for this header */ |
| 812 | sol += hdr_idx_first_pos(idx); |
| 813 | old_idx = 0; |
| 814 | cur_idx = hdr_idx_first_idx(idx); |
| 815 | while (cur_idx) { |
| 816 | eol = sol + idx->v[cur_idx].len; |
| 817 | |
| 818 | len = 0; |
| 819 | while (1) { |
| 820 | if (len >= eol - sol) |
| 821 | goto next_hdr; |
| 822 | if (sol[len] == ':') |
| 823 | break; |
| 824 | len++; |
| 825 | } |
| 826 | |
| 827 | ctx->del = len; |
| 828 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 829 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 830 | sov++; |
| 831 | |
| 832 | ctx->line = sol; |
| 833 | ctx->prev = old_idx; |
| 834 | ctx->idx = cur_idx; |
| 835 | ctx->val = sov - sol; |
| 836 | ctx->tws = 0; |
| 837 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 838 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 839 | eol--; |
| 840 | ctx->tws++; |
| 841 | } |
| 842 | ctx->vlen = eol - sov; |
| 843 | return 1; |
| 844 | |
| 845 | next_hdr: |
| 846 | sol = eol + idx->v[cur_idx].cr + 1; |
| 847 | old_idx = cur_idx; |
| 848 | cur_idx = idx->v[cur_idx].next; |
| 849 | } |
| 850 | return 0; |
| 851 | } |
| 852 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 853 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 854 | * par 3.2 for more information. Note that it requires a valid header to return |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 855 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 856 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 857 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 858 | { |
| 859 | int quoted, qdpair; |
| 860 | |
| 861 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 862 | |
| 863 | #if defined(__x86_64__) || \ |
| 864 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 865 | defined(__ARM_ARCH_7A__) |
| 866 | /* speedup: skip everything not a comma nor a double quote */ |
| 867 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 868 | unsigned int c = *(int *)s; // comma |
| 869 | unsigned int q = c; // quote |
| 870 | |
| 871 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 872 | q ^= 0x22222222; // contains one zero on a quote |
| 873 | |
| 874 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 875 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 876 | |
| 877 | if ((c | q) & 0x80808080) |
| 878 | break; // found a comma or a quote |
| 879 | } |
| 880 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 881 | for (; s < e; s++) { |
| 882 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 883 | else if (quoted) { |
| 884 | if (*s == '\\') qdpair = 1; |
| 885 | else if (*s == '"') quoted = 0; |
| 886 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 887 | else if (*s == '"') quoted = 1; |
| 888 | else if (*s == ',') return s; |
| 889 | } |
| 890 | return s; |
| 891 | } |
| 892 | |
| 893 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 894 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 895 | * structure holds everything necessary to use the header and find next |
| 896 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 897 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 898 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 899 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 900 | * is NULL when searching for a new values of a header, the current header is |
| 901 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 902 | */ |
| 903 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 904 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 905 | struct hdr_ctx *ctx) |
| 906 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 907 | char *eol, *sov; |
| 908 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 909 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 910 | cur_idx = ctx->idx; |
| 911 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 912 | /* We have previously returned a value, let's search |
| 913 | * another one on the same line. |
| 914 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 915 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 916 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 917 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 918 | eol = sol + idx->v[cur_idx].len; |
| 919 | |
| 920 | if (sov >= eol) |
| 921 | /* no more values in this header */ |
| 922 | goto next_hdr; |
| 923 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 924 | /* values remaining for this header, skip the comma but save it |
| 925 | * for later use (eg: for header deletion). |
| 926 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 927 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 928 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 929 | sov++; |
| 930 | |
| 931 | goto return_hdr; |
| 932 | } |
| 933 | |
| 934 | /* first request for this header */ |
| 935 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 936 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 937 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 938 | while (cur_idx) { |
| 939 | eol = sol + idx->v[cur_idx].len; |
| 940 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 941 | if (len == 0) { |
| 942 | /* No argument was passed, we want any header. |
| 943 | * To achieve this, we simply build a fake request. */ |
| 944 | while (sol + len < eol && sol[len] != ':') |
| 945 | len++; |
| 946 | name = sol; |
| 947 | } |
| 948 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 949 | if ((len < eol - sol) && |
| 950 | (sol[len] == ':') && |
| 951 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 952 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 953 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 954 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 955 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 956 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 957 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 958 | ctx->prev = old_idx; |
| 959 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 960 | ctx->idx = cur_idx; |
| 961 | ctx->val = sov - sol; |
| 962 | |
| 963 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 964 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 965 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 966 | eol--; |
| 967 | ctx->tws++; |
| 968 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 969 | ctx->vlen = eol - sov; |
| 970 | return 1; |
| 971 | } |
| 972 | next_hdr: |
| 973 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 974 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 975 | cur_idx = idx->v[cur_idx].next; |
| 976 | } |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 981 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 982 | struct hdr_ctx *ctx) |
| 983 | { |
| 984 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 985 | } |
| 986 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 987 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 988 | * the http_find_header functions. The value is removed, as well as surrounding |
| 989 | * 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] | 990 | * 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] | 991 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 992 | * no more header, so any processing may stop. The ctx is always left in a form |
| 993 | * that can be handled by http_find_header2() to find next occurrence. |
| 994 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 995 | 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] | 996 | { |
| 997 | int cur_idx = ctx->idx; |
| 998 | char *sol = ctx->line; |
| 999 | struct hdr_idx_elem *hdr; |
| 1000 | int delta, skip_comma; |
| 1001 | |
| 1002 | if (!cur_idx) |
| 1003 | return 0; |
| 1004 | |
| 1005 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1006 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1007 | /* 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] | 1008 | 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] | 1009 | http_msg_move_end(msg, delta); |
| 1010 | idx->used--; |
| 1011 | hdr->len = 0; /* unused entry */ |
| 1012 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 1013 | if (idx->tail == ctx->idx) |
| 1014 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1015 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 1016 | 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] | 1017 | 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] | 1018 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1019 | return ctx->idx; |
| 1020 | } |
| 1021 | |
| 1022 | /* 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] | 1023 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 1024 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1025 | */ |
| 1026 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1027 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1028 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1029 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1030 | NULL, 0); |
| 1031 | hdr->len += delta; |
| 1032 | http_msg_move_end(msg, delta); |
| 1033 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 1034 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 1035 | return ctx->idx; |
| 1036 | } |
| 1037 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1038 | /* This function handles a server error at the stream interface level. The |
| 1039 | * stream interface is assumed to be already in a closed state. An optional |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1040 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1041 | * 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] | 1042 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1043 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1044 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1045 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1046 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1047 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1048 | channel_auto_read(si_oc(si)); |
| 1049 | channel_abort(si_oc(si)); |
| 1050 | channel_auto_close(si_oc(si)); |
| 1051 | channel_erase(si_oc(si)); |
| 1052 | channel_auto_close(si_ic(si)); |
| 1053 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1054 | if (msg) |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1055 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1056 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 1057 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1058 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 1059 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1062 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1063 | * and message. |
| 1064 | */ |
| 1065 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1066 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1067 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1068 | const int msgnum = http_get_status_idx(s->txn->status); |
| 1069 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1070 | if (s->be->errmsg[msgnum].str) |
| 1071 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1072 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 1073 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1074 | else |
| 1075 | return &http_err_chunks[msgnum]; |
| 1076 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1077 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1078 | void |
| 1079 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 1080 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1081 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 1082 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1083 | stream_int_retnclose(&s->si[0], msg); |
| 1084 | } |
| 1085 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1086 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 1087 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 1088 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1089 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 1090 | 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] | 1091 | { |
| 1092 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1094 | |
| 1095 | m = ((unsigned)*str - 'A'); |
| 1096 | |
| 1097 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | for (h = http_methods[m]; h->len > 0; h++) { |
| 1099 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1100 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1101 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1102 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1103 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1104 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 1105 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1108 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 1109 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 1110 | * It is returned otherwise. |
| 1111 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 1112 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1113 | { |
| 1114 | char *ptr, *end; |
| 1115 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1116 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1117 | end = ptr + txn->req.sl.rq.u_l; |
| 1118 | |
| 1119 | if (ptr >= end) |
| 1120 | return NULL; |
| 1121 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 1122 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1123 | * Request-URI = "*" | absuri | abspath | authority |
| 1124 | */ |
| 1125 | |
| 1126 | if (*ptr == '*') |
| 1127 | return NULL; |
| 1128 | |
| 1129 | if (isalpha((unsigned char)*ptr)) { |
| 1130 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1131 | ptr++; |
| 1132 | while (ptr < end && |
| 1133 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1134 | ptr++; |
| 1135 | /* skip '://' */ |
| 1136 | if (ptr == end || *ptr++ != ':') |
| 1137 | return NULL; |
| 1138 | if (ptr == end || *ptr++ != '/') |
| 1139 | return NULL; |
| 1140 | if (ptr == end || *ptr++ != '/') |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1144 | |
| 1145 | while (ptr < end && *ptr != '/') |
| 1146 | ptr++; |
| 1147 | |
| 1148 | if (ptr == end) |
| 1149 | return NULL; |
| 1150 | |
| 1151 | /* OK, we got the '/' ! */ |
| 1152 | return ptr; |
| 1153 | } |
| 1154 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1155 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1156 | * If not found, return NULL. It is returned otherwise. |
| 1157 | */ |
| 1158 | static char * |
| 1159 | http_get_path_from_string(char *str) |
| 1160 | { |
| 1161 | char *ptr = str; |
| 1162 | |
| 1163 | /* RFC2616, par. 5.1.2 : |
| 1164 | * Request-URI = "*" | absuri | abspath | authority |
| 1165 | */ |
| 1166 | |
| 1167 | if (*ptr == '*') |
| 1168 | return NULL; |
| 1169 | |
| 1170 | if (isalpha((unsigned char)*ptr)) { |
| 1171 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1172 | ptr++; |
| 1173 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1174 | ptr++; |
| 1175 | /* skip '://' */ |
| 1176 | if (*ptr == '\0' || *ptr++ != ':') |
| 1177 | return NULL; |
| 1178 | if (*ptr == '\0' || *ptr++ != '/') |
| 1179 | return NULL; |
| 1180 | if (*ptr == '\0' || *ptr++ != '/') |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1184 | |
| 1185 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1186 | ptr++; |
| 1187 | |
| 1188 | if (*ptr == '\0' || *ptr == ' ') |
| 1189 | return NULL; |
| 1190 | |
| 1191 | /* OK, we got the '/' ! */ |
| 1192 | return ptr; |
| 1193 | } |
| 1194 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1195 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1196 | * in redirect mode. This may only be called just after the stream interface |
| 1197 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1198 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1199 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1200 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1201 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1202 | { |
| 1203 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1204 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1205 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1206 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1207 | |
| 1208 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1209 | trash.len = strlen(HTTP_302); |
| 1210 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1211 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1212 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1213 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1214 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1215 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1216 | return; |
| 1217 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1218 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1219 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1220 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1221 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1222 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1223 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1224 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1225 | * to temporarily rewind the buffer. |
| 1226 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1227 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1228 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1229 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1230 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1231 | 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] | 1232 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1233 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1234 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1235 | if (!path) |
| 1236 | return; |
| 1237 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1238 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1239 | return; |
| 1240 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1241 | memcpy(trash.str + trash.len, path, len); |
| 1242 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1243 | |
| 1244 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1245 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1246 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1247 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1248 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1249 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1250 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1251 | |
| 1252 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1253 | si_shutr(si); |
| 1254 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1255 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1256 | si->state = SI_ST_CLO; |
| 1257 | |
| 1258 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1259 | txn->status = 302; |
| 1260 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1261 | |
| 1262 | /* 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] | 1263 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1264 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1267 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1268 | * that the server side is closed. Note that err_type is actually a |
| 1269 | * bitmask, where almost only aborts may be cumulated with other |
| 1270 | * values. We consider that aborted operations are more important |
| 1271 | * than timeouts or errors due to the fact that nobody else in the |
| 1272 | * logs might explain incomplete retries. All others should avoid |
| 1273 | * being cumulated. It should normally not be possible to have multiple |
| 1274 | * 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] | 1275 | * Note that connection errors appearing on the second request of a keep-alive |
| 1276 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1277 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1278 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1279 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1280 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1281 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1282 | /* set s->txn->status for http_error_message(s) */ |
| 1283 | s->txn->status = 503; |
| 1284 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1285 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1286 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1287 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1288 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1289 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1290 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1291 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1292 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1293 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1294 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1295 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1296 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1297 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1298 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1299 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1300 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1301 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1302 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1303 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1304 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1305 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1306 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1307 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1308 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1309 | http_error_message(s)); |
| 1310 | else { /* SI_ET_CONN_OTHER and others */ |
| 1311 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1312 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1313 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1314 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1317 | extern const char sess_term_cond[8]; |
| 1318 | extern const char sess_fin_state[8]; |
| 1319 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1320 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1321 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1322 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1323 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1324 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1325 | /* |
| 1326 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1327 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1328 | */ |
| 1329 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1330 | char **cap, struct cap_hdr *cap_hdr) |
| 1331 | { |
| 1332 | char *eol, *sol, *col, *sov; |
| 1333 | int cur_idx; |
| 1334 | struct cap_hdr *h; |
| 1335 | int len; |
| 1336 | |
| 1337 | sol = som + hdr_idx_first_pos(idx); |
| 1338 | cur_idx = hdr_idx_first_idx(idx); |
| 1339 | |
| 1340 | while (cur_idx) { |
| 1341 | eol = sol + idx->v[cur_idx].len; |
| 1342 | |
| 1343 | col = sol; |
| 1344 | while (col < eol && *col != ':') |
| 1345 | col++; |
| 1346 | |
| 1347 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1348 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1349 | sov++; |
| 1350 | |
| 1351 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1352 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1353 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1354 | if (cap[h->index] == NULL) |
| 1355 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1356 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1357 | |
| 1358 | if (cap[h->index] == NULL) { |
| 1359 | Alert("HTTP capture : out of memory.\n"); |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | len = eol - sov; |
| 1364 | if (len > h->len) |
| 1365 | len = h->len; |
| 1366 | |
| 1367 | memcpy(cap[h->index], sov, len); |
| 1368 | cap[h->index][len]=0; |
| 1369 | } |
| 1370 | } |
| 1371 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1372 | cur_idx = idx->v[cur_idx].next; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1377 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1378 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1379 | #define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1380 | |
| 1381 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1382 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1383 | */ |
| 1384 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1385 | ptr++; \ |
| 1386 | if (likely(ptr < end)) \ |
| 1387 | goto good; \ |
| 1388 | else { \ |
| 1389 | state = (st); \ |
| 1390 | goto http_msg_ood; \ |
| 1391 | } \ |
| 1392 | } while (0) |
| 1393 | |
| 1394 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1395 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1396 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1397 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1398 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1399 | * will give undefined results. |
| 1400 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1401 | * and that msg->sol points to the beginning of the response. |
| 1402 | * If a complete line is found (which implies that at least one CR or LF is |
| 1403 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1404 | * returned indicating an incomplete line (which does not mean that parts have |
| 1405 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1406 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1407 | * upon next call. |
| 1408 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1409 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1410 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1411 | * 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] | 1412 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1413 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1414 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1415 | enum ht_state state, const char *ptr, const char *end, |
| 1416 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1417 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1418 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1420 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1421 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1422 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1423 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1424 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1425 | |
| 1426 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1427 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1428 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1429 | } |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1430 | msg->err_state = HTTP_MSG_RPVER; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1431 | state = HTTP_MSG_ERROR; |
| 1432 | break; |
| 1433 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1434 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1435 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1436 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1437 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1438 | goto http_msg_rpcode; |
| 1439 | } |
| 1440 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1441 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1442 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1443 | msg->err_state = HTTP_MSG_RPVER_SP; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1444 | state = HTTP_MSG_ERROR; |
| 1445 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1447 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1448 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1449 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1450 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1451 | |
| 1452 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1453 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1454 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1455 | } |
| 1456 | |
| 1457 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1458 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1459 | http_msg_rsp_reason: |
| 1460 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1461 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1462 | msg->sl.st.r_l = 0; |
| 1463 | goto http_msg_rpline_eol; |
| 1464 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1465 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1466 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1467 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1468 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1469 | goto http_msg_rpreason; |
| 1470 | } |
| 1471 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1472 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1473 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1474 | goto http_msg_rsp_reason; |
| 1475 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1476 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1477 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1478 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1479 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1480 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1481 | http_msg_rpline_eol: |
| 1482 | /* We have seen the end of line. Note that we do not |
| 1483 | * necessarily have the \n yet, but at least we know that we |
| 1484 | * have EITHER \r OR \n, otherwise the response would not be |
| 1485 | * complete. We can then record the response length and return |
| 1486 | * to the caller which will be able to register it. |
| 1487 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1488 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1489 | return ptr; |
| 1490 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1491 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1492 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1493 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1494 | exit(1); |
| 1495 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1496 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1500 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1501 | if (ret_state) |
| 1502 | *ret_state = state; |
| 1503 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1504 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1505 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1508 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1509 | * This function parses a request line between <ptr> and <end>, starting with |
| 1510 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1511 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1512 | * will give undefined results. |
| 1513 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1514 | * and that msg->sol points to the beginning of the request. |
| 1515 | * If a complete line is found (which implies that at least one CR or LF is |
| 1516 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1517 | * returned indicating an incomplete line (which does not mean that parts have |
| 1518 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1519 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1520 | * upon next call. |
| 1521 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1522 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1524 | * 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] | 1525 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1526 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1527 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1528 | enum ht_state state, const char *ptr, const char *end, |
| 1529 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1530 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1531 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1532 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1533 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1535 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1537 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1538 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1540 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1542 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1544 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1545 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1546 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1547 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1548 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1550 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1552 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | msg->sl.rq.v_l = 0; |
| 1554 | goto http_msg_rqline_eol; |
| 1555 | } |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1556 | msg->err_state = HTTP_MSG_RQMETH; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1557 | state = HTTP_MSG_ERROR; |
| 1558 | break; |
| 1559 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1561 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1563 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | goto http_msg_rquri; |
| 1565 | } |
| 1566 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1567 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1568 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1569 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1570 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1572 | http_msg_rquri: |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1573 | #if defined(__x86_64__) || \ |
| 1574 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 1575 | defined(__ARM_ARCH_7A__) |
| 1576 | /* speedup: skip bytes not between 0x21 and 0x7e inclusive */ |
| 1577 | while (ptr <= end - sizeof(int)) { |
| 1578 | int x = *(int *)ptr - 0x21212121; |
| 1579 | if (x & 0x80808080) |
| 1580 | break; |
| 1581 | |
| 1582 | x -= 0x5e5e5e5e; |
| 1583 | if (!(x & 0x80808080)) |
| 1584 | break; |
| 1585 | |
| 1586 | ptr += sizeof(int); |
| 1587 | } |
| 1588 | #endif |
Willy Tarreau | 2afff9c | 2017-01-04 14:44:46 +0100 | [diff] [blame] | 1589 | if (ptr >= end) { |
| 1590 | state = HTTP_MSG_RQURI; |
| 1591 | goto http_msg_ood; |
| 1592 | } |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1593 | http_msg_rquri2: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1594 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 5f10ea3 | 2016-11-05 17:52:06 +0100 | [diff] [blame] | 1595 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1598 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1600 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1601 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1602 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1603 | /* non-ASCII chars are forbidden unless option |
| 1604 | * accept-invalid-http-request is enabled in the frontend. |
| 1605 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1606 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1607 | if (msg->err_pos < -1) |
| 1608 | goto invalid_char; |
| 1609 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1610 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1611 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1612 | } |
| 1613 | |
| 1614 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1615 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1616 | goto http_msg_req09_uri_e; |
| 1617 | } |
| 1618 | |
| 1619 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1620 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1621 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1622 | msg->err_state = HTTP_MSG_RQURI; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1623 | state = HTTP_MSG_ERROR; |
| 1624 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1627 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1628 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1629 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | goto http_msg_rqver; |
| 1631 | } |
| 1632 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1633 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1634 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1635 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1638 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1639 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1641 | |
| 1642 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1643 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1644 | http_msg_rqline_eol: |
| 1645 | /* We have seen the end of line. Note that we do not |
| 1646 | * necessarily have the \n yet, but at least we know that we |
| 1647 | * have EITHER \r OR \n, otherwise the request would not be |
| 1648 | * complete. We can then record the request length and return |
| 1649 | * to the caller which will be able to register it. |
| 1650 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1651 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1652 | return ptr; |
| 1653 | } |
| 1654 | |
| 1655 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1656 | msg->err_state = HTTP_MSG_RQVER; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1657 | state = HTTP_MSG_ERROR; |
| 1658 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1659 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1660 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1661 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1663 | exit(1); |
| 1664 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1665 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1669 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | if (ret_state) |
| 1671 | *ret_state = state; |
| 1672 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1673 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1676 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1677 | /* |
| 1678 | * Returns the data from Authorization header. Function may be called more |
| 1679 | * than once so data is stored in txn->auth_data. When no header is found |
| 1680 | * 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] | 1681 | * searching again for something we are unable to find anyway. However, if |
| 1682 | * 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] | 1683 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1684 | */ |
| 1685 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1686 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1687 | * set according to global.tune.bufsize. |
| 1688 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1689 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1690 | |
| 1691 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1692 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1693 | { |
| 1694 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1695 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1696 | struct chunk auth_method; |
| 1697 | struct hdr_ctx ctx; |
| 1698 | char *h, *p; |
| 1699 | int len; |
| 1700 | |
| 1701 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1702 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1703 | #endif |
| 1704 | |
| 1705 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1706 | return 0; |
| 1707 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1708 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1709 | |
| 1710 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1711 | |
| 1712 | if (txn->flags & TX_USE_PX_CONN) { |
| 1713 | h = "Proxy-Authorization"; |
| 1714 | len = strlen(h); |
| 1715 | } else { |
| 1716 | h = "Authorization"; |
| 1717 | len = strlen(h); |
| 1718 | } |
| 1719 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1720 | 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] | 1721 | return 0; |
| 1722 | |
| 1723 | h = ctx.line + ctx.val; |
| 1724 | |
| 1725 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1726 | len = p - h; |
| 1727 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1728 | return 0; |
| 1729 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1730 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1731 | return 0; |
| 1732 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1733 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1734 | |
| 1735 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1736 | |
| 1737 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1738 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1739 | |
| 1740 | if (len < 0) |
| 1741 | return 0; |
| 1742 | |
| 1743 | |
| 1744 | get_http_auth_buff[len] = '\0'; |
| 1745 | |
| 1746 | p = strchr(get_http_auth_buff, ':'); |
| 1747 | |
| 1748 | if (!p) |
| 1749 | return 0; |
| 1750 | |
| 1751 | txn->auth.user = get_http_auth_buff; |
| 1752 | *p = '\0'; |
| 1753 | txn->auth.pass = p+1; |
| 1754 | |
| 1755 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1756 | return 1; |
| 1757 | } |
| 1758 | |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1762 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1763 | /* |
| 1764 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1765 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1766 | * ensuring that the message does not wrap. The function can be preempted |
| 1767 | * everywhere when data are missing and recalled at the exact same location |
| 1768 | * with no information loss. The message may even be realigned between two |
| 1769 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1770 | * 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] | 1771 | * fields. Note that msg->sol will be initialized after completing the first |
| 1772 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1773 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1774 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1775 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1777 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1779 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1780 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1781 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1782 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1783 | ptr = buf->p + msg->next; |
| 1784 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1785 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | if (unlikely(ptr >= end)) |
| 1787 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1788 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1790 | /* |
| 1791 | * First, states that are specific to the response only. |
| 1792 | * We check them first so that request and headers are |
| 1793 | * closer to each other (accessed more often). |
| 1794 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1795 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1796 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1797 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1798 | /* we have a start of message, but we have to check |
| 1799 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1800 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1801 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1802 | if (unlikely(ptr != buf->p)) { |
| 1803 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1804 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1805 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1806 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1807 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1808 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1809 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1810 | hdr_idx_init(idx); |
| 1811 | state = HTTP_MSG_RPVER; |
| 1812 | goto http_msg_rpver; |
| 1813 | } |
| 1814 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1815 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 1816 | state = HTTP_MSG_RPBEFORE; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1817 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1818 | } |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1819 | |
| 1820 | if (unlikely(*ptr == '\n')) |
| 1821 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1822 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1823 | /* stop here */ |
| 1824 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1825 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1826 | http_msg_rpbefore_cr: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1827 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1828 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1829 | /* stop here */ |
| 1830 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1831 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1832 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1833 | case HTTP_MSG_RPVER_SP: |
| 1834 | case HTTP_MSG_RPCODE: |
| 1835 | case HTTP_MSG_RPCODE_SP: |
| 1836 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1837 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1838 | state, ptr, end, |
| 1839 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1840 | if (unlikely(!ptr)) |
| 1841 | return; |
| 1842 | |
| 1843 | /* we have a full response and we know that we have either a CR |
| 1844 | * or an LF at <ptr>. |
| 1845 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1846 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1847 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1848 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1849 | if (likely(*ptr == '\r')) |
| 1850 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1851 | goto http_msg_rpline_end; |
| 1852 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1853 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1854 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1855 | /* msg->sol must point to the first of CR or LF. */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1856 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1857 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1858 | /* stop here */ |
| 1859 | |
| 1860 | /* |
| 1861 | * Second, states that are specific to the request only |
| 1862 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1864 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1865 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1866 | /* we have a start of message, but we have to check |
| 1867 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1868 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1869 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1870 | if (likely(ptr != buf->p)) { |
| 1871 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1872 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1873 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1874 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1875 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1876 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1877 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1878 | state = HTTP_MSG_RQMETH; |
| 1879 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1880 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1881 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1882 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 1883 | state = HTTP_MSG_RQBEFORE; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1884 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1885 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1887 | if (unlikely(*ptr == '\n')) |
| 1888 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1889 | 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] | 1890 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1891 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1892 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1893 | http_msg_rqbefore_cr: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1894 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1895 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1896 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1897 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1898 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1899 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1900 | case HTTP_MSG_RQMETH_SP: |
| 1901 | case HTTP_MSG_RQURI: |
| 1902 | case HTTP_MSG_RQURI_SP: |
| 1903 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1904 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1905 | state, ptr, end, |
| 1906 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | if (unlikely(!ptr)) |
| 1908 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1909 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1910 | /* we have a full request and we know that we have either a CR |
| 1911 | * or an LF at <ptr>. |
| 1912 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1913 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1914 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1915 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1916 | if (likely(*ptr == '\r')) |
| 1917 | 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] | 1918 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1919 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1920 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1921 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1922 | /* check for HTTP/0.9 request : no version information available. |
| 1923 | * msg->sol must point to the first of CR or LF. |
| 1924 | */ |
| 1925 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1926 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1927 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1928 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1929 | 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] | 1930 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1931 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1932 | /* |
| 1933 | * Common states below |
| 1934 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1935 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1936 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1937 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1938 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1939 | goto http_msg_hdr_name; |
| 1940 | } |
| 1941 | |
| 1942 | if (likely(*ptr == '\r')) |
| 1943 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1944 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1946 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1947 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1948 | /* assumes msg->sol points to the first char */ |
| 1949 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1950 | 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] | 1951 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1952 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1953 | 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] | 1954 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1955 | if (likely(msg->err_pos < -1) || *ptr == '\n') { |
| 1956 | state = HTTP_MSG_HDR_NAME; |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1957 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1958 | } |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1959 | |
| 1960 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1961 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1962 | |
| 1963 | /* and we still accept this non-token character */ |
| 1964 | 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] | 1965 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1966 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1967 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1968 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1969 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1970 | 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] | 1971 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1972 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1973 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1974 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1975 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1976 | goto http_msg_hdr_val; |
| 1977 | } |
| 1978 | |
| 1979 | if (likely(*ptr == '\r')) |
| 1980 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1981 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1982 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1983 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1984 | http_msg_hdr_l1_lf: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1985 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1986 | 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] | 1987 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1988 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1989 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1990 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1991 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1992 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1993 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1994 | goto http_msg_hdr_l1_sp; |
| 1995 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1996 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1997 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1998 | goto http_msg_complete_header; |
| 1999 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2000 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2001 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2002 | /* assumes msg->sol points to the first char, and msg->sov |
| 2003 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2004 | */ |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 2005 | |
| 2006 | /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D |
| 2007 | * and lower. In fact since most of the time is spent in the loop, we |
| 2008 | * also remove the sign bit test so that bytes 0x8e..0x0d break the |
| 2009 | * loop, but we don't care since they're very rare in header values. |
| 2010 | */ |
| 2011 | #if defined(__x86_64__) |
| 2012 | while (ptr <= end - sizeof(long)) { |
| 2013 | if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL) |
| 2014 | goto http_msg_hdr_val2; |
| 2015 | ptr += sizeof(long); |
| 2016 | } |
| 2017 | #endif |
| 2018 | #if defined(__x86_64__) || \ |
| 2019 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 2020 | defined(__ARM_ARCH_7A__) |
| 2021 | while (ptr <= end - sizeof(int)) { |
| 2022 | if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080) |
| 2023 | goto http_msg_hdr_val2; |
| 2024 | ptr += sizeof(int); |
| 2025 | } |
| 2026 | #endif |
Willy Tarreau | 2afff9c | 2017-01-04 14:44:46 +0100 | [diff] [blame] | 2027 | if (ptr >= end) { |
| 2028 | state = HTTP_MSG_HDR_VAL; |
| 2029 | goto http_msg_ood; |
| 2030 | } |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 2031 | http_msg_hdr_val2: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2032 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 0431f9d | 2016-11-05 17:35:40 +0100 | [diff] [blame] | 2033 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2034 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2035 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2036 | /* Note: we could also copy eol into ->eoh so that we have the |
| 2037 | * real header end in case it ends with lots of LWS, but is this |
| 2038 | * really needed ? |
| 2039 | */ |
| 2040 | if (likely(*ptr == '\r')) |
| 2041 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 2042 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2043 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2044 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2045 | http_msg_hdr_l2_lf: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2046 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2047 | 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] | 2048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2049 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2050 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2051 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 2052 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2053 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 2054 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2055 | goto http_msg_hdr_val; |
| 2056 | } |
| 2057 | http_msg_complete_header: |
| 2058 | /* |
| 2059 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2060 | * Assumes msg->sol points to the first char, msg->sov points |
| 2061 | * to the first character of the value and msg->eol to the |
| 2062 | * first CR or LF so we know how the line ends. We insert last |
| 2063 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2064 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2065 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2066 | idx, idx->tail) < 0)) { |
| 2067 | state = HTTP_MSG_HDR_L2_LWS; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2068 | goto http_msg_invalid; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2069 | } |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2070 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2071 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2072 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2073 | goto http_msg_hdr_name; |
| 2074 | } |
| 2075 | |
| 2076 | if (likely(*ptr == '\r')) |
| 2077 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2078 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2079 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2080 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 2081 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 2082 | /* Assumes msg->sol points to the first of either CR or LF. |
| 2083 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 2084 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 2085 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2086 | EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2087 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2088 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2089 | msg->eoh = msg->sol; |
| 2090 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 2091 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2092 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2093 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 2094 | |
| 2095 | case HTTP_MSG_ERROR: |
| 2096 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 2097 | break; |
| 2098 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2099 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 2100 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2101 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 2102 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2103 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 2104 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2105 | } |
| 2106 | http_msg_ood: |
| 2107 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2108 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2109 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2110 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2111 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2112 | http_msg_invalid: |
| 2113 | /* invalid message */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2114 | msg->err_state = state; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2115 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2116 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2117 | return; |
| 2118 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2119 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2120 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 2121 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 2122 | * nothing is done and 1 is returned. |
| 2123 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2124 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2125 | { |
| 2126 | int delta; |
| 2127 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2128 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2129 | |
| 2130 | if (msg->sl.rq.v_l != 0) |
| 2131 | return 1; |
| 2132 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 2133 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 2134 | if (txn->meth != HTTP_METH_GET) |
| 2135 | return 0; |
| 2136 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2137 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2138 | |
| 2139 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 2140 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 2141 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2142 | } |
| 2143 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2144 | 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] | 2145 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2146 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2147 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2148 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2149 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2150 | NULL, NULL); |
| 2151 | if (unlikely(!cur_end)) |
| 2152 | return 0; |
| 2153 | |
| 2154 | /* we have a full HTTP/1.0 request now and we know that |
| 2155 | * we have either a CR or an LF at <ptr>. |
| 2156 | */ |
| 2157 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 2158 | return 1; |
| 2159 | } |
| 2160 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2161 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2162 | * and "keep-alive" values. If we already know that some headers may safely |
| 2163 | * 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] | 2164 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 2165 | * - 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] | 2166 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2167 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 2168 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 2169 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2170 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2171 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2172 | 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] | 2173 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2174 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2175 | const char *hdr_val = "Connection"; |
| 2176 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2177 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2178 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2179 | return; |
| 2180 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2181 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2182 | hdr_val = "Proxy-Connection"; |
| 2183 | hdr_len = 16; |
| 2184 | } |
| 2185 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2186 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2187 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2188 | 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] | 2189 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2190 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2191 | if (to_del & 2) |
| 2192 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2193 | else |
| 2194 | txn->flags |= TX_CON_KAL_SET; |
| 2195 | } |
| 2196 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2197 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2198 | if (to_del & 1) |
| 2199 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2200 | else |
| 2201 | txn->flags |= TX_CON_CLO_SET; |
| 2202 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2203 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2204 | txn->flags |= TX_HDR_CONN_UPG; |
| 2205 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2208 | txn->flags |= TX_HDR_CONN_PRS; |
| 2209 | return; |
| 2210 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2211 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2212 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2213 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2214 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2215 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2216 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2217 | 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] | 2218 | { |
| 2219 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2220 | const char *hdr_val = "Connection"; |
| 2221 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2222 | |
| 2223 | ctx.idx = 0; |
| 2224 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2225 | |
| 2226 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2227 | hdr_val = "Proxy-Connection"; |
| 2228 | hdr_len = 16; |
| 2229 | } |
| 2230 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2231 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2232 | 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] | 2233 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2234 | if (wanted & TX_CON_KAL_SET) |
| 2235 | txn->flags |= TX_CON_KAL_SET; |
| 2236 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2237 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2238 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2239 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2240 | if (wanted & TX_CON_CLO_SET) |
| 2241 | txn->flags |= TX_CON_CLO_SET; |
| 2242 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2243 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2244 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2245 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2246 | |
| 2247 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2248 | return; |
| 2249 | |
| 2250 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2251 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2252 | hdr_val = "Connection: close"; |
| 2253 | hdr_len = 17; |
| 2254 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2255 | hdr_val = "Proxy-Connection: close"; |
| 2256 | hdr_len = 23; |
| 2257 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2258 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2262 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2263 | hdr_val = "Connection: keep-alive"; |
| 2264 | hdr_len = 22; |
| 2265 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2266 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2267 | hdr_len = 28; |
| 2268 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2269 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2270 | } |
| 2271 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2274 | /* Parse the chunk size at msg->next. Once done, caller should adjust ->next to |
| 2275 | * point to the first byte of data after the chunk size, so that we know we can |
| 2276 | * forward exactly msg->next bytes. msg->sol contains the exact number of bytes |
| 2277 | * forming the chunk size. That way it is always possible to differentiate |
| 2278 | * between the start of the body and the start of the data. Return the number |
| 2279 | * of byte parsed on success, 0 when some data is missing, <0 on error. Note: |
| 2280 | * 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] | 2281 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2282 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +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 | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2285 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2286 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2287 | const char *end = buf->data + buf->size; |
| 2288 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2289 | unsigned int chunk = 0; |
| 2290 | |
| 2291 | /* The chunk size is in the following form, though we are only |
| 2292 | * interested in the size and CRLF : |
| 2293 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2294 | */ |
| 2295 | while (1) { |
| 2296 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2297 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2298 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2299 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2300 | if (c < 0) /* not a hex digit anymore */ |
| 2301 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2302 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2303 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2304 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2305 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2306 | chunk = (chunk << 4) + c; |
| 2307 | } |
| 2308 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2309 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2310 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2311 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2312 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 2313 | while (HTTP_IS_SPHT(*ptr)) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2314 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2315 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2316 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2317 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2318 | } |
| 2319 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2320 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2321 | * for the end of chunk size. |
| 2322 | */ |
| 2323 | while (1) { |
| 2324 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2325 | /* we now have a CR or an LF at ptr */ |
| 2326 | if (likely(*ptr == '\r')) { |
| 2327 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2328 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2329 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2330 | return 0; |
| 2331 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2332 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2333 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2334 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2335 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2336 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2337 | /* done */ |
| 2338 | break; |
| 2339 | } |
| 2340 | else if (*ptr == ';') { |
| 2341 | /* chunk extension, ends at next CRLF */ |
| 2342 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2343 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2344 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2345 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2346 | |
| 2347 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2348 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2349 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2350 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2351 | return 0; |
| 2352 | } |
| 2353 | /* we have a CRLF now, loop above */ |
| 2354 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2355 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2356 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2357 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2358 | } |
| 2359 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2360 | /* OK we found our CRLF and now <ptr> points to the next byte, which may |
| 2361 | * or may not be present. We save the number of bytes parsed into |
| 2362 | * msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2363 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2364 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2365 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2366 | msg->sol += buf->size; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2367 | msg->chunk_len = chunk; |
| 2368 | msg->body_len += chunk; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2369 | return msg->sol; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2370 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2371 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2372 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2373 | } |
| 2374 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2375 | /* This function skips trailers in the buffer associated with HTTP message |
| 2376 | * <msg>. The first visited position is msg->next. If the end of the trailers is |
| 2377 | * found, the function returns >0. So, the caller can automatically schedul it |
| 2378 | * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough |
| 2379 | * data are available, the function does not change anything except maybe |
| 2380 | * msg->sol if it could parse some lines, and returns zero. If a parse error |
| 2381 | * is encountered, the function returns < 0 and does not change anything except |
| 2382 | * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS |
| 2383 | * state before calling this function, which implies that all non-trailers data |
| 2384 | * have already been scheduled for forwarding, and that msg->next exactly |
| 2385 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2386 | * important to note that this function is designed to be able to parse wrapped |
| 2387 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2388 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2389 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2390 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2391 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2392 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2393 | /* we have msg->next which points to next line. Look for CRLF. But |
| 2394 | * first, we reset msg->sol */ |
| 2395 | msg->sol = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2396 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2397 | const char *p1 = NULL, *p2 = NULL; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2398 | const char *start = b_ptr(buf, msg->next + msg->sol); |
| 2399 | const char *stop = bi_end(buf); |
| 2400 | const char *ptr = start; |
| 2401 | int bytes = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2402 | |
| 2403 | /* scan current line and stop at LF or CRLF */ |
| 2404 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2405 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2406 | return 0; |
| 2407 | |
| 2408 | if (*ptr == '\n') { |
| 2409 | if (!p1) |
| 2410 | p1 = ptr; |
| 2411 | p2 = ptr; |
| 2412 | break; |
| 2413 | } |
| 2414 | |
| 2415 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2416 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2417 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2418 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2419 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2420 | p1 = ptr; |
| 2421 | } |
| 2422 | |
| 2423 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2424 | if (ptr >= buf->data + buf->size) |
| 2425 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | /* after LF; point to beginning of next line */ |
| 2429 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2430 | if (p2 >= buf->data + buf->size) |
| 2431 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2432 | |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2433 | bytes = p2 - start; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2434 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2435 | bytes += buf->size; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2436 | msg->sol += bytes; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2437 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2438 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2439 | * Everything was scheduled for forwarding, there's nothing left |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2440 | * from this message. */ |
| 2441 | if (p1 == start) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2442 | return 1; |
Christopher Faulet | 2fb2880 | 2015-12-01 10:40:57 +0100 | [diff] [blame] | 2443 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2444 | /* OK, next line then */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2445 | } |
| 2446 | } |
| 2447 | |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2448 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
| 2449 | * a possible LF alone at the end of a chunk. The caller should adjust msg->next |
| 2450 | * in order to include this part into the next forwarding phase. Note that the |
| 2451 | * caller must ensure that ->p points to the first byte to parse. It returns |
| 2452 | * the number of bytes parsed on success, so the caller can set msg_state to |
| 2453 | * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not |
| 2454 | * change anything and returns zero. If a parse error is encountered, the |
| 2455 | * function returns < 0. Note: this function is designed to parse wrapped CRLF |
| 2456 | * at the end of the buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2457 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2458 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2459 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2460 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2461 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2462 | int bytes; |
| 2463 | |
| 2464 | /* NB: we'll check data availabilty at the end. It's not a |
| 2465 | * problem because whatever we match first will be checked |
| 2466 | * against the correct length. |
| 2467 | */ |
| 2468 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2469 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2470 | if (*ptr == '\r') { |
| 2471 | bytes++; |
| 2472 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2473 | if (ptr >= buf->data + buf->size) |
| 2474 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2477 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2478 | return 0; |
| 2479 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2480 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2481 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2482 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2483 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 2484 | return bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2485 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2486 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2487 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2488 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2489 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2490 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2491 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2492 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2493 | { |
| 2494 | int q = 1000; |
| 2495 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2496 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2497 | goto out; |
| 2498 | q = (*qvalue++ - '0') * 1000; |
| 2499 | |
| 2500 | if (*qvalue++ != '.') |
| 2501 | goto out; |
| 2502 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2503 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2504 | goto out; |
| 2505 | q += (*qvalue++ - '0') * 100; |
| 2506 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2507 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2508 | goto out; |
| 2509 | q += (*qvalue++ - '0') * 10; |
| 2510 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2511 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2512 | goto out; |
| 2513 | q += (*qvalue++ - '0') * 1; |
| 2514 | out: |
| 2515 | if (q > 1000) |
| 2516 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2517 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2518 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2519 | return q; |
| 2520 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2521 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2522 | 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] | 2523 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2524 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2525 | int tmp = TX_CON_WANT_KAL; |
| 2526 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2527 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2528 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2529 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2530 | tmp = TX_CON_WANT_TUN; |
| 2531 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2532 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2533 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2534 | tmp = TX_CON_WANT_TUN; |
| 2535 | } |
| 2536 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2537 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2538 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2539 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2540 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2541 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2542 | tmp = TX_CON_WANT_CLO; |
| 2543 | else |
| 2544 | tmp = TX_CON_WANT_SCL; |
| 2545 | } |
| 2546 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2547 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2548 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2549 | tmp = TX_CON_WANT_CLO; |
| 2550 | |
| 2551 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2552 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2553 | |
| 2554 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2555 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2556 | /* parse the Connection header and possibly clean it */ |
| 2557 | int to_del = 0; |
| 2558 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2559 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2560 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2561 | to_del |= 2; /* remove "keep-alive" */ |
| 2562 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2563 | to_del |= 1; /* remove "close" */ |
| 2564 | http_parse_connection_header(txn, msg, to_del); |
| 2565 | } |
| 2566 | |
| 2567 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2568 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2569 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2570 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2571 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2572 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2573 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2574 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2575 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2576 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2577 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2578 | * processing can continue on next analysers, or zero if it either needs more |
| 2579 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2580 | * 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] | 2581 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2582 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2583 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2584 | 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] | 2585 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | /* |
| 2587 | * We will parse the partial (or complete) lines. |
| 2588 | * We will check the request syntax, and also join multi-line |
| 2589 | * headers. An index of all the lines will be elaborated while |
| 2590 | * parsing. |
| 2591 | * |
| 2592 | * For the parsing, we use a 28 states FSM. |
| 2593 | * |
| 2594 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2595 | * req->buf->p = beginning of request |
| 2596 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2597 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2598 | * msg->eol = end of current header or line (LF or CRLF) |
| 2599 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2600 | * |
| 2601 | * 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] | 2602 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2603 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2604 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2605 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2606 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2607 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2608 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2609 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2610 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2611 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2612 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2613 | 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] | 2614 | now_ms, __FUNCTION__, |
| 2615 | s, |
| 2616 | req, |
| 2617 | req->rex, req->wex, |
| 2618 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2619 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2620 | req->analysers); |
| 2621 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2622 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2623 | s->srv_error = http_return_srv_error; |
| 2624 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2625 | /* There's a protected area at the end of the buffer for rewriting |
| 2626 | * purposes. We don't want to start to parse the request if the |
| 2627 | * protected area is affected, because we may have to move processed |
| 2628 | * data later, which is much more complicated. |
| 2629 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2630 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2631 | |
| 2632 | /* This point is executed when some data is avalaible for analysis, |
| 2633 | * so we log the end of the idle time. */ |
| 2634 | if (s->logs.t_idle == -1) |
| 2635 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 2636 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2637 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2638 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2639 | 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] | 2640 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2641 | /* 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] | 2642 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2643 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2644 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2645 | return 0; |
| 2646 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2647 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2648 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2649 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2652 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2653 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2656 | /* 1: we might have to print this header in debug mode */ |
| 2657 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2658 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2659 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2660 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2661 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2662 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2663 | /* this is a bit complex : in case of error on the request line, |
| 2664 | * we know that rq.l is still zero, so we display only the part |
| 2665 | * up to the end of the line (truncated by debug_hdr). |
| 2666 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2667 | 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] | 2668 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2669 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2670 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2671 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2672 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2673 | while (cur_idx) { |
| 2674 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2675 | debug_hdr("clihdr", s, sol, eol); |
| 2676 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2677 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2678 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2679 | } |
| 2680 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2681 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2682 | /* |
| 2683 | * Now we quickly check if we have found a full valid request. |
| 2684 | * If not so, we check the FD and buffer states before leaving. |
| 2685 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2686 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2687 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2688 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2689 | * 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] | 2690 | * 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] | 2691 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2692 | * Last, we may increase some tracked counters' http request errors on |
| 2693 | * the cases that are deliberately the client's fault. For instance, |
| 2694 | * a timeout or connection reset is not counted as an error. However |
| 2695 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2697 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2698 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2699 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2700 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2701 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2702 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2703 | stream_inc_http_req_ctr(s); |
| 2704 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2705 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2706 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2707 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2708 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2709 | /* 1: Since we are in header mode, if there's no space |
| 2710 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2711 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2712 | * must terminate it now. |
| 2713 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2714 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2715 | /* FIXME: check if URI is set and return Status |
| 2716 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2717 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2718 | stream_inc_http_req_ctr(s); |
| 2719 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2720 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2721 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2722 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | goto return_bad_req; |
| 2724 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2725 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2726 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2727 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2728 | if (!(s->flags & SF_ERR_MASK)) |
| 2729 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2730 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2731 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2732 | goto failed_keep_alive; |
| 2733 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2734 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2735 | goto failed_keep_alive; |
| 2736 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2737 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2738 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2739 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2740 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2743 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2744 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2745 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2746 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2747 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2748 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2749 | proxy_inc_fe_req_ctr(sess->fe); |
| 2750 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2751 | if (sess->listener->counters) |
| 2752 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2753 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2754 | if (!(s->flags & SF_FINST_MASK)) |
| 2755 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2756 | return 0; |
| 2757 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2758 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2759 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2760 | 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] | 2761 | if (!(s->flags & SF_ERR_MASK)) |
| 2762 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2763 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2764 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2765 | goto failed_keep_alive; |
| 2766 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2767 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2768 | goto failed_keep_alive; |
| 2769 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2770 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2771 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2772 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2773 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2774 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2775 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2776 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2777 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2778 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2779 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2780 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2781 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2782 | proxy_inc_fe_req_ctr(sess->fe); |
| 2783 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2784 | if (sess->listener->counters) |
| 2785 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2787 | if (!(s->flags & SF_FINST_MASK)) |
| 2788 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2789 | return 0; |
| 2790 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2791 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2792 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2793 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2794 | if (!(s->flags & SF_ERR_MASK)) |
| 2795 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2796 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2797 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2798 | goto failed_keep_alive; |
| 2799 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2800 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2801 | goto failed_keep_alive; |
| 2802 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2803 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2804 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2805 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2806 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2807 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2808 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2809 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2810 | stream_inc_http_err_ctr(s); |
| 2811 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2812 | proxy_inc_fe_req_ctr(sess->fe); |
| 2813 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2814 | if (sess->listener->counters) |
| 2815 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2816 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2817 | if (!(s->flags & SF_FINST_MASK)) |
| 2818 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2819 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2822 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2823 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2824 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2825 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2826 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2827 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2828 | /* We need more data, we have to re-enable quick-ack in case we |
| 2829 | * previously disabled it, otherwise we might cause the client |
| 2830 | * to delay next data. |
| 2831 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2832 | 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] | 2833 | } |
| 2834 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2835 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2836 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2837 | /* If the client starts to talk, let's fall back to |
| 2838 | * request timeout processing. |
| 2839 | */ |
| 2840 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2841 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2842 | } |
| 2843 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2844 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2845 | if (!tick_isset(req->analyse_exp)) { |
| 2846 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2847 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2848 | tick_isset(s->be->timeout.httpka)) |
| 2849 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2850 | else |
| 2851 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2852 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2854 | /* we're not ready yet */ |
| 2855 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2856 | |
| 2857 | failed_keep_alive: |
| 2858 | /* Here we process low-level errors for keep-alive requests. In |
| 2859 | * short, if the request is not the first one and it experiences |
| 2860 | * a timeout, read error or shutdown, we just silently close so |
| 2861 | * that the client can try again. |
| 2862 | */ |
| 2863 | txn->status = 0; |
| 2864 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2865 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2866 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2867 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2868 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2869 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2870 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2871 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2872 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2873 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2874 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2875 | * 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] | 2876 | * 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] | 2877 | * 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] | 2878 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2879 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2880 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2881 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2882 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2883 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2884 | 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] | 2885 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2886 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2887 | /* kill the pending keep-alive timeout */ |
| 2888 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2889 | req->analyse_exp = TICK_ETERNITY; |
| 2890 | } |
| 2891 | |
| 2892 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2893 | /* Maybe we found in invalid header name while we were configured not |
| 2894 | * to block on that, so we have to capture it now. |
| 2895 | */ |
| 2896 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2897 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2898 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2899 | /* |
| 2900 | * 1: identify the method |
| 2901 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2902 | 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] | 2903 | |
| 2904 | /* we can make use of server redirect on GET and HEAD */ |
| 2905 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2906 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2908 | /* |
| 2909 | * 2: check if the URI matches the monitor_uri. |
| 2910 | * We have to do this for every request which gets in, because |
| 2911 | * the monitor-uri is defined by the frontend. |
| 2912 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2913 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2914 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2915 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2916 | sess->fe->monitor_uri, |
| 2917 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2918 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2919 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2920 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2921 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2922 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2923 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2924 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2925 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2926 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2927 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2928 | 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] | 2929 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2930 | ret = acl_pass(ret); |
| 2931 | if (cond->pol == ACL_COND_UNLESS) |
| 2932 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2933 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2934 | if (ret) { |
| 2935 | /* we fail this request, let's return 503 service unavail */ |
| 2936 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2937 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2938 | if (!(s->flags & SF_ERR_MASK)) |
| 2939 | 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] | 2940 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2941 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2942 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2943 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2944 | /* nothing to fail, let's reply normaly */ |
| 2945 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2946 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2947 | if (!(s->flags & SF_ERR_MASK)) |
| 2948 | 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] | 2949 | goto return_prx_cond; |
| 2950 | } |
| 2951 | |
| 2952 | /* |
| 2953 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2954 | * Note: we cannot log anymore if the request has been |
| 2955 | * classified as invalid. |
| 2956 | */ |
| 2957 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2958 | /* we have a complete HTTP request that we must log */ |
| 2959 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2960 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2961 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 2962 | if (urilen >= global.tune.requri_len ) |
| 2963 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2964 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2965 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2966 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2967 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2968 | s->do_log(s); |
| 2969 | } else { |
| 2970 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2971 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2972 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2973 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2974 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2975 | * exactly one digit "." one digit. This check may be disabled using |
| 2976 | * option accept-invalid-http-request. |
| 2977 | */ |
| 2978 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 2979 | if (msg->sl.rq.v_l != 8) { |
| 2980 | msg->err_pos = msg->sl.rq.v; |
| 2981 | goto return_bad_req; |
| 2982 | } |
| 2983 | |
| 2984 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 2985 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 2986 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 2987 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2988 | msg->err_pos = msg->sl.rq.v + 4; |
| 2989 | goto return_bad_req; |
| 2990 | } |
| 2991 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2992 | else { |
| 2993 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2994 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2995 | goto return_bad_req; |
| 2996 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2997 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2998 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2999 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3000 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 3001 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 3002 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3003 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3004 | |
| 3005 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3006 | 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] | 3007 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3008 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 3009 | * we have what looks like a proxied connection instead of a connection, |
| 3010 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 3011 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 3012 | * happen to do that despite being non-standard :-( |
| 3013 | * We consider that a request not beginning with either '/' or '*' is |
| 3014 | * a proxied connection, which covers both "scheme://location" and |
| 3015 | * CONNECT ip:port. |
| 3016 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3017 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3018 | 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] | 3019 | txn->flags |= TX_USE_PX_CONN; |
| 3020 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3021 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3022 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3023 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3024 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3025 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3026 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3027 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3028 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3029 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3030 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3031 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3032 | * The length of a message body is determined by one of the following |
| 3033 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3034 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3035 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3036 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3037 | * code is always terminated by the first empty line after the |
| 3038 | * header fields, regardless of the header fields present in the |
| 3039 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3040 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3041 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3042 | * the connection will become a tunnel immediately after the empty |
| 3043 | * line that concludes the header fields. A client MUST ignore any |
| 3044 | * Content-Length or Transfer-Encoding header fields received in |
| 3045 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3046 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3047 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3048 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3049 | * body length is determined by reading and decoding the chunked |
| 3050 | * data until the transfer coding indicates the data is complete. |
| 3051 | * |
| 3052 | * If a Transfer-Encoding header field is present in a response and |
| 3053 | * the chunked transfer coding is not the final encoding, the |
| 3054 | * message body length is determined by reading the connection until |
| 3055 | * it is closed by the server. If a Transfer-Encoding header field |
| 3056 | * is present in a request and the chunked transfer coding is not |
| 3057 | * the final encoding, the message body length cannot be determined |
| 3058 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3059 | * status code and then close the connection. |
| 3060 | * |
| 3061 | * If a message is received with both a Transfer-Encoding and a |
| 3062 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3063 | * Content-Length. Such a message might indicate an attempt to |
| 3064 | * perform request smuggling (Section 9.5) or response splitting |
| 3065 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3066 | * remove the received Content-Length field prior to forwarding such |
| 3067 | * a message downstream. |
| 3068 | * |
| 3069 | * 4. If a message is received without Transfer-Encoding and with |
| 3070 | * either multiple Content-Length header fields having differing |
| 3071 | * field-values or a single Content-Length header field having an |
| 3072 | * invalid value, then the message framing is invalid and the |
| 3073 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3074 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3075 | * status code and then close the connection. If this is a response |
| 3076 | * message received by a proxy, the proxy MUST close the connection |
| 3077 | * to the server, discard the received response, and send a 502 (Bad |
| 3078 | * Gateway) response to the client. If this is a response message |
| 3079 | * received by a user agent, the user agent MUST close the |
| 3080 | * connection to the server and discard the received response. |
| 3081 | * |
| 3082 | * 5. If a valid Content-Length header field is present without |
| 3083 | * Transfer-Encoding, its decimal value defines the expected message |
| 3084 | * body length in octets. If the sender closes the connection or |
| 3085 | * the recipient times out before the indicated number of octets are |
| 3086 | * received, the recipient MUST consider the message to be |
| 3087 | * incomplete and close the connection. |
| 3088 | * |
| 3089 | * 6. If this is a request message and none of the above are true, then |
| 3090 | * the message body length is zero (no message body is present). |
| 3091 | * |
| 3092 | * 7. Otherwise, this is a response message without a declared message |
| 3093 | * body length, so the message body length is determined by the |
| 3094 | * number of octets received prior to the server closing the |
| 3095 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3096 | */ |
| 3097 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3098 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3099 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3100 | 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] | 3101 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3102 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3103 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3104 | /* chunked not last, return badreq */ |
| 3105 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3106 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3107 | } |
| 3108 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3109 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3110 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3111 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3112 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3113 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3114 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3115 | 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] | 3116 | signed long long cl; |
| 3117 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3118 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3119 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3120 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3121 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3122 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3123 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3124 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3125 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3126 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3127 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3128 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3129 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3130 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3131 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3132 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3133 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3134 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3135 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3136 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3137 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3138 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3139 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3140 | } |
| 3141 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3142 | /* even bodyless requests have a known length */ |
| 3143 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3144 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3145 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3146 | * only change if both the request and the config reference something else. |
| 3147 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3148 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3149 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3150 | * if FE and BE have the same settings (common). The method consists in |
| 3151 | * checking if options changed between the two calls (implying that either |
| 3152 | * one is non-null, or one of them is non-null and we are there for the first |
| 3153 | * time. |
| 3154 | */ |
| 3155 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3156 | ((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] | 3157 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3158 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3159 | /* we may have to wait for the request's body */ |
| 3160 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3161 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3162 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3163 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3164 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3165 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3166 | req->analyse_exp = TICK_ETERNITY; |
| 3167 | return 1; |
| 3168 | |
| 3169 | return_bad_req: |
| 3170 | /* We centralize bad requests processing here */ |
| 3171 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3172 | /* we detected a parsing error. We want to archive this request |
| 3173 | * in the dedicated proxy area for later troubleshooting. |
| 3174 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3175 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3176 | } |
| 3177 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3178 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3179 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3180 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3181 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3182 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3183 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3184 | if (sess->listener->counters) |
| 3185 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3186 | |
| 3187 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3188 | if (!(s->flags & SF_ERR_MASK)) |
| 3189 | s->flags |= SF_ERR_PRXCOND; |
| 3190 | if (!(s->flags & SF_FINST_MASK)) |
| 3191 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3192 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3193 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3194 | req->analyse_exp = TICK_ETERNITY; |
| 3195 | return 0; |
| 3196 | } |
| 3197 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3198 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3199 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3200 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3201 | * and program a response message if something was unexpected. It cannot fail |
| 3202 | * 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] | 3203 | * 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] | 3204 | * 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] | 3205 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3206 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3207 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3208 | { |
| 3209 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3210 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3211 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3212 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3213 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3214 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3215 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3216 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3217 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3218 | appctx = si_appctx(si); |
| 3219 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3220 | appctx->st1 = appctx->st2 = 0; |
| 3221 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3222 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3223 | 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] | 3224 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3225 | |
| 3226 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3227 | lookup = uri + uri_auth->uri_len; |
| 3228 | |
| 3229 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3230 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3231 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3232 | break; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | if (uri_auth->refresh) { |
| 3237 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3238 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3239 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3240 | break; |
| 3241 | } |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3246 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3247 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3248 | break; |
| 3249 | } |
| 3250 | } |
| 3251 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 3252 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 3253 | if (memcmp(h, ";typed", 6) == 0) { |
| 3254 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 3255 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3256 | break; |
| 3257 | } |
| 3258 | } |
| 3259 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3260 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3261 | if (memcmp(h, ";st=", 4) == 0) { |
| 3262 | int i; |
| 3263 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3264 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3265 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3266 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3267 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3268 | break; |
| 3269 | } |
| 3270 | } |
| 3271 | break; |
| 3272 | } |
| 3273 | } |
| 3274 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3275 | appctx->ctx.stats.scope_str = 0; |
| 3276 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3277 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3278 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3279 | int itx = 0; |
| 3280 | const char *h2; |
| 3281 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3282 | const char *err; |
| 3283 | |
| 3284 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3285 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3286 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3287 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3288 | itx++; |
| 3289 | h++; |
| 3290 | } |
| 3291 | |
| 3292 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3293 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3294 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3295 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3296 | /* 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] | 3297 | memcpy(scope_txt, h2, itx); |
| 3298 | scope_txt[itx] = '\0'; |
| 3299 | err = invalid_char(scope_txt); |
| 3300 | if (err) { |
| 3301 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3302 | appctx->ctx.stats.scope_str = 0; |
| 3303 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3304 | } |
| 3305 | break; |
| 3306 | } |
| 3307 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3308 | |
| 3309 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3310 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3311 | int ret = 1; |
| 3312 | |
| 3313 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3314 | 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] | 3315 | ret = acl_pass(ret); |
| 3316 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3317 | ret = !ret; |
| 3318 | } |
| 3319 | |
| 3320 | if (ret) { |
| 3321 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3322 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3323 | break; |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3328 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3329 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3330 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3331 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3332 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3333 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3334 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3335 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3336 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3337 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3338 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3339 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3340 | else { |
| 3341 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3342 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3343 | } |
| 3344 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3345 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3346 | return 1; |
| 3347 | } |
| 3348 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3349 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3350 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3351 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3352 | void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3353 | { |
| 3354 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3355 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3356 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3357 | #endif |
| 3358 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3359 | if (from->ss_family == AF_INET6) { |
| 3360 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3361 | /* v4-mapped addresses need IP_TOS */ |
| 3362 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3363 | else |
| 3364 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3365 | } |
| 3366 | #endif |
| 3367 | } |
| 3368 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3369 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3370 | const char* name, unsigned int name_len, |
| 3371 | const char *str, struct my_regex *re, |
| 3372 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3373 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3374 | struct hdr_ctx ctx; |
| 3375 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3376 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3377 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3378 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3379 | struct chunk *output = get_trash_chunk(); |
| 3380 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3381 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3382 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3383 | /* Choose the header browsing function. */ |
| 3384 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3385 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3386 | http_find_hdr_func = http_find_header2; |
| 3387 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3388 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3389 | http_find_hdr_func = http_find_full_header2; |
| 3390 | break; |
| 3391 | default: /* impossible */ |
| 3392 | return -1; |
| 3393 | } |
| 3394 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3395 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3396 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3397 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3398 | char *val = ctx.line + ctx.val; |
| 3399 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3400 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3401 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3402 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3403 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3404 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3405 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3406 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3407 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3408 | 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] | 3409 | |
| 3410 | hdr->len += delta; |
| 3411 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3412 | |
| 3413 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3414 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | return 0; |
| 3418 | } |
| 3419 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3420 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3421 | const char* name, unsigned int name_len, |
| 3422 | struct list *fmt, struct my_regex *re, |
| 3423 | int action) |
| 3424 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3425 | struct chunk *replace; |
| 3426 | int ret = -1; |
| 3427 | |
| 3428 | replace = alloc_trash_chunk(); |
| 3429 | if (!replace) |
| 3430 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3431 | |
| 3432 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3433 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3434 | goto leave; |
| 3435 | |
| 3436 | ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3437 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3438 | leave: |
| 3439 | free_trash_chunk(replace); |
| 3440 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3441 | } |
| 3442 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3443 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3444 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3445 | * further processing of the request (auth, deny, ...), and defaults to |
| 3446 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3447 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3448 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 3449 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 3450 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3451 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3452 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3453 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3454 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3455 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3456 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3457 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3458 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3459 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3460 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3461 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3462 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3463 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3464 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3465 | * resume condition. If a resume is needed it is always in the action |
| 3466 | * and never in the ACL or converters. In this case, we initialise the |
| 3467 | * current rule, and go to the action execution point. |
| 3468 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3469 | if (s->current_rule) { |
| 3470 | rule = s->current_rule; |
| 3471 | s->current_rule = NULL; |
| 3472 | if (s->current_rule_list == rules) |
| 3473 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3474 | } |
| 3475 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3476 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3477 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3478 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3479 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3480 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3481 | int ret; |
| 3482 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3483 | 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] | 3484 | ret = acl_pass(ret); |
| 3485 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3486 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3487 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3488 | |
| 3489 | if (!ret) /* condition not matched */ |
| 3490 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3491 | } |
| 3492 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3493 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3494 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3495 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3496 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3497 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3498 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3499 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3500 | if (deny_status) |
| 3501 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3502 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3503 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3504 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3505 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3506 | if (deny_status) |
| 3507 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3508 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3509 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3510 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3511 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3512 | auth_realm = rule->arg.auth.realm; |
| 3513 | if (!auth_realm) { |
| 3514 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3515 | auth_realm = STATS_DEFAULT_REALM; |
| 3516 | else |
| 3517 | auth_realm = px->id; |
| 3518 | } |
| 3519 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3520 | * count one error, because normal browsing won't significantly |
| 3521 | * increase the counter but brute force attempts will. |
| 3522 | */ |
| 3523 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3524 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3525 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3526 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3527 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3528 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3529 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3530 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3531 | return HTTP_RULE_RES_BADREQ; |
| 3532 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3533 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3534 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3535 | s->task->nice = rule->arg.nice; |
| 3536 | break; |
| 3537 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3538 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3539 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3540 | 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] | 3541 | break; |
| 3542 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3543 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3544 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3545 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3546 | 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] | 3547 | #endif |
| 3548 | break; |
| 3549 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3550 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3551 | s->logs.level = rule->arg.loglevel; |
| 3552 | break; |
| 3553 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3554 | case ACT_HTTP_REPLACE_HDR: |
| 3555 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3556 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3557 | rule->arg.hdr_add.name_len, |
| 3558 | &rule->arg.hdr_add.fmt, |
| 3559 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3560 | return HTTP_RULE_RES_BADREQ; |
| 3561 | break; |
| 3562 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3563 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3564 | ctx.idx = 0; |
| 3565 | /* remove all occurrences of the header */ |
| 3566 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3567 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3568 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3569 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3570 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3571 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3572 | case ACT_HTTP_SET_HDR: |
| 3573 | case ACT_HTTP_ADD_HDR: |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3574 | /* The scope of the trash buffer must be limited to this function. The |
| 3575 | * build_logline() function can execute a lot of other function which |
| 3576 | * can use the trash buffer. So for limiting the scope of this global |
| 3577 | * buffer, we build first the header value using build_logline, and |
| 3578 | * after we store the header name. |
| 3579 | */ |
| 3580 | len = rule->arg.hdr_add.name_len + 2, |
| 3581 | len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3582 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3583 | trash.str[rule->arg.hdr_add.name_len] = ':'; |
| 3584 | trash.str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 3585 | trash.len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3586 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3587 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3588 | /* remove all occurrences of the header */ |
| 3589 | ctx.idx = 0; |
| 3590 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3591 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3592 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3593 | } |
| 3594 | } |
| 3595 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3596 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3597 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3598 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3599 | case ACT_HTTP_DEL_ACL: |
| 3600 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3601 | struct pat_ref *ref; |
| 3602 | char *key; |
| 3603 | int len; |
| 3604 | |
| 3605 | /* collect reference */ |
| 3606 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3607 | if (!ref) |
| 3608 | continue; |
| 3609 | |
| 3610 | /* collect key */ |
| 3611 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3612 | key = trash.str; |
| 3613 | key[len] = '\0'; |
| 3614 | |
| 3615 | /* perform update */ |
| 3616 | /* returned code: 1=ok, 0=ko */ |
| 3617 | pat_ref_delete(ref, key); |
| 3618 | |
| 3619 | break; |
| 3620 | } |
| 3621 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3622 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3623 | struct pat_ref *ref; |
| 3624 | char *key; |
| 3625 | struct chunk *trash_key; |
| 3626 | int len; |
| 3627 | |
| 3628 | trash_key = get_trash_chunk(); |
| 3629 | |
| 3630 | /* collect reference */ |
| 3631 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3632 | if (!ref) |
| 3633 | continue; |
| 3634 | |
| 3635 | /* collect key */ |
| 3636 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3637 | key = trash_key->str; |
| 3638 | key[len] = '\0'; |
| 3639 | |
| 3640 | /* perform update */ |
| 3641 | /* add entry only if it does not already exist */ |
| 3642 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3643 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3644 | |
| 3645 | break; |
| 3646 | } |
| 3647 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3648 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3649 | struct pat_ref *ref; |
| 3650 | char *key, *value; |
| 3651 | struct chunk *trash_key, *trash_value; |
| 3652 | int len; |
| 3653 | |
| 3654 | trash_key = get_trash_chunk(); |
| 3655 | trash_value = get_trash_chunk(); |
| 3656 | |
| 3657 | /* collect reference */ |
| 3658 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3659 | if (!ref) |
| 3660 | continue; |
| 3661 | |
| 3662 | /* collect key */ |
| 3663 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3664 | key = trash_key->str; |
| 3665 | key[len] = '\0'; |
| 3666 | |
| 3667 | /* collect value */ |
| 3668 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3669 | value = trash_value->str; |
| 3670 | value[len] = '\0'; |
| 3671 | |
| 3672 | /* perform update */ |
| 3673 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3674 | /* update entry if it exists */ |
| 3675 | pat_ref_set(ref, key, value, NULL); |
| 3676 | else |
| 3677 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3678 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3679 | |
| 3680 | break; |
| 3681 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3682 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3683 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3684 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3685 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3686 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3687 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3688 | case ACT_RET_ERR: |
| 3689 | case ACT_RET_CONT: |
| 3690 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3691 | case ACT_RET_STOP: |
| 3692 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3693 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3694 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3695 | return HTTP_RULE_RES_YIELD; |
| 3696 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3697 | break; |
| 3698 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3699 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3700 | /* Note: only the first valid tracking parameter of each |
| 3701 | * applies. |
| 3702 | */ |
| 3703 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3704 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3705 | struct stktable *t; |
| 3706 | struct stksess *ts; |
| 3707 | struct stktable_key *key; |
| 3708 | void *ptr; |
| 3709 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3710 | t = rule->arg.trk_ctr.table.t; |
| 3711 | 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] | 3712 | |
| 3713 | if (key && (ts = stktable_get_entry(t, key))) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3714 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3715 | |
| 3716 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3717 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3718 | if (ptr) |
| 3719 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3720 | |
| 3721 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3722 | if (ptr) |
| 3723 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3724 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3725 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3726 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3727 | if (sess->fe != s->be) |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3728 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3729 | } |
| 3730 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3731 | break; |
| 3732 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3733 | /* other flags exists, but normaly, they never be matched. */ |
| 3734 | default: |
| 3735 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3736 | } |
| 3737 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3738 | |
| 3739 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3740 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3741 | } |
| 3742 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3743 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3744 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3745 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3746 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3747 | * is returned, the process can continue the evaluation of next rule list. If |
| 3748 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3749 | * is returned, it means the operation could not be processed and a server error |
| 3750 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3751 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3752 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3753 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3754 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3755 | 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] | 3756 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3757 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3758 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3759 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3760 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3761 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3762 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3763 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3764 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3765 | * resume condition. If a resume is needed it is always in the action |
| 3766 | * and never in the ACL or converters. In this case, we initialise the |
| 3767 | * current rule, and go to the action execution point. |
| 3768 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3769 | if (s->current_rule) { |
| 3770 | rule = s->current_rule; |
| 3771 | s->current_rule = NULL; |
| 3772 | if (s->current_rule_list == rules) |
| 3773 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3774 | } |
| 3775 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3776 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3777 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3778 | |
| 3779 | /* check optional condition */ |
| 3780 | if (rule->cond) { |
| 3781 | int ret; |
| 3782 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3783 | 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] | 3784 | ret = acl_pass(ret); |
| 3785 | |
| 3786 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3787 | ret = !ret; |
| 3788 | |
| 3789 | if (!ret) /* condition not matched */ |
| 3790 | continue; |
| 3791 | } |
| 3792 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3793 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3794 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3795 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3796 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3797 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3798 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3799 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3800 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3801 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3802 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3803 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3804 | s->task->nice = rule->arg.nice; |
| 3805 | break; |
| 3806 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3807 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3808 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3809 | 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] | 3810 | break; |
| 3811 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3812 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3813 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3814 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3815 | 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] | 3816 | #endif |
| 3817 | break; |
| 3818 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3819 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3820 | s->logs.level = rule->arg.loglevel; |
| 3821 | break; |
| 3822 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3823 | case ACT_HTTP_REPLACE_HDR: |
| 3824 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3825 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3826 | rule->arg.hdr_add.name_len, |
| 3827 | &rule->arg.hdr_add.fmt, |
| 3828 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 3829 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3830 | break; |
| 3831 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3832 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3833 | ctx.idx = 0; |
| 3834 | /* remove all occurrences of the header */ |
| 3835 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3836 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3837 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3838 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3839 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3840 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3841 | case ACT_HTTP_SET_HDR: |
| 3842 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3843 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3844 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3845 | trash.len = rule->arg.hdr_add.name_len; |
| 3846 | trash.str[trash.len++] = ':'; |
| 3847 | trash.str[trash.len++] = ' '; |
| 3848 | 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] | 3849 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3850 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3851 | /* remove all occurrences of the header */ |
| 3852 | ctx.idx = 0; |
| 3853 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3854 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3855 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3856 | } |
| 3857 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3858 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3859 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3860 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3861 | case ACT_HTTP_DEL_ACL: |
| 3862 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3863 | struct pat_ref *ref; |
| 3864 | char *key; |
| 3865 | int len; |
| 3866 | |
| 3867 | /* collect reference */ |
| 3868 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3869 | if (!ref) |
| 3870 | continue; |
| 3871 | |
| 3872 | /* collect key */ |
| 3873 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3874 | key = trash.str; |
| 3875 | key[len] = '\0'; |
| 3876 | |
| 3877 | /* perform update */ |
| 3878 | /* returned code: 1=ok, 0=ko */ |
| 3879 | pat_ref_delete(ref, key); |
| 3880 | |
| 3881 | break; |
| 3882 | } |
| 3883 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3884 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3885 | struct pat_ref *ref; |
| 3886 | char *key; |
| 3887 | struct chunk *trash_key; |
| 3888 | int len; |
| 3889 | |
| 3890 | trash_key = get_trash_chunk(); |
| 3891 | |
| 3892 | /* collect reference */ |
| 3893 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3894 | if (!ref) |
| 3895 | continue; |
| 3896 | |
| 3897 | /* collect key */ |
| 3898 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3899 | key = trash_key->str; |
| 3900 | key[len] = '\0'; |
| 3901 | |
| 3902 | /* perform update */ |
| 3903 | /* check if the entry already exists */ |
| 3904 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3905 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3906 | |
| 3907 | break; |
| 3908 | } |
| 3909 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3910 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3911 | struct pat_ref *ref; |
| 3912 | char *key, *value; |
| 3913 | struct chunk *trash_key, *trash_value; |
| 3914 | int len; |
| 3915 | |
| 3916 | trash_key = get_trash_chunk(); |
| 3917 | trash_value = get_trash_chunk(); |
| 3918 | |
| 3919 | /* collect reference */ |
| 3920 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3921 | if (!ref) |
| 3922 | continue; |
| 3923 | |
| 3924 | /* collect key */ |
| 3925 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3926 | key = trash_key->str; |
| 3927 | key[len] = '\0'; |
| 3928 | |
| 3929 | /* collect value */ |
| 3930 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3931 | value = trash_value->str; |
| 3932 | value[len] = '\0'; |
| 3933 | |
| 3934 | /* perform update */ |
| 3935 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3936 | /* update entry if it exists */ |
| 3937 | pat_ref_set(ref, key, value, NULL); |
| 3938 | else |
| 3939 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3940 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3941 | |
| 3942 | break; |
| 3943 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3944 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3945 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3946 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3947 | return HTTP_RULE_RES_BADREQ; |
| 3948 | return HTTP_RULE_RES_DONE; |
| 3949 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3950 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3951 | /* Note: only the first valid tracking parameter of each |
| 3952 | * applies. |
| 3953 | */ |
| 3954 | |
| 3955 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
| 3956 | struct stktable *t; |
| 3957 | struct stksess *ts; |
| 3958 | struct stktable_key *key; |
| 3959 | void *ptr; |
| 3960 | |
| 3961 | t = rule->arg.trk_ctr.table.t; |
| 3962 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3963 | |
| 3964 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3965 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
| 3966 | |
| 3967 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3968 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3969 | if (ptr) |
| 3970 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3971 | |
| 3972 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3973 | if (ptr) |
| 3974 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3975 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3976 | |
| 3977 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3978 | if (sess->fe != s->be) |
| 3979 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3980 | |
| 3981 | /* When the client triggers a 4xx from the server, it's most often due |
| 3982 | * to a missing object or permission. These events should be tracked |
| 3983 | * because if they happen often, it may indicate a brute force or a |
| 3984 | * vulnerability scan. Normally this is done when receiving the response |
| 3985 | * but here we're tracking after this ought to have been done so we have |
| 3986 | * to do it on purpose. |
| 3987 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3988 | if ((unsigned)(txn->status - 400) < 100) { |
| 3989 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3990 | if (ptr) |
| 3991 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3992 | |
| 3993 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3994 | if (ptr) |
| 3995 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3996 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3997 | } |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | break; |
| 4001 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4002 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4003 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 4004 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 4005 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4006 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4007 | case ACT_RET_ERR: |
| 4008 | case ACT_RET_CONT: |
| 4009 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4010 | case ACT_RET_STOP: |
| 4011 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4012 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 4013 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4014 | return HTTP_RULE_RES_YIELD; |
| 4015 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 4016 | break; |
| 4017 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 4018 | /* other flags exists, but normaly, they never be matched. */ |
| 4019 | default: |
| 4020 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4025 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4029 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 4030 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 4031 | * as too large a request to build a valid response. |
| 4032 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4033 | 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] | 4034 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4035 | struct http_msg *req = &txn->req; |
| 4036 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4037 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4038 | struct chunk *chunk; |
| 4039 | int ret = 0; |
| 4040 | |
| 4041 | chunk = alloc_trash_chunk(); |
| 4042 | if (!chunk) |
| 4043 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4044 | |
| 4045 | /* build redirect message */ |
| 4046 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4047 | case 308: |
| 4048 | msg_fmt = HTTP_308; |
| 4049 | break; |
| 4050 | case 307: |
| 4051 | msg_fmt = HTTP_307; |
| 4052 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4053 | case 303: |
| 4054 | msg_fmt = HTTP_303; |
| 4055 | break; |
| 4056 | case 301: |
| 4057 | msg_fmt = HTTP_301; |
| 4058 | break; |
| 4059 | case 302: |
| 4060 | default: |
| 4061 | msg_fmt = HTTP_302; |
| 4062 | break; |
| 4063 | } |
| 4064 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4065 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 4066 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4067 | |
| 4068 | switch(rule->type) { |
| 4069 | case REDIRECT_TYPE_SCHEME: { |
| 4070 | const char *path; |
| 4071 | const char *host; |
| 4072 | struct hdr_ctx ctx; |
| 4073 | int pathlen; |
| 4074 | int hostlen; |
| 4075 | |
| 4076 | host = ""; |
| 4077 | hostlen = 0; |
| 4078 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4079 | 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] | 4080 | host = ctx.line + ctx.val; |
| 4081 | hostlen = ctx.vlen; |
| 4082 | } |
| 4083 | |
| 4084 | path = http_get_path(txn); |
| 4085 | /* build message using path */ |
| 4086 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4087 | 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] | 4088 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4089 | int qs = 0; |
| 4090 | while (qs < pathlen) { |
| 4091 | if (path[qs] == '?') { |
| 4092 | pathlen = qs; |
| 4093 | break; |
| 4094 | } |
| 4095 | qs++; |
| 4096 | } |
| 4097 | } |
| 4098 | } else { |
| 4099 | path = "/"; |
| 4100 | pathlen = 1; |
| 4101 | } |
| 4102 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4103 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4104 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4105 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 4106 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4107 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4108 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4109 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4110 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4111 | } |
| 4112 | else { |
| 4113 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4114 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4115 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4116 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4117 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 4118 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4119 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4120 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4121 | memcpy(chunk->str + chunk->len, "://", 3); |
| 4122 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4123 | |
| 4124 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4125 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 4126 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4127 | |
| 4128 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4129 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 4130 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4131 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4132 | /* append a slash at the end of the location if needed and missing */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4133 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4134 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4135 | if (chunk->len > chunk->size - 5) |
| 4136 | goto leave; |
| 4137 | chunk->str[chunk->len] = '/'; |
| 4138 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | break; |
| 4142 | } |
| 4143 | case REDIRECT_TYPE_PREFIX: { |
| 4144 | const char *path; |
| 4145 | int pathlen; |
| 4146 | |
| 4147 | path = http_get_path(txn); |
| 4148 | /* build message using path */ |
| 4149 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4150 | 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] | 4151 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4152 | int qs = 0; |
| 4153 | while (qs < pathlen) { |
| 4154 | if (path[qs] == '?') { |
| 4155 | pathlen = qs; |
| 4156 | break; |
| 4157 | } |
| 4158 | qs++; |
| 4159 | } |
| 4160 | } |
| 4161 | } else { |
| 4162 | path = "/"; |
| 4163 | pathlen = 1; |
| 4164 | } |
| 4165 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4166 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4167 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 4168 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4169 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4170 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4171 | * add anything, otherwise it makes it hard for the user to |
| 4172 | * configure a self-redirection. |
| 4173 | */ |
| 4174 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4175 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4176 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4177 | } |
| 4178 | } |
| 4179 | else { |
| 4180 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4181 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4182 | |
| 4183 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4184 | if (chunk->len + pathlen > chunk->size - 4) |
| 4185 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4189 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 4190 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4191 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4192 | /* append a slash at the end of the location if needed and missing */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4193 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4194 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4195 | if (chunk->len > chunk->size - 5) |
| 4196 | goto leave; |
| 4197 | chunk->str[chunk->len] = '/'; |
| 4198 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | break; |
| 4202 | } |
| 4203 | case REDIRECT_TYPE_LOCATION: |
| 4204 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4205 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4206 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 4207 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4208 | |
| 4209 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4210 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4211 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4212 | } |
| 4213 | else { |
| 4214 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4215 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4216 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4217 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4218 | if (chunk->len > chunk->size - 4) |
| 4219 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4220 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4221 | break; |
| 4222 | } |
| 4223 | |
| 4224 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4225 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 4226 | chunk->len += 14; |
| 4227 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 4228 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4229 | } |
| 4230 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 4231 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4232 | txn->status = rule->code; |
| 4233 | /* let's log the request time */ |
| 4234 | s->logs.tv_request = now; |
| 4235 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 4236 | if (((!(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] | 4237 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4238 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4239 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4240 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4241 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4242 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 4243 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4244 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4245 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 4246 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4247 | } |
| 4248 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4249 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 4250 | chunk->len += 4; |
| 4251 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
| 4252 | bo_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4253 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4254 | bi_fast_delete(req->chn->buf, req->sov); |
| 4255 | req->next -= req->sov; |
| 4256 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4257 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END); |
Christopher Faulet | 014e39c | 2017-03-10 13:52:30 +0100 | [diff] [blame] | 4258 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4259 | req->msg_state = HTTP_MSG_CLOSED; |
| 4260 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4261 | /* Trim any possible response */ |
| 4262 | res->chn->buf->i = 0; |
| 4263 | res->next = res->sov = 0; |
Christopher Faulet | 9f724ed | 2017-04-20 14:16:13 +0200 | [diff] [blame] | 4264 | /* If not already done, don't perform any connection establishment */ |
| 4265 | channel_dont_connect(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4266 | } else { |
| 4267 | /* keep-alive not possible */ |
| 4268 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4269 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4270 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4271 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4272 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 4273 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4274 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4275 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4276 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4277 | } |
| 4278 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4279 | if (!(s->flags & SF_ERR_MASK)) |
| 4280 | s->flags |= SF_ERR_LOCAL; |
| 4281 | if (!(s->flags & SF_FINST_MASK)) |
| 4282 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4283 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4284 | ret = 1; |
| 4285 | leave: |
| 4286 | free_trash_chunk(chunk); |
| 4287 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4290 | /* This stream analyser runs all HTTP request processing which is common to |
| 4291 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4292 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4293 | * 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] | 4294 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4295 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4296 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4297 | 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] | 4298 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4299 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4300 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4301 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4302 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4303 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4304 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4305 | int deny_status = HTTP_ERR_403; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4306 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4307 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4308 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4309 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4310 | } |
| 4311 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4312 | 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] | 4313 | now_ms, __FUNCTION__, |
| 4314 | s, |
| 4315 | req, |
| 4316 | req->rex, req->wex, |
| 4317 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4318 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4319 | req->analysers); |
| 4320 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4321 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4322 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4323 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4324 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4325 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4326 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4327 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4328 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4329 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4330 | goto return_prx_yield; |
| 4331 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4332 | case HTTP_RULE_RES_CONT: |
| 4333 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4334 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4335 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4336 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4337 | if (txn->flags & TX_CLTARPIT) |
| 4338 | goto tarpit; |
| 4339 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4340 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4341 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4342 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4343 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4344 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4345 | goto done; |
| 4346 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4347 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4348 | goto return_bad_req; |
| 4349 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4350 | } |
| 4351 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4352 | /* OK at this stage, we know that the request was accepted according to |
| 4353 | * the http-request rules, we can check for the stats. Note that the |
| 4354 | * URI is detected *before* the req* rules in order not to be affected |
| 4355 | * by a possible reqrep, while they are processed *after* so that a |
| 4356 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4357 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4358 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4359 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4360 | 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] | 4361 | txn->status = 500; |
| 4362 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4363 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4364 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4365 | if (!(s->flags & SF_ERR_MASK)) |
| 4366 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4367 | goto return_prx_cond; |
| 4368 | } |
| 4369 | |
| 4370 | /* parse the whole stats request and extract the relevant information */ |
| 4371 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4372 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4373 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4374 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4375 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4376 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4377 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4378 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4379 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4382 | /* evaluate the req* rules except reqadd */ |
| 4383 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4384 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4385 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4386 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4387 | if (txn->flags & TX_CLDENY) |
| 4388 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4389 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4390 | if (txn->flags & TX_CLTARPIT) { |
| 4391 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4392 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4393 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4394 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4395 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4396 | /* add request headers from the rule sets in the same order */ |
| 4397 | list_for_each_entry(wl, &px->req_add, list) { |
| 4398 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4399 | 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] | 4400 | ret = acl_pass(ret); |
| 4401 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4402 | ret = !ret; |
| 4403 | if (!ret) |
| 4404 | continue; |
| 4405 | } |
| 4406 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4407 | 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] | 4408 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4409 | } |
| 4410 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4411 | |
| 4412 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4413 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4414 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4415 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4416 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4417 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4418 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4419 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4420 | if (!(s->flags & SF_FINST_MASK)) |
| 4421 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4422 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4423 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4424 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 4425 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4426 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4427 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4428 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4429 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4430 | /* check whether we have some ACLs set to redirect this request */ |
| 4431 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4432 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4433 | int ret; |
| 4434 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4435 | 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] | 4436 | ret = acl_pass(ret); |
| 4437 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4438 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4439 | if (!ret) |
| 4440 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4441 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4442 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4443 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4444 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4445 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4446 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4447 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4448 | * If this happens, then the data will not come immediately, so we must |
| 4449 | * send all what we have without waiting. Note that due to the small gain |
| 4450 | * 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] | 4451 | * 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] | 4452 | * itself once used. |
| 4453 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4454 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4455 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4456 | done: /* done with this analyser, continue with next ones that the calling |
| 4457 | * points will have set, if any. |
| 4458 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4459 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4460 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4461 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4462 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4463 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4464 | tarpit: |
| 4465 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4466 | * which may be the same as the connect timeout if unspecified. |
| 4467 | * If unset, then set it to zero because we really want it to |
| 4468 | * eventually expire. We build the tarpit as an analyser. |
| 4469 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4470 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4471 | |
| 4472 | /* wipe the request out so that we can drop the connection early |
| 4473 | * if the client closes first. |
| 4474 | */ |
| 4475 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4476 | |
| 4477 | /* Allow cookie logging |
| 4478 | */ |
| 4479 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4480 | manage_client_side_cookies(s, req); |
| 4481 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4482 | txn->status = http_err_codes[deny_status]; |
| 4483 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4484 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4485 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4486 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4487 | if (!req->analyse_exp) |
| 4488 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4489 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4490 | sess->fe->fe_counters.denied_req++; |
| 4491 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4492 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4493 | if (sess->listener->counters) |
| 4494 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4495 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4496 | |
| 4497 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4498 | |
| 4499 | /* Allow cookie logging |
| 4500 | */ |
| 4501 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4502 | manage_client_side_cookies(s, req); |
| 4503 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4504 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4505 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4506 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4507 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4508 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4509 | sess->fe->fe_counters.denied_req++; |
| 4510 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4511 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4512 | if (sess->listener->counters) |
| 4513 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4514 | goto return_prx_cond; |
| 4515 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4516 | return_bad_req: |
| 4517 | /* We centralize bad requests processing here */ |
| 4518 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4519 | /* we detected a parsing error. We want to archive this request |
| 4520 | * in the dedicated proxy area for later troubleshooting. |
| 4521 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4522 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4523 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4524 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4525 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4526 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4527 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4528 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4529 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4530 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4531 | if (sess->listener->counters) |
| 4532 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4533 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4534 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4535 | if (!(s->flags & SF_ERR_MASK)) |
| 4536 | s->flags |= SF_ERR_PRXCOND; |
| 4537 | if (!(s->flags & SF_FINST_MASK)) |
| 4538 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4539 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4540 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4541 | req->analyse_exp = TICK_ETERNITY; |
| 4542 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4543 | |
| 4544 | return_prx_yield: |
| 4545 | channel_dont_connect(req); |
| 4546 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4547 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4548 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4549 | /* This function performs all the processing enabled for the current request. |
| 4550 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4551 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4552 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4553 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4554 | 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] | 4555 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4556 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4557 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4558 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4559 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4560 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4561 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4562 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4563 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4564 | return 0; |
| 4565 | } |
| 4566 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4567 | 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] | 4568 | now_ms, __FUNCTION__, |
| 4569 | s, |
| 4570 | req, |
| 4571 | req->rex, req->wex, |
| 4572 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4573 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4574 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4575 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4576 | /* |
| 4577 | * Right now, we know that we have processed the entire headers |
| 4578 | * and that unwanted requests have been filtered out. We can do |
| 4579 | * whatever we want with the remaining request. Also, now we |
| 4580 | * may have separate values for ->fe, ->be. |
| 4581 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4582 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4583 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4584 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4585 | * incoming request. Note that this requires that a connection is |
| 4586 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4587 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4588 | 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] | 4589 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4590 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4591 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4592 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4593 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4594 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4595 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4596 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4597 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4598 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4599 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4600 | if (!(s->flags & SF_ERR_MASK)) |
| 4601 | s->flags |= SF_ERR_RESOURCE; |
| 4602 | if (!(s->flags & SF_FINST_MASK)) |
| 4603 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4604 | |
| 4605 | return 0; |
| 4606 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4607 | |
| 4608 | path = http_get_path(txn); |
| 4609 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4610 | 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] | 4611 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4612 | /* if the path was found, we have to remove everything between |
| 4613 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4614 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4615 | * u_l characters by a single "/". |
| 4616 | */ |
| 4617 | if (path) { |
| 4618 | char *cur_ptr = req->buf->p; |
| 4619 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4620 | int delta; |
| 4621 | |
| 4622 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4623 | http_msg_move_end(&txn->req, delta); |
| 4624 | cur_end += delta; |
| 4625 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4626 | goto return_bad_req; |
| 4627 | } |
| 4628 | else { |
| 4629 | char *cur_ptr = req->buf->p; |
| 4630 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4631 | int delta; |
| 4632 | |
| 4633 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4634 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4635 | http_msg_move_end(&txn->req, delta); |
| 4636 | cur_end += delta; |
| 4637 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4638 | goto return_bad_req; |
| 4639 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4640 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4641 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4642 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4643 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4644 | * Note that doing so might move headers in the request, but |
| 4645 | * the fields will stay coherent and the URI will not move. |
| 4646 | * This should only be performed in the backend. |
| 4647 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4648 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4649 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4650 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4651 | /* add unique-id if "header-unique-id" is specified */ |
| 4652 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 4653 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4654 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4655 | goto return_bad_req; |
| 4656 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4657 | 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] | 4658 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4659 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4660 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4661 | 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] | 4662 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4663 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4664 | 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] | 4665 | goto return_bad_req; |
| 4666 | } |
| 4667 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4668 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4669 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4670 | * asks for it. |
| 4671 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4672 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4673 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4674 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4675 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4676 | 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] | 4677 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4678 | /* The header is set to be added only if none is present |
| 4679 | * and we found it, so don't do anything. |
| 4680 | */ |
| 4681 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4682 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4683 | /* Add an X-Forwarded-For header unless the source IP is |
| 4684 | * in the 'except' network range. |
| 4685 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4686 | if ((!sess->fe->except_mask.s_addr || |
| 4687 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4688 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4689 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4690 | (((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] | 4691 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4692 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4693 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4694 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4695 | |
| 4696 | /* Note: we rely on the backend to get the header name to be used for |
| 4697 | * x-forwarded-for, because the header is really meant for the backends. |
| 4698 | * However, if the backend did not specify any option, we have to rely |
| 4699 | * on the frontend's header name. |
| 4700 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4701 | if (s->be->fwdfor_hdr_len) { |
| 4702 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4703 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4704 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4705 | len = sess->fe->fwdfor_hdr_len; |
| 4706 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4707 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4708 | 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] | 4709 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4710 | 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] | 4711 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4712 | } |
| 4713 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4714 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4715 | /* FIXME: for the sake of completeness, we should also support |
| 4716 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4717 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4718 | int len; |
| 4719 | char pn[INET6_ADDRSTRLEN]; |
| 4720 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4721 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4722 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4723 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4724 | /* Note: we rely on the backend to get the header name to be used for |
| 4725 | * x-forwarded-for, because the header is really meant for the backends. |
| 4726 | * However, if the backend did not specify any option, we have to rely |
| 4727 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4728 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4729 | if (s->be->fwdfor_hdr_len) { |
| 4730 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4731 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4732 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4733 | len = sess->fe->fwdfor_hdr_len; |
| 4734 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4735 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4736 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4737 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4738 | 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] | 4739 | goto return_bad_req; |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4744 | * 10: add X-Original-To if either the frontend or the backend |
| 4745 | * asks for it. |
| 4746 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4747 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4748 | |
| 4749 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4750 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4751 | /* Add an X-Original-To header unless the destination IP is |
| 4752 | * in the 'except' network range. |
| 4753 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4754 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4755 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4756 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4757 | ((!sess->fe->except_mask_to.s_addr || |
| 4758 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4759 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4760 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4761 | (((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] | 4762 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4763 | int len; |
| 4764 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4765 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4766 | |
| 4767 | /* Note: we rely on the backend to get the header name to be used for |
| 4768 | * x-original-to, because the header is really meant for the backends. |
| 4769 | * However, if the backend did not specify any option, we have to rely |
| 4770 | * on the frontend's header name. |
| 4771 | */ |
| 4772 | if (s->be->orgto_hdr_len) { |
| 4773 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4774 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4775 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4776 | len = sess->fe->orgto_hdr_len; |
| 4777 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4778 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4779 | 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] | 4780 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4781 | 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] | 4782 | goto return_bad_req; |
| 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4787 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4788 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4789 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4790 | * "Upgrade" is present in the Connection header. |
| 4791 | */ |
| 4792 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4793 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4794 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4795 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4796 | unsigned int want_flags = 0; |
| 4797 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4798 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4799 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4800 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4801 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4802 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4803 | want_flags |= TX_CON_CLO_SET; |
| 4804 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4805 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4806 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4807 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4808 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4809 | want_flags |= TX_CON_KAL_SET; |
| 4810 | } |
| 4811 | |
| 4812 | 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] | 4813 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4814 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4815 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4816 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4817 | /* If we have no server assigned yet and we're balancing on url_param |
| 4818 | * with a POST request, we may be interested in checking the body for |
| 4819 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4820 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4821 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4822 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4823 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4824 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4825 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4826 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4827 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 4828 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 4829 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4830 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 4831 | /* We expect some data from the client. Unless we know for sure |
| 4832 | * we already have a full request, we have to re-enable quick-ack |
| 4833 | * in case we previously disabled it, otherwise we might cause |
| 4834 | * the client to delay further data. |
| 4835 | */ |
| 4836 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 4837 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 4838 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 4839 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
| 4840 | 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] | 4841 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4842 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4843 | /************************************************************* |
| 4844 | * OK, that's finished for the headers. We have done what we * |
| 4845 | * could. Let's switch to the DATA state. * |
| 4846 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4847 | req->analyse_exp = TICK_ETERNITY; |
| 4848 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4849 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4850 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4851 | /* OK let's go on with the BODY now */ |
| 4852 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4853 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4854 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4855 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4856 | /* we detected a parsing error. We want to archive this request |
| 4857 | * in the dedicated proxy area for later troubleshooting. |
| 4858 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4859 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4860 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4861 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4862 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4863 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4864 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4865 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4866 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4867 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4868 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4869 | if (sess->listener->counters) |
| 4870 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4871 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4872 | if (!(s->flags & SF_ERR_MASK)) |
| 4873 | s->flags |= SF_ERR_PRXCOND; |
| 4874 | if (!(s->flags & SF_FINST_MASK)) |
| 4875 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4876 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4877 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4878 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4879 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4880 | * returns zero, at the beginning because it prevents any other processing |
| 4881 | * from occurring, and at the end because it terminates the request. |
| 4882 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4883 | 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] | 4884 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4885 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4886 | |
| 4887 | /* This connection is being tarpitted. The CLIENT side has |
| 4888 | * already set the connect expiration date to the right |
| 4889 | * timeout. We just have to check that the client is still |
| 4890 | * there and that the timeout has not expired. |
| 4891 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4892 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4893 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4894 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4895 | return 0; |
| 4896 | |
| 4897 | /* We will set the queue timer to the time spent, just for |
| 4898 | * logging purposes. We fake a 500 server error, so that the |
| 4899 | * attacker will not suspect his connection has been tarpitted. |
| 4900 | * It will not cause trouble to the logs because we can exclude |
| 4901 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4902 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4903 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4904 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4905 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4906 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4907 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4908 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4909 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4910 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4911 | if (!(s->flags & SF_ERR_MASK)) |
| 4912 | s->flags |= SF_ERR_PRXCOND; |
| 4913 | if (!(s->flags & SF_FINST_MASK)) |
| 4914 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4915 | return 0; |
| 4916 | } |
| 4917 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4918 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4919 | * for either the buffer to be full, or the full advertised contents to have |
| 4920 | * reached the buffer. It must only be called after the standard HTTP request |
| 4921 | * processing has occurred, because it expects the request to be parsed and will |
| 4922 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4923 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4924 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4925 | * 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] | 4926 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4927 | 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] | 4928 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4929 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4930 | struct http_txn *txn = s->txn; |
| 4931 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4932 | |
| 4933 | /* We have to parse the HTTP request body to find any required data. |
| 4934 | * "balance url_param check_post" should have been the only way to get |
| 4935 | * into this. We were brought here after HTTP header analysis, so all |
| 4936 | * related structures are ready. |
| 4937 | */ |
| 4938 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4939 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4940 | /* This is the first call */ |
| 4941 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4942 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4943 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4944 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4945 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4946 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4947 | */ |
| 4948 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4949 | struct hdr_ctx ctx; |
| 4950 | ctx.idx = 0; |
| 4951 | /* Expect is allowed in 1.1, look for it */ |
| 4952 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4953 | 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] | 4954 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 4955 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4956 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4957 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4958 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4959 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4960 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4961 | /* 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] | 4962 | * req->buf->p still points to the beginning of the message. We |
| 4963 | * must save the body in msg->next because it survives buffer |
| 4964 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4965 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4966 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4967 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4968 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4969 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4970 | else |
| 4971 | msg->msg_state = HTTP_MSG_DATA; |
| 4972 | } |
| 4973 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4974 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4975 | /* 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] | 4976 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4977 | goto missing_data; |
| 4978 | |
| 4979 | /* OK we have everything we need now */ |
| 4980 | goto http_end; |
| 4981 | } |
| 4982 | |
| 4983 | /* OK here we're parsing a chunked-encoded message */ |
| 4984 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4985 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4986 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4987 | * 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] | 4988 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4989 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4990 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4991 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4992 | if (!ret) |
| 4993 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4994 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4995 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4996 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4997 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4998 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4999 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5000 | } |
| 5001 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5002 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5003 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 5004 | * for at least a whole chunk or the whole content length bytes after |
| 5005 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5006 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 5007 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 5008 | goto http_end; |
| 5009 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5010 | 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] | 5011 | goto http_end; |
| 5012 | |
| 5013 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5014 | /* we get here if we need to wait for more data. If the buffer is full, |
| 5015 | * we have the maximum we can expect. |
| 5016 | */ |
| 5017 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 5018 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5019 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5020 | 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] | 5021 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5022 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5023 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5024 | if (!(s->flags & SF_ERR_MASK)) |
| 5025 | s->flags |= SF_ERR_CLITO; |
| 5026 | if (!(s->flags & SF_FINST_MASK)) |
| 5027 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5028 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5029 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5030 | |
| 5031 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5032 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5033 | /* Not enough data. We'll re-use the http-request |
| 5034 | * timeout here. Ideally, we should set the timeout |
| 5035 | * relative to the accept() date. We just set the |
| 5036 | * request timeout once at the beginning of the |
| 5037 | * request. |
| 5038 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5039 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5040 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5041 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5042 | return 0; |
| 5043 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5044 | |
| 5045 | http_end: |
| 5046 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5047 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5048 | req->analysers &= ~an_bit; |
| 5049 | req->analyse_exp = TICK_ETERNITY; |
| 5050 | return 1; |
| 5051 | |
| 5052 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5053 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5054 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5055 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5056 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5057 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5058 | if (!(s->flags & SF_ERR_MASK)) |
| 5059 | s->flags |= SF_ERR_PRXCOND; |
| 5060 | if (!(s->flags & SF_FINST_MASK)) |
| 5061 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5062 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5063 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5064 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5065 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5066 | if (sess->listener->counters) |
| 5067 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5068 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5069 | } |
| 5070 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5071 | /* send a server's name with an outgoing request over an established connection. |
| 5072 | * Note: this function is designed to be called once the request has been scheduled |
| 5073 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5074 | * proceeding. |
| 5075 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5076 | 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] | 5077 | |
| 5078 | struct hdr_ctx ctx; |
| 5079 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5080 | char *hdr_name = be->server_id_hdr_name; |
| 5081 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5082 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5083 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5084 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5085 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5086 | ctx.idx = 0; |
| 5087 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5088 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5089 | if (old_o) { |
| 5090 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5091 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5092 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5093 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5094 | } |
| 5095 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5096 | old_i = chn->buf->i; |
| 5097 | 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] | 5098 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5099 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5100 | } |
| 5101 | |
| 5102 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5103 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5104 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5105 | hdr_val += hdr_name_len; |
| 5106 | *hdr_val++ = ':'; |
| 5107 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5108 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5109 | 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] | 5110 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5111 | if (old_o) { |
| 5112 | /* If this was a forwarded request, we must readjust the amount of |
| 5113 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5114 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5115 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5116 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5117 | old_o += chn->buf->i - old_i; |
| 5118 | b_adv(chn->buf, old_o); |
| 5119 | txn->req.next -= old_o; |
| 5120 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5121 | } |
| 5122 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5123 | return 0; |
| 5124 | } |
| 5125 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5126 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5127 | * right now but it works. |
| 5128 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5129 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5130 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5131 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5132 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5133 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5134 | struct connection *srv_conn; |
| 5135 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5136 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5137 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5138 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5139 | * server's connections. We need a safer way to reinitialize buffer |
| 5140 | * flags. We also need a more accurate method for computing per-request |
| 5141 | * data. |
| 5142 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5143 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5144 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5145 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5146 | * to the server. |
| 5147 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5148 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5149 | !si_conn_ready(&s->si[1])) { |
| 5150 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5151 | si_shutr(&s->si[1]); |
| 5152 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5153 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5154 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5155 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5156 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5157 | if (unlikely(s->srv_conn)) |
| 5158 | sess_change_server(s, NULL); |
| 5159 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5160 | |
| 5161 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5162 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5163 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5164 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5165 | int n; |
| 5166 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5167 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5168 | if (n < 1 || n > 5) |
| 5169 | n = 0; |
| 5170 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5171 | if (fe->mode == PR_MODE_HTTP) { |
| 5172 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5173 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5174 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5175 | (be->mode == PR_MODE_HTTP)) { |
| 5176 | be->be_counters.p.http.rsp[n]++; |
| 5177 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5178 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5182 | s->logs.bytes_in -= s->req.buf->i; |
| 5183 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5184 | |
| 5185 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5186 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5187 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5188 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5189 | s->do_log(s); |
| 5190 | } |
| 5191 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5192 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5193 | stream_stop_content_counters(s); |
| 5194 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5195 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5196 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5197 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 5198 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 5199 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5200 | tv_zero(&s->logs.tv_request); |
| 5201 | s->logs.t_queue = -1; |
| 5202 | s->logs.t_connect = -1; |
| 5203 | s->logs.t_data = -1; |
| 5204 | s->logs.t_close = 0; |
| 5205 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5206 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5207 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5208 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5209 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5210 | |
| 5211 | if (s->pend_pos) |
| 5212 | pendconn_free(s->pend_pos); |
| 5213 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5214 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5215 | if (s->flags & SF_CURR_SESS) { |
| 5216 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5217 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5218 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5219 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5220 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5221 | } |
| 5222 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5223 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5224 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5225 | /* only release our endpoint if we don't intend to reuse the |
| 5226 | * connection. |
| 5227 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5228 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5229 | !si_conn_ready(&s->si[1])) { |
| 5230 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5231 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5232 | } |
| 5233 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5234 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5235 | s->si[1].err_type = SI_ET_NONE; |
| 5236 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5237 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5238 | 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] | 5239 | 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); |
| 5240 | 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] | 5241 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5242 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5243 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5244 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5245 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5246 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5247 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5248 | |
| 5249 | if (prev_status == 401 || prev_status == 407) { |
| 5250 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5251 | * a chance of being able to send the visitor again to the same |
| 5252 | * server over the same connection. This is required by some |
| 5253 | * broken protocols such as NTLM, and anyway whenever there is |
| 5254 | * an opportunity for sending the challenge to the proper place, |
| 5255 | * it's better to do it (at least it helps with debugging). |
| 5256 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5257 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5258 | if (srv_conn) |
| 5259 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5260 | } |
| 5261 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5262 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 5263 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 5264 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5265 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5266 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5267 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5268 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5269 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5270 | s->req.flags |= CF_NEVER_WAIT; |
| 5271 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5272 | } |
| 5273 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 5274 | /* we're removing the analysers, we MUST re-enable events detection. |
| 5275 | * We don't enable close on the response channel since it's either |
| 5276 | * already closed, or in keep-alive with an idle connection handler. |
| 5277 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5278 | channel_auto_read(&s->req); |
| 5279 | channel_auto_close(&s->req); |
| 5280 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5281 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5282 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5283 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5284 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5285 | if (!srv) |
| 5286 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5287 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5288 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5289 | else if (prev_flags & TX_NOT_FIRST) |
| 5290 | /* note: we check the request, not the connection, but |
| 5291 | * this is valid for strategies SAFE and AGGR, and in |
| 5292 | * case of ALWS, we don't care anyway. |
| 5293 | */ |
| 5294 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5295 | else |
| 5296 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5297 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 5298 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 5299 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | |
| 5303 | /* This function updates the request state machine according to the response |
| 5304 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5305 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5306 | * it is only used to find when a request/response couple is complete. Both |
| 5307 | * this function and its equivalent should loop until both return zero. It |
| 5308 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5309 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5310 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5311 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5312 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5313 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5314 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5315 | unsigned int old_state = txn->req.msg_state; |
| 5316 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5318 | return 0; |
| 5319 | |
| 5320 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5321 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5322 | * We can shut the read side unless we want to abort_on_close, |
| 5323 | * or we have a POST request. The issue with POST requests is |
| 5324 | * that some browsers still send a CRLF after the request, and |
| 5325 | * this CRLF must be read so that it does not remain in the kernel |
| 5326 | * buffers, otherwise a close could cause an RST on some systems |
| 5327 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5328 | * Note that if we're using keep-alive on the client side, we'd |
| 5329 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5330 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5331 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5332 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5333 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5334 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5335 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5336 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5337 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5338 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5339 | /* if the server closes the connection, we want to immediately react |
| 5340 | * and close the socket to save packets and syscalls. |
| 5341 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5342 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5343 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5344 | /* In any case we've finished parsing the request so we must |
| 5345 | * disable Nagle when sending data because 1) we're not going |
| 5346 | * to shut this side, and 2) the server is waiting for us to |
| 5347 | * send pending data. |
| 5348 | */ |
| 5349 | chn->flags |= CF_NEVER_WAIT; |
| 5350 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5351 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5352 | goto wait_other_side; |
| 5353 | |
| 5354 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5355 | /* The server has not finished to respond, so we |
| 5356 | * don't want to move in order not to upset it. |
| 5357 | */ |
| 5358 | goto wait_other_side; |
| 5359 | } |
| 5360 | |
| 5361 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5362 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5363 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5364 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5365 | goto wait_other_side; |
| 5366 | } |
| 5367 | |
| 5368 | /* When we get here, it means that both the request and the |
| 5369 | * response have finished receiving. Depending on the connection |
| 5370 | * mode, we'll have to wait for the last bytes to leave in either |
| 5371 | * direction, and sometimes for a close to be effective. |
| 5372 | */ |
| 5373 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5374 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5375 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5376 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5377 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5378 | } |
| 5379 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5380 | /* Option forceclose is set, or either side wants to close, |
| 5381 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5382 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5383 | * once both states are CLOSED. |
| 5384 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5385 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5386 | channel_shutr_now(chn); |
| 5387 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5388 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5389 | } |
| 5390 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5391 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5392 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5393 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5394 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5395 | channel_auto_read(chn); |
| 5396 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5397 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5398 | } |
| 5399 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5400 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5401 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5402 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5403 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5404 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5405 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5406 | goto http_msg_closing; |
| 5407 | } |
| 5408 | else { |
| 5409 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5410 | goto http_msg_closed; |
| 5411 | } |
| 5412 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5413 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5417 | http_msg_closing: |
| 5418 | /* nothing else to forward, just waiting for the output buffer |
| 5419 | * to be empty and for the shutw_now to take effect. |
| 5420 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5421 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5422 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5423 | goto http_msg_closed; |
| 5424 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5425 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5426 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5427 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5428 | goto wait_other_side; |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5433 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5434 | /* see above in MSG_DONE why we only do this in these states */ |
| 5435 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5436 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5437 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5438 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5439 | goto wait_other_side; |
| 5440 | } |
| 5441 | |
| 5442 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5443 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5444 | } |
| 5445 | |
| 5446 | |
| 5447 | /* This function updates the response state machine according to the request |
| 5448 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5449 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5450 | * it is only used to find when a request/response couple is complete. Both |
| 5451 | * this function and its equivalent should loop until both return zero. It |
| 5452 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5453 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5454 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5455 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5456 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5457 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5458 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5459 | unsigned int old_state = txn->rsp.msg_state; |
| 5460 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5461 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5462 | return 0; |
| 5463 | |
| 5464 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5465 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5466 | * still monitor the server connection for a possible close |
| 5467 | * while the request is being uploaded, so we don't disable |
| 5468 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5469 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5470 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5471 | |
| 5472 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5473 | goto wait_other_side; |
| 5474 | |
| 5475 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5476 | /* The client seems to still be sending data, probably |
| 5477 | * because we got an error response during an upload. |
| 5478 | * We have the choice of either breaking the connection |
| 5479 | * or letting it pass through. Let's do the later. |
| 5480 | */ |
| 5481 | goto wait_other_side; |
| 5482 | } |
| 5483 | |
| 5484 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5485 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5486 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5487 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5488 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5489 | goto wait_other_side; |
| 5490 | } |
| 5491 | |
| 5492 | /* When we get here, it means that both the request and the |
| 5493 | * response have finished receiving. Depending on the connection |
| 5494 | * mode, we'll have to wait for the last bytes to leave in either |
| 5495 | * direction, and sometimes for a close to be effective. |
| 5496 | */ |
| 5497 | |
| 5498 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5499 | /* Server-close mode : shut read and wait for the request |
| 5500 | * side to close its output buffer. The caller will detect |
| 5501 | * when we're in DONE and the other is in CLOSED and will |
| 5502 | * catch that for the final cleanup. |
| 5503 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5504 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5505 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5506 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5507 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5508 | /* Option forceclose is set, or either side wants to close, |
| 5509 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5510 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5511 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5512 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5513 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5514 | channel_shutr_now(chn); |
| 5515 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5516 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5517 | } |
| 5518 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5519 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5520 | * need to forward remaining data. Keep-alive will need to monitor |
| 5521 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5522 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5523 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5524 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5525 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5526 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5527 | } |
| 5528 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5529 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5530 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5531 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5532 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5533 | goto http_msg_closing; |
| 5534 | } |
| 5535 | else { |
| 5536 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5537 | goto http_msg_closed; |
| 5538 | } |
| 5539 | } |
| 5540 | goto wait_other_side; |
| 5541 | } |
| 5542 | |
| 5543 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5544 | http_msg_closing: |
| 5545 | /* nothing else to forward, just waiting for the output buffer |
| 5546 | * to be empty and for the shutw_now to take effect. |
| 5547 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5548 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5549 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5550 | goto http_msg_closed; |
| 5551 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5552 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5553 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5554 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5555 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5556 | if (objt_server(s->target)) |
| 5557 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5558 | goto wait_other_side; |
| 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5563 | http_msg_closed: |
| 5564 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5565 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5566 | channel_auto_close(chn); |
| 5567 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5568 | goto wait_other_side; |
| 5569 | } |
| 5570 | |
| 5571 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5572 | /* We force the response to leave immediately if we're waiting for the |
| 5573 | * other side, since there is no pending shutdown to push it out. |
| 5574 | */ |
| 5575 | if (!channel_is_empty(chn)) |
| 5576 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5577 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | |
| 5581 | /* Resync the request and response state machines. Return 1 if either state |
| 5582 | * changes. |
| 5583 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5584 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5585 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5586 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5587 | int old_req_state = txn->req.msg_state; |
| 5588 | int old_res_state = txn->rsp.msg_state; |
| 5589 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5590 | http_sync_req_state(s); |
| 5591 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5592 | if (!http_sync_res_state(s)) |
| 5593 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5594 | if (!http_sync_req_state(s)) |
| 5595 | break; |
| 5596 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5597 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5598 | DPRINTF(stderr,"[%u] %s: stream=%p old=%d,%d cur=%d,%d\n", |
| 5599 | now_ms, __FUNCTION__, |
| 5600 | s, |
| 5601 | old_req_state, old_res_state, |
| 5602 | txn->req.msg_state, txn->rsp.msg_state); |
| 5603 | |
| 5604 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5605 | /* OK, both state machines agree on a compatible state. |
| 5606 | * There are a few cases we're interested in : |
| 5607 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5608 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5609 | * directions, so let's simply disable both analysers. |
| 5610 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5611 | * request. |
| 5612 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5613 | * with server-close mode means we've completed one request and we |
| 5614 | * must re-initialize the server connection. |
| 5615 | */ |
| 5616 | |
| 5617 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5618 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5619 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5620 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5621 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5622 | channel_auto_close(&s->req); |
| 5623 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5624 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5625 | channel_auto_close(&s->res); |
| 5626 | channel_auto_read(&s->res); |
Christopher Faulet | 6962f4e | 2016-11-28 14:21:27 +0100 | [diff] [blame] | 5627 | if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5628 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 6962f4e | 2016-11-28 14:21:27 +0100 | [diff] [blame] | 5629 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5630 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5631 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5632 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5633 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5634 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5635 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5636 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5637 | channel_auto_close(&s->res); |
| 5638 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5639 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5640 | channel_abort(&s->req); |
| 5641 | channel_auto_close(&s->req); |
| 5642 | channel_auto_read(&s->req); |
| 5643 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5644 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5645 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5646 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5647 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5648 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5649 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5650 | /* server-close/keep-alive: terminate this transaction, |
| 5651 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5652 | * a fresh-new transaction, but only once we're sure there's |
| 5653 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 5654 | * another request. They must not hold any pending output data |
| 5655 | * and the response buffer must realigned |
| 5656 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5657 | */ |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5658 | if (s->req.buf->o) |
| 5659 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 5660 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5661 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 5662 | else { |
| 5663 | s->req.analysers = AN_REQ_FLT_END; |
| 5664 | s->res.analysers = AN_RES_FLT_END; |
| 5665 | txn->flags |= TX_WAIT_CLEANUP; |
| 5666 | return 1; |
| 5667 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5668 | } |
| 5669 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5670 | return txn->req.msg_state != old_req_state || |
| 5671 | txn->rsp.msg_state != old_res_state; |
| 5672 | } |
| 5673 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5674 | /* This function is an analyser which forwards request body (including chunk |
| 5675 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5676 | * zero byte. The only situation where it must not be called is when we're in |
| 5677 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5678 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5679 | * 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] | 5680 | * 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] | 5681 | * 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] | 5682 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5683 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5684 | 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] | 5685 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5686 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5687 | struct http_txn *txn = s->txn; |
| 5688 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5689 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5690 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5691 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 5692 | now_ms, __FUNCTION__, |
| 5693 | s, |
| 5694 | req, |
| 5695 | req->rex, req->wex, |
| 5696 | req->flags, |
| 5697 | req->buf->i, |
| 5698 | req->analysers); |
| 5699 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5700 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5701 | return 0; |
| 5702 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5703 | 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] | 5704 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5705 | /* Output closed while we were sending data. We must abort and |
| 5706 | * wake the other side up. |
| 5707 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5708 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5709 | msg->msg_state = HTTP_MSG_ERROR; |
| 5710 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5711 | return 1; |
| 5712 | } |
| 5713 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5714 | /* Note that we don't have to send 100-continue back because we don't |
| 5715 | * need the data to complete our job, and it's up to the server to |
| 5716 | * decide whether to return 100, 417 or anything else in return of |
| 5717 | * an "Expect: 100-continue" header. |
| 5718 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5719 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5720 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5721 | ? HTTP_MSG_CHUNK_SIZE |
| 5722 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5723 | |
| 5724 | /* TODO/filters: when http-buffer-request option is set or if a |
| 5725 | * rule on url_param exists, the first chunk size could be |
| 5726 | * already parsed. In that case, msg->next is after the chunk |
| 5727 | * size (including the CRLF after the size). So this case should |
| 5728 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5729 | } |
| 5730 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5731 | /* Some post-connect processing might want us to refrain from starting to |
| 5732 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5733 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5734 | */ |
| 5735 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5736 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5737 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5738 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5739 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5740 | } |
| 5741 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5742 | } |
| 5743 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5744 | /* in most states, we should abort in case of early close */ |
| 5745 | channel_auto_close(req); |
| 5746 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5747 | if (req->to_forward) { |
| 5748 | /* We can't process the buffer's contents yet */ |
| 5749 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5750 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5751 | } |
| 5752 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5753 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5754 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5755 | ? http_msg_forward_chunked_body(s, msg) |
| 5756 | : http_msg_forward_body(s, msg)); |
| 5757 | if (!ret) |
| 5758 | goto missing_data_or_waiting; |
| 5759 | if (ret < 0) |
| 5760 | goto return_bad_req; |
| 5761 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5762 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5763 | /* other states, DONE...TUNNEL */ |
| 5764 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 5765 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5766 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5767 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5768 | if (http_resync_states(s)) { |
| 5769 | /* some state changes occurred, maybe the analyser |
| 5770 | * was disabled too. */ |
| 5771 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5772 | if (req->flags & CF_SHUTW) { |
| 5773 | /* request errors are most likely due to the |
| 5774 | * server aborting the transfer. */ |
| 5775 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5776 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5777 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5778 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5779 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5780 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5781 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5782 | } |
| 5783 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5784 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 5785 | * the client's connection and forward any shutdown notification to the |
| 5786 | * server, which will decide whether to close or to go on processing the |
| 5787 | * request. We only do that in tunnel mode, and not in other modes since |
| 5788 | * it can be abused to exhaust source ports. */ |
| 5789 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 5790 | channel_auto_read(req); |
| 5791 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5792 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5793 | s->si[1].flags |= SI_FL_NOLINGER; |
| 5794 | channel_auto_close(req); |
| 5795 | } |
| 5796 | else if (s->txn->meth == HTTP_METH_POST) { |
| 5797 | /* POST requests may require to read extra CRLF sent by broken |
| 5798 | * browsers and which could cause an RST to be sent upon close |
| 5799 | * on some systems (eg: Linux). */ |
| 5800 | channel_auto_read(req); |
| 5801 | } |
| 5802 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5803 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5804 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5805 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5806 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5807 | if (!(s->flags & SF_ERR_MASK)) |
| 5808 | s->flags |= SF_ERR_CLICL; |
| 5809 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5810 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5811 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5812 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5813 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5814 | } |
| 5815 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5816 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5817 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5818 | if (objt_server(s->target)) |
| 5819 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5820 | |
| 5821 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5822 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5823 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5824 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5825 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5826 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5827 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5828 | /* 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] | 5829 | * 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] | 5830 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5831 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5832 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5833 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5834 | /* 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] | 5835 | * 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] | 5836 | * 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] | 5837 | * modes are already handled by the stream sock layer. We must not do |
| 5838 | * this in content-length mode because it could present the MSG_MORE |
| 5839 | * flag with the last block of forwarded data, which would cause an |
| 5840 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5841 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5842 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5843 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5844 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5845 | return 0; |
| 5846 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5847 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5848 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5849 | if (sess->listener->counters) |
| 5850 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5851 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5852 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5853 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5854 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5855 | if (txn->status) { |
| 5856 | /* 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] | 5857 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5858 | } else { |
| 5859 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5860 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5861 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5862 | req->analysers &= AN_REQ_FLT_END; |
| 5863 | s->res.analysers &= AN_RES_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] | 5864 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5865 | if (!(s->flags & SF_ERR_MASK)) |
| 5866 | s->flags |= SF_ERR_PRXCOND; |
| 5867 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5868 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5869 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5870 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5871 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5872 | } |
| 5873 | return 0; |
| 5874 | |
| 5875 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5876 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5877 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5878 | if (txn->status) { |
| 5879 | /* 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] | 5880 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5881 | } else { |
| 5882 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5883 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5884 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5885 | req->analysers &= AN_REQ_FLT_END; |
| 5886 | s->res.analysers &= AN_RES_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] | 5887 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5888 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5889 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5890 | if (objt_server(s->target)) |
| 5891 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5892 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5893 | if (!(s->flags & SF_ERR_MASK)) |
| 5894 | s->flags |= SF_ERR_SRVCL; |
| 5895 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5896 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5897 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5898 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5899 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5900 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5901 | return 0; |
| 5902 | } |
| 5903 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5904 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5905 | * processing can continue on next analysers, or zero if it either needs more |
| 5906 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5907 | * 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] | 5908 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5909 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5910 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5911 | 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] | 5912 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5913 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5914 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5915 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5916 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5917 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5918 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5919 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5920 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5921 | 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] | 5922 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5923 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5924 | rep, |
| 5925 | rep->rex, rep->wex, |
| 5926 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5927 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5928 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5929 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5930 | /* |
| 5931 | * Now parse the partial (or complete) lines. |
| 5932 | * We will check the response syntax, and also join multi-line |
| 5933 | * headers. An index of all the lines will be elaborated while |
| 5934 | * parsing. |
| 5935 | * |
| 5936 | * For the parsing, we use a 28 states FSM. |
| 5937 | * |
| 5938 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5939 | * rep->buf->p = beginning of response |
| 5940 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5941 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5942 | * msg->eol = end of current header or line (LF or CRLF) |
| 5943 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5944 | */ |
| 5945 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5946 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5947 | /* There's a protected area at the end of the buffer for rewriting |
| 5948 | * purposes. We don't want to start to parse the request if the |
| 5949 | * protected area is affected, because we may have to move processed |
| 5950 | * data later, which is much more complicated. |
| 5951 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5952 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5953 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5954 | /* some data has still not left the buffer, wake us once that's done */ |
| 5955 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5956 | goto abort_response; |
| 5957 | channel_dont_close(rep); |
| 5958 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5959 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5960 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5961 | } |
| 5962 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5963 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5964 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5965 | buffer_slow_realign(rep->buf); |
| 5966 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5967 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5968 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5969 | } |
| 5970 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5971 | /* 1: we might have to print this header in debug mode */ |
| 5972 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5973 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5974 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5975 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5976 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5977 | sol = rep->buf->p; |
| 5978 | 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] | 5979 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5980 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5981 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5982 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5983 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5984 | while (cur_idx) { |
| 5985 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5986 | debug_hdr("srvhdr", s, sol, eol); |
| 5987 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5988 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5989 | } |
| 5990 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5991 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5992 | /* |
| 5993 | * Now we quickly check if we have found a full valid response. |
| 5994 | * If not so, we check the FD and buffer states before leaving. |
| 5995 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5996 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5997 | * responses are checked first. |
| 5998 | * |
| 5999 | * Depending on whether the client is still there or not, we |
| 6000 | * may send an error response back or not. Note that normally |
| 6001 | * we should only check for HTTP status there, and check I/O |
| 6002 | * errors somewhere else. |
| 6003 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6004 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6005 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6006 | /* Invalid response */ |
| 6007 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6008 | /* we detected a parsing error. We want to archive this response |
| 6009 | * in the dedicated proxy area for later troubleshooting. |
| 6010 | */ |
| 6011 | hdr_response_bad: |
| 6012 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6013 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6014 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6015 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6016 | if (objt_server(s->target)) { |
| 6017 | objt_server(s->target)->counters.failed_resp++; |
| 6018 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6019 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 6020 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6021 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6022 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6023 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6024 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6025 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6026 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6027 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6028 | if (!(s->flags & SF_ERR_MASK)) |
| 6029 | s->flags |= SF_ERR_PRXCOND; |
| 6030 | if (!(s->flags & SF_FINST_MASK)) |
| 6031 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6032 | |
| 6033 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6034 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6035 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6036 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6037 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 6038 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6039 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6040 | goto hdr_response_bad; |
| 6041 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6042 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6043 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6044 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6045 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6046 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6047 | else if (txn->flags & TX_NOT_FIRST) |
| 6048 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6049 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6050 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6051 | if (objt_server(s->target)) { |
| 6052 | objt_server(s->target)->counters.failed_resp++; |
| 6053 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6054 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6055 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6056 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6057 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6058 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6059 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6060 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6061 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 6062 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6063 | if (!(s->flags & SF_ERR_MASK)) |
| 6064 | s->flags |= SF_ERR_SRVCL; |
| 6065 | if (!(s->flags & SF_FINST_MASK)) |
| 6066 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6067 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6068 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6069 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6070 | /* read timeout : return a 504 to the client. */ |
| 6071 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6072 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6073 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6074 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6075 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6076 | if (objt_server(s->target)) { |
| 6077 | objt_server(s->target)->counters.failed_resp++; |
| 6078 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6079 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6080 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6081 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6082 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6083 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6084 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6085 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6086 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6087 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6088 | if (!(s->flags & SF_ERR_MASK)) |
| 6089 | s->flags |= SF_ERR_SRVTO; |
| 6090 | if (!(s->flags & SF_FINST_MASK)) |
| 6091 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6092 | return 0; |
| 6093 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6094 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6095 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6096 | 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] | 6097 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6098 | s->be->be_counters.cli_aborts++; |
| 6099 | if (objt_server(s->target)) |
| 6100 | objt_server(s->target)->counters.cli_aborts++; |
| 6101 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6102 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6103 | channel_auto_close(rep); |
| 6104 | |
| 6105 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6106 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6107 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6108 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6109 | if (!(s->flags & SF_ERR_MASK)) |
| 6110 | s->flags |= SF_ERR_CLICL; |
| 6111 | if (!(s->flags & SF_FINST_MASK)) |
| 6112 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6113 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6114 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6115 | return 0; |
| 6116 | } |
| 6117 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6118 | /* 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] | 6119 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6120 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6121 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6122 | else if (txn->flags & TX_NOT_FIRST) |
| 6123 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6124 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6125 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6126 | if (objt_server(s->target)) { |
| 6127 | objt_server(s->target)->counters.failed_resp++; |
| 6128 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6129 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6130 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6131 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6132 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6133 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6134 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6135 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6136 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6137 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6138 | if (!(s->flags & SF_ERR_MASK)) |
| 6139 | s->flags |= SF_ERR_SRVCL; |
| 6140 | if (!(s->flags & SF_FINST_MASK)) |
| 6141 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6142 | return 0; |
| 6143 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6144 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6145 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6146 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6147 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6148 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6149 | else if (txn->flags & TX_NOT_FIRST) |
| 6150 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6151 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6152 | s->be->be_counters.failed_resp++; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6153 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6154 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6155 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6156 | if (!(s->flags & SF_ERR_MASK)) |
| 6157 | s->flags |= SF_ERR_CLICL; |
| 6158 | if (!(s->flags & SF_FINST_MASK)) |
| 6159 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6160 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6161 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6162 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6163 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6164 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6165 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6166 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6167 | return 0; |
| 6168 | } |
| 6169 | |
| 6170 | /* More interesting part now : we know that we have a complete |
| 6171 | * response which at least looks like HTTP. We have an indicator |
| 6172 | * of each header's length, so we can parse them quickly. |
| 6173 | */ |
| 6174 | |
| 6175 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6176 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6177 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6178 | /* |
| 6179 | * 1: get the status code |
| 6180 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6181 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6182 | if (n < 1 || n > 5) |
| 6183 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6184 | /* when the client triggers a 4xx from the server, it's most often due |
| 6185 | * to a missing object or permission. These events should be tracked |
| 6186 | * because if they happen often, it may indicate a brute force or a |
| 6187 | * vulnerability scan. |
| 6188 | */ |
| 6189 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6190 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6191 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6192 | if (objt_server(s->target)) |
| 6193 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6194 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6195 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6196 | * exactly one digit "." one digit. This check may be disabled using |
| 6197 | * option accept-invalid-http-response. |
| 6198 | */ |
| 6199 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6200 | if (msg->sl.st.v_l != 8) { |
| 6201 | msg->err_pos = 0; |
| 6202 | goto hdr_response_bad; |
| 6203 | } |
| 6204 | |
| 6205 | if (rep->buf->p[4] != '/' || |
| 6206 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6207 | rep->buf->p[6] != '.' || |
| 6208 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6209 | msg->err_pos = 4; |
| 6210 | goto hdr_response_bad; |
| 6211 | } |
| 6212 | } |
| 6213 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6214 | /* check if the response is HTTP/1.1 or above */ |
| 6215 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6216 | ((rep->buf->p[5] > '1') || |
| 6217 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6218 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6219 | |
| 6220 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6221 | 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] | 6222 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6223 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6224 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6225 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6226 | 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] | 6227 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6228 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6229 | * and 505 are triggered on demand by client request, so we must not |
| 6230 | * count them as server failures. |
| 6231 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6232 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6233 | 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] | 6234 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6235 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6236 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6237 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6238 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6239 | /* |
| 6240 | * 2: check for cacheability. |
| 6241 | */ |
| 6242 | |
| 6243 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6244 | case 100: |
| 6245 | /* |
| 6246 | * We may be facing a 100-continue response, in which case this |
| 6247 | * is not the right response, and we're waiting for the next one. |
| 6248 | * Let's allow this response to go to the client and wait for the |
| 6249 | * next one. |
| 6250 | */ |
| 6251 | hdr_idx_init(&txn->hdr_idx); |
| 6252 | msg->next -= channel_forward(rep, msg->next); |
| 6253 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6254 | txn->status = 0; |
| 6255 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6256 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6257 | goto next_one; |
| 6258 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6259 | case 200: |
| 6260 | case 203: |
| 6261 | case 206: |
| 6262 | case 300: |
| 6263 | case 301: |
| 6264 | case 410: |
| 6265 | /* RFC2616 @13.4: |
| 6266 | * "A response received with a status code of |
| 6267 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6268 | * by a cache (...) unless a cache-control |
| 6269 | * directive prohibits caching." |
| 6270 | * |
| 6271 | * RFC2616 @9.5: POST method : |
| 6272 | * "Responses to this method are not cacheable, |
| 6273 | * unless the response includes appropriate |
| 6274 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6275 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6276 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6277 | ((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] | 6278 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6279 | break; |
| 6280 | default: |
| 6281 | break; |
| 6282 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6283 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6284 | /* |
| 6285 | * 3: we may need to capture headers |
| 6286 | */ |
| 6287 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6288 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6289 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6290 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6291 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6292 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6293 | * by RFC7230#3.3.3 : |
| 6294 | * |
| 6295 | * The length of a message body is determined by one of the following |
| 6296 | * (in order of precedence): |
| 6297 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6298 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 6299 | * the connection will become a tunnel immediately after the empty |
| 6300 | * line that concludes the header fields. A client MUST ignore |
| 6301 | * any Content-Length or Transfer-Encoding header fields received |
| 6302 | * in such a message. Any 101 response (Switching Protocols) is |
| 6303 | * managed in the same manner. |
| 6304 | * |
| 6305 | * 2. Any response to a HEAD request and any response with a 1xx |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6306 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6307 | * code is always terminated by the first empty line after the |
| 6308 | * header fields, regardless of the header fields present in the |
| 6309 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6310 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6311 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6312 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6313 | * body length is determined by reading and decoding the chunked |
| 6314 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6315 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6316 | * If a Transfer-Encoding header field is present in a response and |
| 6317 | * the chunked transfer coding is not the final encoding, the |
| 6318 | * message body length is determined by reading the connection until |
| 6319 | * it is closed by the server. If a Transfer-Encoding header field |
| 6320 | * is present in a request and the chunked transfer coding is not |
| 6321 | * the final encoding, the message body length cannot be determined |
| 6322 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6323 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6324 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6325 | * If a message is received with both a Transfer-Encoding and a |
| 6326 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6327 | * Content-Length. Such a message might indicate an attempt to |
| 6328 | * perform request smuggling (Section 9.5) or response splitting |
| 6329 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6330 | * remove the received Content-Length field prior to forwarding such |
| 6331 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6332 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6333 | * 4. If a message is received without Transfer-Encoding and with |
| 6334 | * either multiple Content-Length header fields having differing |
| 6335 | * field-values or a single Content-Length header field having an |
| 6336 | * invalid value, then the message framing is invalid and the |
| 6337 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6338 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6339 | * status code and then close the connection. If this is a response |
| 6340 | * message received by a proxy, the proxy MUST close the connection |
| 6341 | * to the server, discard the received response, and send a 502 (Bad |
| 6342 | * Gateway) response to the client. If this is a response message |
| 6343 | * received by a user agent, the user agent MUST close the |
| 6344 | * connection to the server and discard the received response. |
| 6345 | * |
| 6346 | * 5. If a valid Content-Length header field is present without |
| 6347 | * Transfer-Encoding, its decimal value defines the expected message |
| 6348 | * body length in octets. If the sender closes the connection or |
| 6349 | * the recipient times out before the indicated number of octets are |
| 6350 | * received, the recipient MUST consider the message to be |
| 6351 | * incomplete and close the connection. |
| 6352 | * |
| 6353 | * 6. If this is a request message and none of the above are true, then |
| 6354 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6355 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6356 | * 7. Otherwise, this is a response message without a declared message |
| 6357 | * body length, so the message body length is determined by the |
| 6358 | * number of octets received prior to the server closing the |
| 6359 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6360 | */ |
| 6361 | |
| 6362 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6363 | * 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] | 6364 | * 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] | 6365 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6366 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6367 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6368 | txn->status == 101)) { |
| 6369 | /* Either we've established an explicit tunnel, or we're |
| 6370 | * switching the protocol. In both cases, we're very unlikely |
| 6371 | * to understand the next protocols. We have to switch to tunnel |
| 6372 | * mode, so that we transfer the request and responses then let |
| 6373 | * this protocol pass unmodified. When we later implement specific |
| 6374 | * parsers for such protocols, we'll want to check the Upgrade |
| 6375 | * header which contains information about that protocol for |
| 6376 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 6377 | */ |
| 6378 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6379 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 6380 | goto end; |
| 6381 | } |
| 6382 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6383 | if (txn->meth == HTTP_METH_HEAD || |
| 6384 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6385 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6386 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6387 | goto skip_content_length; |
| 6388 | } |
| 6389 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6390 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6391 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6392 | 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] | 6393 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6394 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6395 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6396 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6397 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6398 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6399 | break; |
| 6400 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6401 | } |
| 6402 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6403 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6404 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6405 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6406 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6407 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6408 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6409 | 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] | 6410 | signed long long cl; |
| 6411 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6412 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6413 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6414 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6415 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6416 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6417 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6418 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6419 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6420 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6421 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6422 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6423 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6424 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6425 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6426 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6427 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6428 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6429 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6430 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6431 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6432 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6433 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6434 | } |
| 6435 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6436 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6437 | /* Now we have to check if we need to modify the Connection header. |
| 6438 | * This is more difficult on the response than it is on the request, |
| 6439 | * because we can have two different HTTP versions and we don't know |
| 6440 | * how the client will interprete a response. For instance, let's say |
| 6441 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6442 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6443 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6444 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6445 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6446 | * indicating how a request MAY be understood by the client. In case |
| 6447 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6448 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6449 | * will fall back to explicit close. Note that we won't take risks with |
| 6450 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6451 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6452 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6453 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6454 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 6455 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6456 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6457 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6458 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6459 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6460 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6461 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6462 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6463 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6464 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6465 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6466 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6467 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6468 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6469 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6470 | /* now adjust header transformations depending on current state */ |
| 6471 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6472 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6473 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6474 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6475 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6476 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6477 | else { /* SCL / KAL */ |
| 6478 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6479 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6480 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6481 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6482 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6483 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6484 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6485 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6486 | /* Some keep-alive responses are converted to Server-close if |
| 6487 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6488 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6489 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6490 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6491 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6492 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6493 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6494 | } |
| 6495 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6496 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6497 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6498 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6499 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6500 | /* end of job, return OK */ |
| 6501 | rep->analysers &= ~an_bit; |
| 6502 | rep->analyse_exp = TICK_ETERNITY; |
| 6503 | channel_auto_close(rep); |
| 6504 | return 1; |
| 6505 | |
| 6506 | abort_keep_alive: |
| 6507 | /* A keep-alive request to the server failed on a network error. |
| 6508 | * The client is required to retry. We need to close without returning |
| 6509 | * any other information so that the client retries. |
| 6510 | */ |
| 6511 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6512 | rep->analysers &= AN_RES_FLT_END; |
| 6513 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6514 | channel_auto_close(rep); |
| 6515 | s->logs.logwait = 0; |
| 6516 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6517 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6518 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6519 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6520 | return 0; |
| 6521 | } |
| 6522 | |
| 6523 | /* This function performs all the processing enabled for the current response. |
| 6524 | * 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] | 6525 | * 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] | 6526 | * other functions. It works like process_request (see indications above). |
| 6527 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6528 | 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] | 6529 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6530 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6531 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6532 | struct http_msg *msg = &txn->rsp; |
| 6533 | struct proxy *cur_proxy; |
| 6534 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6535 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6536 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6537 | 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] | 6538 | now_ms, __FUNCTION__, |
| 6539 | s, |
| 6540 | rep, |
| 6541 | rep->rex, rep->wex, |
| 6542 | rep->flags, |
| 6543 | rep->buf->i, |
| 6544 | rep->analysers); |
| 6545 | |
| 6546 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6547 | return 0; |
| 6548 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6549 | /* The stats applet needs to adjust the Connection header but we don't |
| 6550 | * apply any filter there. |
| 6551 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6552 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6553 | rep->analysers &= ~an_bit; |
| 6554 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6555 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6556 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6557 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6558 | /* |
| 6559 | * We will have to evaluate the filters. |
| 6560 | * As opposed to version 1.2, now they will be evaluated in the |
| 6561 | * filters order and not in the header order. This means that |
| 6562 | * each filter has to be validated among all headers. |
| 6563 | * |
| 6564 | * Filters are tried with ->be first, then with ->fe if it is |
| 6565 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6566 | * |
| 6567 | * Maybe we are in resume condiion. In this case I choose the |
| 6568 | * "struct proxy" which contains the rule list matching the resume |
| 6569 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6570 | * the process with the first one. |
| 6571 | * |
| 6572 | * In fact, I check only correspondance betwwen the current list |
| 6573 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6574 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6575 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6576 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6577 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6578 | else |
| 6579 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6580 | while (1) { |
| 6581 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6582 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6583 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6584 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6585 | 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] | 6586 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6587 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6588 | goto return_srv_prx_502; |
| 6589 | |
| 6590 | if (ret == HTTP_RULE_RES_DONE) { |
| 6591 | rep->analysers &= ~an_bit; |
| 6592 | rep->analyse_exp = TICK_ETERNITY; |
| 6593 | return 1; |
| 6594 | } |
| 6595 | } |
| 6596 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6597 | /* we need to be called again. */ |
| 6598 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6599 | channel_dont_close(rep); |
| 6600 | return 0; |
| 6601 | } |
| 6602 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6603 | /* try headers filters */ |
| 6604 | if (rule_set->rsp_exp != NULL) { |
| 6605 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6606 | return_bad_resp: |
| 6607 | if (objt_server(s->target)) { |
| 6608 | objt_server(s->target)->counters.failed_resp++; |
| 6609 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6610 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6611 | s->be->be_counters.failed_resp++; |
| 6612 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6613 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6614 | txn->status = 502; |
| 6615 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6616 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6617 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6618 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6619 | if (!(s->flags & SF_ERR_MASK)) |
| 6620 | s->flags |= SF_ERR_PRXCOND; |
| 6621 | if (!(s->flags & SF_FINST_MASK)) |
| 6622 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6623 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6624 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6625 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6626 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6627 | /* has the response been denied ? */ |
| 6628 | if (txn->flags & TX_SVDENY) { |
| 6629 | if (objt_server(s->target)) |
| 6630 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6631 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6632 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6633 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6634 | if (sess->listener->counters) |
| 6635 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6636 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6637 | goto return_srv_prx_502; |
| 6638 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6639 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6640 | /* add response headers from the rule sets in the same order */ |
| 6641 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6642 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6643 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6644 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6645 | 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] | 6646 | ret = acl_pass(ret); |
| 6647 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6648 | ret = !ret; |
| 6649 | if (!ret) |
| 6650 | continue; |
| 6651 | } |
| 6652 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6653 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6654 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6655 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6656 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6657 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6658 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6659 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6660 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6661 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6662 | /* After this point, this anayzer can't return yield, so we can |
| 6663 | * remove the bit corresponding to this analyzer from the list. |
| 6664 | * |
| 6665 | * Note that the intermediate returns and goto found previously |
| 6666 | * reset the analyzers. |
| 6667 | */ |
| 6668 | rep->analysers &= ~an_bit; |
| 6669 | rep->analyse_exp = TICK_ETERNITY; |
| 6670 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6671 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6672 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6673 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6674 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6675 | /* |
| 6676 | * Now check for a server cookie. |
| 6677 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6678 | 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] | 6679 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6680 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6681 | /* |
| 6682 | * Check for cache-control or pragma headers if required. |
| 6683 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6684 | 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] | 6685 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6686 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6687 | /* |
| 6688 | * Add server cookie in the response if needed |
| 6689 | */ |
| 6690 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6691 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6692 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6693 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6694 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6695 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6696 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6697 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6698 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6699 | /* the server is known, it's not the one the client requested, or the |
| 6700 | * cookie's last seen date needs to be refreshed. We have to |
| 6701 | * insert a set-cookie here, except if we want to insert only on POST |
| 6702 | * requests and this one isn't. Note that servers which don't have cookies |
| 6703 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6704 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6705 | if (!objt_server(s->target)->cookie) { |
| 6706 | chunk_printf(&trash, |
| 6707 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6708 | s->be->cookie_name); |
| 6709 | } |
| 6710 | else { |
| 6711 | 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] | 6712 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6713 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6714 | /* emit last_date, which is mandatory */ |
| 6715 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6716 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6717 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6718 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6719 | if (s->be->cookie_maxlife) { |
| 6720 | /* emit first_date, which is either the original one or |
| 6721 | * the current date. |
| 6722 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6723 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6724 | s30tob64(txn->cookie_first_date ? |
| 6725 | txn->cookie_first_date >> 2 : |
| 6726 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6727 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6728 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6729 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6730 | chunk_appendf(&trash, "; path=/"); |
| 6731 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6732 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6733 | if (s->be->cookie_domain) |
| 6734 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6735 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6736 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6737 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6738 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6739 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6740 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6741 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6742 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6743 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6744 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6745 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6746 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6747 | /* the server did not change, only the date was updated */ |
| 6748 | txn->flags |= TX_SCK_UPDATED; |
| 6749 | else |
| 6750 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6751 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6752 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6753 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6754 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6755 | * 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] | 6756 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6757 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6758 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6759 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6760 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6761 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6762 | "Cache-control: private", 22) < 0)) |
| 6763 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6764 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6765 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6766 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6767 | /* |
| 6768 | * Check if result will be cacheable with a cookie. |
| 6769 | * We'll block the response if security checks have caught |
| 6770 | * nasty things such as a cacheable cookie. |
| 6771 | */ |
| 6772 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6773 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6774 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6775 | /* we're in presence of a cacheable response containing |
| 6776 | * a set-cookie header. We'll block it as requested by |
| 6777 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6778 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6779 | if (objt_server(s->target)) |
| 6780 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6781 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6782 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6783 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6784 | if (sess->listener->counters) |
| 6785 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6786 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6787 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6788 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6789 | send_log(s->be, LOG_ALERT, |
| 6790 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6791 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6792 | goto return_srv_prx_502; |
| 6793 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6794 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6795 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6796 | /* |
| 6797 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6798 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6799 | * 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] | 6800 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6801 | * want to touch any 101 response either since it's switching to another |
| 6802 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6803 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6804 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6805 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6806 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6807 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6808 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6809 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6810 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6811 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6812 | /* we want a keep-alive response here. Keep-alive header |
| 6813 | * required if either side is not 1.1. |
| 6814 | */ |
| 6815 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6816 | want_flags |= TX_CON_KAL_SET; |
| 6817 | } |
| 6818 | else { |
| 6819 | /* we want a close response here. Close header required if |
| 6820 | * the server is 1.1, regardless of the client. |
| 6821 | */ |
| 6822 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6823 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6824 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6825 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6826 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6827 | http_change_connection_header(txn, msg, want_flags); |
| 6828 | } |
| 6829 | |
| 6830 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6831 | /* Always enter in the body analyzer */ |
| 6832 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 6833 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6834 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6835 | /* if the user wants to log as soon as possible, without counting |
| 6836 | * bytes from the server, then this is the right moment. We have |
| 6837 | * to temporarily assign bytes_out to log what we currently have. |
| 6838 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6839 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6840 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6841 | s->logs.bytes_out = txn->rsp.eoh; |
| 6842 | s->do_log(s); |
| 6843 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6844 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6845 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6846 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6847 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6848 | /* This function is an analyser which forwards response body (including chunk |
| 6849 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6850 | * zero byte. The only situation where it must not be called is when we're in |
| 6851 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6852 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6853 | * 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] | 6854 | * 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] | 6855 | * |
| 6856 | * It is capable of compressing response data both in content-length mode and |
| 6857 | * in chunked mode. The state machines follows different flows depending on |
| 6858 | * whether content-length and chunked modes are used, since there are no |
| 6859 | * trailers in content-length : |
| 6860 | * |
| 6861 | * chk-mode cl-mode |
| 6862 | * ,----- BODY -----. |
| 6863 | * / \ |
| 6864 | * V size > 0 V chk-mode |
| 6865 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6866 | * | | size == 0 | last byte | |
| 6867 | * | v final crlf v inspected | |
| 6868 | * | TRAILERS -----------> DONE | |
| 6869 | * | | |
| 6870 | * `----------------------------------------------' |
| 6871 | * |
| 6872 | * Compression only happens in the DATA state, and must be flushed in final |
| 6873 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6874 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6875 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6876 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6877 | 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] | 6878 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6879 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6880 | struct http_txn *txn = s->txn; |
| 6881 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6882 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6883 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 6884 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6885 | now_ms, __FUNCTION__, |
| 6886 | s, |
| 6887 | res, |
| 6888 | res->rex, res->wex, |
| 6889 | res->flags, |
| 6890 | res->buf->i, |
| 6891 | res->analysers); |
| 6892 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6893 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6894 | return 0; |
| 6895 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6896 | 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] | 6897 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6898 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6899 | /* Output closed while we were sending data. We must abort and |
| 6900 | * wake the other side up. |
| 6901 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6902 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6903 | msg->msg_state = HTTP_MSG_ERROR; |
| 6904 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6905 | return 1; |
| 6906 | } |
| 6907 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6908 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6909 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6910 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6911 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6912 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6913 | ? HTTP_MSG_CHUNK_SIZE |
| 6914 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6915 | } |
| 6916 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6917 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6918 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6919 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6920 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6921 | } |
| 6922 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6923 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6924 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6925 | ? http_msg_forward_chunked_body(s, msg) |
| 6926 | : http_msg_forward_body(s, msg)); |
| 6927 | if (!ret) |
| 6928 | goto missing_data_or_waiting; |
| 6929 | if (ret < 0) |
| 6930 | goto return_bad_res; |
| 6931 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6932 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6933 | /* other states, DONE...TUNNEL */ |
| 6934 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6935 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6936 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6937 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6938 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6939 | if (http_resync_states(s)) { |
| 6940 | /* some state changes occurred, maybe the analyser was disabled |
| 6941 | * too. */ |
| 6942 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6943 | if (res->flags & CF_SHUTW) { |
| 6944 | /* response errors are most likely due to the |
| 6945 | * client aborting the transfer. */ |
| 6946 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6947 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6948 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6949 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6950 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6951 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6952 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6953 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6954 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6955 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6956 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6957 | if (res->flags & CF_SHUTW) |
| 6958 | goto aborted_xfer; |
| 6959 | |
| 6960 | /* stop waiting for data if the input is closed before the end. If the |
| 6961 | * client side was already closed, it means that the client has aborted, |
| 6962 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6963 | * server abort. |
| 6964 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6965 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6966 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6967 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6968 | /* If we have some pending data, we continue the processing */ |
| 6969 | if (!buffer_pending(res->buf)) { |
| 6970 | if (!(s->flags & SF_ERR_MASK)) |
| 6971 | s->flags |= SF_ERR_SRVCL; |
| 6972 | s->be->be_counters.srv_aborts++; |
| 6973 | if (objt_server(s->target)) |
| 6974 | objt_server(s->target)->counters.srv_aborts++; |
| 6975 | goto return_bad_res_stats_ok; |
| 6976 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6977 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6978 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6979 | /* 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] | 6980 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6981 | goto return_bad_res; |
| 6982 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6983 | /* When TE: chunked is used, we need to get there again to parse |
| 6984 | * remaining chunks even if the server has closed, so we don't want to |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6985 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6986 | * or if there are filters registered on the stream, we don't want to |
| 6987 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6988 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6989 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6990 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6991 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6992 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6993 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6994 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6995 | /* 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] | 6996 | * 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] | 6997 | * 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] | 6998 | * modes are already handled by the stream sock layer. We must not do |
| 6999 | * this in content-length mode because it could present the MSG_MORE |
| 7000 | * flag with the last block of forwarded data, which would cause an |
| 7001 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7002 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 7003 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7004 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7005 | |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 7006 | /* If there is neither content-length, nor transfer-encoding header |
| 7007 | * _AND_ there is no data filtering, we can safely forward all data |
| 7008 | * indefinitely. */ |
| 7009 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_DATA_FILTERS(s, res)) { |
| 7010 | buffer_flush(res->buf); |
| 7011 | channel_forward_forever(res); |
| 7012 | } |
| 7013 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7014 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7015 | return 0; |
| 7016 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7017 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7018 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7019 | if (objt_server(s->target)) |
| 7020 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7021 | |
| 7022 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 7023 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7024 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7025 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 7026 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 7027 | res->analysers &= AN_RES_FLT_END; |
| 7028 | s->req.analysers &= AN_REQ_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] | 7029 | if (objt_server(s->target)) |
| 7030 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7031 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7032 | if (!(s->flags & SF_ERR_MASK)) |
| 7033 | s->flags |= SF_ERR_PRXCOND; |
| 7034 | if (!(s->flags & SF_FINST_MASK)) |
| 7035 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7036 | return 0; |
| 7037 | |
| 7038 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 7039 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7040 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7041 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 7042 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 7043 | res->analysers &= AN_RES_FLT_END; |
| 7044 | s->req.analysers &= AN_REQ_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] | 7045 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7046 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7047 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7048 | if (objt_server(s->target)) |
| 7049 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7050 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7051 | if (!(s->flags & SF_ERR_MASK)) |
| 7052 | s->flags |= SF_ERR_CLICL; |
| 7053 | if (!(s->flags & SF_FINST_MASK)) |
| 7054 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7055 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | |
| 7059 | static inline int |
| 7060 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 7061 | { |
| 7062 | struct channel *chn = msg->chn; |
| 7063 | int ret; |
| 7064 | |
| 7065 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 7066 | |
| 7067 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 7068 | goto ending; |
| 7069 | |
| 7070 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 7071 | * read the body until the server connection is closed. In that case, we |
| 7072 | * eat data as they come. Of course, this happens for response only. */ |
| 7073 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 7074 | unsigned long long len = (chn->buf->i - msg->next); |
| 7075 | msg->chunk_len += len; |
| 7076 | msg->body_len += len; |
| 7077 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7078 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7079 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7080 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7081 | msg->next += ret; |
| 7082 | msg->chunk_len -= ret; |
| 7083 | if (msg->chunk_len) { |
| 7084 | /* input empty or output full */ |
| 7085 | if (chn->buf->i > msg->next) |
| 7086 | chn->flags |= CF_WAKE_WRITE; |
| 7087 | goto missing_data_or_waiting; |
| 7088 | } |
| 7089 | |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 7090 | /* The server still sending data that should be filtered */ |
| 7091 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7092 | goto missing_data_or_waiting; |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 7093 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7094 | msg->msg_state = HTTP_MSG_ENDING; |
| 7095 | |
| 7096 | ending: |
| 7097 | /* we may have some pending data starting at res->buf->p such as a last |
| 7098 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7099 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7100 | /* default_ret */ msg->next, |
| 7101 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7102 | b_adv(chn->buf, ret); |
| 7103 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7104 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7105 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7106 | if (msg->next) |
| 7107 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7108 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7109 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 7110 | /* default_ret */ 1, |
| 7111 | /* on_error */ goto error, |
| 7112 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7113 | msg->msg_state = HTTP_MSG_DONE; |
| 7114 | return 1; |
| 7115 | |
| 7116 | missing_data_or_waiting: |
| 7117 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7118 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7119 | /* default_ret */ msg->next, |
| 7120 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7121 | b_adv(chn->buf, ret); |
| 7122 | msg->next -= ret; |
| 7123 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7124 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7125 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7126 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7127 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7128 | return 0; |
| 7129 | error: |
| 7130 | return -1; |
| 7131 | } |
| 7132 | |
| 7133 | static inline int |
| 7134 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 7135 | { |
| 7136 | struct channel *chn = msg->chn; |
| 7137 | int ret; |
| 7138 | |
| 7139 | /* Here we have the guarantee to be in one of the following state: |
| 7140 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 7141 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 7142 | |
| 7143 | switch_states: |
| 7144 | switch (msg->msg_state) { |
| 7145 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7146 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7147 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7148 | /* on_error */ goto error); |
| 7149 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7150 | msg->chunk_len -= ret; |
| 7151 | if (msg->chunk_len) { |
| 7152 | /* input empty or output full */ |
| 7153 | if (chn->buf->i > msg->next) |
| 7154 | chn->flags |= CF_WAKE_WRITE; |
| 7155 | goto missing_data_or_waiting; |
| 7156 | } |
| 7157 | |
| 7158 | /* nothing left to forward for this chunk*/ |
| 7159 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 7160 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7161 | |
| 7162 | case HTTP_MSG_CHUNK_CRLF: |
| 7163 | /* we want the CRLF after the data */ |
| 7164 | ret = http_skip_chunk_crlf(msg); |
| 7165 | if (ret == 0) |
| 7166 | goto missing_data_or_waiting; |
| 7167 | if (ret < 0) |
| 7168 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7169 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7170 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 7171 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 7172 | |
| 7173 | case HTTP_MSG_CHUNK_SIZE: |
| 7174 | /* read the chunk size and assign it to ->chunk_len, |
| 7175 | * then set ->next to point to the body and switch to |
| 7176 | * DATA or TRAILERS state. |
| 7177 | */ |
| 7178 | ret = http_parse_chunk_size(msg); |
| 7179 | if (ret == 0) |
| 7180 | goto missing_data_or_waiting; |
| 7181 | if (ret < 0) |
| 7182 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7183 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7184 | if (msg->chunk_len) { |
| 7185 | msg->msg_state = HTTP_MSG_DATA; |
| 7186 | goto switch_states; |
| 7187 | } |
| 7188 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 7189 | /* fall through for HTTP_MSG_TRAILERS */ |
| 7190 | |
| 7191 | case HTTP_MSG_TRAILERS: |
| 7192 | ret = http_forward_trailers(msg); |
| 7193 | if (ret < 0) |
| 7194 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7195 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 7196 | /* default_ret */ 1, |
| 7197 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7198 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7199 | if (!ret) |
| 7200 | goto missing_data_or_waiting; |
| 7201 | break; |
| 7202 | |
| 7203 | case HTTP_MSG_ENDING: |
| 7204 | goto ending; |
| 7205 | |
| 7206 | default: |
| 7207 | /* This should no happen in this function */ |
| 7208 | goto error; |
| 7209 | } |
| 7210 | |
| 7211 | msg->msg_state = HTTP_MSG_ENDING; |
| 7212 | ending: |
| 7213 | /* we may have some pending data starting at res->buf->p such as a last |
| 7214 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7215 | 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] | 7216 | /* default_ret */ msg->next, |
| 7217 | /* on_error */ goto error); |
| 7218 | b_adv(chn->buf, ret); |
| 7219 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7220 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7221 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7222 | if (msg->next) |
| 7223 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7224 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7225 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7226 | /* default_ret */ 1, |
| 7227 | /* on_error */ goto error, |
| 7228 | /* on_wait */ goto waiting); |
| 7229 | msg->msg_state = HTTP_MSG_DONE; |
| 7230 | return 1; |
| 7231 | |
| 7232 | missing_data_or_waiting: |
| 7233 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7234 | 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] | 7235 | /* default_ret */ msg->next, |
| 7236 | /* on_error */ goto error); |
| 7237 | b_adv(chn->buf, ret); |
| 7238 | msg->next -= ret; |
| 7239 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7240 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7241 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7242 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7243 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7244 | return 0; |
| 7245 | |
| 7246 | chunk_parsing_error: |
| 7247 | if (msg->err_pos >= 0) { |
| 7248 | if (chn->flags & CF_ISRESP) |
| 7249 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 7250 | msg->msg_state, strm_fe(s)); |
| 7251 | else |
| 7252 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 7253 | msg, msg->msg_state, s->be); |
| 7254 | } |
| 7255 | error: |
| 7256 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7257 | } |
| 7258 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7259 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7260 | /* Iterate the same filter through all request headers. |
| 7261 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7262 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7263 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7264 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7265 | 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] | 7266 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7267 | char *cur_ptr, *cur_end, *cur_next; |
| 7268 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7269 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7270 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7271 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7273 | last_hdr = 0; |
| 7274 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7275 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7276 | old_idx = 0; |
| 7277 | |
| 7278 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7279 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7280 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7281 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7282 | (exp->action == ACT_ALLOW || |
| 7283 | exp->action == ACT_DENY || |
| 7284 | exp->action == ACT_TARPIT)) |
| 7285 | return 0; |
| 7286 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7287 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7288 | if (!cur_idx) |
| 7289 | break; |
| 7290 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7291 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7292 | cur_ptr = cur_next; |
| 7293 | cur_end = cur_ptr + cur_hdr->len; |
| 7294 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7295 | |
| 7296 | /* Now we have one header between cur_ptr and cur_end, |
| 7297 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7298 | */ |
| 7299 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7300 | 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] | 7301 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7302 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7303 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7304 | last_hdr = 1; |
| 7305 | break; |
| 7306 | |
| 7307 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7308 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7309 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7310 | break; |
| 7311 | |
| 7312 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7313 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7314 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7315 | break; |
| 7316 | |
| 7317 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7318 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7319 | if (trash.len < 0) |
| 7320 | return -1; |
| 7321 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7322 | 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] | 7323 | /* FIXME: if the user adds a newline in the replacement, the |
| 7324 | * index will not be recalculated for now, and the new line |
| 7325 | * will not be counted as a new header. |
| 7326 | */ |
| 7327 | |
| 7328 | cur_end += delta; |
| 7329 | cur_next += delta; |
| 7330 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7331 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7332 | break; |
| 7333 | |
| 7334 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7335 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7336 | cur_next += delta; |
| 7337 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7338 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7339 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7340 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7341 | cur_hdr->len = 0; |
| 7342 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7343 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7344 | break; |
| 7345 | |
| 7346 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7347 | } |
| 7348 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7349 | /* keep the link from this header to next one in case of later |
| 7350 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7351 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7352 | old_idx = cur_idx; |
| 7353 | } |
| 7354 | return 0; |
| 7355 | } |
| 7356 | |
| 7357 | |
| 7358 | /* Apply the filter to the request line. |
| 7359 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7360 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7361 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7362 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7363 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7364 | 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] | 7365 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7366 | char *cur_ptr, *cur_end; |
| 7367 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7368 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7369 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7370 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7371 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7372 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7373 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7374 | (exp->action == ACT_ALLOW || |
| 7375 | exp->action == ACT_DENY || |
| 7376 | exp->action == ACT_TARPIT)) |
| 7377 | return 0; |
| 7378 | else if (exp->action == ACT_REMOVE) |
| 7379 | return 0; |
| 7380 | |
| 7381 | done = 0; |
| 7382 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7383 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7384 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7385 | |
| 7386 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7387 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7388 | 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] | 7389 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7390 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7391 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7392 | done = 1; |
| 7393 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7395 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7396 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7397 | done = 1; |
| 7398 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7399 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7400 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7401 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7402 | done = 1; |
| 7403 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7404 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7405 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7406 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7407 | if (trash.len < 0) |
| 7408 | return -1; |
| 7409 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7410 | 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] | 7411 | /* FIXME: if the user adds a newline in the replacement, the |
| 7412 | * index will not be recalculated for now, and the new line |
| 7413 | * will not be counted as a new header. |
| 7414 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7415 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7416 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7417 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7418 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7419 | HTTP_MSG_RQMETH, |
| 7420 | cur_ptr, cur_end + 1, |
| 7421 | NULL, NULL); |
| 7422 | if (unlikely(!cur_end)) |
| 7423 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7425 | /* we have a full request and we know that we have either a CR |
| 7426 | * or an LF at <ptr>. |
| 7427 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7428 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7429 | 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] | 7430 | /* there is no point trying this regex on headers */ |
| 7431 | return 1; |
| 7432 | } |
| 7433 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7434 | return done; |
| 7435 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7436 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7437 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7439 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7440 | * 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] | 7441 | * 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] | 7442 | * unparsable request. Since it can manage the switch to another backend, it |
| 7443 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7444 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7445 | 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] | 7446 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7447 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7448 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7449 | struct hdr_exp *exp; |
| 7450 | |
| 7451 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7452 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7453 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7454 | /* |
| 7455 | * The interleaving of transformations and verdicts |
| 7456 | * makes it difficult to decide to continue or stop |
| 7457 | * the evaluation. |
| 7458 | */ |
| 7459 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7460 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7461 | break; |
| 7462 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7463 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7464 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7465 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7466 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7467 | |
| 7468 | /* if this filter had a condition, evaluate it now and skip to |
| 7469 | * next filter if the condition does not match. |
| 7470 | */ |
| 7471 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7472 | 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] | 7473 | ret = acl_pass(ret); |
| 7474 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7475 | ret = !ret; |
| 7476 | |
| 7477 | if (!ret) |
| 7478 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7479 | } |
| 7480 | |
| 7481 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7482 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7483 | if (unlikely(ret < 0)) |
| 7484 | return -1; |
| 7485 | |
| 7486 | if (likely(ret == 0)) { |
| 7487 | /* The filter did not match the request, it can be |
| 7488 | * iterated through all headers. |
| 7489 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7490 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7491 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7492 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7493 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7494 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7495 | } |
| 7496 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7497 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7498 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7499 | * same way as with headers above except that the semi-colon also ends a token. |
| 7500 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7501 | * return a valid result. |
| 7502 | */ |
| 7503 | char *find_cookie_value_end(char *s, const char *e) |
| 7504 | { |
| 7505 | int quoted, qdpair; |
| 7506 | |
| 7507 | quoted = qdpair = 0; |
| 7508 | for (; s < e; s++) { |
| 7509 | if (qdpair) qdpair = 0; |
| 7510 | else if (quoted) { |
| 7511 | if (*s == '\\') qdpair = 1; |
| 7512 | else if (*s == '"') quoted = 0; |
| 7513 | } |
| 7514 | else if (*s == '"') quoted = 1; |
| 7515 | else if (*s == ',' || *s == ';') return s; |
| 7516 | } |
| 7517 | return s; |
| 7518 | } |
| 7519 | |
| 7520 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7521 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7522 | * the first delimiter is updated if required. The function tries as much as |
| 7523 | * possible to respect the following principles : |
| 7524 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7525 | * colon, in which case <next> is simply removed |
| 7526 | * - set exactly one space character after the new first delimiter, unless |
| 7527 | * there are not enough characters in the block being moved to do so. |
| 7528 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7529 | * one. |
| 7530 | * |
| 7531 | * It is the caller's responsibility to ensure that : |
| 7532 | * - <from> points to a valid delimiter or the colon ; |
| 7533 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7534 | * - there are non-space chars before <from> ; |
| 7535 | * - there is a CR/LF at or after <next>. |
| 7536 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7537 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7538 | { |
| 7539 | char *prev = *from; |
| 7540 | |
| 7541 | if (*prev == ':') { |
| 7542 | /* We're removing the first value, preserve the colon and add a |
| 7543 | * space if possible. |
| 7544 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7545 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7546 | next++; |
| 7547 | prev++; |
| 7548 | if (prev < next) |
| 7549 | *prev++ = ' '; |
| 7550 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7551 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7552 | next++; |
| 7553 | } else { |
| 7554 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7555 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7556 | prev--; |
| 7557 | *from = prev; |
| 7558 | |
| 7559 | /* copy the delimiter and if possible a space if we're |
| 7560 | * not at the end of the line. |
| 7561 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7562 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7563 | *prev++ = *next++; |
| 7564 | if (prev + 1 < next) |
| 7565 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7566 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7567 | next++; |
| 7568 | } |
| 7569 | } |
| 7570 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7571 | } |
| 7572 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7573 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7574 | * 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] | 7575 | * desirable to call it only when needed. This code is quite complex because |
| 7576 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7577 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7578 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7579 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7580 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7581 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7582 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7583 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7584 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7585 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7586 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7587 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7588 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7589 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7590 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7591 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7592 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7593 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7594 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7595 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7596 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7597 | hdr_beg = hdr_next; |
| 7598 | hdr_end = hdr_beg + cur_hdr->len; |
| 7599 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7600 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7601 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7602 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7603 | * "Cookie:" headers. |
| 7604 | */ |
| 7605 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7606 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7607 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7608 | old_idx = cur_idx; |
| 7609 | continue; |
| 7610 | } |
| 7611 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7612 | del_from = NULL; /* nothing to be deleted */ |
| 7613 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7614 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7615 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7616 | * attributes whose name begin with a '$', and associate them with |
| 7617 | * the right cookie, if we want to delete this cookie. |
| 7618 | * So there are 3 cases for each cookie read : |
| 7619 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7620 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7621 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7622 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7623 | * "special" cookie. |
| 7624 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7625 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7626 | * *MUST* delete it. |
| 7627 | * |
| 7628 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7629 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7630 | * the RFC explicitly allows spaces before it, and not within the |
| 7631 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7632 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7633 | * after the equal sign too, resulting in some (rare) buggy |
| 7634 | * implementations trying to do that. So let's do what servers do. |
| 7635 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7636 | * allowed quoted strings in values, with any possible character |
| 7637 | * after a backslash, including control chars and delimitors, which |
| 7638 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7639 | * within values even without quotes. |
| 7640 | * |
| 7641 | * We have to keep multiple pointers in order to support cookie |
| 7642 | * removal at the beginning, middle or end of header without |
| 7643 | * corrupting the header. All of these headers are valid : |
| 7644 | * |
| 7645 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7646 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7647 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7648 | * | | | | | | | | | |
| 7649 | * | | | | | | | | hdr_end <--+ |
| 7650 | * | | | | | | | +--> next |
| 7651 | * | | | | | | +----> val_end |
| 7652 | * | | | | | +-----------> val_beg |
| 7653 | * | | | | +--------------> equal |
| 7654 | * | | | +----------------> att_end |
| 7655 | * | | +---------------------> att_beg |
| 7656 | * | +--------------------------> prev |
| 7657 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7658 | */ |
| 7659 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7660 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7661 | /* Iterate through all cookies on this line */ |
| 7662 | |
| 7663 | /* find att_beg */ |
| 7664 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7665 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7666 | att_beg++; |
| 7667 | |
| 7668 | /* find att_end : this is the first character after the last non |
| 7669 | * space before the equal. It may be equal to hdr_end. |
| 7670 | */ |
| 7671 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7672 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7673 | while (equal < hdr_end) { |
| 7674 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7675 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7676 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7677 | continue; |
| 7678 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7679 | } |
| 7680 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7681 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7682 | * is between <att_beg> and <equal>, both may be identical. |
| 7683 | */ |
| 7684 | |
| 7685 | /* look for end of cookie if there is an equal sign */ |
| 7686 | if (equal < hdr_end && *equal == '=') { |
| 7687 | /* look for the beginning of the value */ |
| 7688 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7689 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7690 | val_beg++; |
| 7691 | |
| 7692 | /* find the end of the value, respecting quotes */ |
| 7693 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7694 | |
| 7695 | /* make val_end point to the first white space or delimitor after the value */ |
| 7696 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7697 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7698 | val_end--; |
| 7699 | } else { |
| 7700 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7701 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7702 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7703 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7704 | * they will automatically be removed if a header before them is removed, |
| 7705 | * since they're supposed to be linked together. |
| 7706 | */ |
| 7707 | if (*att_beg == '$') |
| 7708 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7709 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7710 | /* Ignore cookies with no equal sign */ |
| 7711 | if (equal == next) { |
| 7712 | /* This is not our cookie, so we must preserve it. But if we already |
| 7713 | * scheduled another cookie for removal, we cannot remove the |
| 7714 | * complete header, but we can remove the previous block itself. |
| 7715 | */ |
| 7716 | preserve_hdr = 1; |
| 7717 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7718 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7719 | val_end += delta; |
| 7720 | next += delta; |
| 7721 | hdr_end += delta; |
| 7722 | hdr_next += delta; |
| 7723 | cur_hdr->len += delta; |
| 7724 | http_msg_move_end(&txn->req, delta); |
| 7725 | prev = del_from; |
| 7726 | del_from = NULL; |
| 7727 | } |
| 7728 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7729 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7730 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7731 | /* if there are spaces around the equal sign, we need to |
| 7732 | * strip them otherwise we'll get trouble for cookie captures, |
| 7733 | * or even for rewrites. Since this happens extremely rarely, |
| 7734 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7735 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7736 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7737 | int stripped_before = 0; |
| 7738 | int stripped_after = 0; |
| 7739 | |
| 7740 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7741 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7742 | equal += stripped_before; |
| 7743 | val_beg += stripped_before; |
| 7744 | } |
| 7745 | |
| 7746 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7747 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7748 | val_beg += stripped_after; |
| 7749 | stripped_before += stripped_after; |
| 7750 | } |
| 7751 | |
| 7752 | val_end += stripped_before; |
| 7753 | next += stripped_before; |
| 7754 | hdr_end += stripped_before; |
| 7755 | hdr_next += stripped_before; |
| 7756 | cur_hdr->len += stripped_before; |
| 7757 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7758 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7760 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7761 | /* First, let's see if we want to capture this cookie. We check |
| 7762 | * that we don't already have a client side cookie, because we |
| 7763 | * can only capture one. Also as an optimisation, we ignore |
| 7764 | * cookies shorter than the declared name. |
| 7765 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7766 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7767 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7768 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7769 | int log_len = val_end - att_beg; |
| 7770 | |
| 7771 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7772 | Alert("HTTP logging : out of memory.\n"); |
| 7773 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7774 | if (log_len > sess->fe->capture_len) |
| 7775 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7776 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7777 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7778 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7779 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7780 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7781 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7782 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7783 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7784 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7785 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7786 | * For cookies in prefix mode, the form is : |
| 7787 | * |
| 7788 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7789 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7790 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7791 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7792 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7793 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7794 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7795 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7796 | * have the server ID between val_beg and delim, and the original cookie between |
| 7797 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7798 | * |
| 7799 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7800 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7801 | * | || || | |+-> next |
| 7802 | * | || || | +--> val_end |
| 7803 | * | || || +---------> delim |
| 7804 | * | || |+------------> val_beg |
| 7805 | * | || +-------------> att_end = equal |
| 7806 | * | |+-----------------> att_beg |
| 7807 | * | +------------------> prev |
| 7808 | * +-------------------------> hdr_beg |
| 7809 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7810 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7811 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7812 | for (delim = val_beg; delim < val_end; delim++) |
| 7813 | if (*delim == COOKIE_DELIM) |
| 7814 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7815 | } else { |
| 7816 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7817 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7818 | /* Now check if the cookie contains a date field, which would |
| 7819 | * appear after a vertical bar ('|') just after the server name |
| 7820 | * and before the delimiter. |
| 7821 | */ |
| 7822 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7823 | if (vbar1) { |
| 7824 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7825 | * right is the last seen date. It is a base64 encoded |
| 7826 | * 30-bit value representing the UNIX date since the |
| 7827 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7828 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7829 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7830 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7831 | if (val_end - vbar1 >= 5) { |
| 7832 | val = b64tos30(vbar1); |
| 7833 | if (val > 0) |
| 7834 | txn->cookie_last_date = val << 2; |
| 7835 | } |
| 7836 | /* look for a second vertical bar */ |
| 7837 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7838 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7839 | val = b64tos30(vbar1 + 1); |
| 7840 | if (val > 0) |
| 7841 | txn->cookie_first_date = val << 2; |
| 7842 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7843 | } |
| 7844 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7845 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7846 | /* if the cookie has an expiration date and the proxy wants to check |
| 7847 | * it, then we do that now. We first check if the cookie is too old, |
| 7848 | * then only if it has expired. We detect strict overflow because the |
| 7849 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7850 | * in the future are ignored if their offset is beyond one day. This |
| 7851 | * allows an admin to fix timezone issues without expiring everyone |
| 7852 | * and at the same time avoids keeping unwanted side effects for too |
| 7853 | * long. |
| 7854 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7855 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7856 | (((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] | 7857 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7858 | txn->flags &= ~TX_CK_MASK; |
| 7859 | txn->flags |= TX_CK_OLD; |
| 7860 | delim = val_beg; // let's pretend we have not found the cookie |
| 7861 | txn->cookie_first_date = 0; |
| 7862 | txn->cookie_last_date = 0; |
| 7863 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7864 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7865 | (((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] | 7866 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7867 | txn->flags &= ~TX_CK_MASK; |
| 7868 | txn->flags |= TX_CK_EXPIRED; |
| 7869 | delim = val_beg; // let's pretend we have not found the cookie |
| 7870 | txn->cookie_first_date = 0; |
| 7871 | txn->cookie_last_date = 0; |
| 7872 | } |
| 7873 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7874 | /* Here, we'll look for the first running server which supports the cookie. |
| 7875 | * This allows to share a same cookie between several servers, for example |
| 7876 | * to dedicate backup servers to specific servers only. |
| 7877 | * However, to prevent clients from sticking to cookie-less backup server |
| 7878 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7879 | * empty cookies and mark them as invalid. |
| 7880 | * The same behaviour is applied when persistence must be ignored. |
| 7881 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7882 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7883 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7884 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7885 | while (srv) { |
| 7886 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7887 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7888 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7889 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7890 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7891 | /* we found the server and we can use it */ |
| 7892 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7893 | 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] | 7894 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7895 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7896 | break; |
| 7897 | } else { |
| 7898 | /* we found a server, but it's down, |
| 7899 | * mark it as such and go on in case |
| 7900 | * another one is available. |
| 7901 | */ |
| 7902 | txn->flags &= ~TX_CK_MASK; |
| 7903 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7904 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7905 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7906 | srv = srv->next; |
| 7907 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7908 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7909 | 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] | 7910 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7911 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7912 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7913 | txn->flags |= TX_CK_UNUSED; |
| 7914 | else |
| 7915 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7916 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7917 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7918 | /* depending on the cookie mode, we may have to either : |
| 7919 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7920 | * the server never sees it ; |
| 7921 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7922 | * application cookie so that it does not get accidentely removed later, |
| 7923 | * if we're in cookie prefix mode |
| 7924 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7925 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7926 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7927 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7928 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7929 | val_end += delta; |
| 7930 | next += delta; |
| 7931 | hdr_end += delta; |
| 7932 | hdr_next += delta; |
| 7933 | cur_hdr->len += delta; |
| 7934 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7935 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7936 | del_from = NULL; |
| 7937 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7938 | } |
| 7939 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7940 | (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] | 7941 | del_from = prev; |
| 7942 | } |
| 7943 | } else { |
| 7944 | /* This is not our cookie, so we must preserve it. But if we already |
| 7945 | * scheduled another cookie for removal, we cannot remove the |
| 7946 | * complete header, but we can remove the previous block itself. |
| 7947 | */ |
| 7948 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7949 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7950 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7951 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7952 | if (att_beg >= del_from) |
| 7953 | att_beg += delta; |
| 7954 | if (att_end >= del_from) |
| 7955 | att_end += delta; |
| 7956 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7957 | val_end += delta; |
| 7958 | next += delta; |
| 7959 | hdr_end += delta; |
| 7960 | hdr_next += delta; |
| 7961 | cur_hdr->len += delta; |
| 7962 | http_msg_move_end(&txn->req, delta); |
| 7963 | prev = del_from; |
| 7964 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7965 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7966 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7967 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7968 | /* continue with next cookie on this header line */ |
| 7969 | att_beg = next; |
| 7970 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7971 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7972 | /* There are no more cookies on this line. |
| 7973 | * We may still have one (or several) marked for deletion at the |
| 7974 | * end of the line. We must do this now in two ways : |
| 7975 | * - if some cookies must be preserved, we only delete from the |
| 7976 | * mark to the end of line ; |
| 7977 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7978 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7979 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7980 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7981 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7982 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7983 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7984 | cur_hdr->len += delta; |
| 7985 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7986 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7987 | |
| 7988 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7989 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7990 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7991 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7992 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7993 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7994 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7995 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7996 | } |
| 7997 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7998 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7999 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 8000 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8001 | } |
| 8002 | |
| 8003 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8004 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 8005 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 8006 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8007 | 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] | 8008 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8009 | char *cur_ptr, *cur_end, *cur_next; |
| 8010 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8011 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8012 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8013 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8014 | |
| 8015 | last_hdr = 0; |
| 8016 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8017 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8018 | old_idx = 0; |
| 8019 | |
| 8020 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8021 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8022 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8023 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8024 | (exp->action == ACT_ALLOW || |
| 8025 | exp->action == ACT_DENY)) |
| 8026 | return 0; |
| 8027 | |
| 8028 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 8029 | if (!cur_idx) |
| 8030 | break; |
| 8031 | |
| 8032 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8033 | cur_ptr = cur_next; |
| 8034 | cur_end = cur_ptr + cur_hdr->len; |
| 8035 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8036 | |
| 8037 | /* Now we have one header between cur_ptr and cur_end, |
| 8038 | * and the next header starts at cur_next. |
| 8039 | */ |
| 8040 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8041 | 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] | 8042 | switch (exp->action) { |
| 8043 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8044 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8045 | last_hdr = 1; |
| 8046 | break; |
| 8047 | |
| 8048 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8049 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | last_hdr = 1; |
| 8051 | break; |
| 8052 | |
| 8053 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8054 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8055 | if (trash.len < 0) |
| 8056 | return -1; |
| 8057 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8058 | 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] | 8059 | /* FIXME: if the user adds a newline in the replacement, the |
| 8060 | * index will not be recalculated for now, and the new line |
| 8061 | * will not be counted as a new header. |
| 8062 | */ |
| 8063 | |
| 8064 | cur_end += delta; |
| 8065 | cur_next += delta; |
| 8066 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8067 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8068 | break; |
| 8069 | |
| 8070 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8071 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8072 | cur_next += delta; |
| 8073 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8074 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8075 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8076 | txn->hdr_idx.used--; |
| 8077 | cur_hdr->len = 0; |
| 8078 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8079 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8080 | break; |
| 8081 | |
| 8082 | } |
| 8083 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8084 | |
| 8085 | /* keep the link from this header to next one in case of later |
| 8086 | * removal of next header. |
| 8087 | */ |
| 8088 | old_idx = cur_idx; |
| 8089 | } |
| 8090 | return 0; |
| 8091 | } |
| 8092 | |
| 8093 | |
| 8094 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8095 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8096 | * or -1 if a replacement resulted in an invalid status line. |
| 8097 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8098 | 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] | 8099 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8100 | char *cur_ptr, *cur_end; |
| 8101 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8102 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8103 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8104 | |
| 8105 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8106 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8107 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8108 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8109 | (exp->action == ACT_ALLOW || |
| 8110 | exp->action == ACT_DENY)) |
| 8111 | return 0; |
| 8112 | else if (exp->action == ACT_REMOVE) |
| 8113 | return 0; |
| 8114 | |
| 8115 | done = 0; |
| 8116 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8117 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8118 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8119 | |
| 8120 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8121 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8122 | 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] | 8123 | switch (exp->action) { |
| 8124 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8125 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8126 | done = 1; |
| 8127 | break; |
| 8128 | |
| 8129 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8130 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8131 | done = 1; |
| 8132 | break; |
| 8133 | |
| 8134 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8135 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8136 | if (trash.len < 0) |
| 8137 | return -1; |
| 8138 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8139 | 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] | 8140 | /* FIXME: if the user adds a newline in the replacement, the |
| 8141 | * index will not be recalculated for now, and the new line |
| 8142 | * will not be counted as a new header. |
| 8143 | */ |
| 8144 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8145 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8146 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8147 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8148 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8149 | cur_ptr, cur_end + 1, |
| 8150 | NULL, NULL); |
| 8151 | if (unlikely(!cur_end)) |
| 8152 | return -1; |
| 8153 | |
| 8154 | /* we have a full respnse and we know that we have either a CR |
| 8155 | * or an LF at <ptr>. |
| 8156 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8157 | 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] | 8158 | 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] | 8159 | /* there is no point trying this regex on headers */ |
| 8160 | return 1; |
| 8161 | } |
| 8162 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8163 | return done; |
| 8164 | } |
| 8165 | |
| 8166 | |
| 8167 | |
| 8168 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8169 | * 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] | 8170 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8171 | * unparsable response. |
| 8172 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8173 | 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] | 8174 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8175 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8176 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8177 | struct hdr_exp *exp; |
| 8178 | |
| 8179 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8180 | int ret; |
| 8181 | |
| 8182 | /* |
| 8183 | * The interleaving of transformations and verdicts |
| 8184 | * makes it difficult to decide to continue or stop |
| 8185 | * the evaluation. |
| 8186 | */ |
| 8187 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8188 | if (txn->flags & TX_SVDENY) |
| 8189 | break; |
| 8190 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8191 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8192 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8193 | exp->action == ACT_PASS)) { |
| 8194 | exp = exp->next; |
| 8195 | continue; |
| 8196 | } |
| 8197 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8198 | /* if this filter had a condition, evaluate it now and skip to |
| 8199 | * next filter if the condition does not match. |
| 8200 | */ |
| 8201 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8202 | 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] | 8203 | ret = acl_pass(ret); |
| 8204 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8205 | ret = !ret; |
| 8206 | if (!ret) |
| 8207 | continue; |
| 8208 | } |
| 8209 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8210 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8211 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8212 | if (unlikely(ret < 0)) |
| 8213 | return -1; |
| 8214 | |
| 8215 | if (likely(ret == 0)) { |
| 8216 | /* The filter did not match the response, it can be |
| 8217 | * iterated through all headers. |
| 8218 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8219 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8220 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8222 | } |
| 8223 | return 0; |
| 8224 | } |
| 8225 | |
| 8226 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8227 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8228 | * 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] | 8229 | * desirable to call it only when needed. This function is also used when we |
| 8230 | * 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] | 8231 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8232 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8233 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8234 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8235 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8236 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8237 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8238 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8239 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8240 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8241 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8242 | /* Iterate through the headers. |
| 8243 | * we start with the start line. |
| 8244 | */ |
| 8245 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8246 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8247 | |
| 8248 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8249 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8250 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8251 | |
| 8252 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8253 | hdr_beg = hdr_next; |
| 8254 | hdr_end = hdr_beg + cur_hdr->len; |
| 8255 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8256 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8257 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8258 | * next header starts at hdr_next. We're only interested in |
| 8259 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8260 | */ |
| 8261 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8262 | is_cookie2 = 0; |
| 8263 | prev = hdr_beg + 10; |
| 8264 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8265 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8266 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8267 | if (!val) { |
| 8268 | old_idx = cur_idx; |
| 8269 | continue; |
| 8270 | } |
| 8271 | is_cookie2 = 1; |
| 8272 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8273 | } |
| 8274 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8275 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8276 | * <prev> points to the colon. |
| 8277 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8278 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8279 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8280 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8281 | * check-cache is enabled) and we are not interested in checking |
| 8282 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8283 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8284 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8285 | return; |
| 8286 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8287 | /* OK so now we know we have to process this response cookie. |
| 8288 | * The format of the Set-Cookie header is slightly different |
| 8289 | * from the format of the Cookie header in that it does not |
| 8290 | * support the comma as a cookie delimiter (thus the header |
| 8291 | * cannot be folded) because the Expires attribute described in |
| 8292 | * the original Netscape's spec may contain an unquoted date |
| 8293 | * with a comma inside. We have to live with this because |
| 8294 | * many browsers don't support Max-Age and some browsers don't |
| 8295 | * support quoted strings. However the Set-Cookie2 header is |
| 8296 | * clean. |
| 8297 | * |
| 8298 | * We have to keep multiple pointers in order to support cookie |
| 8299 | * removal at the beginning, middle or end of header without |
| 8300 | * corrupting the header (in case of set-cookie2). A special |
| 8301 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8302 | * fields after the first semi-colon. The <next> pointer points |
| 8303 | * either to the end of line (set-cookie) or next unquoted comma |
| 8304 | * (set-cookie2). All of these headers are valid : |
| 8305 | * |
| 8306 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8307 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8308 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8309 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8310 | * | | | | | | | | | | |
| 8311 | * | | | | | | | | +-> next hdr_end <--+ |
| 8312 | * | | | | | | | +------------> scav |
| 8313 | * | | | | | | +--------------> val_end |
| 8314 | * | | | | | +--------------------> val_beg |
| 8315 | * | | | | +----------------------> equal |
| 8316 | * | | | +------------------------> att_end |
| 8317 | * | | +----------------------------> att_beg |
| 8318 | * | +------------------------------> prev |
| 8319 | * +-----------------------------------------> hdr_beg |
| 8320 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8321 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8322 | for (; prev < hdr_end; prev = next) { |
| 8323 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8324 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8325 | /* find att_beg */ |
| 8326 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8327 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8328 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8329 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8330 | /* find att_end : this is the first character after the last non |
| 8331 | * space before the equal. It may be equal to hdr_end. |
| 8332 | */ |
| 8333 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8334 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8335 | while (equal < hdr_end) { |
| 8336 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8337 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8338 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8339 | continue; |
| 8340 | att_end = equal; |
| 8341 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8342 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8343 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8344 | * is between <att_beg> and <equal>, both may be identical. |
| 8345 | */ |
| 8346 | |
| 8347 | /* look for end of cookie if there is an equal sign */ |
| 8348 | if (equal < hdr_end && *equal == '=') { |
| 8349 | /* look for the beginning of the value */ |
| 8350 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8351 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8352 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8353 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8354 | /* find the end of the value, respecting quotes */ |
| 8355 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8356 | |
| 8357 | /* make val_end point to the first white space or delimitor after the value */ |
| 8358 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8359 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8360 | val_end--; |
| 8361 | } else { |
| 8362 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8363 | val_beg = val_end = next = equal; |
| 8364 | } |
| 8365 | |
| 8366 | if (next < hdr_end) { |
| 8367 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8368 | * a colon or semi-colon before the end. So skip all attr-value |
| 8369 | * pairs and look for the next comma. For Set-Cookie, since |
| 8370 | * commas are permitted in values, skip to the end. |
| 8371 | */ |
| 8372 | if (is_cookie2) |
| 8373 | next = find_hdr_value_end(next, hdr_end); |
| 8374 | else |
| 8375 | next = hdr_end; |
| 8376 | } |
| 8377 | |
| 8378 | /* Now everything is as on the diagram above */ |
| 8379 | |
| 8380 | /* Ignore cookies with no equal sign */ |
| 8381 | if (equal == val_end) |
| 8382 | continue; |
| 8383 | |
| 8384 | /* If there are spaces around the equal sign, we need to |
| 8385 | * strip them otherwise we'll get trouble for cookie captures, |
| 8386 | * or even for rewrites. Since this happens extremely rarely, |
| 8387 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8388 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8389 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8390 | int stripped_before = 0; |
| 8391 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8392 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8393 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8394 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8395 | equal += stripped_before; |
| 8396 | val_beg += stripped_before; |
| 8397 | } |
| 8398 | |
| 8399 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8400 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8401 | val_beg += stripped_after; |
| 8402 | stripped_before += stripped_after; |
| 8403 | } |
| 8404 | |
| 8405 | val_end += stripped_before; |
| 8406 | next += stripped_before; |
| 8407 | hdr_end += stripped_before; |
| 8408 | hdr_next += stripped_before; |
| 8409 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8410 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8411 | } |
| 8412 | |
| 8413 | /* First, let's see if we want to capture this cookie. We check |
| 8414 | * that we don't already have a server side cookie, because we |
| 8415 | * can only capture one. Also as an optimisation, we ignore |
| 8416 | * cookies shorter than the declared name. |
| 8417 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8418 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8419 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8420 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8421 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8422 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8423 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8424 | Alert("HTTP logging : out of memory.\n"); |
| 8425 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8426 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8427 | if (log_len > sess->fe->capture_len) |
| 8428 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8429 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8430 | txn->srv_cookie[log_len] = 0; |
| 8431 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8432 | } |
| 8433 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8434 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8435 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8436 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8437 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8438 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8439 | /* assume passive cookie by default */ |
| 8440 | txn->flags &= ~TX_SCK_MASK; |
| 8441 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8442 | |
| 8443 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8444 | * this occurrence because we'll insert another one later. |
| 8445 | * 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] | 8446 | * a direct access. |
| 8447 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8448 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8449 | /* The "preserve" flag was set, we don't want to touch the |
| 8450 | * server's cookie. |
| 8451 | */ |
| 8452 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8453 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8454 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8455 | /* this cookie must be deleted */ |
| 8456 | if (*prev == ':' && next == hdr_end) { |
| 8457 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8458 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8459 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8460 | txn->hdr_idx.used--; |
| 8461 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8462 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8463 | hdr_next += delta; |
| 8464 | http_msg_move_end(&txn->rsp, delta); |
| 8465 | /* note: while both invalid now, <next> and <hdr_end> |
| 8466 | * are still equal, so the for() will stop as expected. |
| 8467 | */ |
| 8468 | } else { |
| 8469 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8470 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8471 | next = prev; |
| 8472 | hdr_end += delta; |
| 8473 | hdr_next += delta; |
| 8474 | cur_hdr->len += delta; |
| 8475 | http_msg_move_end(&txn->rsp, delta); |
| 8476 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8477 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8478 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8479 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8480 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8481 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8482 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8483 | * with this server since we know it. |
| 8484 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8485 | 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] | 8486 | next += delta; |
| 8487 | hdr_end += delta; |
| 8488 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8489 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8490 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8491 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8492 | txn->flags &= ~TX_SCK_MASK; |
| 8493 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8494 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8495 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8496 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8497 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8498 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8499 | 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] | 8500 | next += delta; |
| 8501 | hdr_end += delta; |
| 8502 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8503 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8504 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8505 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8506 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8507 | txn->flags &= ~TX_SCK_MASK; |
| 8508 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8509 | } |
| 8510 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8511 | /* that's done for this cookie, check the next one on the same |
| 8512 | * line when next != hdr_end (only if is_cookie2). |
| 8513 | */ |
| 8514 | } |
| 8515 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8516 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8517 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8518 | } |
| 8519 | |
| 8520 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8521 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8522 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8523 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8524 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8525 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8526 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8527 | char *p1, *p2; |
| 8528 | |
| 8529 | char *cur_ptr, *cur_end, *cur_next; |
| 8530 | int cur_idx; |
| 8531 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8532 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8533 | return; |
| 8534 | |
| 8535 | /* Iterate through the headers. |
| 8536 | * we start with the start line. |
| 8537 | */ |
| 8538 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8539 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8540 | |
| 8541 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8542 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8543 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8544 | |
| 8545 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8546 | cur_ptr = cur_next; |
| 8547 | cur_end = cur_ptr + cur_hdr->len; |
| 8548 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8549 | |
| 8550 | /* We have one full header between cur_ptr and cur_end, and the |
| 8551 | * next header starts at cur_next. We're only interested in |
| 8552 | * "Cookie:" headers. |
| 8553 | */ |
| 8554 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8555 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8556 | if (val) { |
| 8557 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8558 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8559 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8560 | return; |
| 8561 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8562 | } |
| 8563 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8564 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8565 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8566 | continue; |
| 8567 | |
| 8568 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8569 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8570 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8571 | |
| 8572 | if (p1 >= cur_end) /* no more info */ |
| 8573 | continue; |
| 8574 | |
| 8575 | /* p1 is at the beginning of the value */ |
| 8576 | p2 = p1; |
| 8577 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8578 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8579 | p2++; |
| 8580 | |
| 8581 | /* we have a complete value between p1 and p2 */ |
| 8582 | if (p2 < cur_end && *p2 == '=') { |
| 8583 | /* we have something of the form no-cache="set-cookie" */ |
| 8584 | if ((cur_end - p1 >= 21) && |
| 8585 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8586 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8587 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8588 | continue; |
| 8589 | } |
| 8590 | |
| 8591 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8592 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8593 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8594 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8595 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8596 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8597 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8598 | return; |
| 8599 | } |
| 8600 | |
| 8601 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8602 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8603 | continue; |
| 8604 | } |
| 8605 | } |
| 8606 | } |
| 8607 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8608 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8609 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8610 | * 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] | 8611 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8612 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8613 | * 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] | 8614 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8615 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8616 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8617 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8618 | 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] | 8619 | { |
| 8620 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8621 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8622 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8623 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8624 | if (!uri_auth) |
| 8625 | return 0; |
| 8626 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8627 | 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] | 8628 | return 0; |
| 8629 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8630 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8631 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8632 | return 0; |
| 8633 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8634 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8635 | return 0; |
| 8636 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8637 | return 1; |
| 8638 | } |
| 8639 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8640 | /* |
| 8641 | * 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] | 8642 | * By default it tries to report the error position as msg->err_pos. However if |
| 8643 | * this one is not set, it will then report msg->next, which is the last known |
| 8644 | * 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] | 8645 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8646 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8647 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8648 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8649 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8650 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8651 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8652 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8653 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8654 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8655 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8656 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8657 | len1 = MIN(len1, es->len); |
| 8658 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8659 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8660 | if (!es->buf) |
| 8661 | es->buf = malloc(global.tune.bufsize); |
| 8662 | |
| 8663 | if (es->buf) { |
| 8664 | memcpy(es->buf, chn->buf->p, len1); |
| 8665 | if (len2) |
| 8666 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 8667 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8668 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8669 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8670 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8671 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8672 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8673 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8674 | es->when = date; // user-visible date |
| 8675 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8676 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8677 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8678 | if (objt_conn(sess->origin)) |
| 8679 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8680 | else |
| 8681 | memset(&es->src, 0, sizeof(es->src)); |
| 8682 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8683 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8684 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8685 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8686 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8687 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8688 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8689 | es->b_out = chn->buf->o; |
| 8690 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8691 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8692 | es->m_clen = msg->chunk_len; |
| 8693 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8694 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8695 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8696 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8697 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8698 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8699 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8700 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8701 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8702 | * 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] | 8703 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8704 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8705 | * 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] | 8706 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8707 | 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] | 8708 | struct hdr_idx *idx, int occ, |
| 8709 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8710 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8711 | struct hdr_ctx local_ctx; |
| 8712 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8713 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8714 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8715 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8716 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8717 | if (!ctx) { |
| 8718 | local_ctx.idx = 0; |
| 8719 | ctx = &local_ctx; |
| 8720 | } |
| 8721 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8722 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8723 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8724 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8725 | occ--; |
| 8726 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8727 | *vptr = ctx->line + ctx->val; |
| 8728 | *vlen = ctx->vlen; |
| 8729 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8730 | } |
| 8731 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8732 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8733 | } |
| 8734 | |
| 8735 | /* negative occurrence, we scan all the list then walk back */ |
| 8736 | if (-occ > MAX_HDR_HISTORY) |
| 8737 | return 0; |
| 8738 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8739 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8740 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8741 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8742 | len_hist[hist_ptr] = ctx->vlen; |
| 8743 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8744 | hist_ptr = 0; |
| 8745 | found++; |
| 8746 | } |
| 8747 | if (-occ > found) |
| 8748 | return 0; |
| 8749 | /* 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] | 8750 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8751 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8752 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8753 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8754 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8755 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8756 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8757 | *vptr = ptr_hist[hist_ptr]; |
| 8758 | *vlen = len_hist[hist_ptr]; |
| 8759 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8760 | } |
| 8761 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8762 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8763 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8764 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8765 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8766 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8767 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8768 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8769 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8770 | * line header values and does not stop at commas. |
| 8771 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8772 | */ |
| 8773 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8774 | struct hdr_idx *idx, int occ, |
| 8775 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8776 | { |
| 8777 | struct hdr_ctx local_ctx; |
| 8778 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8779 | int len_hist[MAX_HDR_HISTORY]; |
| 8780 | unsigned int hist_ptr; |
| 8781 | int found; |
| 8782 | |
| 8783 | if (!ctx) { |
| 8784 | local_ctx.idx = 0; |
| 8785 | ctx = &local_ctx; |
| 8786 | } |
| 8787 | |
| 8788 | if (occ >= 0) { |
| 8789 | /* search from the beginning */ |
| 8790 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8791 | occ--; |
| 8792 | if (occ <= 0) { |
| 8793 | *vptr = ctx->line + ctx->val; |
| 8794 | *vlen = ctx->vlen; |
| 8795 | return 1; |
| 8796 | } |
| 8797 | } |
| 8798 | return 0; |
| 8799 | } |
| 8800 | |
| 8801 | /* negative occurrence, we scan all the list then walk back */ |
| 8802 | if (-occ > MAX_HDR_HISTORY) |
| 8803 | return 0; |
| 8804 | |
| 8805 | found = hist_ptr = 0; |
| 8806 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8807 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8808 | len_hist[hist_ptr] = ctx->vlen; |
| 8809 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8810 | hist_ptr = 0; |
| 8811 | found++; |
| 8812 | } |
| 8813 | if (-occ > found) |
| 8814 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8815 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8816 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8817 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8818 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8819 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8820 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8821 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8822 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8823 | hist_ptr -= MAX_HDR_HISTORY; |
| 8824 | *vptr = ptr_hist[hist_ptr]; |
| 8825 | *vlen = len_hist[hist_ptr]; |
| 8826 | return 1; |
| 8827 | } |
| 8828 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8829 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8830 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8831 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8832 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8833 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8834 | 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] | 8835 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8836 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8837 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8838 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8839 | 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] | 8840 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8841 | 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] | 8842 | 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] | 8843 | |
| 8844 | for (max = 0; start + max < end; max++) |
| 8845 | if (start[max] == '\r' || start[max] == '\n') |
| 8846 | break; |
| 8847 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8848 | UBOUND(max, trash.size - trash.len - 3); |
| 8849 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8850 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8851 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8852 | } |
| 8853 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8854 | |
| 8855 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8856 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8857 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8858 | * assigned to the stream and returned. |
| 8859 | */ |
| 8860 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8861 | { |
| 8862 | struct http_txn *txn = s->txn; |
| 8863 | |
| 8864 | if (txn) |
| 8865 | return txn; |
| 8866 | |
| 8867 | txn = pool_alloc2(pool2_http_txn); |
| 8868 | if (!txn) |
| 8869 | return txn; |
| 8870 | |
| 8871 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8872 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8873 | if (!txn->hdr_idx.v) { |
| 8874 | pool_free2(pool2_http_txn, txn); |
| 8875 | return NULL; |
| 8876 | } |
| 8877 | |
| 8878 | s->txn = txn; |
| 8879 | return txn; |
| 8880 | } |
| 8881 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8882 | void http_txn_reset_req(struct http_txn *txn) |
| 8883 | { |
| 8884 | txn->req.flags = 0; |
| 8885 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8886 | txn->req.next = 0; |
| 8887 | txn->req.chunk_len = 0LL; |
| 8888 | txn->req.body_len = 0LL; |
| 8889 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8890 | } |
| 8891 | |
| 8892 | void http_txn_reset_res(struct http_txn *txn) |
| 8893 | { |
| 8894 | txn->rsp.flags = 0; |
| 8895 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8896 | txn->rsp.next = 0; |
| 8897 | txn->rsp.chunk_len = 0LL; |
| 8898 | txn->rsp.body_len = 0LL; |
| 8899 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8900 | } |
| 8901 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8902 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8903 | * 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] | 8904 | * the required fields are properly allocated and that we only need to (re)init |
| 8905 | * them. This should be used before processing any new request. |
| 8906 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8907 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8908 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8909 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8910 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8911 | |
| 8912 | txn->flags = 0; |
| 8913 | txn->status = -1; |
| 8914 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8915 | txn->cookie_first_date = 0; |
| 8916 | txn->cookie_last_date = 0; |
| 8917 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8918 | txn->srv_cookie = NULL; |
| 8919 | txn->cli_cookie = NULL; |
| 8920 | txn->uri = NULL; |
| 8921 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8922 | http_txn_reset_req(txn); |
| 8923 | http_txn_reset_res(txn); |
| 8924 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8925 | txn->req.chn = &s->req; |
| 8926 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8927 | |
| 8928 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8929 | |
| 8930 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8931 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8932 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8933 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8934 | if (txn->hdr_idx.v) |
| 8935 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8936 | |
| 8937 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8938 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8939 | } |
| 8940 | |
| 8941 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8942 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8943 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8944 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8945 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8946 | |
| 8947 | /* these ones will have been dynamically allocated */ |
| 8948 | pool_free2(pool2_requri, txn->uri); |
| 8949 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8950 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8951 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8952 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8953 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8954 | txn->uri = NULL; |
| 8955 | txn->srv_cookie = NULL; |
| 8956 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8957 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8958 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8959 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8960 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8961 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8962 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8963 | } |
| 8964 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8965 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8966 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8967 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8968 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8969 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8970 | } |
| 8971 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8972 | vars_prune(&s->vars_txn, s->sess, s); |
| 8973 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8974 | } |
| 8975 | |
| 8976 | /* 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] | 8977 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8978 | { |
| 8979 | http_end_txn(s); |
| 8980 | http_init_txn(s); |
| 8981 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8982 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8983 | * any rulelist match the NULL pointer. |
| 8984 | */ |
| 8985 | s->current_rule_list = NULL; |
| 8986 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8987 | s->be = strm_fe(s); |
| 8988 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8989 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8990 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8991 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8992 | /* re-init store persistence */ |
| 8993 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8994 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8995 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8996 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8997 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8998 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8999 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9000 | /* We must trim any excess data from the response buffer, because we |
| 9001 | * may have blocked an invalid response from a server that we don't |
| 9002 | * want to accidentely forward once we disable the analysers, nor do |
| 9003 | * we want those data to come along with next response. A typical |
| 9004 | * example of such data would be from a buggy server responding to |
| 9005 | * a HEAD with some data, or sending more than the advertised |
| 9006 | * content-length. |
| 9007 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9008 | if (unlikely(s->res.buf->i)) |
| 9009 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9010 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 9011 | /* Now we can realign the response buffer */ |
| 9012 | buffer_realign(s->res.buf); |
| 9013 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9014 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9015 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9016 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9017 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9018 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9019 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9020 | s->req.rex = TICK_ETERNITY; |
| 9021 | s->req.wex = TICK_ETERNITY; |
| 9022 | s->req.analyse_exp = TICK_ETERNITY; |
| 9023 | s->res.rex = TICK_ETERNITY; |
| 9024 | s->res.wex = TICK_ETERNITY; |
| 9025 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 9026 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9027 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9028 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9029 | void free_http_res_rules(struct list *r) |
| 9030 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9031 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9032 | |
| 9033 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9034 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9035 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9036 | free(pr); |
| 9037 | } |
| 9038 | } |
| 9039 | |
| 9040 | void free_http_req_rules(struct list *r) |
| 9041 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9042 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9043 | |
| 9044 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9045 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9046 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9047 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9048 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9049 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9050 | free(pr); |
| 9051 | } |
| 9052 | } |
| 9053 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9054 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9055 | 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] | 9056 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9057 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9058 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9059 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9060 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9061 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 9062 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9063 | if (!rule) { |
| 9064 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9065 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9066 | } |
| 9067 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9068 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9069 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9070 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9071 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9072 | int code; |
| 9073 | int hc; |
| 9074 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9075 | if (!strcmp(args[0], "tarpit")) { |
| 9076 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 9077 | rule->deny_status = HTTP_ERR_500; |
| 9078 | } |
| 9079 | else { |
| 9080 | rule->action = ACT_ACTION_DENY; |
| 9081 | rule->deny_status = HTTP_ERR_403; |
| 9082 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9083 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9084 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9085 | cur_arg++; |
| 9086 | if (!args[cur_arg]) { |
| 9087 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9088 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9089 | goto out_err; |
| 9090 | } |
| 9091 | |
| 9092 | code = atol(args[cur_arg]); |
| 9093 | cur_arg++; |
| 9094 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9095 | if (http_err_codes[hc] == code) { |
| 9096 | rule->deny_status = hc; |
| 9097 | break; |
| 9098 | } |
| 9099 | } |
| 9100 | |
| 9101 | if (hc >= HTTP_ERR_SIZE) { |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9102 | Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 9103 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9104 | } |
| 9105 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9106 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9107 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9108 | cur_arg = 1; |
| 9109 | |
| 9110 | while(*args[cur_arg]) { |
| 9111 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9112 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9113 | cur_arg+=2; |
| 9114 | continue; |
| 9115 | } else |
| 9116 | break; |
| 9117 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9118 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9119 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9120 | cur_arg = 1; |
| 9121 | |
| 9122 | if (!*args[cur_arg] || |
| 9123 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9124 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9125 | file, linenum, args[0]); |
| 9126 | goto out_err; |
| 9127 | } |
| 9128 | rule->arg.nice = atoi(args[cur_arg]); |
| 9129 | if (rule->arg.nice < -1024) |
| 9130 | rule->arg.nice = -1024; |
| 9131 | else if (rule->arg.nice > 1024) |
| 9132 | rule->arg.nice = 1024; |
| 9133 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9134 | } else if (!strcmp(args[0], "set-tos")) { |
| 9135 | #ifdef IP_TOS |
| 9136 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9137 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9138 | cur_arg = 1; |
| 9139 | |
| 9140 | if (!*args[cur_arg] || |
| 9141 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9142 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9143 | file, linenum, args[0]); |
| 9144 | goto out_err; |
| 9145 | } |
| 9146 | |
| 9147 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9148 | if (err && *err != '\0') { |
| 9149 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9150 | file, linenum, err, args[0]); |
| 9151 | goto out_err; |
| 9152 | } |
| 9153 | cur_arg++; |
| 9154 | #else |
| 9155 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9156 | goto out_err; |
| 9157 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9158 | } else if (!strcmp(args[0], "set-mark")) { |
| 9159 | #ifdef SO_MARK |
| 9160 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9161 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9162 | cur_arg = 1; |
| 9163 | |
| 9164 | if (!*args[cur_arg] || |
| 9165 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9166 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9167 | file, linenum, args[0]); |
| 9168 | goto out_err; |
| 9169 | } |
| 9170 | |
| 9171 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9172 | if (err && *err != '\0') { |
| 9173 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9174 | file, linenum, err, args[0]); |
| 9175 | goto out_err; |
| 9176 | } |
| 9177 | cur_arg++; |
| 9178 | global.last_checks |= LSTCHK_NETADM; |
| 9179 | #else |
| 9180 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9181 | goto out_err; |
| 9182 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9183 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9184 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9185 | cur_arg = 1; |
| 9186 | |
| 9187 | if (!*args[cur_arg] || |
| 9188 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9189 | bad_log_level: |
| 9190 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9191 | file, linenum, args[0]); |
| 9192 | goto out_err; |
| 9193 | } |
| 9194 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9195 | rule->arg.loglevel = -1; |
| 9196 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9197 | goto bad_log_level; |
| 9198 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9199 | } 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] | 9200 | 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] | 9201 | cur_arg = 1; |
| 9202 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9203 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9204 | (*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] | 9205 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9206 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9207 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9208 | } |
| 9209 | |
| 9210 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9211 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9212 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9213 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9214 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9215 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9216 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9217 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9218 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9219 | file, linenum, args[0], error); |
| 9220 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9221 | goto out_err; |
| 9222 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9223 | free(proxy->conf.lfs_file); |
| 9224 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9225 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9226 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9227 | } 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] | 9228 | 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] | 9229 | cur_arg = 1; |
| 9230 | |
| 9231 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9232 | (*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] | 9233 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9234 | file, linenum, args[0]); |
| 9235 | goto out_err; |
| 9236 | } |
| 9237 | |
| 9238 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9239 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9240 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9241 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9242 | error = NULL; |
| 9243 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9244 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9245 | args[cur_arg + 1], error); |
| 9246 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9247 | goto out_err; |
| 9248 | } |
| 9249 | |
| 9250 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9251 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9252 | if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9253 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9254 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9255 | file, linenum, args[0], error); |
| 9256 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9257 | goto out_err; |
| 9258 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9259 | |
| 9260 | free(proxy->conf.lfs_file); |
| 9261 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9262 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9263 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9264 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9265 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9266 | cur_arg = 1; |
| 9267 | |
| 9268 | if (!*args[cur_arg] || |
| 9269 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9270 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9271 | file, linenum, args[0]); |
| 9272 | goto out_err; |
| 9273 | } |
| 9274 | |
| 9275 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9276 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9277 | |
| 9278 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9279 | free(proxy->conf.lfs_file); |
| 9280 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9281 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9282 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9283 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9284 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9285 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9286 | struct sample_expr *expr; |
| 9287 | unsigned int where; |
| 9288 | char *err = NULL; |
| 9289 | |
| 9290 | cur_arg = 1; |
| 9291 | proxy->conf.args.ctx = ARGC_TRK; |
| 9292 | |
| 9293 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9294 | if (!expr) { |
| 9295 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9296 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9297 | free(err); |
| 9298 | goto out_err; |
| 9299 | } |
| 9300 | |
| 9301 | where = 0; |
| 9302 | if (proxy->cap & PR_CAP_FE) |
| 9303 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9304 | if (proxy->cap & PR_CAP_BE) |
| 9305 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9306 | |
| 9307 | if (!(expr->fetch->val & where)) { |
| 9308 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9309 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9310 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9311 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9312 | free(expr); |
| 9313 | goto out_err; |
| 9314 | } |
| 9315 | |
| 9316 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9317 | cur_arg++; |
| 9318 | if (!args[cur_arg]) { |
| 9319 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9320 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9321 | free(expr); |
| 9322 | goto out_err; |
| 9323 | } |
| 9324 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9325 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9326 | cur_arg++; |
| 9327 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9328 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9329 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9330 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9331 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9332 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9333 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9334 | 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] | 9335 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9336 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9337 | goto out_err; |
| 9338 | } |
| 9339 | |
| 9340 | /* this redirect rule might already contain a parsed condition which |
| 9341 | * we'll pass to the http-request rule. |
| 9342 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9343 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9344 | rule->arg.redir = redir; |
| 9345 | rule->cond = redir->cond; |
| 9346 | redir->cond = NULL; |
| 9347 | cur_arg = 2; |
| 9348 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9349 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9350 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9351 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9352 | /* |
| 9353 | * '+ 8' for 'add-acl(' |
| 9354 | * '- 9' for 'add-acl(' + trailing ')' |
| 9355 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9356 | 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] | 9357 | |
| 9358 | cur_arg = 1; |
| 9359 | |
| 9360 | if (!*args[cur_arg] || |
| 9361 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9362 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9363 | file, linenum, args[0]); |
| 9364 | goto out_err; |
| 9365 | } |
| 9366 | |
| 9367 | LIST_INIT(&rule->arg.map.key); |
| 9368 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9369 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9370 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9371 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9372 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9373 | file, linenum, args[0], error); |
| 9374 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9375 | goto out_err; |
| 9376 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9377 | free(proxy->conf.lfs_file); |
| 9378 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9379 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9380 | cur_arg += 1; |
| 9381 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9382 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9383 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9384 | /* |
| 9385 | * '+ 8' for 'del-acl(' |
| 9386 | * '- 9' for 'del-acl(' + trailing ')' |
| 9387 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9388 | 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] | 9389 | |
| 9390 | cur_arg = 1; |
| 9391 | |
| 9392 | if (!*args[cur_arg] || |
| 9393 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9394 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9395 | file, linenum, args[0]); |
| 9396 | goto out_err; |
| 9397 | } |
| 9398 | |
| 9399 | LIST_INIT(&rule->arg.map.key); |
| 9400 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9401 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9402 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9403 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9404 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9405 | file, linenum, args[0], error); |
| 9406 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9407 | goto out_err; |
| 9408 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9409 | free(proxy->conf.lfs_file); |
| 9410 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9411 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9412 | cur_arg += 1; |
| 9413 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9414 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9415 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9416 | /* |
| 9417 | * '+ 8' for 'del-map(' |
| 9418 | * '- 9' for 'del-map(' + trailing ')' |
| 9419 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9420 | 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] | 9421 | |
| 9422 | cur_arg = 1; |
| 9423 | |
| 9424 | if (!*args[cur_arg] || |
| 9425 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9426 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9427 | file, linenum, args[0]); |
| 9428 | goto out_err; |
| 9429 | } |
| 9430 | |
| 9431 | LIST_INIT(&rule->arg.map.key); |
| 9432 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9433 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9434 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9435 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9436 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9437 | file, linenum, args[0], error); |
| 9438 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9439 | goto out_err; |
| 9440 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9441 | free(proxy->conf.lfs_file); |
| 9442 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9443 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9444 | cur_arg += 1; |
| 9445 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9446 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9447 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9448 | /* |
| 9449 | * '+ 8' for 'set-map(' |
| 9450 | * '- 9' for 'set-map(' + 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] || !*args[cur_arg+1] || |
| 9457 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9458 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9459 | file, linenum, args[0]); |
| 9460 | goto out_err; |
| 9461 | } |
| 9462 | |
| 9463 | LIST_INIT(&rule->arg.map.key); |
| 9464 | LIST_INIT(&rule->arg.map.value); |
| 9465 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9466 | |
| 9467 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9468 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9469 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9470 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9471 | Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 9472 | file, linenum, args[0], error); |
| 9473 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9474 | goto out_err; |
| 9475 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9476 | |
| 9477 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9478 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9479 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9480 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9481 | Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 9482 | file, linenum, args[0], error); |
| 9483 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9484 | goto out_err; |
| 9485 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9486 | free(proxy->conf.lfs_file); |
| 9487 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9488 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9489 | |
| 9490 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9491 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9492 | char *errmsg = NULL; |
| 9493 | cur_arg = 1; |
| 9494 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9495 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9496 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9497 | 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] | 9498 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9499 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9500 | free(errmsg); |
| 9501 | goto out_err; |
| 9502 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9503 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9504 | action_build_list(&http_req_keywords.list, &trash); |
| 9505 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9506 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9507 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9508 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9509 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9510 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9511 | } |
| 9512 | |
| 9513 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9514 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9515 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9516 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9517 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9518 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9519 | file, linenum, args[0], errmsg); |
| 9520 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9521 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9522 | } |
| 9523 | rule->cond = cond; |
| 9524 | } |
| 9525 | else if (*args[cur_arg]) { |
| 9526 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9527 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9528 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9529 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9530 | } |
| 9531 | |
| 9532 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9533 | out_err: |
| 9534 | free(rule); |
| 9535 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9536 | } |
| 9537 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9538 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9539 | 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] | 9540 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9541 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9542 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9543 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9544 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9545 | |
| 9546 | rule = calloc(1, sizeof(*rule)); |
| 9547 | if (!rule) { |
| 9548 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9549 | goto out_err; |
| 9550 | } |
| 9551 | |
| 9552 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9553 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9554 | cur_arg = 1; |
| 9555 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9556 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9557 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9558 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9559 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9560 | cur_arg = 1; |
| 9561 | |
| 9562 | if (!*args[cur_arg] || |
| 9563 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9564 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9565 | file, linenum, args[0]); |
| 9566 | goto out_err; |
| 9567 | } |
| 9568 | rule->arg.nice = atoi(args[cur_arg]); |
| 9569 | if (rule->arg.nice < -1024) |
| 9570 | rule->arg.nice = -1024; |
| 9571 | else if (rule->arg.nice > 1024) |
| 9572 | rule->arg.nice = 1024; |
| 9573 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9574 | } else if (!strcmp(args[0], "set-tos")) { |
| 9575 | #ifdef IP_TOS |
| 9576 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9577 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9578 | cur_arg = 1; |
| 9579 | |
| 9580 | if (!*args[cur_arg] || |
| 9581 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9582 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9583 | file, linenum, args[0]); |
| 9584 | goto out_err; |
| 9585 | } |
| 9586 | |
| 9587 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9588 | if (err && *err != '\0') { |
| 9589 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9590 | file, linenum, err, args[0]); |
| 9591 | goto out_err; |
| 9592 | } |
| 9593 | cur_arg++; |
| 9594 | #else |
| 9595 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9596 | goto out_err; |
| 9597 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9598 | } else if (!strcmp(args[0], "set-mark")) { |
| 9599 | #ifdef SO_MARK |
| 9600 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9601 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9602 | cur_arg = 1; |
| 9603 | |
| 9604 | if (!*args[cur_arg] || |
| 9605 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9606 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9607 | file, linenum, args[0]); |
| 9608 | goto out_err; |
| 9609 | } |
| 9610 | |
| 9611 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9612 | if (err && *err != '\0') { |
| 9613 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9614 | file, linenum, err, args[0]); |
| 9615 | goto out_err; |
| 9616 | } |
| 9617 | cur_arg++; |
| 9618 | global.last_checks |= LSTCHK_NETADM; |
| 9619 | #else |
| 9620 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9621 | goto out_err; |
| 9622 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9623 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9624 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9625 | cur_arg = 1; |
| 9626 | |
| 9627 | if (!*args[cur_arg] || |
| 9628 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9629 | bad_log_level: |
| 9630 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9631 | file, linenum, args[0]); |
| 9632 | goto out_err; |
| 9633 | } |
| 9634 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9635 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 9636 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9637 | goto bad_log_level; |
| 9638 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9639 | } 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] | 9640 | 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] | 9641 | cur_arg = 1; |
| 9642 | |
| 9643 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9644 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9645 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9646 | file, linenum, args[0]); |
| 9647 | goto out_err; |
| 9648 | } |
| 9649 | |
| 9650 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9651 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9652 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9653 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9654 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9655 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9656 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9657 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9658 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9659 | file, linenum, args[0], error); |
| 9660 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9661 | goto out_err; |
| 9662 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9663 | free(proxy->conf.lfs_file); |
| 9664 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9665 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9666 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9667 | } 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] | 9668 | 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] | 9669 | cur_arg = 1; |
| 9670 | |
| 9671 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9672 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9673 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9674 | file, linenum, args[0]); |
| 9675 | goto out_err; |
| 9676 | } |
| 9677 | |
| 9678 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9679 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9680 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9681 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9682 | error = NULL; |
| 9683 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9684 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9685 | args[cur_arg + 1], error); |
| 9686 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9687 | goto out_err; |
| 9688 | } |
| 9689 | |
| 9690 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9691 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9692 | if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9693 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9694 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9695 | file, linenum, args[0], error); |
| 9696 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9697 | goto out_err; |
| 9698 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9699 | |
| 9700 | free(proxy->conf.lfs_file); |
| 9701 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9702 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9703 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9704 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9705 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9706 | cur_arg = 1; |
| 9707 | |
| 9708 | if (!*args[cur_arg] || |
| 9709 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9710 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9711 | file, linenum, args[0]); |
| 9712 | goto out_err; |
| 9713 | } |
| 9714 | |
| 9715 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9716 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9717 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9718 | proxy->conf.args.ctx = ARGC_HRS; |
| 9719 | free(proxy->conf.lfs_file); |
| 9720 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9721 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9722 | cur_arg += 1; |
| 9723 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9724 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9725 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9726 | /* |
| 9727 | * '+ 8' for 'add-acl(' |
| 9728 | * '- 9' for 'add-acl(' + trailing ')' |
| 9729 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9730 | 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] | 9731 | |
| 9732 | cur_arg = 1; |
| 9733 | |
| 9734 | if (!*args[cur_arg] || |
| 9735 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9736 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9737 | file, linenum, args[0]); |
| 9738 | goto out_err; |
| 9739 | } |
| 9740 | |
| 9741 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9742 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9743 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9744 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9745 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9746 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9747 | file, linenum, args[0], error); |
| 9748 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9749 | goto out_err; |
| 9750 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9751 | free(proxy->conf.lfs_file); |
| 9752 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9753 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9754 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9755 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9756 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9757 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9758 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9759 | /* |
| 9760 | * '+ 8' for 'del-acl(' |
| 9761 | * '- 9' for 'del-acl(' + trailing ')' |
| 9762 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9763 | 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] | 9764 | |
| 9765 | cur_arg = 1; |
| 9766 | |
| 9767 | if (!*args[cur_arg] || |
| 9768 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9769 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9770 | file, linenum, args[0]); |
| 9771 | goto out_err; |
| 9772 | } |
| 9773 | |
| 9774 | LIST_INIT(&rule->arg.map.key); |
| 9775 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9776 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9777 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9778 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9779 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9780 | file, linenum, args[0], error); |
| 9781 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9782 | goto out_err; |
| 9783 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9784 | free(proxy->conf.lfs_file); |
| 9785 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9786 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9787 | cur_arg += 1; |
| 9788 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9789 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9790 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9791 | /* |
| 9792 | * '+ 8' for 'del-map(' |
| 9793 | * '- 9' for 'del-map(' + trailing ')' |
| 9794 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9795 | 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] | 9796 | |
| 9797 | cur_arg = 1; |
| 9798 | |
| 9799 | if (!*args[cur_arg] || |
| 9800 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9801 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9802 | file, linenum, args[0]); |
| 9803 | goto out_err; |
| 9804 | } |
| 9805 | |
| 9806 | LIST_INIT(&rule->arg.map.key); |
| 9807 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9808 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9809 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9810 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9811 | Alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9812 | file, linenum, args[0], error); |
| 9813 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9814 | goto out_err; |
| 9815 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9816 | free(proxy->conf.lfs_file); |
| 9817 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9818 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9819 | cur_arg += 1; |
| 9820 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9821 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9822 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9823 | /* |
| 9824 | * '+ 8' for 'set-map(' |
| 9825 | * '- 9' for 'set-map(' + trailing ')' |
| 9826 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9827 | 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] | 9828 | |
| 9829 | cur_arg = 1; |
| 9830 | |
| 9831 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9832 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9833 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9834 | file, linenum, args[0]); |
| 9835 | goto out_err; |
| 9836 | } |
| 9837 | |
| 9838 | LIST_INIT(&rule->arg.map.key); |
| 9839 | LIST_INIT(&rule->arg.map.value); |
| 9840 | |
| 9841 | proxy->conf.args.ctx = ARGC_HRS; |
| 9842 | |
| 9843 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9844 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9845 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9846 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9847 | Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 9848 | file, linenum, args[0], error); |
| 9849 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9850 | goto out_err; |
| 9851 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9852 | |
| 9853 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9854 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9855 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9856 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9857 | Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9858 | file, linenum, args[0], error); |
| 9859 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9860 | goto out_err; |
| 9861 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9862 | |
| 9863 | free(proxy->conf.lfs_file); |
| 9864 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9865 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9866 | |
| 9867 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9868 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9869 | struct redirect_rule *redir; |
| 9870 | char *errmsg = NULL; |
| 9871 | |
| 9872 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9873 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9874 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9875 | goto out_err; |
| 9876 | } |
| 9877 | |
| 9878 | /* this redirect rule might already contain a parsed condition which |
| 9879 | * we'll pass to the http-request rule. |
| 9880 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9881 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9882 | rule->arg.redir = redir; |
| 9883 | rule->cond = redir->cond; |
| 9884 | redir->cond = NULL; |
| 9885 | cur_arg = 2; |
| 9886 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9887 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9888 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 9889 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 9890 | struct sample_expr *expr; |
| 9891 | unsigned int where; |
| 9892 | char *err = NULL; |
| 9893 | |
| 9894 | cur_arg = 1; |
| 9895 | proxy->conf.args.ctx = ARGC_TRK; |
| 9896 | |
| 9897 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9898 | if (!expr) { |
| 9899 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9900 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9901 | free(err); |
| 9902 | goto out_err; |
| 9903 | } |
| 9904 | |
| 9905 | where = 0; |
| 9906 | if (proxy->cap & PR_CAP_FE) |
| 9907 | where |= SMP_VAL_FE_HRS_HDR; |
| 9908 | if (proxy->cap & PR_CAP_BE) |
| 9909 | where |= SMP_VAL_BE_HRS_HDR; |
| 9910 | |
| 9911 | if (!(expr->fetch->val & where)) { |
| 9912 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9913 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9914 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9915 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9916 | free(expr); |
| 9917 | goto out_err; |
| 9918 | } |
| 9919 | |
| 9920 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9921 | cur_arg++; |
| 9922 | if (!args[cur_arg]) { |
| 9923 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9924 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9925 | free(expr); |
| 9926 | goto out_err; |
| 9927 | } |
| 9928 | /* we copy the table name for now, it will be resolved later */ |
| 9929 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9930 | cur_arg++; |
| 9931 | } |
| 9932 | rule->arg.trk_ctr.expr = expr; |
| 9933 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9934 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9935 | char *errmsg = NULL; |
| 9936 | cur_arg = 1; |
| 9937 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9938 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9939 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9940 | 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] | 9941 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9942 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9943 | free(errmsg); |
| 9944 | goto out_err; |
| 9945 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9946 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9947 | action_build_list(&http_res_keywords.list, &trash); |
| 9948 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9949 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9950 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9951 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9952 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9953 | goto out_err; |
| 9954 | } |
| 9955 | |
| 9956 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9957 | struct acl_cond *cond; |
| 9958 | char *errmsg = NULL; |
| 9959 | |
| 9960 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9961 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9962 | file, linenum, args[0], errmsg); |
| 9963 | free(errmsg); |
| 9964 | goto out_err; |
| 9965 | } |
| 9966 | rule->cond = cond; |
| 9967 | } |
| 9968 | else if (*args[cur_arg]) { |
| 9969 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9970 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9971 | file, linenum, args[0], args[cur_arg]); |
| 9972 | goto out_err; |
| 9973 | } |
| 9974 | |
| 9975 | return rule; |
| 9976 | out_err: |
| 9977 | free(rule); |
| 9978 | return NULL; |
| 9979 | } |
| 9980 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9981 | /* 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] | 9982 | * 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] | 9983 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9984 | * 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] | 9985 | */ |
| 9986 | 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] | 9987 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9988 | { |
| 9989 | struct redirect_rule *rule; |
| 9990 | int cur_arg; |
| 9991 | int type = REDIRECT_TYPE_NONE; |
| 9992 | int code = 302; |
| 9993 | const char *destination = NULL; |
| 9994 | const char *cookie = NULL; |
| 9995 | int cookie_set = 0; |
| 9996 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9997 | struct acl_cond *cond = NULL; |
| 9998 | |
| 9999 | cur_arg = 0; |
| 10000 | while (*(args[cur_arg])) { |
| 10001 | if (strcmp(args[cur_arg], "location") == 0) { |
| 10002 | if (!*args[cur_arg + 1]) |
| 10003 | goto missing_arg; |
| 10004 | |
| 10005 | type = REDIRECT_TYPE_LOCATION; |
| 10006 | cur_arg++; |
| 10007 | destination = args[cur_arg]; |
| 10008 | } |
| 10009 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 10010 | if (!*args[cur_arg + 1]) |
| 10011 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10012 | type = REDIRECT_TYPE_PREFIX; |
| 10013 | cur_arg++; |
| 10014 | destination = args[cur_arg]; |
| 10015 | } |
| 10016 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 10017 | if (!*args[cur_arg + 1]) |
| 10018 | goto missing_arg; |
| 10019 | |
| 10020 | type = REDIRECT_TYPE_SCHEME; |
| 10021 | cur_arg++; |
| 10022 | destination = args[cur_arg]; |
| 10023 | } |
| 10024 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 10025 | if (!*args[cur_arg + 1]) |
| 10026 | goto missing_arg; |
| 10027 | |
| 10028 | cur_arg++; |
| 10029 | cookie = args[cur_arg]; |
| 10030 | cookie_set = 1; |
| 10031 | } |
| 10032 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 10033 | if (!*args[cur_arg + 1]) |
| 10034 | goto missing_arg; |
| 10035 | |
| 10036 | cur_arg++; |
| 10037 | cookie = args[cur_arg]; |
| 10038 | cookie_set = 0; |
| 10039 | } |
| 10040 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 10041 | if (!*args[cur_arg + 1]) |
| 10042 | goto missing_arg; |
| 10043 | |
| 10044 | cur_arg++; |
| 10045 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10046 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10047 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10048 | "'%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] | 10049 | args[cur_arg - 1], args[cur_arg]); |
| 10050 | return NULL; |
| 10051 | } |
| 10052 | } |
| 10053 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 10054 | flags |= REDIRECT_FLAG_DROP_QS; |
| 10055 | } |
| 10056 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 10057 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 10058 | } |
| 10059 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 10060 | strcmp(args[cur_arg], "unless") == 0) { |
| 10061 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 10062 | if (!cond) { |
| 10063 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 10064 | return NULL; |
| 10065 | } |
| 10066 | break; |
| 10067 | } |
| 10068 | else { |
| 10069 | memprintf(errmsg, |
| 10070 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 10071 | args[cur_arg]); |
| 10072 | return NULL; |
| 10073 | } |
| 10074 | cur_arg++; |
| 10075 | } |
| 10076 | |
| 10077 | if (type == REDIRECT_TYPE_NONE) { |
| 10078 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 10079 | return NULL; |
| 10080 | } |
| 10081 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10082 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 10083 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 10084 | return NULL; |
| 10085 | } |
| 10086 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10087 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10088 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 10089 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10090 | |
| 10091 | if (!use_fmt) { |
| 10092 | /* old-style static redirect rule */ |
| 10093 | rule->rdr_str = strdup(destination); |
| 10094 | rule->rdr_len = strlen(destination); |
| 10095 | } |
| 10096 | else { |
| 10097 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10098 | |
| 10099 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 10100 | * if prefix == "/", we don't want to add anything, otherwise it |
| 10101 | * makes it hard for the user to configure a self-redirection. |
| 10102 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 10103 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10104 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10105 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 10106 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 10107 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 10108 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10109 | return NULL; |
| 10110 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 10111 | free(curproxy->conf.lfs_file); |
| 10112 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10113 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10114 | } |
| 10115 | } |
| 10116 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10117 | if (cookie) { |
| 10118 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10119 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10120 | */ |
| 10121 | rule->cookie_len = strlen(cookie); |
| 10122 | if (cookie_set) { |
| 10123 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10124 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10125 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10126 | rule->cookie_len += 9; |
| 10127 | } else { |
| 10128 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10129 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10130 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10131 | rule->cookie_len += 20; |
| 10132 | } |
| 10133 | } |
| 10134 | rule->type = type; |
| 10135 | rule->code = code; |
| 10136 | rule->flags = flags; |
| 10137 | LIST_INIT(&rule->list); |
| 10138 | return rule; |
| 10139 | |
| 10140 | missing_arg: |
| 10141 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10142 | return NULL; |
| 10143 | } |
| 10144 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10145 | /************************************************************************/ |
| 10146 | /* The code below is dedicated to ACL parsing and matching */ |
| 10147 | /************************************************************************/ |
| 10148 | |
| 10149 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10150 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10151 | * which means that a request or response is ready. If some data is missing, |
| 10152 | * 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] | 10153 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10154 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10155 | * |
| 10156 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10157 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10158 | * decide whether or not an HTTP message is present ; |
| 10159 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10160 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10161 | * 1 if an HTTP message is ready |
| 10162 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 10163 | 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] | 10164 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10165 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10166 | struct http_txn *txn; |
| 10167 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10168 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10169 | /* Note: it is possible that <s> is NULL when called before stream |
| 10170 | * initialization (eg: tcp-request connection), so this function is the |
| 10171 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10172 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10173 | if (!s) |
| 10174 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10175 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 10176 | if (!s->txn) { |
| 10177 | if (unlikely(!http_alloc_txn(s))) |
| 10178 | return 0; /* not enough memory */ |
| 10179 | http_init_txn(s); |
| 10180 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10181 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10182 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10183 | |
| 10184 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10185 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10186 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10187 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10188 | /* If the buffer does not leave enough free space at the end, |
| 10189 | * we must first realign it. |
| 10190 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10191 | if (s->req.buf->p > s->req.buf->data && |
| 10192 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10193 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10194 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10195 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10196 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10197 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10198 | |
| 10199 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10200 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10201 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10202 | |
| 10203 | /* Still no valid request ? */ |
| 10204 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10205 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10206 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10207 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10208 | } |
| 10209 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10210 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10211 | return 0; |
| 10212 | } |
| 10213 | |
| 10214 | /* OK we just got a valid HTTP request. We have some minor |
| 10215 | * preparation to perform so that further checks can rely |
| 10216 | * on HTTP tests. |
| 10217 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10218 | |
| 10219 | /* If the request was parsed but was too large, we must absolutely |
| 10220 | * return an error so that it is not processed. At the moment this |
| 10221 | * cannot happen, but if the parsers are to change in the future, |
| 10222 | * we want this check to be maintained. |
| 10223 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10224 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10225 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 10226 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10227 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10228 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10229 | return 1; |
| 10230 | } |
| 10231 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10232 | 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] | 10233 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10234 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10235 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10236 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10237 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10238 | } |
| 10239 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10240 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10241 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10242 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10243 | |
| 10244 | /* otherwise everything's ready for the request */ |
| 10245 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10246 | else { |
| 10247 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10248 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10249 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10250 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10251 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10252 | } |
| 10253 | |
| 10254 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10255 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10256 | return 1; |
| 10257 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10258 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10259 | /* 1. Check on METHOD |
| 10260 | * We use the pre-parsed method if it is known, and store its number as an |
| 10261 | * integer. If it is unknown, we use the pointer and the length. |
| 10262 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10263 | 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] | 10264 | { |
| 10265 | int len, meth; |
| 10266 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10267 | len = strlen(text); |
| 10268 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10269 | |
| 10270 | pattern->val.i = meth; |
| 10271 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10272 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10273 | pattern->len = len; |
| 10274 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10275 | else { |
| 10276 | pattern->ptr.str = NULL; |
| 10277 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10278 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10279 | return 1; |
| 10280 | } |
| 10281 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10282 | /* This function fetches the method of current HTTP request and stores |
| 10283 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10284 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10285 | * in <len> and <ptr> is NULL ; |
| 10286 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10287 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10288 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10289 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10290 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10291 | 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] | 10292 | { |
| 10293 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10294 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10295 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10296 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10297 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10298 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10299 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10300 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10301 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10302 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10303 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10304 | /* ensure the indexes are not affected */ |
| 10305 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10306 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10307 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10308 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10309 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10310 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10311 | return 1; |
| 10312 | } |
| 10313 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10314 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10315 | 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] | 10316 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10317 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10318 | struct pattern_list *lst; |
| 10319 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10320 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10321 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10322 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10323 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10324 | /* well-known method */ |
| 10325 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10326 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10327 | return pattern; |
| 10328 | else |
| 10329 | continue; |
| 10330 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10331 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10332 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10333 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10334 | continue; |
| 10335 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10336 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10337 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10338 | (!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] | 10339 | return pattern; |
| 10340 | } |
| 10341 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10342 | } |
| 10343 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10344 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10345 | 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] | 10346 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10347 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10348 | char *ptr; |
| 10349 | int len; |
| 10350 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10351 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10352 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10353 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10354 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10355 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10356 | |
| 10357 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10358 | if (len <= 0) |
| 10359 | return 0; |
| 10360 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10361 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10362 | smp->data.u.str.str = ptr; |
| 10363 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10364 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10365 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10366 | return 1; |
| 10367 | } |
| 10368 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10369 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10370 | 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] | 10371 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10372 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10373 | char *ptr; |
| 10374 | int len; |
| 10375 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10376 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10377 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10378 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10379 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10380 | return 0; |
| 10381 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10382 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10383 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10384 | |
| 10385 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10386 | if (len <= 0) |
| 10387 | return 0; |
| 10388 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10389 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10390 | smp->data.u.str.str = ptr; |
| 10391 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10392 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10393 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10394 | return 1; |
| 10395 | } |
| 10396 | |
| 10397 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10398 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10399 | 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] | 10400 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10401 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10402 | char *ptr; |
| 10403 | int len; |
| 10404 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10405 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10406 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10407 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10408 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10409 | return 0; |
| 10410 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10411 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10412 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10413 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10414 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10415 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10416 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10417 | return 1; |
| 10418 | } |
| 10419 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 10420 | static int |
| 10421 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10422 | { |
| 10423 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 10424 | return 0; |
| 10425 | |
| 10426 | if (!smp->strm->unique_id) { |
| 10427 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 10428 | return 0; |
| 10429 | smp->strm->unique_id[0] = '\0'; |
| 10430 | } |
| 10431 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 10432 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 10433 | |
| 10434 | smp->data.type = SMP_T_STR; |
| 10435 | smp->data.u.str.str = smp->strm->unique_id; |
| 10436 | smp->flags = SMP_F_CONST; |
| 10437 | return 1; |
| 10438 | } |
| 10439 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 10440 | /* Returns a string block containing all headers including the |
| 10441 | * empty line wich separes headers from the body. This is useful |
| 10442 | * form some headers analysis. |
| 10443 | */ |
| 10444 | static int |
| 10445 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10446 | { |
| 10447 | struct http_msg *msg; |
| 10448 | struct hdr_idx *idx; |
| 10449 | struct http_txn *txn; |
| 10450 | |
| 10451 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10452 | |
| 10453 | txn = smp->strm->txn; |
| 10454 | idx = &txn->hdr_idx; |
| 10455 | msg = &txn->req; |
| 10456 | |
| 10457 | smp->data.type = SMP_T_STR; |
| 10458 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 10459 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 10460 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 10461 | |
| 10462 | return 1; |
| 10463 | } |
| 10464 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 10465 | /* Returns the header request in a length/value encoded format. |
| 10466 | * This is useful for exchanges with the SPOE. |
| 10467 | * |
| 10468 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 10469 | * SPOE format. The encoding is the following: |
| 10470 | * |
| 10471 | * Each couple "header name" / "header value" is composed |
| 10472 | * like this: |
| 10473 | * "length value" "header name bytes" |
| 10474 | * "length value" "header value bytes" |
| 10475 | * When the last header is reached, the header name and the header |
| 10476 | * value are empty. Their length are 0 |
| 10477 | */ |
| 10478 | static int |
| 10479 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10480 | { |
| 10481 | struct http_msg *msg; |
| 10482 | struct chunk *temp; |
| 10483 | struct hdr_idx *idx; |
| 10484 | const char *cur_ptr, *cur_next, *p; |
| 10485 | int old_idx, cur_idx; |
| 10486 | struct hdr_idx_elem *cur_hdr; |
| 10487 | const char *hn, *hv; |
| 10488 | int hnl, hvl; |
| 10489 | int ret; |
| 10490 | struct http_txn *txn; |
| 10491 | char *buf; |
| 10492 | char *end; |
| 10493 | |
| 10494 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10495 | |
| 10496 | temp = get_trash_chunk(); |
| 10497 | buf = temp->str; |
| 10498 | end = temp->str + temp->size; |
| 10499 | |
| 10500 | txn = smp->strm->txn; |
| 10501 | idx = &txn->hdr_idx; |
| 10502 | msg = &txn->req; |
| 10503 | |
| 10504 | /* Build array of headers. */ |
| 10505 | old_idx = 0; |
| 10506 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 10507 | while (1) { |
| 10508 | cur_idx = idx->v[old_idx].next; |
| 10509 | if (!cur_idx) |
| 10510 | break; |
| 10511 | old_idx = cur_idx; |
| 10512 | |
| 10513 | cur_hdr = &idx->v[cur_idx]; |
| 10514 | cur_ptr = cur_next; |
| 10515 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 10516 | |
| 10517 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 10518 | * and the next header starts at cur_next. We'll check |
| 10519 | * this header in the list as well as against the default |
| 10520 | * rule. |
| 10521 | */ |
| 10522 | |
| 10523 | /* look for ': *'. */ |
| 10524 | hn = cur_ptr; |
| 10525 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 10526 | if (p >= cur_ptr+cur_hdr->len) |
| 10527 | continue; |
| 10528 | hnl = p - hn; |
| 10529 | p++; |
| 10530 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 10531 | p++; |
| 10532 | if (p >= cur_ptr + cur_hdr->len) |
| 10533 | continue; |
| 10534 | hv = p; |
| 10535 | hvl = cur_ptr + cur_hdr->len-p; |
| 10536 | |
| 10537 | /* encode the header name. */ |
| 10538 | ret = encode_varint(hnl, &buf, end); |
| 10539 | if (ret == -1) |
| 10540 | return 0; |
| 10541 | if (buf + hnl > end) |
| 10542 | return 0; |
| 10543 | memcpy(buf, hn, hnl); |
| 10544 | buf += hnl; |
| 10545 | |
| 10546 | /* encode and copy the value. */ |
| 10547 | ret = encode_varint(hvl, &buf, end); |
| 10548 | if (ret == -1) |
| 10549 | return 0; |
| 10550 | if (buf + hvl > end) |
| 10551 | return 0; |
| 10552 | memcpy(buf, hv, hvl); |
| 10553 | buf += hvl; |
| 10554 | } |
| 10555 | |
| 10556 | /* encode the end of the header list with empty |
| 10557 | * header name and header value. |
| 10558 | */ |
| 10559 | ret = encode_varint(0, &buf, end); |
| 10560 | if (ret == -1) |
| 10561 | return 0; |
| 10562 | ret = encode_varint(0, &buf, end); |
| 10563 | if (ret == -1) |
| 10564 | return 0; |
| 10565 | |
| 10566 | /* Initialise sample data which will be filled. */ |
| 10567 | smp->data.type = SMP_T_BIN; |
| 10568 | smp->data.u.str.str = temp->str; |
| 10569 | smp->data.u.str.len = buf - temp->str; |
| 10570 | smp->data.u.str.size = temp->size; |
| 10571 | |
| 10572 | return 1; |
| 10573 | } |
| 10574 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10575 | /* returns the longest available part of the body. This requires that the body |
| 10576 | * has been waited for using http-buffer-request. |
| 10577 | */ |
| 10578 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10579 | 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] | 10580 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10581 | struct http_msg *msg; |
| 10582 | unsigned long len; |
| 10583 | unsigned long block1; |
| 10584 | char *body; |
| 10585 | struct chunk *temp; |
| 10586 | |
| 10587 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10588 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10589 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10590 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10591 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10592 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10593 | |
| 10594 | len = http_body_bytes(msg); |
| 10595 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10596 | |
| 10597 | block1 = len; |
| 10598 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10599 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10600 | |
| 10601 | if (block1 == len) { |
| 10602 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10603 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10604 | smp->data.u.str.str = body; |
| 10605 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10606 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10607 | } |
| 10608 | else { |
| 10609 | /* buffer is wrapped, we need to defragment it */ |
| 10610 | temp = get_trash_chunk(); |
| 10611 | memcpy(temp->str, body, block1); |
| 10612 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10613 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10614 | smp->data.u.str.str = temp->str; |
| 10615 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10616 | smp->flags = SMP_F_VOL_TEST; |
| 10617 | } |
| 10618 | return 1; |
| 10619 | } |
| 10620 | |
| 10621 | |
| 10622 | /* returns the available length of the body. This requires that the body |
| 10623 | * has been waited for using http-buffer-request. |
| 10624 | */ |
| 10625 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10626 | 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] | 10627 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10628 | struct http_msg *msg; |
| 10629 | |
| 10630 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10631 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10632 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10633 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10634 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10635 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10636 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10637 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10638 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10639 | |
| 10640 | smp->flags = SMP_F_VOL_TEST; |
| 10641 | return 1; |
| 10642 | } |
| 10643 | |
| 10644 | |
| 10645 | /* returns the advertised length of the body, or the advertised size of the |
| 10646 | * chunks available in the buffer. This requires that the body has been waited |
| 10647 | * for using http-buffer-request. |
| 10648 | */ |
| 10649 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10650 | 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] | 10651 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10652 | struct http_msg *msg; |
| 10653 | |
| 10654 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10655 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10656 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10657 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10658 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10659 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10660 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10661 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10662 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10663 | |
| 10664 | smp->flags = SMP_F_VOL_TEST; |
| 10665 | return 1; |
| 10666 | } |
| 10667 | |
| 10668 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10669 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10670 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10671 | 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] | 10672 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10673 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10674 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10675 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10676 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10677 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10678 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10679 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10680 | 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] | 10681 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10682 | return 1; |
| 10683 | } |
| 10684 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10685 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10686 | 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] | 10687 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10688 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10689 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10690 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10691 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10692 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10693 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10694 | 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] | 10695 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10696 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10697 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10698 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10699 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10700 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10701 | return 1; |
| 10702 | } |
| 10703 | |
| 10704 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10705 | 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] | 10706 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10707 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10708 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10709 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10710 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10711 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10712 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10713 | 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] | 10714 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10715 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10716 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10717 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10718 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10719 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10720 | return 1; |
| 10721 | } |
| 10722 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10723 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10724 | * Accepts an optional argument of type string containing the header field name, |
| 10725 | * and an optional argument of type signed or unsigned integer to request an |
| 10726 | * 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] | 10727 | * headers are considered from the first one. It does not stop on commas and |
| 10728 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10729 | */ |
| 10730 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10731 | 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] | 10732 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10733 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10734 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10735 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10736 | int occ = 0; |
| 10737 | const char *name_str = NULL; |
| 10738 | int name_len = 0; |
| 10739 | |
| 10740 | if (!ctx) { |
| 10741 | /* first call */ |
| 10742 | ctx = &static_hdr_ctx; |
| 10743 | ctx->idx = 0; |
| 10744 | smp->ctx.a[0] = ctx; |
| 10745 | } |
| 10746 | |
| 10747 | if (args) { |
| 10748 | if (args[0].type != ARGT_STR) |
| 10749 | return 0; |
| 10750 | name_str = args[0].data.str.str; |
| 10751 | name_len = args[0].data.str.len; |
| 10752 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10753 | if (args[1].type == ARGT_SINT) |
| 10754 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10755 | } |
| 10756 | |
| 10757 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10758 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10759 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10760 | 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] | 10761 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10762 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10763 | /* search for header from the beginning */ |
| 10764 | ctx->idx = 0; |
| 10765 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10766 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10767 | /* no explicit occurrence and single fetch => last header by default */ |
| 10768 | occ = -1; |
| 10769 | |
| 10770 | if (!occ) |
| 10771 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10772 | smp->flags |= SMP_F_NOT_LAST; |
| 10773 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10774 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10775 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10776 | 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] | 10777 | return 1; |
| 10778 | |
| 10779 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10780 | return 0; |
| 10781 | } |
| 10782 | |
| 10783 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10784 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10785 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10786 | */ |
| 10787 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10788 | 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] | 10789 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10790 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10791 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10792 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10793 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10794 | const char *name = NULL; |
| 10795 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10796 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10797 | if (args && args->type == ARGT_STR) { |
| 10798 | name = args->data.str.str; |
| 10799 | len = args->data.str.len; |
| 10800 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10801 | |
| 10802 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10803 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10804 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10805 | 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] | 10806 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10807 | ctx.idx = 0; |
| 10808 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10809 | 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] | 10810 | cnt++; |
| 10811 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10812 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10813 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10814 | smp->flags = SMP_F_VOL_HDR; |
| 10815 | return 1; |
| 10816 | } |
| 10817 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10818 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10819 | 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] | 10820 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10821 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10822 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10823 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10824 | struct chunk *temp; |
| 10825 | char del = ','; |
| 10826 | |
| 10827 | if (args && args->type == ARGT_STR) |
| 10828 | del = *args[0].data.str.str; |
| 10829 | |
| 10830 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10831 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10832 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10833 | 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] | 10834 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10835 | temp = get_trash_chunk(); |
| 10836 | |
| 10837 | ctx.idx = 0; |
| 10838 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10839 | if (temp->len) |
| 10840 | temp->str[temp->len++] = del; |
| 10841 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10842 | temp->len += ctx.del; |
| 10843 | } |
| 10844 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10845 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10846 | smp->data.u.str.str = temp->str; |
| 10847 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10848 | smp->flags = SMP_F_VOL_HDR; |
| 10849 | return 1; |
| 10850 | } |
| 10851 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10852 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10853 | * Accepts an optional argument of type string containing the header field name, |
| 10854 | * and an optional argument of type signed or unsigned integer to request an |
| 10855 | * 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] | 10856 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10857 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10858 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10859 | 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] | 10860 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10861 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10862 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10863 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10864 | int occ = 0; |
| 10865 | const char *name_str = NULL; |
| 10866 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10867 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10868 | if (!ctx) { |
| 10869 | /* first call */ |
| 10870 | ctx = &static_hdr_ctx; |
| 10871 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10872 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10873 | } |
| 10874 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10875 | if (args) { |
| 10876 | if (args[0].type != ARGT_STR) |
| 10877 | return 0; |
| 10878 | name_str = args[0].data.str.str; |
| 10879 | name_len = args[0].data.str.len; |
| 10880 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10881 | if (args[1].type == ARGT_SINT) |
| 10882 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10883 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10884 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10885 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10886 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10887 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10888 | 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] | 10889 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10890 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10891 | /* search for header from the beginning */ |
| 10892 | ctx->idx = 0; |
| 10893 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10894 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10895 | /* no explicit occurrence and single fetch => last header by default */ |
| 10896 | occ = -1; |
| 10897 | |
| 10898 | if (!occ) |
| 10899 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10900 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10901 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10902 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10903 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10904 | 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] | 10905 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10906 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10907 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10908 | return 0; |
| 10909 | } |
| 10910 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10911 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10912 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10913 | */ |
| 10914 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10915 | 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] | 10916 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10917 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10918 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10919 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10920 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10921 | const char *name = NULL; |
| 10922 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10923 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10924 | if (args && args->type == ARGT_STR) { |
| 10925 | name = args->data.str.str; |
| 10926 | len = args->data.str.len; |
| 10927 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10928 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10929 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10930 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10931 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10932 | 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] | 10933 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10934 | ctx.idx = 0; |
| 10935 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10936 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10937 | cnt++; |
| 10938 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10939 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10940 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10941 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10942 | return 1; |
| 10943 | } |
| 10944 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10945 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10946 | * takes a mandatory argument of type string and an optional one of type int |
| 10947 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10948 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10949 | */ |
| 10950 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10951 | 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] | 10952 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10953 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10954 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10955 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10956 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10957 | 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] | 10958 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10959 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10960 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10961 | } |
| 10962 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10963 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10964 | * and an optional one of type int to designate a specific occurrence. |
| 10965 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10966 | */ |
| 10967 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10968 | 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] | 10969 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10970 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10971 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10972 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10973 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10974 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10975 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10976 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10977 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10978 | if (smp->data.u.str.len < temp->size - 1) { |
| 10979 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10980 | temp->str[smp->data.u.str.len] = '\0'; |
| 10981 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10982 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10983 | break; |
| 10984 | } |
| 10985 | } |
| 10986 | } |
| 10987 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10988 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10989 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10990 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10991 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10992 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10993 | } |
| 10994 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10995 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10996 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10997 | */ |
| 10998 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10999 | 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] | 11000 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11001 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11002 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 11003 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11004 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 11005 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11006 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11007 | 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] | 11008 | ptr = http_get_path(txn); |
| 11009 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11010 | return 0; |
| 11011 | |
| 11012 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11013 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11014 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11015 | |
| 11016 | while (ptr < end && *ptr != '?') |
| 11017 | ptr++; |
| 11018 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11019 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11020 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 11021 | return 1; |
| 11022 | } |
| 11023 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11024 | /* This produces a concatenation of the first occurrence of the Host header |
| 11025 | * followed by the path component if it begins with a slash ('/'). This means |
| 11026 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 11027 | * If no Host header is found, then the path is returned as-is. The returned |
| 11028 | * 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] | 11029 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11030 | */ |
| 11031 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11032 | 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] | 11033 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11034 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11035 | char *ptr, *end, *beg; |
| 11036 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 11037 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11038 | |
| 11039 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11040 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11041 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11042 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11043 | 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] | 11044 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11045 | |
| 11046 | /* 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] | 11047 | temp = get_trash_chunk(); |
| 11048 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11049 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11050 | smp->data.u.str.str = temp->str; |
| 11051 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11052 | |
| 11053 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11054 | 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] | 11055 | beg = http_get_path(txn); |
| 11056 | if (!beg) |
| 11057 | beg = end; |
| 11058 | |
| 11059 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 11060 | |
| 11061 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11062 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 11063 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11064 | } |
| 11065 | |
| 11066 | smp->flags = SMP_F_VOL_1ST; |
| 11067 | return 1; |
| 11068 | } |
| 11069 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11070 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11071 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11072 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11073 | * 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] | 11074 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 11075 | * 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] | 11076 | * high-traffic sites without having to store whole paths. |
| 11077 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 11078 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11079 | 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] | 11080 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11081 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11082 | struct hdr_ctx ctx; |
| 11083 | unsigned int hash = 0; |
| 11084 | char *ptr, *beg, *end; |
| 11085 | int len; |
| 11086 | |
| 11087 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11088 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11089 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11090 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11091 | 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] | 11092 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11093 | ptr = ctx.line + ctx.val; |
| 11094 | len = ctx.vlen; |
| 11095 | while (len--) |
| 11096 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11097 | } |
| 11098 | |
| 11099 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11100 | 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] | 11101 | beg = http_get_path(txn); |
| 11102 | if (!beg) |
| 11103 | beg = end; |
| 11104 | |
| 11105 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 11106 | |
| 11107 | if (beg < ptr && *beg == '/') { |
| 11108 | while (beg < ptr) |
| 11109 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11110 | } |
| 11111 | hash = full_hash(hash); |
| 11112 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11113 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11114 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 11115 | smp->flags = SMP_F_VOL_1ST; |
| 11116 | return 1; |
| 11117 | } |
| 11118 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11119 | /* 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] | 11120 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11121 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 11122 | * 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] | 11123 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11124 | * 8 bytes would still work. |
| 11125 | */ |
| 11126 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11127 | 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] | 11128 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11129 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11130 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11131 | |
| 11132 | if (!cli_conn) |
| 11133 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11134 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11135 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11136 | return 0; |
| 11137 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11138 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11139 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 11140 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11141 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11142 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11143 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11144 | 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] | 11145 | temp->len += 4; |
| 11146 | break; |
| 11147 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11148 | 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] | 11149 | temp->len += 16; |
| 11150 | break; |
| 11151 | default: |
| 11152 | return 0; |
| 11153 | } |
| 11154 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11155 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11156 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11157 | return 1; |
| 11158 | } |
| 11159 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11160 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 11161 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 11162 | * of type string carrying the whole query string. |
| 11163 | */ |
| 11164 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11165 | 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] | 11166 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11167 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11168 | char *ptr, *end; |
| 11169 | |
| 11170 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11171 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11172 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11173 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 11174 | end = ptr + txn->req.sl.rq.u_l; |
| 11175 | |
| 11176 | /* look up the '?' */ |
| 11177 | do { |
| 11178 | if (ptr == end) |
| 11179 | return 0; |
| 11180 | } while (*ptr++ != '?'); |
| 11181 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11182 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11183 | smp->data.u.str.str = ptr; |
| 11184 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11185 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11186 | return 1; |
| 11187 | } |
| 11188 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11189 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11190 | 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] | 11191 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11192 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 11193 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 11194 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11195 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11196 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11197 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11198 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11199 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11200 | return 1; |
| 11201 | } |
| 11202 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11203 | /* return a valid test if the current request is the first one on the connection */ |
| 11204 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11205 | 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] | 11206 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11207 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11208 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11209 | return 1; |
| 11210 | } |
| 11211 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11212 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11213 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11214 | 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] | 11215 | { |
| 11216 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11217 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11218 | return 0; |
| 11219 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11220 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11221 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11222 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11223 | return 0; |
| 11224 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11225 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11226 | 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] | 11227 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11228 | return 1; |
| 11229 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11230 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11231 | /* Accepts exactly 1 argument of type userlist */ |
| 11232 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11233 | 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] | 11234 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11235 | if (!args || args->type != ARGT_USR) |
| 11236 | return 0; |
| 11237 | |
| 11238 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11239 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11240 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11241 | return 0; |
| 11242 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11243 | /* if the user does not belong to the userlist or has a wrong password, |
| 11244 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11245 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11246 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11247 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11248 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11249 | return 0; |
| 11250 | |
| 11251 | /* pat_match_auth() will need the user list */ |
| 11252 | smp->ctx.a[0] = args->data.usr; |
| 11253 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11254 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11255 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11256 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 11257 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11258 | |
| 11259 | return 1; |
| 11260 | } |
| 11261 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11262 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 11263 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 11264 | * the cookie value is returned into *value and *value_l, and the function |
| 11265 | * returns a pointer to the next pointer to search from if the value was found. |
| 11266 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11267 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11268 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11269 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11270 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11271 | * list is expected. |
| 11272 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 11273 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11274 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11275 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11276 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11277 | { |
| 11278 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11279 | char *next; |
| 11280 | |
| 11281 | /* we search at least a cookie name followed by an equal, and more |
| 11282 | * generally something like this : |
| 11283 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11284 | */ |
| 11285 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11286 | /* Iterate through all cookies on this line */ |
| 11287 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11288 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11289 | att_beg++; |
| 11290 | |
| 11291 | /* find att_end : this is the first character after the last non |
| 11292 | * space before the equal. It may be equal to hdr_end. |
| 11293 | */ |
| 11294 | equal = att_end = att_beg; |
| 11295 | |
| 11296 | while (equal < hdr_end) { |
| 11297 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11298 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11299 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11300 | continue; |
| 11301 | att_end = equal; |
| 11302 | } |
| 11303 | |
| 11304 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11305 | * is between <att_beg> and <equal>, both may be identical. |
| 11306 | */ |
| 11307 | |
| 11308 | /* look for end of cookie if there is an equal sign */ |
| 11309 | if (equal < hdr_end && *equal == '=') { |
| 11310 | /* look for the beginning of the value */ |
| 11311 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11312 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11313 | val_beg++; |
| 11314 | |
| 11315 | /* find the end of the value, respecting quotes */ |
| 11316 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11317 | |
| 11318 | /* make val_end point to the first white space or delimitor after the value */ |
| 11319 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11320 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11321 | val_end--; |
| 11322 | } else { |
| 11323 | val_beg = val_end = next = equal; |
| 11324 | } |
| 11325 | |
| 11326 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11327 | * they will automatically be removed if a header before them is removed, |
| 11328 | * since they're supposed to be linked together. |
| 11329 | */ |
| 11330 | if (*att_beg == '$') |
| 11331 | continue; |
| 11332 | |
| 11333 | /* Ignore cookies with no equal sign */ |
| 11334 | if (equal == next) |
| 11335 | continue; |
| 11336 | |
| 11337 | /* Now we have the cookie name between att_beg and att_end, and |
| 11338 | * its value between val_beg and val_end. |
| 11339 | */ |
| 11340 | |
| 11341 | if (att_end - att_beg == cookie_name_l && |
| 11342 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11343 | /* let's return this value and indicate where to go on from */ |
| 11344 | *value = val_beg; |
| 11345 | *value_l = val_end - val_beg; |
| 11346 | return next + 1; |
| 11347 | } |
| 11348 | |
| 11349 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11350 | if (!list) |
| 11351 | break; |
| 11352 | } |
| 11353 | |
| 11354 | return NULL; |
| 11355 | } |
| 11356 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11357 | /* Fetch a captured HTTP request header. The index is the position of |
| 11358 | * the "capture" option in the configuration file |
| 11359 | */ |
| 11360 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11361 | 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] | 11362 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11363 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11364 | int idx; |
| 11365 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11366 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11367 | return 0; |
| 11368 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11369 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11370 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11371 | 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] | 11372 | return 0; |
| 11373 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11374 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11375 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11376 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 11377 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11378 | |
| 11379 | return 1; |
| 11380 | } |
| 11381 | |
| 11382 | /* Fetch a captured HTTP response header. The index is the position of |
| 11383 | * the "capture" option in the configuration file |
| 11384 | */ |
| 11385 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11386 | 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] | 11387 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11388 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11389 | int idx; |
| 11390 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11391 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11392 | return 0; |
| 11393 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11394 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11395 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11396 | 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] | 11397 | return 0; |
| 11398 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11399 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11400 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11401 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 11402 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11403 | |
| 11404 | return 1; |
| 11405 | } |
| 11406 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11407 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11408 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11409 | 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] | 11410 | { |
| 11411 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11412 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11413 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11414 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11415 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11416 | return 0; |
| 11417 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11418 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11419 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11420 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11421 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11422 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11423 | temp = get_trash_chunk(); |
| 11424 | temp->str = txn->uri; |
| 11425 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11426 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11427 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11428 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11429 | |
| 11430 | return 1; |
| 11431 | |
| 11432 | } |
| 11433 | |
| 11434 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11435 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11436 | 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] | 11437 | { |
| 11438 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11439 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11440 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11441 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11442 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11443 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11444 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11445 | ptr = txn->uri; |
| 11446 | |
| 11447 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11448 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11449 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11450 | if (!*ptr) |
| 11451 | return 0; |
| 11452 | |
| 11453 | ptr++; /* skip the space */ |
| 11454 | |
| 11455 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11456 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11457 | if (!ptr) |
| 11458 | return 0; |
| 11459 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11460 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11461 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11462 | smp->data.u.str = *temp; |
| 11463 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11464 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11465 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11466 | |
| 11467 | return 1; |
| 11468 | } |
| 11469 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11470 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11471 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11472 | */ |
| 11473 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11474 | 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] | 11475 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11476 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11477 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11478 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11479 | return 0; |
| 11480 | |
| 11481 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11482 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11483 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11484 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11485 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11486 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11487 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11488 | smp->flags = SMP_F_CONST; |
| 11489 | return 1; |
| 11490 | |
| 11491 | } |
| 11492 | |
| 11493 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11494 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11495 | */ |
| 11496 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11497 | 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] | 11498 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11499 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11500 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11501 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11502 | return 0; |
| 11503 | |
| 11504 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11505 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11506 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11507 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11508 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11509 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11510 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11511 | smp->flags = SMP_F_CONST; |
| 11512 | return 1; |
| 11513 | |
| 11514 | } |
| 11515 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11516 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11517 | /* 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] | 11518 | * 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] | 11519 | * 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] | 11520 | * 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] | 11521 | * 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] | 11522 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11523 | * 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] | 11524 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11525 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11526 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11527 | 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] | 11528 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11529 | struct http_txn *txn; |
| 11530 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11531 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11532 | const struct http_msg *msg; |
| 11533 | const char *hdr_name; |
| 11534 | int hdr_name_len; |
| 11535 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11536 | int occ = 0; |
| 11537 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11538 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11539 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11540 | return 0; |
| 11541 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11542 | if (!ctx) { |
| 11543 | /* first call */ |
| 11544 | ctx = &static_hdr_ctx; |
| 11545 | ctx->idx = 0; |
| 11546 | smp->ctx.a[2] = ctx; |
| 11547 | } |
| 11548 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11549 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11550 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11551 | txn = smp->strm->txn; |
| 11552 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11553 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11554 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11555 | msg = &txn->req; |
| 11556 | hdr_name = "Cookie"; |
| 11557 | hdr_name_len = 6; |
| 11558 | } else { |
| 11559 | msg = &txn->rsp; |
| 11560 | hdr_name = "Set-Cookie"; |
| 11561 | hdr_name_len = 10; |
| 11562 | } |
| 11563 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11564 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11565 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11566 | occ = -1; |
| 11567 | |
| 11568 | /* OK so basically here, either we want only one value and it's the |
| 11569 | * last one, or we want to iterate over all of them and we fetch the |
| 11570 | * next one. |
| 11571 | */ |
| 11572 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11573 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11574 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11575 | /* search for the header from the beginning, we must first initialize |
| 11576 | * the search parameters. |
| 11577 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11578 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11579 | ctx->idx = 0; |
| 11580 | } |
| 11581 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11582 | smp->flags |= SMP_F_VOL_HDR; |
| 11583 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11584 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11585 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11586 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11587 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11588 | goto out; |
| 11589 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11590 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11591 | continue; |
| 11592 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11593 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11594 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11595 | } |
| 11596 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11597 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11598 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11599 | 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] | 11600 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11601 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11602 | &smp->data.u.str.str, |
| 11603 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11604 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11605 | found = 1; |
| 11606 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11607 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11608 | smp->flags |= SMP_F_NOT_LAST; |
| 11609 | return 1; |
| 11610 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11611 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11612 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11613 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11614 | /* all cookie headers and values were scanned. If we're looking for the |
| 11615 | * last occurrence, we may return it now. |
| 11616 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11617 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11618 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11619 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11620 | } |
| 11621 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11622 | /* 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] | 11623 | * 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] | 11624 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11625 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11626 | */ |
| 11627 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11628 | 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] | 11629 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11630 | struct http_txn *txn; |
| 11631 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11632 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11633 | const struct http_msg *msg; |
| 11634 | const char *hdr_name; |
| 11635 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11636 | int cnt; |
| 11637 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11638 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11639 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11640 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11641 | return 0; |
| 11642 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11643 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11644 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11645 | txn = smp->strm->txn; |
| 11646 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11647 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11648 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11649 | msg = &txn->req; |
| 11650 | hdr_name = "Cookie"; |
| 11651 | hdr_name_len = 6; |
| 11652 | } else { |
| 11653 | msg = &txn->rsp; |
| 11654 | hdr_name = "Set-Cookie"; |
| 11655 | hdr_name_len = 10; |
| 11656 | } |
| 11657 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11658 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11659 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11660 | ctx.idx = 0; |
| 11661 | cnt = 0; |
| 11662 | |
| 11663 | while (1) { |
| 11664 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11665 | if (!val_beg) { |
| 11666 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11667 | break; |
| 11668 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11669 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11670 | continue; |
| 11671 | |
| 11672 | val_beg = ctx.line + ctx.val; |
| 11673 | val_end = val_beg + ctx.vlen; |
| 11674 | } |
| 11675 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11676 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11677 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11678 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11679 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11680 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11681 | &smp->data.u.str.str, |
| 11682 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11683 | cnt++; |
| 11684 | } |
| 11685 | } |
| 11686 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11687 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11688 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11689 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11690 | return 1; |
| 11691 | } |
| 11692 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11693 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11694 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11695 | */ |
| 11696 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11697 | 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] | 11698 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11699 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11700 | |
| 11701 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11702 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11703 | 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] | 11704 | } |
| 11705 | |
| 11706 | return ret; |
| 11707 | } |
| 11708 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11709 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11710 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11711 | /************************************************************************/ |
| 11712 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11713 | /* |
| 11714 | * Given a path string and its length, find the position of beginning of the |
| 11715 | * query string. Returns NULL if no query string is found in the path. |
| 11716 | * |
| 11717 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11718 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11719 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11720 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11721 | 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] | 11722 | { |
| 11723 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11724 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11725 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11726 | return p ? p + 1 : NULL; |
| 11727 | } |
| 11728 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11729 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11730 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11731 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11732 | } |
| 11733 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11734 | /* after increasing a pointer value, it can exceed the first buffer |
| 11735 | * size. This function transform the value of <ptr> according with |
| 11736 | * the expected position. <chunks> is an array of the one or two |
| 11737 | * avalaible chunks. The first value is the start of the first chunk, |
| 11738 | * the second value if the end+1 of the first chunks. The third value |
| 11739 | * is NULL or the start of the second chunk and the fourth value is |
| 11740 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11741 | * wrap, else returns 0. |
| 11742 | */ |
| 11743 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11744 | { |
| 11745 | if (*ptr < chunks[1]) |
| 11746 | return 0; |
| 11747 | if (!chunks[2]) |
| 11748 | return 0; |
| 11749 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11750 | return 1; |
| 11751 | } |
| 11752 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11753 | /* |
| 11754 | * Given a url parameter, find the starting position of the first occurence, |
| 11755 | * or NULL if the parameter is not found. |
| 11756 | * |
| 11757 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11758 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11759 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11760 | * 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] | 11761 | * or the second chunk. The caller must be check the position before using the |
| 11762 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11763 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11764 | static const char * |
| 11765 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11766 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11767 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11768 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11769 | const char *pos, *last, *equal; |
| 11770 | const char **bufs = chunks; |
| 11771 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11772 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11773 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11774 | pos = bufs[0]; |
| 11775 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11776 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11777 | /* Check the equal. */ |
| 11778 | equal = pos + url_param_name_l; |
| 11779 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11780 | if (equal >= chunks[3]) |
| 11781 | return NULL; |
| 11782 | } else { |
| 11783 | if (equal >= chunks[1]) |
| 11784 | return NULL; |
| 11785 | } |
| 11786 | if (*equal == '=') { |
| 11787 | if (pos + url_param_name_l > last) { |
| 11788 | /* process wrap case, we detect a wrap. In this case, the |
| 11789 | * comparison is performed in two parts. |
| 11790 | */ |
| 11791 | |
| 11792 | /* This is the end, we dont have any other chunk. */ |
| 11793 | if (bufs != chunks || !bufs[2]) |
| 11794 | return NULL; |
| 11795 | |
| 11796 | /* Compute the length of each part of the comparison. */ |
| 11797 | l1 = last - pos; |
| 11798 | l2 = url_param_name_l - l1; |
| 11799 | |
| 11800 | /* The second buffer is too short to contain the compared string. */ |
| 11801 | if (bufs[2] + l2 > bufs[3]) |
| 11802 | return NULL; |
| 11803 | |
| 11804 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11805 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11806 | return pos; |
| 11807 | |
| 11808 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11809 | bufs += 2; |
| 11810 | pos = bufs[0] + l2; |
| 11811 | last = bufs[1]; |
| 11812 | |
| 11813 | } else { |
| 11814 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11815 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11816 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11817 | pos += url_param_name_l + 1; |
| 11818 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11819 | last = bufs[2]; |
| 11820 | } |
| 11821 | } |
| 11822 | |
| 11823 | while (1) { |
| 11824 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11825 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11826 | pos++; |
| 11827 | if (pos < last) |
| 11828 | break; |
| 11829 | /* process buffer wrapping. */ |
| 11830 | if (bufs != chunks || !bufs[2]) |
| 11831 | return NULL; |
| 11832 | bufs += 2; |
| 11833 | pos = bufs[0]; |
| 11834 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11835 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11836 | pos++; |
| 11837 | } |
| 11838 | return NULL; |
| 11839 | } |
| 11840 | |
| 11841 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11842 | * Given a url parameter name and a query string, find the next value. |
| 11843 | * An empty url_param_name matches the first available parameter. |
| 11844 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11845 | * respectively provide a pointer to the value and its end. |
| 11846 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11847 | */ |
| 11848 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11849 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11850 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11851 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11852 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11853 | const char *arg_start, *qs_end; |
| 11854 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11855 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11856 | arg_start = chunks[0]; |
| 11857 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11858 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11859 | /* Looks for an argument name. */ |
| 11860 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11861 | url_param_name, url_param_name_l, |
| 11862 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11863 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11864 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11865 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11866 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11867 | if (!arg_start) |
| 11868 | return 0; |
| 11869 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11870 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11871 | while (1) { |
| 11872 | /* looks for the first argument. */ |
| 11873 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11874 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11875 | /* Check for wrapping. */ |
| 11876 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11877 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11878 | chunks[2]) { |
| 11879 | arg_start = chunks[2]; |
| 11880 | qs_end = chunks[3]; |
| 11881 | continue; |
| 11882 | } |
| 11883 | return 0; |
| 11884 | } |
| 11885 | break; |
| 11886 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11887 | value_start++; |
| 11888 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11889 | else { |
| 11890 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11891 | value_start = arg_start + url_param_name_l + 1; |
| 11892 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11893 | /* Check for pointer wrapping. */ |
| 11894 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11895 | /* Update the end pointer. */ |
| 11896 | qs_end = chunks[3]; |
| 11897 | |
| 11898 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11899 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11900 | return 0; |
| 11901 | } |
| 11902 | } |
| 11903 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11904 | value_end = value_start; |
| 11905 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11906 | while (1) { |
| 11907 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11908 | value_end++; |
| 11909 | if (value_end < qs_end) |
| 11910 | break; |
| 11911 | /* process buffer wrapping. */ |
| 11912 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11913 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11914 | chunks[2]) { |
| 11915 | value_end = chunks[2]; |
| 11916 | qs_end = chunks[3]; |
| 11917 | continue; |
| 11918 | } |
| 11919 | break; |
| 11920 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11921 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11922 | *vstart = value_start; |
| 11923 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11924 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11925 | } |
| 11926 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11927 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11928 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11929 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11930 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11931 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11932 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11933 | 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] | 11934 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11935 | const char *vstart, *vend; |
| 11936 | struct chunk *temp; |
| 11937 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11938 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11939 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11940 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11941 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11942 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11943 | return 0; |
| 11944 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11945 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11946 | * if the value is wrapped, copy-it in a buffer. |
| 11947 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11948 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11949 | if (chunks[2] && |
| 11950 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11951 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11952 | /* Wrapped case. */ |
| 11953 | temp = get_trash_chunk(); |
| 11954 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11955 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11956 | smp->data.u.str.str = temp->str; |
| 11957 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11958 | } else { |
| 11959 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11960 | smp->data.u.str.str = (char *)vstart; |
| 11961 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11962 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11963 | } |
| 11964 | |
| 11965 | /* Update context, check wrapping. */ |
| 11966 | chunks[0] = vend; |
| 11967 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11968 | chunks[1] = chunks[3]; |
| 11969 | chunks[2] = NULL; |
| 11970 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11971 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11972 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11973 | smp->flags |= SMP_F_NOT_LAST; |
| 11974 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11975 | return 1; |
| 11976 | } |
| 11977 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11978 | /* This function iterates over each parameter of the query string. It uses |
| 11979 | * 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] | 11980 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11981 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11982 | * considered. It supports an optional delimiter argument for the beginning of |
| 11983 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11984 | */ |
| 11985 | static int |
| 11986 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11987 | { |
| 11988 | struct http_msg *msg; |
| 11989 | char delim = '?'; |
| 11990 | const char *name; |
| 11991 | int name_len; |
| 11992 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11993 | if (!args || |
| 11994 | (args[0].type && args[0].type != ARGT_STR) || |
| 11995 | (args[1].type && args[1].type != ARGT_STR)) |
| 11996 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11997 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11998 | name = ""; |
| 11999 | name_len = 0; |
| 12000 | if (args->type == ARGT_STR) { |
| 12001 | name = args->data.str.str; |
| 12002 | name_len = args->data.str.len; |
| 12003 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12004 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 12005 | if (args[1].type) |
| 12006 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12007 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 12008 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12009 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12010 | |
| 12011 | msg = &smp->strm->txn->req; |
| 12012 | |
| 12013 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 12014 | msg->sl.rq.u_l, delim); |
| 12015 | if (!smp->ctx.a[0]) |
| 12016 | return 0; |
| 12017 | |
| 12018 | 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] | 12019 | |
| 12020 | /* Assume that the context is filled with NULL pointer |
| 12021 | * before the first call. |
| 12022 | * smp->ctx.a[2] = NULL; |
| 12023 | * smp->ctx.a[3] = NULL; |
| 12024 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 12025 | } |
| 12026 | |
| 12027 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 12028 | } |
| 12029 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12030 | /* This function iterates over each parameter of the body. This requires |
| 12031 | * 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] | 12032 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 12033 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 12034 | * optional second part if the body wraps at the end of the buffer. An optional |
| 12035 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12036 | */ |
| 12037 | static int |
| 12038 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 12039 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12040 | struct http_msg *msg; |
| 12041 | unsigned long len; |
| 12042 | unsigned long block1; |
| 12043 | char *body; |
| 12044 | const char *name; |
| 12045 | int name_len; |
| 12046 | |
| 12047 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 12048 | return 0; |
| 12049 | |
| 12050 | name = ""; |
| 12051 | name_len = 0; |
| 12052 | if (args[0].type == ARGT_STR) { |
| 12053 | name = args[0].data.str.str; |
| 12054 | name_len = args[0].data.str.len; |
| 12055 | } |
| 12056 | |
| 12057 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 12058 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12059 | |
| 12060 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 12061 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12062 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 12063 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12064 | |
| 12065 | len = http_body_bytes(msg); |
| 12066 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 12067 | |
| 12068 | block1 = len; |
| 12069 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 12070 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 12071 | |
| 12072 | if (block1 == len) { |
| 12073 | /* buffer is not wrapped (or empty) */ |
| 12074 | smp->ctx.a[0] = body; |
| 12075 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 12076 | |
| 12077 | /* Assume that the context is filled with NULL pointer |
| 12078 | * before the first call. |
| 12079 | * smp->ctx.a[2] = NULL; |
| 12080 | * smp->ctx.a[3] = NULL; |
| 12081 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12082 | } |
| 12083 | else { |
| 12084 | /* buffer is wrapped, we need to defragment it */ |
| 12085 | smp->ctx.a[0] = body; |
| 12086 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 12087 | smp->ctx.a[2] = msg->chn->buf->data; |
| 12088 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12089 | } |
| 12090 | } |
| 12091 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 12092 | } |
| 12093 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 12094 | /* Return the signed integer value for the specified url parameter (see url_param |
| 12095 | * above). |
| 12096 | */ |
| 12097 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12098 | 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] | 12099 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12100 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 12101 | |
| 12102 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12103 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12104 | 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] | 12105 | } |
| 12106 | |
| 12107 | return ret; |
| 12108 | } |
| 12109 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12110 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 12111 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 12112 | * This means that '*' will not be added, resulting in exactly the first Host |
| 12113 | * entry. If no Host header is found, then the path is used. The resulting value |
| 12114 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 12115 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 12116 | * high-traffic sites without having to store whole paths. |
| 12117 | * this differs from the base32 functions in that it includes the url parameters |
| 12118 | * as well as the path |
| 12119 | */ |
| 12120 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12121 | 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] | 12122 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 12123 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12124 | struct hdr_ctx ctx; |
| 12125 | unsigned int hash = 0; |
| 12126 | char *ptr, *beg, *end; |
| 12127 | int len; |
| 12128 | |
| 12129 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12130 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12131 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12132 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12133 | 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] | 12134 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 12135 | ptr = ctx.line + ctx.val; |
| 12136 | len = ctx.vlen; |
| 12137 | while (len--) |
| 12138 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 12139 | } |
| 12140 | |
| 12141 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12142 | 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] | 12143 | beg = http_get_path(txn); |
| 12144 | if (!beg) |
| 12145 | beg = end; |
| 12146 | |
| 12147 | for (ptr = beg; ptr < end ; ptr++); |
| 12148 | |
| 12149 | if (beg < ptr && *beg == '/') { |
| 12150 | while (beg < ptr) |
| 12151 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 12152 | } |
| 12153 | hash = full_hash(hash); |
| 12154 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12155 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12156 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12157 | smp->flags = SMP_F_VOL_1ST; |
| 12158 | return 1; |
| 12159 | } |
| 12160 | |
| 12161 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 12162 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 12163 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 12164 | * on the source address length. The URL hash is stored before the address so |
| 12165 | * that in environments where IPv6 is insignificant, truncating the output to |
| 12166 | * 8 bytes would still work. |
| 12167 | */ |
| 12168 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12169 | 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] | 12170 | { |
| 12171 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12172 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12173 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 12174 | if (!cli_conn) |
| 12175 | return 0; |
| 12176 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12177 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12178 | return 0; |
| 12179 | |
| 12180 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 12181 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 12182 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12183 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12184 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12185 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12186 | 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] | 12187 | temp->len += 4; |
| 12188 | break; |
| 12189 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12190 | 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] | 12191 | temp->len += 16; |
| 12192 | break; |
| 12193 | default: |
| 12194 | return 0; |
| 12195 | } |
| 12196 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12197 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12198 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12199 | return 1; |
| 12200 | } |
| 12201 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12202 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 12203 | * keyword. These keywords support an optional positive or negative occurrence |
| 12204 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 12205 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 12206 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 12207 | * error message in case of error, that the caller is responsible for freeing. |
| 12208 | * The initial location must either be freeable or NULL. |
| 12209 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 12210 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12211 | { |
| 12212 | 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] | 12213 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12214 | return 0; |
| 12215 | } |
| 12216 | return 1; |
| 12217 | } |
| 12218 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12219 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 12220 | * adds an optional offset found in args[0] and emits a string representing |
| 12221 | * the date in RFC-1123/5322 format. |
| 12222 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12223 | 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] | 12224 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 12225 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12226 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 12227 | struct chunk *temp; |
| 12228 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12229 | /* 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] | 12230 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12231 | |
| 12232 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12233 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12234 | curr_date += args[0].data.sint; |
| 12235 | |
| 12236 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 12237 | if (!tm) |
| 12238 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12239 | |
| 12240 | temp = get_trash_chunk(); |
| 12241 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 12242 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 12243 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 12244 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 12245 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12246 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12247 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12248 | return 1; |
| 12249 | } |
| 12250 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12251 | /* Match language range with language tag. RFC2616 14.4: |
| 12252 | * |
| 12253 | * A language-range matches a language-tag if it exactly equals |
| 12254 | * the tag, or if it exactly equals a prefix of the tag such |
| 12255 | * that the first tag character following the prefix is "-". |
| 12256 | * |
| 12257 | * Return 1 if the strings match, else return 0. |
| 12258 | */ |
| 12259 | static inline int language_range_match(const char *range, int range_len, |
| 12260 | const char *tag, int tag_len) |
| 12261 | { |
| 12262 | const char *end = range + range_len; |
| 12263 | const char *tend = tag + tag_len; |
| 12264 | while (range < end) { |
| 12265 | if (*range == '-' && tag == tend) |
| 12266 | return 1; |
| 12267 | if (*range != *tag || tag == tend) |
| 12268 | return 0; |
| 12269 | range++; |
| 12270 | tag++; |
| 12271 | } |
| 12272 | /* Return true only if the last char of the tag is matched. */ |
| 12273 | return tag == tend; |
| 12274 | } |
| 12275 | |
| 12276 | /* 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] | 12277 | 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] | 12278 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12279 | const char *al = smp->data.u.str.str; |
| 12280 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12281 | const char *token; |
| 12282 | int toklen; |
| 12283 | int qvalue; |
| 12284 | const char *str; |
| 12285 | const char *w; |
| 12286 | int best_q = 0; |
| 12287 | |
| 12288 | /* Set the constant to the sample, because the output of the |
| 12289 | * function will be peek in the constant configuration string. |
| 12290 | */ |
| 12291 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12292 | smp->data.u.str.size = 0; |
| 12293 | smp->data.u.str.str = ""; |
| 12294 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12295 | |
| 12296 | /* Parse the accept language */ |
| 12297 | while (1) { |
| 12298 | |
| 12299 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12300 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12301 | al++; |
| 12302 | if (al >= end) |
| 12303 | break; |
| 12304 | |
| 12305 | /* Start of the fisrt word. */ |
| 12306 | token = al; |
| 12307 | |
| 12308 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12309 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12310 | al++; |
| 12311 | if (al == token) |
| 12312 | goto expect_comma; |
| 12313 | |
| 12314 | /* Length of the token. */ |
| 12315 | toklen = al - token; |
| 12316 | qvalue = 1000; |
| 12317 | |
| 12318 | /* Check if the token exists in the list. If the token not exists, |
| 12319 | * jump to the next token. |
| 12320 | */ |
| 12321 | str = args[0].data.str.str; |
| 12322 | w = str; |
| 12323 | while (1) { |
| 12324 | if (*str == ';' || *str == '\0') { |
| 12325 | if (language_range_match(token, toklen, w, str-w)) |
| 12326 | goto look_for_q; |
| 12327 | if (*str == '\0') |
| 12328 | goto expect_comma; |
| 12329 | w = str + 1; |
| 12330 | } |
| 12331 | str++; |
| 12332 | } |
| 12333 | goto expect_comma; |
| 12334 | |
| 12335 | look_for_q: |
| 12336 | |
| 12337 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12338 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12339 | al++; |
| 12340 | if (al >= end) |
| 12341 | goto process_value; |
| 12342 | |
| 12343 | /* If ',' is found, process the result */ |
| 12344 | if (*al == ',') |
| 12345 | goto process_value; |
| 12346 | |
| 12347 | /* If the character is different from ';', look |
| 12348 | * for the end of the header part in best effort. |
| 12349 | */ |
| 12350 | if (*al != ';') |
| 12351 | goto expect_comma; |
| 12352 | |
| 12353 | /* Assumes that the char is ';', now expect "q=". */ |
| 12354 | al++; |
| 12355 | |
| 12356 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12357 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12358 | al++; |
| 12359 | if (al >= end) |
| 12360 | goto process_value; |
| 12361 | |
| 12362 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12363 | if (*al != 'q') |
| 12364 | goto process_value; |
| 12365 | al++; |
| 12366 | |
| 12367 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12368 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12369 | al++; |
| 12370 | if (al >= end) |
| 12371 | goto process_value; |
| 12372 | |
| 12373 | /* Expect '='. If no '=', continue in best effort */ |
| 12374 | if (*al != '=') |
| 12375 | goto process_value; |
| 12376 | al++; |
| 12377 | |
| 12378 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12379 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12380 | al++; |
| 12381 | if (al >= end) |
| 12382 | goto process_value; |
| 12383 | |
| 12384 | /* Parse the q value. */ |
| 12385 | qvalue = parse_qvalue(al, &al); |
| 12386 | |
| 12387 | process_value: |
| 12388 | |
| 12389 | /* If the new q value is the best q value, then store the associated |
| 12390 | * language in the response. If qvalue is the biggest value (1000), |
| 12391 | * break the process. |
| 12392 | */ |
| 12393 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12394 | smp->data.u.str.str = (char *)w; |
| 12395 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12396 | if (qvalue >= 1000) |
| 12397 | break; |
| 12398 | best_q = qvalue; |
| 12399 | } |
| 12400 | |
| 12401 | expect_comma: |
| 12402 | |
| 12403 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12404 | while (al < end && *al != ',') |
| 12405 | al++; |
| 12406 | if (al >= end) |
| 12407 | break; |
| 12408 | |
| 12409 | /* Comma is found, jump it and restart the analyzer. */ |
| 12410 | al++; |
| 12411 | } |
| 12412 | |
| 12413 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12414 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12415 | smp->data.u.str.str = args[1].data.str.str; |
| 12416 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12417 | } |
| 12418 | |
| 12419 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12420 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12421 | } |
| 12422 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12423 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12424 | 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] | 12425 | { |
| 12426 | /* If the constant flag is set or if not size is avalaible at |
| 12427 | * the end of the buffer, copy the string in other buffer |
| 12428 | * before decoding. |
| 12429 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12430 | 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] | 12431 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12432 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12433 | smp->data.u.str.str = str->str; |
| 12434 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12435 | smp->flags &= ~SMP_F_CONST; |
| 12436 | } |
| 12437 | |
| 12438 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12439 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12440 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12441 | return 1; |
| 12442 | } |
| 12443 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12444 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12445 | { |
| 12446 | struct proxy *fe = strm_fe(smp->strm); |
| 12447 | int idx, i; |
| 12448 | struct cap_hdr *hdr; |
| 12449 | int len; |
| 12450 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12451 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12452 | return 0; |
| 12453 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12454 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12455 | |
| 12456 | /* Check the availibity of the capture id. */ |
| 12457 | if (idx > fe->nb_req_cap - 1) |
| 12458 | return 0; |
| 12459 | |
| 12460 | /* Look for the original configuration. */ |
| 12461 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12462 | hdr != NULL && i != idx ; |
| 12463 | i--, hdr = hdr->next); |
| 12464 | if (!hdr) |
| 12465 | return 0; |
| 12466 | |
| 12467 | /* check for the memory allocation */ |
| 12468 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12469 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12470 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12471 | return 0; |
| 12472 | |
| 12473 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12474 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12475 | if (len > hdr->len) |
| 12476 | len = hdr->len; |
| 12477 | |
| 12478 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12479 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12480 | smp->strm->req_cap[idx][len] = '\0'; |
| 12481 | |
| 12482 | return 1; |
| 12483 | } |
| 12484 | |
| 12485 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12486 | { |
| 12487 | struct proxy *fe = strm_fe(smp->strm); |
| 12488 | int idx, i; |
| 12489 | struct cap_hdr *hdr; |
| 12490 | int len; |
| 12491 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12492 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12493 | return 0; |
| 12494 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12495 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12496 | |
| 12497 | /* Check the availibity of the capture id. */ |
| 12498 | if (idx > fe->nb_rsp_cap - 1) |
| 12499 | return 0; |
| 12500 | |
| 12501 | /* Look for the original configuration. */ |
| 12502 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12503 | hdr != NULL && i != idx ; |
| 12504 | i--, hdr = hdr->next); |
| 12505 | if (!hdr) |
| 12506 | return 0; |
| 12507 | |
| 12508 | /* check for the memory allocation */ |
| 12509 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12510 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12511 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12512 | return 0; |
| 12513 | |
| 12514 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12515 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12516 | if (len > hdr->len) |
| 12517 | len = hdr->len; |
| 12518 | |
| 12519 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12520 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12521 | smp->strm->res_cap[idx][len] = '\0'; |
| 12522 | |
| 12523 | return 1; |
| 12524 | } |
| 12525 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12526 | /* 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] | 12527 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12528 | * the relevant part of the request line accordingly. Then it updates various |
| 12529 | * pointers to the next elements which were moved, and the total buffer length. |
| 12530 | * 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] | 12531 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12532 | * error, though this can be revisited when this code is finally exploited. |
| 12533 | * |
| 12534 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12535 | * query string and 3 to replace uri. |
| 12536 | * |
| 12537 | * In query string case, the mark question '?' must be set at the start of the |
| 12538 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12539 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12540 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12541 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12542 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12543 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12544 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12545 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12546 | int delta; |
| 12547 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12548 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12549 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12550 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12551 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12552 | |
| 12553 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12554 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12555 | txn->req.sl.rq.m_l += delta; |
| 12556 | txn->req.sl.rq.u += delta; |
| 12557 | txn->req.sl.rq.v += delta; |
| 12558 | break; |
| 12559 | |
| 12560 | case 1: // path |
| 12561 | cur_ptr = http_get_path(txn); |
| 12562 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12563 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12564 | |
| 12565 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12566 | 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] | 12567 | cur_end++; |
| 12568 | |
| 12569 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12570 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12571 | txn->req.sl.rq.u_l += delta; |
| 12572 | txn->req.sl.rq.v += delta; |
| 12573 | break; |
| 12574 | |
| 12575 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12576 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12577 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12578 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12579 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12580 | cur_ptr++; |
| 12581 | |
| 12582 | /* skip the question mark or indicate that we must insert it |
| 12583 | * (but only if the format string is not empty then). |
| 12584 | */ |
| 12585 | if (cur_ptr < cur_end) |
| 12586 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12587 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12588 | offset = 0; |
| 12589 | |
| 12590 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12591 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12592 | txn->req.sl.rq.u_l += delta; |
| 12593 | txn->req.sl.rq.v += delta; |
| 12594 | break; |
| 12595 | |
| 12596 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12597 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12598 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12599 | |
| 12600 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12601 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12602 | txn->req.sl.rq.u_l += delta; |
| 12603 | txn->req.sl.rq.v += delta; |
| 12604 | break; |
| 12605 | |
| 12606 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12607 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12608 | } |
| 12609 | |
| 12610 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12611 | 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] | 12612 | txn->req.sl.rq.l += delta; |
| 12613 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12614 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12615 | return 0; |
| 12616 | } |
| 12617 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12618 | /* This function replace the HTTP status code and the associated message. The |
| 12619 | * variable <status> contains the new status code. This function never fails. |
| 12620 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12621 | void http_set_status(unsigned int status, const char *reason, struct stream *s) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12622 | { |
| 12623 | struct http_txn *txn = s->txn; |
| 12624 | char *cur_ptr, *cur_end; |
| 12625 | int delta; |
| 12626 | char *res; |
| 12627 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12628 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12629 | int msg_len; |
| 12630 | |
| 12631 | chunk_reset(&trash); |
| 12632 | |
| 12633 | res = ultoa_o(status, trash.str, trash.size); |
| 12634 | c_l = res - trash.str; |
| 12635 | |
| 12636 | trash.str[c_l] = ' '; |
| 12637 | trash.len = c_l + 1; |
| 12638 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12639 | /* Do we have a custom reason format string? */ |
| 12640 | if (msg == NULL) |
| 12641 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12642 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12643 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12644 | trash.len += msg_len; |
| 12645 | |
| 12646 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12647 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12648 | |
| 12649 | /* commit changes and adjust message */ |
| 12650 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12651 | |
| 12652 | /* adjust res line offsets and lengths */ |
| 12653 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12654 | txn->rsp.sl.st.c_l = c_l; |
| 12655 | txn->rsp.sl.st.r_l = msg_len; |
| 12656 | |
| 12657 | delta = trash.len - (cur_end - cur_ptr); |
| 12658 | txn->rsp.sl.st.l += delta; |
| 12659 | txn->hdr_idx.v[0].len += delta; |
| 12660 | http_msg_move_end(&txn->rsp, delta); |
| 12661 | } |
| 12662 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12663 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12664 | * 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] | 12665 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12666 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12667 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12668 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12669 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12670 | 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] | 12671 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12672 | { |
| 12673 | chunk_reset(&trash); |
| 12674 | |
| 12675 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12676 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12677 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12678 | 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] | 12679 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12680 | 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] | 12681 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12682 | } |
| 12683 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12684 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12685 | 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] | 12686 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12687 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12688 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12689 | return ACT_RET_CONT; |
| 12690 | } |
| 12691 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12692 | /* parse an http-request action among : |
| 12693 | * set-method |
| 12694 | * set-path |
| 12695 | * set-query |
| 12696 | * set-uri |
| 12697 | * |
| 12698 | * All of them accept a single argument of type string representing a log-format. |
| 12699 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12700 | * 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] | 12701 | * 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] | 12702 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12703 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12704 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12705 | { |
| 12706 | int cur_arg = *orig_arg; |
| 12707 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12708 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12709 | |
| 12710 | switch (args[0][4]) { |
| 12711 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12712 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12713 | rule->action_ptr = http_action_set_req_line; |
| 12714 | break; |
| 12715 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12716 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12717 | rule->action_ptr = http_action_set_req_line; |
| 12718 | break; |
| 12719 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12720 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12721 | rule->action_ptr = http_action_set_req_line; |
| 12722 | break; |
| 12723 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12724 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12725 | rule->action_ptr = http_action_set_req_line; |
| 12726 | break; |
| 12727 | default: |
| 12728 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12729 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12730 | } |
| 12731 | |
| 12732 | if (!*args[cur_arg] || |
| 12733 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12734 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12735 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12736 | } |
| 12737 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12738 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12739 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 12740 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 12741 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 12742 | return ACT_RET_PRS_ERR; |
| 12743 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12744 | |
| 12745 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12746 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12747 | } |
| 12748 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12749 | /* parse set-status action: |
| 12750 | * This action accepts a single argument of type int representing |
| 12751 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12752 | * ACT_RET_PRS_ERR on error. |
| 12753 | */ |
| 12754 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12755 | struct act_rule *rule, char **err) |
| 12756 | { |
| 12757 | char *error; |
| 12758 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12759 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12760 | rule->action_ptr = action_http_set_status; |
| 12761 | |
| 12762 | /* Check if an argument is available */ |
| 12763 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12764 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12765 | return ACT_RET_PRS_ERR; |
| 12766 | } |
| 12767 | |
| 12768 | /* convert status code as integer */ |
| 12769 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12770 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12771 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12772 | return ACT_RET_PRS_ERR; |
| 12773 | } |
| 12774 | |
| 12775 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12776 | |
| 12777 | /* set custom reason string */ |
| 12778 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 12779 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 12780 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 12781 | (*orig_arg)++; |
| 12782 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 12783 | (*orig_arg)++; |
| 12784 | } |
| 12785 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12786 | return ACT_RET_PRS_OK; |
| 12787 | } |
| 12788 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12789 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12790 | * turns the result into a string and puts it in a capture slot. It always |
| 12791 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12792 | * processing continues. |
| 12793 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12794 | 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] | 12795 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12796 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12797 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12798 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12799 | char **cap = s->req_cap; |
| 12800 | int len; |
| 12801 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12802 | 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] | 12803 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12804 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12805 | |
| 12806 | if (cap[h->index] == NULL) |
| 12807 | cap[h->index] = pool_alloc2(h->pool); |
| 12808 | |
| 12809 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12810 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12811 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12812 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12813 | if (len > h->len) |
| 12814 | len = h->len; |
| 12815 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12816 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12817 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12818 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12819 | } |
| 12820 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12821 | /* This function executes the "capture" action and store the result in a |
| 12822 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12823 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12824 | * error occurs the action is cancelled, but the rule processing continues. |
| 12825 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12826 | 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] | 12827 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12828 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12829 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12830 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12831 | char **cap = s->req_cap; |
| 12832 | struct proxy *fe = strm_fe(s); |
| 12833 | int len; |
| 12834 | int i; |
| 12835 | |
| 12836 | /* Look for the original configuration. */ |
| 12837 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12838 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12839 | i--, h = h->next); |
| 12840 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12841 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12842 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12843 | 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] | 12844 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12845 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12846 | |
| 12847 | if (cap[h->index] == NULL) |
| 12848 | cap[h->index] = pool_alloc2(h->pool); |
| 12849 | |
| 12850 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12851 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12852 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12853 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12854 | if (len > h->len) |
| 12855 | len = h->len; |
| 12856 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12857 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12858 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12859 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12860 | } |
| 12861 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12862 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12863 | * 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] | 12864 | * 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] | 12865 | * 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] | 12866 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12867 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12868 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12869 | { |
| 12870 | struct sample_expr *expr; |
| 12871 | struct cap_hdr *hdr; |
| 12872 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12873 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12874 | |
| 12875 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12876 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12877 | strcmp(args[cur_arg], "unless") == 0) |
| 12878 | break; |
| 12879 | |
| 12880 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12881 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12882 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12883 | } |
| 12884 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12885 | cur_arg = *orig_arg; |
| 12886 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12887 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12888 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12889 | |
| 12890 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12891 | memprintf(err, |
| 12892 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12893 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12894 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12895 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12896 | } |
| 12897 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12898 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12899 | memprintf(err, "expects 'len or 'id'"); |
| 12900 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12901 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12902 | } |
| 12903 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12904 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12905 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12906 | |
| 12907 | if (!(px->cap & PR_CAP_FE)) { |
| 12908 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12909 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12910 | } |
| 12911 | |
| 12912 | proxy->conf.args.ctx = ARGC_CAP; |
| 12913 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12914 | if (!args[cur_arg]) { |
| 12915 | memprintf(err, "missing length value"); |
| 12916 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12917 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12918 | } |
| 12919 | /* we copy the table name for now, it will be resolved later */ |
| 12920 | len = atoi(args[cur_arg]); |
| 12921 | if (len <= 0) { |
| 12922 | memprintf(err, "length must be > 0"); |
| 12923 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12924 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12925 | } |
| 12926 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12927 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12928 | if (!len) { |
| 12929 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12930 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12931 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12932 | } |
| 12933 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12934 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12935 | hdr->next = px->req_cap; |
| 12936 | hdr->name = NULL; /* not a header capture */ |
| 12937 | hdr->namelen = 0; |
| 12938 | hdr->len = len; |
| 12939 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12940 | hdr->index = px->nb_req_cap++; |
| 12941 | |
| 12942 | px->req_cap = hdr; |
| 12943 | px->to_log |= LW_REQHDR; |
| 12944 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12945 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12946 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12947 | rule->arg.cap.expr = expr; |
| 12948 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12949 | } |
| 12950 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12951 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12952 | int id; |
| 12953 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12954 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12955 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12956 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12957 | if (!args[cur_arg]) { |
| 12958 | memprintf(err, "missing id value"); |
| 12959 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12960 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12961 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12962 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12963 | id = strtol(args[cur_arg], &error, 10); |
| 12964 | if (*error != '\0') { |
| 12965 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12966 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12967 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12968 | } |
| 12969 | cur_arg++; |
| 12970 | |
| 12971 | proxy->conf.args.ctx = ARGC_CAP; |
| 12972 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12973 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12974 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12975 | rule->arg.capid.expr = expr; |
| 12976 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12977 | } |
| 12978 | |
| 12979 | else { |
| 12980 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12981 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12982 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12983 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12984 | |
| 12985 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12986 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12987 | } |
| 12988 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12989 | /* This function executes the "capture" action and store the result in a |
| 12990 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12991 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12992 | * error occurs the action is cancelled, but the rule processing continues. |
| 12993 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12994 | 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] | 12995 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12996 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12997 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12998 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12999 | char **cap = s->res_cap; |
| 13000 | struct proxy *fe = strm_fe(s); |
| 13001 | int len; |
| 13002 | int i; |
| 13003 | |
| 13004 | /* Look for the original configuration. */ |
| 13005 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 13006 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13007 | i--, h = h->next); |
| 13008 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13009 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13010 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 13011 | 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] | 13012 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13013 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13014 | |
| 13015 | if (cap[h->index] == NULL) |
| 13016 | cap[h->index] = pool_alloc2(h->pool); |
| 13017 | |
| 13018 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13019 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13020 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 13021 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13022 | if (len > h->len) |
| 13023 | len = h->len; |
| 13024 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 13025 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13026 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 13027 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13028 | } |
| 13029 | |
| 13030 | /* parse an "http-response capture" action. It takes a single argument which is |
| 13031 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 13032 | * 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] | 13033 | * 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] | 13034 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13035 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 13036 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13037 | { |
| 13038 | struct sample_expr *expr; |
| 13039 | int cur_arg; |
| 13040 | int id; |
| 13041 | char *error; |
| 13042 | |
| 13043 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 13044 | if (strcmp(args[cur_arg], "if") == 0 || |
| 13045 | strcmp(args[cur_arg], "unless") == 0) |
| 13046 | break; |
| 13047 | |
| 13048 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 13049 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13050 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13051 | } |
| 13052 | |
| 13053 | cur_arg = *orig_arg; |
| 13054 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 13055 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13056 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13057 | |
| 13058 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 13059 | memprintf(err, |
| 13060 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 13061 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 13062 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13063 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13064 | } |
| 13065 | |
| 13066 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 13067 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13068 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13069 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13070 | } |
| 13071 | |
| 13072 | if (strcmp(args[cur_arg], "id") != 0) { |
| 13073 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 13074 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13075 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13076 | } |
| 13077 | |
| 13078 | cur_arg++; |
| 13079 | |
| 13080 | if (!args[cur_arg]) { |
| 13081 | memprintf(err, "missing id value"); |
| 13082 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13083 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13084 | } |
| 13085 | |
| 13086 | id = strtol(args[cur_arg], &error, 10); |
| 13087 | if (*error != '\0') { |
| 13088 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 13089 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13090 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13091 | } |
| 13092 | cur_arg++; |
| 13093 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13094 | proxy->conf.args.ctx = ARGC_CAP; |
| 13095 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 13096 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13097 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 13098 | rule->arg.capid.expr = expr; |
| 13099 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13100 | |
| 13101 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 13102 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13103 | } |
| 13104 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13105 | /* |
| 13106 | * Return the struct http_req_action_kw associated to a keyword. |
| 13107 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13108 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13109 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 13110 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13111 | } |
| 13112 | |
| 13113 | /* |
| 13114 | * Return the struct http_res_action_kw associated to a keyword. |
| 13115 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13116 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13117 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 13118 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 13119 | } |
| 13120 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13121 | |
| 13122 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 13123 | * now. |
| 13124 | */ |
| 13125 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 13126 | { |
| 13127 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 13128 | return 1; |
| 13129 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 13130 | if (*args[2]) { |
| 13131 | struct proxy *px; |
| 13132 | |
| 13133 | px = proxy_find_by_name(args[2], 0, 0); |
| 13134 | if (px) |
| 13135 | appctx->ctx.errors.iid = px->uuid; |
| 13136 | else |
| 13137 | appctx->ctx.errors.iid = atoi(args[2]); |
| 13138 | |
| 13139 | if (!appctx->ctx.errors.iid) { |
| 13140 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 13141 | appctx->st0 = CLI_ST_PRINT; |
| 13142 | return 1; |
| 13143 | } |
| 13144 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13145 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 13146 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 13147 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13148 | appctx->ctx.errors.flag = 0; |
| 13149 | if (strcmp(args[3], "request") == 0) |
| 13150 | appctx->ctx.errors.flag |= 4; // ignore response |
| 13151 | else if (strcmp(args[3], "response") == 0) |
| 13152 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13153 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13154 | return 0; |
| 13155 | } |
| 13156 | |
| 13157 | /* This function dumps all captured errors onto the stream interface's |
| 13158 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 13159 | * to be called again, otherwise non-zero. |
| 13160 | */ |
| 13161 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 13162 | { |
| 13163 | struct stream_interface *si = appctx->owner; |
| 13164 | extern const char *monthname[12]; |
| 13165 | |
| 13166 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 13167 | return 1; |
| 13168 | |
| 13169 | chunk_reset(&trash); |
| 13170 | |
| 13171 | if (!appctx->ctx.errors.px) { |
| 13172 | /* the function had not been called yet, let's prepare the |
| 13173 | * buffer for a response. |
| 13174 | */ |
| 13175 | struct tm tm; |
| 13176 | |
| 13177 | get_localtime(date.tv_sec, &tm); |
| 13178 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 13179 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13180 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 13181 | error_snapshot_id); |
| 13182 | |
| 13183 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13184 | /* Socket buffer full. Let's try again later from the same point */ |
| 13185 | si_applet_cant_put(si); |
| 13186 | return 0; |
| 13187 | } |
| 13188 | |
| 13189 | appctx->ctx.errors.px = proxy; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13190 | appctx->ctx.errors.bol = 0; |
| 13191 | appctx->ctx.errors.ptr = -1; |
| 13192 | } |
| 13193 | |
| 13194 | /* we have two inner loops here, one for the proxy, the other one for |
| 13195 | * the buffer. |
| 13196 | */ |
| 13197 | while (appctx->ctx.errors.px) { |
| 13198 | struct error_snapshot *es; |
| 13199 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13200 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13201 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13202 | if (appctx->ctx.errors.flag & 2) // skip req |
| 13203 | goto next; |
| 13204 | } |
| 13205 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13206 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13207 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 13208 | goto next; |
| 13209 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13210 | |
| 13211 | if (!es->when.tv_sec) |
| 13212 | goto next; |
| 13213 | |
| 13214 | if (appctx->ctx.errors.iid >= 0 && |
| 13215 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 13216 | es->oe->uuid != appctx->ctx.errors.iid) |
| 13217 | goto next; |
| 13218 | |
| 13219 | if (appctx->ctx.errors.ptr < 0) { |
| 13220 | /* just print headers now */ |
| 13221 | |
| 13222 | char pn[INET6_ADDRSTRLEN]; |
| 13223 | struct tm tm; |
| 13224 | int port; |
| 13225 | |
| 13226 | get_localtime(es->when.tv_sec, &tm); |
| 13227 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 13228 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13229 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 13230 | |
| 13231 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 13232 | case AF_INET: |
| 13233 | case AF_INET6: |
| 13234 | port = get_host_port(&es->src); |
| 13235 | break; |
| 13236 | default: |
| 13237 | port = 0; |
| 13238 | } |
| 13239 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13240 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13241 | case 0: |
| 13242 | chunk_appendf(&trash, |
| 13243 | " frontend %s (#%d): invalid request\n" |
| 13244 | " backend %s (#%d)", |
| 13245 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13246 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 13247 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 13248 | break; |
| 13249 | case 1: |
| 13250 | chunk_appendf(&trash, |
| 13251 | " backend %s (#%d): invalid response\n" |
| 13252 | " frontend %s (#%d)", |
| 13253 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13254 | es->oe->id, es->oe->uuid); |
| 13255 | break; |
| 13256 | } |
| 13257 | |
| 13258 | chunk_appendf(&trash, |
| 13259 | ", server %s (#%d), event #%u\n" |
| 13260 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 13261 | " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n" |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13262 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 13263 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 13264 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 13265 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 13266 | es->ev_id, |
| 13267 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 13268 | http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags, |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13269 | es->m_clen, es->m_blen, |
| 13270 | es->b_flags, es->b_out, es->b_tot, |
| 13271 | es->len, es->b_wrap, es->pos); |
| 13272 | |
| 13273 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13274 | /* Socket buffer full. Let's try again later from the same point */ |
| 13275 | si_applet_cant_put(si); |
| 13276 | return 0; |
| 13277 | } |
| 13278 | appctx->ctx.errors.ptr = 0; |
| 13279 | appctx->ctx.errors.sid = es->sid; |
| 13280 | } |
| 13281 | |
| 13282 | if (appctx->ctx.errors.sid != es->sid) { |
| 13283 | /* the snapshot changed while we were dumping it */ |
| 13284 | chunk_appendf(&trash, |
| 13285 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
| 13286 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13287 | si_applet_cant_put(si); |
| 13288 | return 0; |
| 13289 | } |
| 13290 | goto next; |
| 13291 | } |
| 13292 | |
| 13293 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 13294 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 13295 | int newptr; |
| 13296 | int newline; |
| 13297 | |
| 13298 | newline = appctx->ctx.errors.bol; |
| 13299 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 13300 | if (newptr == appctx->ctx.errors.ptr) |
| 13301 | return 0; |
| 13302 | |
| 13303 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13304 | /* Socket buffer full. Let's try again later from the same point */ |
| 13305 | si_applet_cant_put(si); |
| 13306 | return 0; |
| 13307 | } |
| 13308 | appctx->ctx.errors.ptr = newptr; |
| 13309 | appctx->ctx.errors.bol = newline; |
| 13310 | }; |
| 13311 | next: |
| 13312 | appctx->ctx.errors.bol = 0; |
| 13313 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13314 | appctx->ctx.errors.flag ^= 1; |
| 13315 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13316 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13317 | } |
| 13318 | |
| 13319 | /* dump complete */ |
| 13320 | return 1; |
| 13321 | } |
| 13322 | |
| 13323 | /* register cli keywords */ |
| 13324 | static struct cli_kw_list cli_kws = {{ },{ |
| 13325 | { { "show", "errors", NULL }, |
| 13326 | "show errors : report last request and response errors for each proxy", |
| 13327 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 13328 | }, |
| 13329 | {{},} |
| 13330 | }}; |
| 13331 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13332 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13333 | /* All supported ACL keywords must be declared here. */ |
| 13334 | /************************************************************************/ |
| 13335 | |
| 13336 | /* Note: must not be declared <const> as its list will be overwritten. |
| 13337 | * Please take care of keeping this list alphabetically sorted. |
| 13338 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13339 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13340 | { "base", "base", PAT_MATCH_STR }, |
| 13341 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 13342 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 13343 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 13344 | { "base_end", "base", PAT_MATCH_END }, |
| 13345 | { "base_len", "base", PAT_MATCH_LEN }, |
| 13346 | { "base_reg", "base", PAT_MATCH_REG }, |
| 13347 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 13348 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13349 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 13350 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 13351 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 13352 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 13353 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 13354 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 13355 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 13356 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13357 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13358 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 13359 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 13360 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 13361 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 13362 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 13363 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 13364 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 13365 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13366 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13367 | /* these two declarations uses strings with list storage (in place |
| 13368 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 13369 | * and delete functions are relative to the list management. The parse |
| 13370 | * and match method are related to the corresponding fetch methods. This |
| 13371 | * is very particular ACL declaration mode. |
| 13372 | */ |
| 13373 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 13374 | { "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] | 13375 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13376 | { "path", "path", PAT_MATCH_STR }, |
| 13377 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 13378 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 13379 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 13380 | { "path_end", "path", PAT_MATCH_END }, |
| 13381 | { "path_len", "path", PAT_MATCH_LEN }, |
| 13382 | { "path_reg", "path", PAT_MATCH_REG }, |
| 13383 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13384 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13385 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 13386 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13387 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13388 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 13389 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 13390 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 13391 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 13392 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 13393 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 13394 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 13395 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13396 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13397 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 13398 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 13399 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 13400 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 13401 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 13402 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 13403 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 13404 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13405 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13406 | { "url", "url", PAT_MATCH_STR }, |
| 13407 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 13408 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 13409 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 13410 | { "url_end", "url", PAT_MATCH_END }, |
| 13411 | { "url_len", "url", PAT_MATCH_LEN }, |
| 13412 | { "url_reg", "url", PAT_MATCH_REG }, |
| 13413 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13414 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13415 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 13416 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 13417 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 13418 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 13419 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 13420 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 13421 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 13422 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13423 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 13424 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13425 | }}; |
| 13426 | |
| 13427 | /************************************************************************/ |
| 13428 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13429 | /************************************************************************/ |
| 13430 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13431 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13432 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13433 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13434 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13435 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 13436 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13437 | { "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] | 13438 | |
| 13439 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13440 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13441 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13442 | { "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] | 13443 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13444 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 13445 | { "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] | 13446 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13447 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 13448 | * are only here to match the ACL's name, are request-only and are used |
| 13449 | * for ACL compatibility only. |
| 13450 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13451 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13452 | { "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] | 13453 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13454 | { "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] | 13455 | |
| 13456 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 13457 | * only here to match the ACL's name, are request-only and are used for |
| 13458 | * ACL compatibility only. |
| 13459 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13460 | { "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] | 13461 | { "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] | 13462 | { "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] | 13463 | { "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] | 13464 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 13465 | { "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] | 13466 | { "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] | 13467 | { "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] | 13468 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13469 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 13470 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13471 | |
| 13472 | /* HTTP protocol on the request path */ |
| 13473 | { "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] | 13474 | { "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] | 13475 | |
| 13476 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13477 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13478 | { "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] | 13479 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 13480 | { "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] | 13481 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13482 | { "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] | 13483 | { "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] | 13484 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 13485 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 13486 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13487 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13488 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13489 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 13490 | { "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] | 13491 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13492 | /* 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] | 13493 | { "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] | 13494 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13495 | { "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] | 13496 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13497 | { "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] | 13498 | { "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] | 13499 | { "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] | 13500 | { "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] | 13501 | { "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] | 13502 | { "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] | 13503 | { "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] | 13504 | |
| 13505 | /* 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] | 13506 | { "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] | 13507 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13508 | { "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] | 13509 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13510 | { "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] | 13511 | { "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] | 13512 | { "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] | 13513 | { "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] | 13514 | { "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] | 13515 | { "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] | 13516 | { "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] | 13517 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13518 | /* 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] | 13519 | { "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] | 13520 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13521 | { "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] | 13522 | { "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] | 13523 | |
| 13524 | /* 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] | 13525 | { "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] | 13526 | { "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] | 13527 | { "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] | 13528 | { "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] | 13529 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13530 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 13531 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13532 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13533 | { "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] | 13534 | { "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] | 13535 | { "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] | 13536 | { "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] | 13537 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13538 | { "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] | 13539 | { "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] | 13540 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13541 | }}; |
| 13542 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13543 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13544 | /************************************************************************/ |
| 13545 | /* All supported converter keywords must be declared here. */ |
| 13546 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13547 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13548 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13549 | { "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] | 13550 | { "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] | 13551 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13552 | { "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] | 13553 | { "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] | 13554 | { NULL, NULL, 0, 0, 0 }, |
| 13555 | }}; |
| 13556 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13557 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13558 | /************************************************************************/ |
| 13559 | /* All supported http-request action keywords must be declared here. */ |
| 13560 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13561 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13562 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13563 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13564 | { "set-method", parse_set_req_line }, |
| 13565 | { "set-path", parse_set_req_line }, |
| 13566 | { "set-query", parse_set_req_line }, |
| 13567 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13568 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13569 | } |
| 13570 | }; |
| 13571 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13572 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13573 | .kw = { |
| 13574 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 13575 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13576 | { NULL, NULL } |
| 13577 | } |
| 13578 | }; |
| 13579 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13580 | __attribute__((constructor)) |
| 13581 | static void __http_protocol_init(void) |
| 13582 | { |
| 13583 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13584 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13585 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13586 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13587 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13588 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13589 | } |
| 13590 | |
| 13591 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13592 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13593 | * Local variables: |
| 13594 | * c-indent-level: 8 |
| 13595 | * c-basic-offset: 8 |
| 13596 | * End: |
| 13597 | */ |