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); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 462 | pool2_requri = create_pool("requri", 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 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 853 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 854 | * par 2.2 for more information. Note that it requires a valid header to return |
| 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 | |
| 1122 | /* RFC2616, par. 5.1.2 : |
| 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 | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2652 | /* Note that we have the same problem with the response ; we |
| 2653 | * may want to send a redirect, error or anything which requires |
| 2654 | * some spare space. So we'll ensure that we have at least |
| 2655 | * maxrewrite bytes available in the response buffer before |
| 2656 | * processing that one. This will only affect pipelined |
| 2657 | * keep-alive requests. |
| 2658 | */ |
| 2659 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2660 | unlikely(!channel_is_rewritable(&s->res) || |
| 2661 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2662 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2663 | if (s->res.buf->o) { |
| 2664 | if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2665 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2666 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2667 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2668 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2669 | s->res.flags |= CF_WAKE_WRITE; |
| 2670 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2671 | return 0; |
| 2672 | } |
| 2673 | } |
| 2674 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2675 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2676 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2679 | /* 1: we might have to print this header in debug mode */ |
| 2680 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2681 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2682 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2683 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2684 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2685 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2686 | /* this is a bit complex : in case of error on the request line, |
| 2687 | * we know that rq.l is still zero, so we display only the part |
| 2688 | * up to the end of the line (truncated by debug_hdr). |
| 2689 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2690 | 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] | 2691 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2692 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2693 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2694 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2695 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | while (cur_idx) { |
| 2697 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2698 | debug_hdr("clihdr", s, sol, eol); |
| 2699 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2700 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2701 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | } |
| 2703 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2705 | /* |
| 2706 | * Now we quickly check if we have found a full valid request. |
| 2707 | * If not so, we check the FD and buffer states before leaving. |
| 2708 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2709 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2710 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2711 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2712 | * 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] | 2713 | * 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] | 2714 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2715 | * Last, we may increase some tracked counters' http request errors on |
| 2716 | * the cases that are deliberately the client's fault. For instance, |
| 2717 | * a timeout or connection reset is not counted as an error. However |
| 2718 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2719 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2720 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2721 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2722 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2724 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2725 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2726 | stream_inc_http_req_ctr(s); |
| 2727 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2728 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2729 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2730 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | /* 1: Since we are in header mode, if there's no space |
| 2733 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2734 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2735 | * must terminate it now. |
| 2736 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2737 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2738 | /* FIXME: check if URI is set and return Status |
| 2739 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2740 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2741 | stream_inc_http_req_ctr(s); |
| 2742 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2743 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2744 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2745 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2746 | goto return_bad_req; |
| 2747 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2748 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2749 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2750 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2751 | if (!(s->flags & SF_ERR_MASK)) |
| 2752 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2753 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2754 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2755 | goto failed_keep_alive; |
| 2756 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2757 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2758 | goto failed_keep_alive; |
| 2759 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2760 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2761 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2762 | 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] | 2763 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2764 | } |
| 2765 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2766 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2767 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2768 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2769 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2770 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2771 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2772 | proxy_inc_fe_req_ctr(sess->fe); |
| 2773 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2774 | if (sess->listener->counters) |
| 2775 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2776 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2777 | if (!(s->flags & SF_FINST_MASK)) |
| 2778 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2779 | return 0; |
| 2780 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2781 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2782 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2783 | 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] | 2784 | if (!(s->flags & SF_ERR_MASK)) |
| 2785 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2786 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2787 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2788 | goto failed_keep_alive; |
| 2789 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2790 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2791 | goto failed_keep_alive; |
| 2792 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2793 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2794 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2795 | 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] | 2796 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2797 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2798 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2799 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2801 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2802 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2803 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2804 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2805 | proxy_inc_fe_req_ctr(sess->fe); |
| 2806 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2807 | if (sess->listener->counters) |
| 2808 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2809 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2810 | if (!(s->flags & SF_FINST_MASK)) |
| 2811 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2812 | return 0; |
| 2813 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2814 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2815 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2816 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2817 | if (!(s->flags & SF_ERR_MASK)) |
| 2818 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2820 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2821 | goto failed_keep_alive; |
| 2822 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2823 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2824 | goto failed_keep_alive; |
| 2825 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2826 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2827 | 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] | 2828 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2829 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2830 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2831 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2832 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2833 | stream_inc_http_err_ctr(s); |
| 2834 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2835 | proxy_inc_fe_req_ctr(sess->fe); |
| 2836 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2837 | if (sess->listener->counters) |
| 2838 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2839 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2840 | if (!(s->flags & SF_FINST_MASK)) |
| 2841 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2842 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2843 | } |
| 2844 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2845 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2846 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2847 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2848 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2849 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2850 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2851 | /* We need more data, we have to re-enable quick-ack in case we |
| 2852 | * previously disabled it, otherwise we might cause the client |
| 2853 | * to delay next data. |
| 2854 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2855 | 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] | 2856 | } |
| 2857 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2858 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2859 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2860 | /* If the client starts to talk, let's fall back to |
| 2861 | * request timeout processing. |
| 2862 | */ |
| 2863 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2864 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2865 | } |
| 2866 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2867 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2868 | if (!tick_isset(req->analyse_exp)) { |
| 2869 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2870 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2871 | tick_isset(s->be->timeout.httpka)) |
| 2872 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2873 | else |
| 2874 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2875 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2876 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2877 | /* we're not ready yet */ |
| 2878 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2879 | |
| 2880 | failed_keep_alive: |
| 2881 | /* Here we process low-level errors for keep-alive requests. In |
| 2882 | * short, if the request is not the first one and it experiences |
| 2883 | * a timeout, read error or shutdown, we just silently close so |
| 2884 | * that the client can try again. |
| 2885 | */ |
| 2886 | txn->status = 0; |
| 2887 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2888 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2889 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2890 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2891 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2892 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2893 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2894 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2895 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2896 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2897 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2898 | * 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] | 2899 | * 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] | 2900 | * 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] | 2901 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2902 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2903 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2904 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2905 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2906 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2907 | 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] | 2908 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2909 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2910 | /* kill the pending keep-alive timeout */ |
| 2911 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2912 | req->analyse_exp = TICK_ETERNITY; |
| 2913 | } |
| 2914 | |
| 2915 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2916 | /* Maybe we found in invalid header name while we were configured not |
| 2917 | * to block on that, so we have to capture it now. |
| 2918 | */ |
| 2919 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2920 | 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] | 2921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2922 | /* |
| 2923 | * 1: identify the method |
| 2924 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2925 | 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] | 2926 | |
| 2927 | /* we can make use of server redirect on GET and HEAD */ |
| 2928 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2929 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2930 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2931 | /* |
| 2932 | * 2: check if the URI matches the monitor_uri. |
| 2933 | * We have to do this for every request which gets in, because |
| 2934 | * the monitor-uri is defined by the frontend. |
| 2935 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2936 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2937 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2938 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2939 | sess->fe->monitor_uri, |
| 2940 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2941 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2942 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2943 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2944 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2945 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2946 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2947 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2948 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2949 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2950 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2951 | 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] | 2952 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2953 | ret = acl_pass(ret); |
| 2954 | if (cond->pol == ACL_COND_UNLESS) |
| 2955 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2956 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2957 | if (ret) { |
| 2958 | /* we fail this request, let's return 503 service unavail */ |
| 2959 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2960 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2961 | if (!(s->flags & SF_ERR_MASK)) |
| 2962 | 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] | 2963 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2964 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2965 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2966 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2967 | /* nothing to fail, let's reply normaly */ |
| 2968 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2969 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2970 | if (!(s->flags & SF_ERR_MASK)) |
| 2971 | 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] | 2972 | goto return_prx_cond; |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2977 | * Note: we cannot log anymore if the request has been |
| 2978 | * classified as invalid. |
| 2979 | */ |
| 2980 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2981 | /* we have a complete HTTP request that we must log */ |
| 2982 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2983 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2984 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2985 | if (urilen >= REQURI_LEN) |
| 2986 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2987 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2988 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2989 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2990 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2991 | s->do_log(s); |
| 2992 | } else { |
| 2993 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2994 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2995 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2996 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2997 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 2998 | * exactly one digit "." one digit. This check may be disabled using |
| 2999 | * option accept-invalid-http-request. |
| 3000 | */ |
| 3001 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 3002 | if (msg->sl.rq.v_l != 8) { |
| 3003 | msg->err_pos = msg->sl.rq.v; |
| 3004 | goto return_bad_req; |
| 3005 | } |
| 3006 | |
| 3007 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 3008 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 3009 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 3010 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 3011 | msg->err_pos = msg->sl.rq.v + 4; |
| 3012 | goto return_bad_req; |
| 3013 | } |
| 3014 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 3015 | else { |
| 3016 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 3017 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 3018 | goto return_bad_req; |
| 3019 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3020 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3021 | /* ... and check if the request is HTTP/1.1 or above */ |
| 3022 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3023 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 3024 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 3025 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3026 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3027 | |
| 3028 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3029 | 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] | 3030 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3031 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 3032 | * we have what looks like a proxied connection instead of a connection, |
| 3033 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 3034 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 3035 | * happen to do that despite being non-standard :-( |
| 3036 | * We consider that a request not beginning with either '/' or '*' is |
| 3037 | * a proxied connection, which covers both "scheme://location" and |
| 3038 | * CONNECT ip:port. |
| 3039 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3040 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3041 | 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] | 3042 | txn->flags |= TX_USE_PX_CONN; |
| 3043 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3044 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3045 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3046 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3047 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3048 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3049 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3050 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3051 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3052 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3053 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3054 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3055 | * The length of a message body is determined by one of the following |
| 3056 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3057 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3058 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3059 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3060 | * code is always terminated by the first empty line after the |
| 3061 | * header fields, regardless of the header fields present in the |
| 3062 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3063 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3064 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3065 | * the connection will become a tunnel immediately after the empty |
| 3066 | * line that concludes the header fields. A client MUST ignore any |
| 3067 | * Content-Length or Transfer-Encoding header fields received in |
| 3068 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3069 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3070 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3071 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3072 | * body length is determined by reading and decoding the chunked |
| 3073 | * data until the transfer coding indicates the data is complete. |
| 3074 | * |
| 3075 | * If a Transfer-Encoding header field is present in a response and |
| 3076 | * the chunked transfer coding is not the final encoding, the |
| 3077 | * message body length is determined by reading the connection until |
| 3078 | * it is closed by the server. If a Transfer-Encoding header field |
| 3079 | * is present in a request and the chunked transfer coding is not |
| 3080 | * the final encoding, the message body length cannot be determined |
| 3081 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3082 | * status code and then close the connection. |
| 3083 | * |
| 3084 | * If a message is received with both a Transfer-Encoding and a |
| 3085 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3086 | * Content-Length. Such a message might indicate an attempt to |
| 3087 | * perform request smuggling (Section 9.5) or response splitting |
| 3088 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3089 | * remove the received Content-Length field prior to forwarding such |
| 3090 | * a message downstream. |
| 3091 | * |
| 3092 | * 4. If a message is received without Transfer-Encoding and with |
| 3093 | * either multiple Content-Length header fields having differing |
| 3094 | * field-values or a single Content-Length header field having an |
| 3095 | * invalid value, then the message framing is invalid and the |
| 3096 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3097 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3098 | * status code and then close the connection. If this is a response |
| 3099 | * message received by a proxy, the proxy MUST close the connection |
| 3100 | * to the server, discard the received response, and send a 502 (Bad |
| 3101 | * Gateway) response to the client. If this is a response message |
| 3102 | * received by a user agent, the user agent MUST close the |
| 3103 | * connection to the server and discard the received response. |
| 3104 | * |
| 3105 | * 5. If a valid Content-Length header field is present without |
| 3106 | * Transfer-Encoding, its decimal value defines the expected message |
| 3107 | * body length in octets. If the sender closes the connection or |
| 3108 | * the recipient times out before the indicated number of octets are |
| 3109 | * received, the recipient MUST consider the message to be |
| 3110 | * incomplete and close the connection. |
| 3111 | * |
| 3112 | * 6. If this is a request message and none of the above are true, then |
| 3113 | * the message body length is zero (no message body is present). |
| 3114 | * |
| 3115 | * 7. Otherwise, this is a response message without a declared message |
| 3116 | * body length, so the message body length is determined by the |
| 3117 | * number of octets received prior to the server closing the |
| 3118 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3119 | */ |
| 3120 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3121 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3122 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3123 | 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] | 3124 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3125 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3126 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3127 | /* chunked not last, return badreq */ |
| 3128 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3129 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3130 | } |
| 3131 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3132 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3133 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3134 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3135 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3136 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3137 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3138 | 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] | 3139 | signed long long cl; |
| 3140 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3141 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3142 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3143 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3144 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3145 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3146 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3147 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3148 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3149 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3150 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3151 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3152 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3153 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3154 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3155 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3156 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3157 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3158 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3159 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3160 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3161 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3162 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3163 | } |
| 3164 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3165 | /* even bodyless requests have a known length */ |
| 3166 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3167 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3168 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3169 | * only change if both the request and the config reference something else. |
| 3170 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3171 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3172 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3173 | * if FE and BE have the same settings (common). The method consists in |
| 3174 | * checking if options changed between the two calls (implying that either |
| 3175 | * one is non-null, or one of them is non-null and we are there for the first |
| 3176 | * time. |
| 3177 | */ |
| 3178 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3179 | ((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] | 3180 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3181 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3182 | /* we may have to wait for the request's body */ |
| 3183 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3184 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3185 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3186 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3187 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3188 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3189 | req->analyse_exp = TICK_ETERNITY; |
| 3190 | return 1; |
| 3191 | |
| 3192 | return_bad_req: |
| 3193 | /* We centralize bad requests processing here */ |
| 3194 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3195 | /* we detected a parsing error. We want to archive this request |
| 3196 | * in the dedicated proxy area for later troubleshooting. |
| 3197 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3198 | 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] | 3199 | } |
| 3200 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3201 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3202 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3203 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3204 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3205 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3206 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3207 | if (sess->listener->counters) |
| 3208 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3209 | |
| 3210 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3211 | if (!(s->flags & SF_ERR_MASK)) |
| 3212 | s->flags |= SF_ERR_PRXCOND; |
| 3213 | if (!(s->flags & SF_FINST_MASK)) |
| 3214 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3215 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3216 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3217 | req->analyse_exp = TICK_ETERNITY; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3221 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3222 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3223 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3224 | * and program a response message if something was unexpected. It cannot fail |
| 3225 | * 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] | 3226 | * 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] | 3227 | * 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] | 3228 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3229 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3230 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3231 | { |
| 3232 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3233 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3234 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3235 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3236 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3237 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3238 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3239 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3240 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3241 | appctx = si_appctx(si); |
| 3242 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3243 | appctx->st1 = appctx->st2 = 0; |
| 3244 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3245 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3246 | 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] | 3247 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3248 | |
| 3249 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3250 | lookup = uri + uri_auth->uri_len; |
| 3251 | |
| 3252 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3253 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3254 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3255 | break; |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | if (uri_auth->refresh) { |
| 3260 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3261 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3262 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3263 | break; |
| 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3269 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3270 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3271 | break; |
| 3272 | } |
| 3273 | } |
| 3274 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 3275 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 3276 | if (memcmp(h, ";typed", 6) == 0) { |
| 3277 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 3278 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3279 | break; |
| 3280 | } |
| 3281 | } |
| 3282 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3283 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3284 | if (memcmp(h, ";st=", 4) == 0) { |
| 3285 | int i; |
| 3286 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3287 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3288 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3289 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3290 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | break; |
| 3295 | } |
| 3296 | } |
| 3297 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3298 | appctx->ctx.stats.scope_str = 0; |
| 3299 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3300 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3301 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3302 | int itx = 0; |
| 3303 | const char *h2; |
| 3304 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3305 | const char *err; |
| 3306 | |
| 3307 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3308 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3309 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3310 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3311 | itx++; |
| 3312 | h++; |
| 3313 | } |
| 3314 | |
| 3315 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3316 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3317 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3318 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3319 | /* 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] | 3320 | memcpy(scope_txt, h2, itx); |
| 3321 | scope_txt[itx] = '\0'; |
| 3322 | err = invalid_char(scope_txt); |
| 3323 | if (err) { |
| 3324 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3325 | appctx->ctx.stats.scope_str = 0; |
| 3326 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3327 | } |
| 3328 | break; |
| 3329 | } |
| 3330 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3331 | |
| 3332 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3333 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3334 | int ret = 1; |
| 3335 | |
| 3336 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3337 | 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] | 3338 | ret = acl_pass(ret); |
| 3339 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3340 | ret = !ret; |
| 3341 | } |
| 3342 | |
| 3343 | if (ret) { |
| 3344 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3345 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3351 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3352 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3353 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3354 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3355 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3356 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3357 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3358 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3359 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3360 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3361 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3362 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3363 | else { |
| 3364 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3365 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3366 | } |
| 3367 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3368 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3369 | return 1; |
| 3370 | } |
| 3371 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3372 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3373 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3374 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3375 | 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] | 3376 | { |
| 3377 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3378 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3379 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3380 | #endif |
| 3381 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3382 | if (from->ss_family == AF_INET6) { |
| 3383 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3384 | /* v4-mapped addresses need IP_TOS */ |
| 3385 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3386 | else |
| 3387 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3388 | } |
| 3389 | #endif |
| 3390 | } |
| 3391 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3392 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3393 | const char* name, unsigned int name_len, |
| 3394 | const char *str, struct my_regex *re, |
| 3395 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3396 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3397 | struct hdr_ctx ctx; |
| 3398 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3399 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3400 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3401 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3402 | struct chunk *output = get_trash_chunk(); |
| 3403 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3404 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3405 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3406 | /* Choose the header browsing function. */ |
| 3407 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3408 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3409 | http_find_hdr_func = http_find_header2; |
| 3410 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3411 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3412 | http_find_hdr_func = http_find_full_header2; |
| 3413 | break; |
| 3414 | default: /* impossible */ |
| 3415 | return -1; |
| 3416 | } |
| 3417 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3418 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3419 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3420 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3421 | char *val = ctx.line + ctx.val; |
| 3422 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3423 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3424 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3425 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3426 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3427 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3428 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3429 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3430 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3431 | 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] | 3432 | |
| 3433 | hdr->len += delta; |
| 3434 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3435 | |
| 3436 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3437 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | return 0; |
| 3441 | } |
| 3442 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3443 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3444 | const char* name, unsigned int name_len, |
| 3445 | struct list *fmt, struct my_regex *re, |
| 3446 | int action) |
| 3447 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3448 | struct chunk *replace; |
| 3449 | int ret = -1; |
| 3450 | |
| 3451 | replace = alloc_trash_chunk(); |
| 3452 | if (!replace) |
| 3453 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3454 | |
| 3455 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3456 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3457 | goto leave; |
| 3458 | |
| 3459 | 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] | 3460 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 3461 | leave: |
| 3462 | free_trash_chunk(replace); |
| 3463 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3464 | } |
| 3465 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3466 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3467 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3468 | * further processing of the request (auth, deny, ...), and defaults to |
| 3469 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3470 | * 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] | 3471 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 3472 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 3473 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3474 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3475 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3476 | 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] | 3477 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3478 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3479 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3480 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3481 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3482 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3483 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3484 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3485 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3486 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3487 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3488 | * resume condition. If a resume is needed it is always in the action |
| 3489 | * and never in the ACL or converters. In this case, we initialise the |
| 3490 | * current rule, and go to the action execution point. |
| 3491 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3492 | if (s->current_rule) { |
| 3493 | rule = s->current_rule; |
| 3494 | s->current_rule = NULL; |
| 3495 | if (s->current_rule_list == rules) |
| 3496 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3497 | } |
| 3498 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3499 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3500 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3501 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3502 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3503 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3504 | int ret; |
| 3505 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3506 | 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] | 3507 | ret = acl_pass(ret); |
| 3508 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3509 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3510 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3511 | |
| 3512 | if (!ret) /* condition not matched */ |
| 3513 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3514 | } |
| 3515 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3516 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3517 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3518 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3519 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3520 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3521 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3522 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3523 | if (deny_status) |
| 3524 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3525 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3526 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3527 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3528 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3529 | if (deny_status) |
| 3530 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3531 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3532 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3533 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3534 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3535 | auth_realm = rule->arg.auth.realm; |
| 3536 | if (!auth_realm) { |
| 3537 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3538 | auth_realm = STATS_DEFAULT_REALM; |
| 3539 | else |
| 3540 | auth_realm = px->id; |
| 3541 | } |
| 3542 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3543 | * count one error, because normal browsing won't significantly |
| 3544 | * increase the counter but brute force attempts will. |
| 3545 | */ |
| 3546 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3547 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 3548 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3549 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3550 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3551 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3552 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3553 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3554 | return HTTP_RULE_RES_BADREQ; |
| 3555 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3556 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3557 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3558 | s->task->nice = rule->arg.nice; |
| 3559 | break; |
| 3560 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3561 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3562 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3563 | 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] | 3564 | break; |
| 3565 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3566 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3567 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3568 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3569 | 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] | 3570 | #endif |
| 3571 | break; |
| 3572 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3573 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3574 | s->logs.level = rule->arg.loglevel; |
| 3575 | break; |
| 3576 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3577 | case ACT_HTTP_REPLACE_HDR: |
| 3578 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3579 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3580 | rule->arg.hdr_add.name_len, |
| 3581 | &rule->arg.hdr_add.fmt, |
| 3582 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3583 | return HTTP_RULE_RES_BADREQ; |
| 3584 | break; |
| 3585 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3586 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3587 | ctx.idx = 0; |
| 3588 | /* remove all occurrences of the header */ |
| 3589 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3590 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3591 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3592 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3593 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3594 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3595 | case ACT_HTTP_SET_HDR: |
| 3596 | case ACT_HTTP_ADD_HDR: |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 3597 | /* The scope of the trash buffer must be limited to this function. The |
| 3598 | * build_logline() function can execute a lot of other function which |
| 3599 | * can use the trash buffer. So for limiting the scope of this global |
| 3600 | * buffer, we build first the header value using build_logline, and |
| 3601 | * after we store the header name. |
| 3602 | */ |
| 3603 | len = rule->arg.hdr_add.name_len + 2, |
| 3604 | 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] | 3605 | 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] | 3606 | trash.str[rule->arg.hdr_add.name_len] = ':'; |
| 3607 | trash.str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 3608 | trash.len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3609 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3610 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3611 | /* remove all occurrences of the header */ |
| 3612 | ctx.idx = 0; |
| 3613 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3614 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3615 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3616 | } |
| 3617 | } |
| 3618 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3619 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3620 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3621 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3622 | case ACT_HTTP_DEL_ACL: |
| 3623 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3624 | struct pat_ref *ref; |
| 3625 | char *key; |
| 3626 | int len; |
| 3627 | |
| 3628 | /* collect reference */ |
| 3629 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3630 | if (!ref) |
| 3631 | continue; |
| 3632 | |
| 3633 | /* collect key */ |
| 3634 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3635 | key = trash.str; |
| 3636 | key[len] = '\0'; |
| 3637 | |
| 3638 | /* perform update */ |
| 3639 | /* returned code: 1=ok, 0=ko */ |
| 3640 | pat_ref_delete(ref, key); |
| 3641 | |
| 3642 | break; |
| 3643 | } |
| 3644 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3645 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3646 | struct pat_ref *ref; |
| 3647 | char *key; |
| 3648 | struct chunk *trash_key; |
| 3649 | int len; |
| 3650 | |
| 3651 | trash_key = get_trash_chunk(); |
| 3652 | |
| 3653 | /* collect reference */ |
| 3654 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3655 | if (!ref) |
| 3656 | continue; |
| 3657 | |
| 3658 | /* collect key */ |
| 3659 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3660 | key = trash_key->str; |
| 3661 | key[len] = '\0'; |
| 3662 | |
| 3663 | /* perform update */ |
| 3664 | /* add entry only if it does not already exist */ |
| 3665 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3666 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3667 | |
| 3668 | break; |
| 3669 | } |
| 3670 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3671 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3672 | struct pat_ref *ref; |
| 3673 | char *key, *value; |
| 3674 | struct chunk *trash_key, *trash_value; |
| 3675 | int len; |
| 3676 | |
| 3677 | trash_key = get_trash_chunk(); |
| 3678 | trash_value = get_trash_chunk(); |
| 3679 | |
| 3680 | /* collect reference */ |
| 3681 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3682 | if (!ref) |
| 3683 | continue; |
| 3684 | |
| 3685 | /* collect key */ |
| 3686 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3687 | key = trash_key->str; |
| 3688 | key[len] = '\0'; |
| 3689 | |
| 3690 | /* collect value */ |
| 3691 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3692 | value = trash_value->str; |
| 3693 | value[len] = '\0'; |
| 3694 | |
| 3695 | /* perform update */ |
| 3696 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3697 | /* update entry if it exists */ |
| 3698 | pat_ref_set(ref, key, value, NULL); |
| 3699 | else |
| 3700 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3701 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3702 | |
| 3703 | break; |
| 3704 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3705 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3706 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3707 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3708 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3709 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3710 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3711 | case ACT_RET_ERR: |
| 3712 | case ACT_RET_CONT: |
| 3713 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3714 | case ACT_RET_STOP: |
| 3715 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3716 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3717 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3718 | return HTTP_RULE_RES_YIELD; |
| 3719 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3720 | break; |
| 3721 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3722 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3723 | /* Note: only the first valid tracking parameter of each |
| 3724 | * applies. |
| 3725 | */ |
| 3726 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3727 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3728 | struct stktable *t; |
| 3729 | struct stksess *ts; |
| 3730 | struct stktable_key *key; |
| 3731 | void *ptr; |
| 3732 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3733 | t = rule->arg.trk_ctr.table.t; |
| 3734 | 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] | 3735 | |
| 3736 | if (key && (ts = stktable_get_entry(t, key))) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3737 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3738 | |
| 3739 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3740 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3741 | if (ptr) |
| 3742 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3743 | |
| 3744 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3745 | if (ptr) |
| 3746 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3747 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3748 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3749 | 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] | 3750 | if (sess->fe != s->be) |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3751 | 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] | 3752 | } |
| 3753 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3754 | break; |
| 3755 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3756 | /* other flags exists, but normaly, they never be matched. */ |
| 3757 | default: |
| 3758 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3759 | } |
| 3760 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3761 | |
| 3762 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3763 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3764 | } |
| 3765 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3766 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3767 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3768 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3769 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3770 | * is returned, the process can continue the evaluation of next rule list. If |
| 3771 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3772 | * is returned, it means the operation could not be processed and a server error |
| 3773 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3774 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3775 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3776 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3777 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3778 | 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] | 3779 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3780 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3781 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3782 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3783 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3784 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3785 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3786 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3787 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3788 | * resume condition. If a resume is needed it is always in the action |
| 3789 | * and never in the ACL or converters. In this case, we initialise the |
| 3790 | * current rule, and go to the action execution point. |
| 3791 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3792 | if (s->current_rule) { |
| 3793 | rule = s->current_rule; |
| 3794 | s->current_rule = NULL; |
| 3795 | if (s->current_rule_list == rules) |
| 3796 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3797 | } |
| 3798 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3799 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3800 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3801 | |
| 3802 | /* check optional condition */ |
| 3803 | if (rule->cond) { |
| 3804 | int ret; |
| 3805 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3806 | 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] | 3807 | ret = acl_pass(ret); |
| 3808 | |
| 3809 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3810 | ret = !ret; |
| 3811 | |
| 3812 | if (!ret) /* condition not matched */ |
| 3813 | continue; |
| 3814 | } |
| 3815 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3816 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3817 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3818 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3819 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3820 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3821 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3822 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3823 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3824 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3825 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3826 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3827 | s->task->nice = rule->arg.nice; |
| 3828 | break; |
| 3829 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3830 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3831 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 3832 | 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] | 3833 | break; |
| 3834 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3835 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3836 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3837 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3838 | 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] | 3839 | #endif |
| 3840 | break; |
| 3841 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3842 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3843 | s->logs.level = rule->arg.loglevel; |
| 3844 | break; |
| 3845 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3846 | case ACT_HTTP_REPLACE_HDR: |
| 3847 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3848 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3849 | rule->arg.hdr_add.name_len, |
| 3850 | &rule->arg.hdr_add.fmt, |
| 3851 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 3852 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3853 | break; |
| 3854 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3855 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3856 | ctx.idx = 0; |
| 3857 | /* remove all occurrences of the header */ |
| 3858 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3859 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3860 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3861 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3862 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3863 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3864 | case ACT_HTTP_SET_HDR: |
| 3865 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3866 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3867 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3868 | trash.len = rule->arg.hdr_add.name_len; |
| 3869 | trash.str[trash.len++] = ':'; |
| 3870 | trash.str[trash.len++] = ' '; |
| 3871 | 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] | 3872 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3873 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3874 | /* remove all occurrences of the header */ |
| 3875 | ctx.idx = 0; |
| 3876 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3877 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3878 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3879 | } |
| 3880 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3881 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3882 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3883 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3884 | case ACT_HTTP_DEL_ACL: |
| 3885 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3886 | struct pat_ref *ref; |
| 3887 | char *key; |
| 3888 | int len; |
| 3889 | |
| 3890 | /* collect reference */ |
| 3891 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3892 | if (!ref) |
| 3893 | continue; |
| 3894 | |
| 3895 | /* collect key */ |
| 3896 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3897 | key = trash.str; |
| 3898 | key[len] = '\0'; |
| 3899 | |
| 3900 | /* perform update */ |
| 3901 | /* returned code: 1=ok, 0=ko */ |
| 3902 | pat_ref_delete(ref, key); |
| 3903 | |
| 3904 | break; |
| 3905 | } |
| 3906 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3907 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3908 | struct pat_ref *ref; |
| 3909 | char *key; |
| 3910 | struct chunk *trash_key; |
| 3911 | int len; |
| 3912 | |
| 3913 | trash_key = get_trash_chunk(); |
| 3914 | |
| 3915 | /* collect reference */ |
| 3916 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3917 | if (!ref) |
| 3918 | continue; |
| 3919 | |
| 3920 | /* collect key */ |
| 3921 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3922 | key = trash_key->str; |
| 3923 | key[len] = '\0'; |
| 3924 | |
| 3925 | /* perform update */ |
| 3926 | /* check if the entry already exists */ |
| 3927 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3928 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3929 | |
| 3930 | break; |
| 3931 | } |
| 3932 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3933 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3934 | struct pat_ref *ref; |
| 3935 | char *key, *value; |
| 3936 | struct chunk *trash_key, *trash_value; |
| 3937 | int len; |
| 3938 | |
| 3939 | trash_key = get_trash_chunk(); |
| 3940 | trash_value = get_trash_chunk(); |
| 3941 | |
| 3942 | /* collect reference */ |
| 3943 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3944 | if (!ref) |
| 3945 | continue; |
| 3946 | |
| 3947 | /* collect key */ |
| 3948 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3949 | key = trash_key->str; |
| 3950 | key[len] = '\0'; |
| 3951 | |
| 3952 | /* collect value */ |
| 3953 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3954 | value = trash_value->str; |
| 3955 | value[len] = '\0'; |
| 3956 | |
| 3957 | /* perform update */ |
| 3958 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3959 | /* update entry if it exists */ |
| 3960 | pat_ref_set(ref, key, value, NULL); |
| 3961 | else |
| 3962 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3963 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3964 | |
| 3965 | break; |
| 3966 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3967 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3968 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3969 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3970 | return HTTP_RULE_RES_BADREQ; |
| 3971 | return HTTP_RULE_RES_DONE; |
| 3972 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3973 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3974 | /* Note: only the first valid tracking parameter of each |
| 3975 | * applies. |
| 3976 | */ |
| 3977 | |
| 3978 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
| 3979 | struct stktable *t; |
| 3980 | struct stksess *ts; |
| 3981 | struct stktable_key *key; |
| 3982 | void *ptr; |
| 3983 | |
| 3984 | t = rule->arg.trk_ctr.table.t; |
| 3985 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3986 | |
| 3987 | if (key && (ts = stktable_get_entry(t, key))) { |
| 3988 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
| 3989 | |
| 3990 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3991 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3992 | if (ptr) |
| 3993 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3994 | |
| 3995 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3996 | if (ptr) |
| 3997 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3998 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3999 | |
| 4000 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 4001 | if (sess->fe != s->be) |
| 4002 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 4003 | |
| 4004 | /* When the client triggers a 4xx from the server, it's most often due |
| 4005 | * to a missing object or permission. These events should be tracked |
| 4006 | * because if they happen often, it may indicate a brute force or a |
| 4007 | * vulnerability scan. Normally this is done when receiving the response |
| 4008 | * but here we're tracking after this ought to have been done so we have |
| 4009 | * to do it on purpose. |
| 4010 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 4011 | if ((unsigned)(txn->status - 400) < 100) { |
| 4012 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 4013 | if (ptr) |
| 4014 | stktable_data_cast(ptr, http_err_cnt)++; |
| 4015 | |
| 4016 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 4017 | if (ptr) |
| 4018 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 4019 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 4020 | } |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | break; |
| 4024 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4025 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4026 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 4027 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 4028 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4029 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4030 | case ACT_RET_ERR: |
| 4031 | case ACT_RET_CONT: |
| 4032 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4033 | case ACT_RET_STOP: |
| 4034 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4035 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 4036 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4037 | return HTTP_RULE_RES_YIELD; |
| 4038 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 4039 | break; |
| 4040 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 4041 | /* other flags exists, but normaly, they never be matched. */ |
| 4042 | default: |
| 4043 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4048 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4052 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 4053 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 4054 | * as too large a request to build a valid response. |
| 4055 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4056 | 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] | 4057 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4058 | struct http_msg *req = &txn->req; |
| 4059 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4060 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4061 | struct chunk *chunk; |
| 4062 | int ret = 0; |
| 4063 | |
| 4064 | chunk = alloc_trash_chunk(); |
| 4065 | if (!chunk) |
| 4066 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4067 | |
| 4068 | /* build redirect message */ |
| 4069 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4070 | case 308: |
| 4071 | msg_fmt = HTTP_308; |
| 4072 | break; |
| 4073 | case 307: |
| 4074 | msg_fmt = HTTP_307; |
| 4075 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4076 | case 303: |
| 4077 | msg_fmt = HTTP_303; |
| 4078 | break; |
| 4079 | case 301: |
| 4080 | msg_fmt = HTTP_301; |
| 4081 | break; |
| 4082 | case 302: |
| 4083 | default: |
| 4084 | msg_fmt = HTTP_302; |
| 4085 | break; |
| 4086 | } |
| 4087 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4088 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 4089 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4090 | |
| 4091 | switch(rule->type) { |
| 4092 | case REDIRECT_TYPE_SCHEME: { |
| 4093 | const char *path; |
| 4094 | const char *host; |
| 4095 | struct hdr_ctx ctx; |
| 4096 | int pathlen; |
| 4097 | int hostlen; |
| 4098 | |
| 4099 | host = ""; |
| 4100 | hostlen = 0; |
| 4101 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4102 | 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] | 4103 | host = ctx.line + ctx.val; |
| 4104 | hostlen = ctx.vlen; |
| 4105 | } |
| 4106 | |
| 4107 | path = http_get_path(txn); |
| 4108 | /* build message using path */ |
| 4109 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4110 | 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] | 4111 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4112 | int qs = 0; |
| 4113 | while (qs < pathlen) { |
| 4114 | if (path[qs] == '?') { |
| 4115 | pathlen = qs; |
| 4116 | break; |
| 4117 | } |
| 4118 | qs++; |
| 4119 | } |
| 4120 | } |
| 4121 | } else { |
| 4122 | path = "/"; |
| 4123 | pathlen = 1; |
| 4124 | } |
| 4125 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4126 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4127 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4128 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 4129 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4130 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4131 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4132 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4133 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4134 | } |
| 4135 | else { |
| 4136 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4137 | 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] | 4138 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4139 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4140 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 4141 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4142 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4143 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4144 | memcpy(chunk->str + chunk->len, "://", 3); |
| 4145 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4146 | |
| 4147 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4148 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 4149 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4150 | |
| 4151 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4152 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 4153 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4154 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4155 | /* 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] | 4156 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4157 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4158 | if (chunk->len > chunk->size - 5) |
| 4159 | goto leave; |
| 4160 | chunk->str[chunk->len] = '/'; |
| 4161 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | break; |
| 4165 | } |
| 4166 | case REDIRECT_TYPE_PREFIX: { |
| 4167 | const char *path; |
| 4168 | int pathlen; |
| 4169 | |
| 4170 | path = http_get_path(txn); |
| 4171 | /* build message using path */ |
| 4172 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4173 | 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] | 4174 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4175 | int qs = 0; |
| 4176 | while (qs < pathlen) { |
| 4177 | if (path[qs] == '?') { |
| 4178 | pathlen = qs; |
| 4179 | break; |
| 4180 | } |
| 4181 | qs++; |
| 4182 | } |
| 4183 | } |
| 4184 | } else { |
| 4185 | path = "/"; |
| 4186 | pathlen = 1; |
| 4187 | } |
| 4188 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4189 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4190 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 4191 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4192 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4193 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4194 | * add anything, otherwise it makes it hard for the user to |
| 4195 | * configure a self-redirection. |
| 4196 | */ |
| 4197 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4198 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4199 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | else { |
| 4203 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4204 | 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] | 4205 | |
| 4206 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4207 | if (chunk->len + pathlen > chunk->size - 4) |
| 4208 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4212 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 4213 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4214 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4215 | /* 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] | 4216 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4217 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4218 | if (chunk->len > chunk->size - 5) |
| 4219 | goto leave; |
| 4220 | chunk->str[chunk->len] = '/'; |
| 4221 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | break; |
| 4225 | } |
| 4226 | case REDIRECT_TYPE_LOCATION: |
| 4227 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4228 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4229 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 4230 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4231 | |
| 4232 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4233 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 4234 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4235 | } |
| 4236 | else { |
| 4237 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4238 | 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] | 4239 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4240 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4241 | if (chunk->len > chunk->size - 4) |
| 4242 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4243 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4244 | break; |
| 4245 | } |
| 4246 | |
| 4247 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4248 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 4249 | chunk->len += 14; |
| 4250 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 4251 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4252 | } |
| 4253 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 4254 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4255 | txn->status = rule->code; |
| 4256 | /* let's log the request time */ |
| 4257 | s->logs.tv_request = now; |
| 4258 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 4259 | if ((req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4260 | ((!(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] | 4261 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4262 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4263 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4264 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4265 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4266 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 4267 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4268 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4269 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 4270 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4271 | } |
| 4272 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4273 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 4274 | chunk->len += 4; |
| 4275 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
| 4276 | bo_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4277 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4278 | bi_fast_delete(req->chn->buf, req->sov); |
| 4279 | req->next -= req->sov; |
| 4280 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4281 | 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] | 4282 | 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] | 4283 | req->msg_state = HTTP_MSG_CLOSED; |
| 4284 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4285 | /* Trim any possible response */ |
| 4286 | res->chn->buf->i = 0; |
| 4287 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4288 | } else { |
| 4289 | /* keep-alive not possible */ |
| 4290 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4291 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4292 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4293 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4294 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 4295 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4296 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4297 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4298 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4301 | if (!(s->flags & SF_ERR_MASK)) |
| 4302 | s->flags |= SF_ERR_LOCAL; |
| 4303 | if (!(s->flags & SF_FINST_MASK)) |
| 4304 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4305 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 4306 | ret = 1; |
| 4307 | leave: |
| 4308 | free_trash_chunk(chunk); |
| 4309 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4310 | } |
| 4311 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4312 | /* This stream analyser runs all HTTP request processing which is common to |
| 4313 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4314 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4315 | * 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] | 4316 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4317 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4318 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4319 | 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] | 4320 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4321 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4322 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4323 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4324 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4325 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4326 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4327 | int deny_status = HTTP_ERR_403; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4328 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4329 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4330 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4331 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4332 | } |
| 4333 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4334 | 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] | 4335 | now_ms, __FUNCTION__, |
| 4336 | s, |
| 4337 | req, |
| 4338 | req->rex, req->wex, |
| 4339 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4340 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4341 | req->analysers); |
| 4342 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4343 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4344 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4345 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4346 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4347 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4348 | 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] | 4349 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4350 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4351 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4352 | goto return_prx_yield; |
| 4353 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4354 | case HTTP_RULE_RES_CONT: |
| 4355 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4356 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4357 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4358 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4359 | if (txn->flags & TX_CLTARPIT) |
| 4360 | goto tarpit; |
| 4361 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4362 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4363 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4364 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4365 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4366 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4367 | goto done; |
| 4368 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4369 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4370 | goto return_bad_req; |
| 4371 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4372 | } |
| 4373 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4374 | /* OK at this stage, we know that the request was accepted according to |
| 4375 | * the http-request rules, we can check for the stats. Note that the |
| 4376 | * URI is detected *before* the req* rules in order not to be affected |
| 4377 | * by a possible reqrep, while they are processed *after* so that a |
| 4378 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4379 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4380 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4381 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4382 | 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] | 4383 | txn->status = 500; |
| 4384 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4385 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4386 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4387 | if (!(s->flags & SF_ERR_MASK)) |
| 4388 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4389 | goto return_prx_cond; |
| 4390 | } |
| 4391 | |
| 4392 | /* parse the whole stats request and extract the relevant information */ |
| 4393 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4394 | 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] | 4395 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4396 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4397 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4398 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4399 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4400 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4401 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4402 | } |
| 4403 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4404 | /* evaluate the req* rules except reqadd */ |
| 4405 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4406 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4407 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4408 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4409 | if (txn->flags & TX_CLDENY) |
| 4410 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4411 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4412 | if (txn->flags & TX_CLTARPIT) { |
| 4413 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4414 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4415 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4416 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4417 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4418 | /* add request headers from the rule sets in the same order */ |
| 4419 | list_for_each_entry(wl, &px->req_add, list) { |
| 4420 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4421 | 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] | 4422 | ret = acl_pass(ret); |
| 4423 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4424 | ret = !ret; |
| 4425 | if (!ret) |
| 4426 | continue; |
| 4427 | } |
| 4428 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4429 | 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] | 4430 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4431 | } |
| 4432 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4433 | |
| 4434 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4435 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4436 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4437 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4438 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4439 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4440 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4441 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4442 | if (!(s->flags & SF_FINST_MASK)) |
| 4443 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4444 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4445 | /* 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] | 4446 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 4447 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4448 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4449 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4450 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4451 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4452 | /* check whether we have some ACLs set to redirect this request */ |
| 4453 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4454 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4455 | int ret; |
| 4456 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4457 | 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] | 4458 | ret = acl_pass(ret); |
| 4459 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4460 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4461 | if (!ret) |
| 4462 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4463 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4464 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4465 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4466 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4467 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4468 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4469 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4470 | * If this happens, then the data will not come immediately, so we must |
| 4471 | * send all what we have without waiting. Note that due to the small gain |
| 4472 | * 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] | 4473 | * 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] | 4474 | * itself once used. |
| 4475 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4476 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4477 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4478 | done: /* done with this analyser, continue with next ones that the calling |
| 4479 | * points will have set, if any. |
| 4480 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4481 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4482 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4483 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4484 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4485 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4486 | tarpit: |
| 4487 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4488 | * which may be the same as the connect timeout if unspecified. |
| 4489 | * If unset, then set it to zero because we really want it to |
| 4490 | * eventually expire. We build the tarpit as an analyser. |
| 4491 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4492 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4493 | |
| 4494 | /* wipe the request out so that we can drop the connection early |
| 4495 | * if the client closes first. |
| 4496 | */ |
| 4497 | channel_dont_connect(req); |
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 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 4504 | txn->status = http_err_codes[deny_status]; |
| 4505 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4506 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4507 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4508 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4509 | if (!req->analyse_exp) |
| 4510 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4511 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4512 | sess->fe->fe_counters.denied_req++; |
| 4513 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4514 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4515 | if (sess->listener->counters) |
| 4516 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4517 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4518 | |
| 4519 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4520 | |
| 4521 | /* Allow cookie logging |
| 4522 | */ |
| 4523 | if (s->be->cookie_name || sess->fe->capture_name) |
| 4524 | manage_client_side_cookies(s, req); |
| 4525 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4526 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 4527 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4528 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4529 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4530 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4531 | sess->fe->fe_counters.denied_req++; |
| 4532 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4533 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4534 | if (sess->listener->counters) |
| 4535 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4536 | goto return_prx_cond; |
| 4537 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4538 | return_bad_req: |
| 4539 | /* We centralize bad requests processing here */ |
| 4540 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4541 | /* we detected a parsing error. We want to archive this request |
| 4542 | * in the dedicated proxy area for later troubleshooting. |
| 4543 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4544 | 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] | 4545 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4546 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4547 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4548 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4549 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4550 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4551 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4552 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4553 | if (sess->listener->counters) |
| 4554 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4555 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4556 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4557 | if (!(s->flags & SF_ERR_MASK)) |
| 4558 | s->flags |= SF_ERR_PRXCOND; |
| 4559 | if (!(s->flags & SF_FINST_MASK)) |
| 4560 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4561 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4562 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4563 | req->analyse_exp = TICK_ETERNITY; |
| 4564 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4565 | |
| 4566 | return_prx_yield: |
| 4567 | channel_dont_connect(req); |
| 4568 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4569 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4570 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4571 | /* This function performs all the processing enabled for the current request. |
| 4572 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4573 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4574 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4575 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4576 | 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] | 4577 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4578 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4579 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4580 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4581 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4582 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4583 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4584 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4585 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4586 | return 0; |
| 4587 | } |
| 4588 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4589 | 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] | 4590 | now_ms, __FUNCTION__, |
| 4591 | s, |
| 4592 | req, |
| 4593 | req->rex, req->wex, |
| 4594 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4595 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4596 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4597 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4598 | /* |
| 4599 | * Right now, we know that we have processed the entire headers |
| 4600 | * and that unwanted requests have been filtered out. We can do |
| 4601 | * whatever we want with the remaining request. Also, now we |
| 4602 | * may have separate values for ->fe, ->be. |
| 4603 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4604 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4605 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4606 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4607 | * incoming request. Note that this requires that a connection is |
| 4608 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4609 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4610 | 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] | 4611 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4612 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4613 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4614 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4615 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4616 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4617 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4618 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4619 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4620 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4621 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4622 | if (!(s->flags & SF_ERR_MASK)) |
| 4623 | s->flags |= SF_ERR_RESOURCE; |
| 4624 | if (!(s->flags & SF_FINST_MASK)) |
| 4625 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4626 | |
| 4627 | return 0; |
| 4628 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4629 | |
| 4630 | path = http_get_path(txn); |
| 4631 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4632 | 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] | 4633 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4634 | /* if the path was found, we have to remove everything between |
| 4635 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4636 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4637 | * u_l characters by a single "/". |
| 4638 | */ |
| 4639 | if (path) { |
| 4640 | char *cur_ptr = req->buf->p; |
| 4641 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4642 | int delta; |
| 4643 | |
| 4644 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4645 | http_msg_move_end(&txn->req, delta); |
| 4646 | cur_end += delta; |
| 4647 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4648 | goto return_bad_req; |
| 4649 | } |
| 4650 | else { |
| 4651 | char *cur_ptr = req->buf->p; |
| 4652 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4653 | int delta; |
| 4654 | |
| 4655 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4656 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4657 | http_msg_move_end(&txn->req, delta); |
| 4658 | cur_end += delta; |
| 4659 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4660 | goto return_bad_req; |
| 4661 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4662 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4663 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4664 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4665 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4666 | * Note that doing so might move headers in the request, but |
| 4667 | * the fields will stay coherent and the URI will not move. |
| 4668 | * This should only be performed in the backend. |
| 4669 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 4670 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4671 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4672 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4673 | /* add unique-id if "header-unique-id" is specified */ |
| 4674 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 4675 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4676 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4677 | goto return_bad_req; |
| 4678 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4679 | 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] | 4680 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4681 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4682 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4683 | 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] | 4684 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4685 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4686 | 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] | 4687 | goto return_bad_req; |
| 4688 | } |
| 4689 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4690 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4691 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4692 | * asks for it. |
| 4693 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4694 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4695 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4696 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4697 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4698 | 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] | 4699 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4700 | /* The header is set to be added only if none is present |
| 4701 | * and we found it, so don't do anything. |
| 4702 | */ |
| 4703 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4704 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4705 | /* Add an X-Forwarded-For header unless the source IP is |
| 4706 | * in the 'except' network range. |
| 4707 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4708 | if ((!sess->fe->except_mask.s_addr || |
| 4709 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4710 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4711 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4712 | (((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] | 4713 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4714 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4715 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4716 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4717 | |
| 4718 | /* Note: we rely on the backend to get the header name to be used for |
| 4719 | * x-forwarded-for, because the header is really meant for the backends. |
| 4720 | * However, if the backend did not specify any option, we have to rely |
| 4721 | * on the frontend's header name. |
| 4722 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4723 | if (s->be->fwdfor_hdr_len) { |
| 4724 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4725 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4726 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4727 | len = sess->fe->fwdfor_hdr_len; |
| 4728 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4729 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4730 | 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] | 4731 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4732 | 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] | 4733 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4734 | } |
| 4735 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4736 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4737 | /* FIXME: for the sake of completeness, we should also support |
| 4738 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4739 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4740 | int len; |
| 4741 | char pn[INET6_ADDRSTRLEN]; |
| 4742 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4743 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4744 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4745 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4746 | /* Note: we rely on the backend to get the header name to be used for |
| 4747 | * x-forwarded-for, because the header is really meant for the backends. |
| 4748 | * However, if the backend did not specify any option, we have to rely |
| 4749 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4750 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4751 | if (s->be->fwdfor_hdr_len) { |
| 4752 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4753 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4754 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4755 | len = sess->fe->fwdfor_hdr_len; |
| 4756 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4757 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4758 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4759 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4760 | 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] | 4761 | goto return_bad_req; |
| 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4766 | * 10: add X-Original-To if either the frontend or the backend |
| 4767 | * asks for it. |
| 4768 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4769 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4770 | |
| 4771 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4772 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4773 | /* Add an X-Original-To header unless the destination IP is |
| 4774 | * in the 'except' network range. |
| 4775 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4776 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4777 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4778 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4779 | ((!sess->fe->except_mask_to.s_addr || |
| 4780 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4781 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4782 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4783 | (((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] | 4784 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4785 | int len; |
| 4786 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4787 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4788 | |
| 4789 | /* Note: we rely on the backend to get the header name to be used for |
| 4790 | * x-original-to, because the header is really meant for the backends. |
| 4791 | * However, if the backend did not specify any option, we have to rely |
| 4792 | * on the frontend's header name. |
| 4793 | */ |
| 4794 | if (s->be->orgto_hdr_len) { |
| 4795 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4796 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4797 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4798 | len = sess->fe->orgto_hdr_len; |
| 4799 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4800 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4801 | 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] | 4802 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4803 | 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] | 4804 | goto return_bad_req; |
| 4805 | } |
| 4806 | } |
| 4807 | } |
| 4808 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4809 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4810 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4811 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4812 | * "Upgrade" is present in the Connection header. |
| 4813 | */ |
| 4814 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4815 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4816 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4817 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4818 | unsigned int want_flags = 0; |
| 4819 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4820 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4821 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4822 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4823 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4824 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4825 | want_flags |= TX_CON_CLO_SET; |
| 4826 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4827 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4828 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4829 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4830 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4831 | want_flags |= TX_CON_KAL_SET; |
| 4832 | } |
| 4833 | |
| 4834 | 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] | 4835 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4836 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4837 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4838 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4839 | /* If we have no server assigned yet and we're balancing on url_param |
| 4840 | * with a POST request, we may be interested in checking the body for |
| 4841 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4842 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4843 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4844 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4845 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4846 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4847 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4848 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4849 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4850 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4851 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4852 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4853 | #ifdef TCP_QUICKACK |
| 4854 | /* We expect some data from the client. Unless we know for sure |
| 4855 | * we already have a full request, we have to re-enable quick-ack |
| 4856 | * in case we previously disabled it, otherwise we might cause |
| 4857 | * the client to delay further data. |
| 4858 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4859 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4860 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4861 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4862 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4863 | 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] | 4864 | #endif |
| 4865 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4866 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4867 | /************************************************************* |
| 4868 | * OK, that's finished for the headers. We have done what we * |
| 4869 | * could. Let's switch to the DATA state. * |
| 4870 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4871 | req->analyse_exp = TICK_ETERNITY; |
| 4872 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4873 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4874 | /* if the server closes the connection, we want to immediately react |
| 4875 | * and close the socket to save packets and syscalls. |
| 4876 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4877 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4878 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4879 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4880 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4881 | /* OK let's go on with the BODY now */ |
| 4882 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4883 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4884 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4885 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4886 | /* we detected a parsing error. We want to archive this request |
| 4887 | * in the dedicated proxy area for later troubleshooting. |
| 4888 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4889 | 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] | 4890 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4891 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4892 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4893 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4894 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4895 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4896 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4897 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4898 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4899 | if (sess->listener->counters) |
| 4900 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4901 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4902 | if (!(s->flags & SF_ERR_MASK)) |
| 4903 | s->flags |= SF_ERR_PRXCOND; |
| 4904 | if (!(s->flags & SF_FINST_MASK)) |
| 4905 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4906 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4907 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4908 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4909 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4910 | * returns zero, at the beginning because it prevents any other processing |
| 4911 | * from occurring, and at the end because it terminates the request. |
| 4912 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4913 | 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] | 4914 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4915 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4916 | |
| 4917 | /* This connection is being tarpitted. The CLIENT side has |
| 4918 | * already set the connect expiration date to the right |
| 4919 | * timeout. We just have to check that the client is still |
| 4920 | * there and that the timeout has not expired. |
| 4921 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4922 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4923 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4924 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4925 | return 0; |
| 4926 | |
| 4927 | /* We will set the queue timer to the time spent, just for |
| 4928 | * logging purposes. We fake a 500 server error, so that the |
| 4929 | * attacker will not suspect his connection has been tarpitted. |
| 4930 | * It will not cause trouble to the logs because we can exclude |
| 4931 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4932 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4933 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4934 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4935 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4936 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4937 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4938 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4939 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4940 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4941 | if (!(s->flags & SF_ERR_MASK)) |
| 4942 | s->flags |= SF_ERR_PRXCOND; |
| 4943 | if (!(s->flags & SF_FINST_MASK)) |
| 4944 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4945 | return 0; |
| 4946 | } |
| 4947 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4948 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4949 | * for either the buffer to be full, or the full advertised contents to have |
| 4950 | * reached the buffer. It must only be called after the standard HTTP request |
| 4951 | * processing has occurred, because it expects the request to be parsed and will |
| 4952 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4953 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4954 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4955 | * 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] | 4956 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4957 | 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] | 4958 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4959 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4960 | struct http_txn *txn = s->txn; |
| 4961 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4962 | |
| 4963 | /* We have to parse the HTTP request body to find any required data. |
| 4964 | * "balance url_param check_post" should have been the only way to get |
| 4965 | * into this. We were brought here after HTTP header analysis, so all |
| 4966 | * related structures are ready. |
| 4967 | */ |
| 4968 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4969 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4970 | /* This is the first call */ |
| 4971 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4972 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4973 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4974 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4975 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4976 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4977 | */ |
| 4978 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4979 | struct hdr_ctx ctx; |
| 4980 | ctx.idx = 0; |
| 4981 | /* Expect is allowed in 1.1, look for it */ |
| 4982 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4983 | 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] | 4984 | 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] | 4985 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4986 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4987 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4988 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4989 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4990 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4991 | /* 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] | 4992 | * req->buf->p still points to the beginning of the message. We |
| 4993 | * must save the body in msg->next because it survives buffer |
| 4994 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4995 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4996 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4997 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4998 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4999 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5000 | else |
| 5001 | msg->msg_state = HTTP_MSG_DATA; |
| 5002 | } |
| 5003 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 5004 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 5005 | /* 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] | 5006 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 5007 | goto missing_data; |
| 5008 | |
| 5009 | /* OK we have everything we need now */ |
| 5010 | goto http_end; |
| 5011 | } |
| 5012 | |
| 5013 | /* OK here we're parsing a chunked-encoded message */ |
| 5014 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5015 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5016 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5017 | * 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] | 5018 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5019 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5020 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5021 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5022 | if (!ret) |
| 5023 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5024 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5025 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5026 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5027 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5028 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5029 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5030 | } |
| 5031 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5032 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5033 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 5034 | * for at least a whole chunk or the whole content length bytes after |
| 5035 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5036 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 5037 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 5038 | goto http_end; |
| 5039 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5040 | 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] | 5041 | goto http_end; |
| 5042 | |
| 5043 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5044 | /* we get here if we need to wait for more data. If the buffer is full, |
| 5045 | * we have the maximum we can expect. |
| 5046 | */ |
| 5047 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 5048 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5049 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5050 | 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] | 5051 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5052 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5053 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5054 | if (!(s->flags & SF_ERR_MASK)) |
| 5055 | s->flags |= SF_ERR_CLITO; |
| 5056 | if (!(s->flags & SF_FINST_MASK)) |
| 5057 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5058 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5059 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5060 | |
| 5061 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5062 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5063 | /* Not enough data. We'll re-use the http-request |
| 5064 | * timeout here. Ideally, we should set the timeout |
| 5065 | * relative to the accept() date. We just set the |
| 5066 | * request timeout once at the beginning of the |
| 5067 | * request. |
| 5068 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5069 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5070 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5071 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5072 | return 0; |
| 5073 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5074 | |
| 5075 | http_end: |
| 5076 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5077 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5078 | req->analysers &= ~an_bit; |
| 5079 | req->analyse_exp = TICK_ETERNITY; |
| 5080 | return 1; |
| 5081 | |
| 5082 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5083 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5084 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5085 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5086 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5087 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5088 | if (!(s->flags & SF_ERR_MASK)) |
| 5089 | s->flags |= SF_ERR_PRXCOND; |
| 5090 | if (!(s->flags & SF_FINST_MASK)) |
| 5091 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5092 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5093 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5094 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5095 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5096 | if (sess->listener->counters) |
| 5097 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5098 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5099 | } |
| 5100 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5101 | /* send a server's name with an outgoing request over an established connection. |
| 5102 | * Note: this function is designed to be called once the request has been scheduled |
| 5103 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5104 | * proceeding. |
| 5105 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5106 | 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] | 5107 | |
| 5108 | struct hdr_ctx ctx; |
| 5109 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5110 | char *hdr_name = be->server_id_hdr_name; |
| 5111 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5112 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5113 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5114 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5115 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5116 | ctx.idx = 0; |
| 5117 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5118 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5119 | if (old_o) { |
| 5120 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5121 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5122 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5123 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5124 | } |
| 5125 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5126 | old_i = chn->buf->i; |
| 5127 | 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] | 5128 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5129 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5133 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5134 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5135 | hdr_val += hdr_name_len; |
| 5136 | *hdr_val++ = ':'; |
| 5137 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5138 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5139 | 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] | 5140 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5141 | if (old_o) { |
| 5142 | /* If this was a forwarded request, we must readjust the amount of |
| 5143 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5144 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5145 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5146 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5147 | old_o += chn->buf->i - old_i; |
| 5148 | b_adv(chn->buf, old_o); |
| 5149 | txn->req.next -= old_o; |
| 5150 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5151 | } |
| 5152 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5153 | return 0; |
| 5154 | } |
| 5155 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5156 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5157 | * right now but it works. |
| 5158 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5159 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5160 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5161 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5162 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5163 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5164 | struct connection *srv_conn; |
| 5165 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5166 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5167 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5168 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5169 | * server's connections. We need a safer way to reinitialize buffer |
| 5170 | * flags. We also need a more accurate method for computing per-request |
| 5171 | * data. |
| 5172 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5173 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5174 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5175 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5176 | * to the server. |
| 5177 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5178 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5179 | !si_conn_ready(&s->si[1])) { |
| 5180 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5181 | si_shutr(&s->si[1]); |
| 5182 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5183 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5184 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5185 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5186 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5187 | if (unlikely(s->srv_conn)) |
| 5188 | sess_change_server(s, NULL); |
| 5189 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5190 | |
| 5191 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5192 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5193 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5194 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5195 | int n; |
| 5196 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5197 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5198 | if (n < 1 || n > 5) |
| 5199 | n = 0; |
| 5200 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5201 | if (fe->mode == PR_MODE_HTTP) { |
| 5202 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5203 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5204 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5205 | (be->mode == PR_MODE_HTTP)) { |
| 5206 | be->be_counters.p.http.rsp[n]++; |
| 5207 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5208 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5212 | s->logs.bytes_in -= s->req.buf->i; |
| 5213 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5214 | |
| 5215 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5216 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5217 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5218 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5219 | s->do_log(s); |
| 5220 | } |
| 5221 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5222 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5223 | stream_stop_content_counters(s); |
| 5224 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5225 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5226 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5227 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 5228 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 5229 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5230 | tv_zero(&s->logs.tv_request); |
| 5231 | s->logs.t_queue = -1; |
| 5232 | s->logs.t_connect = -1; |
| 5233 | s->logs.t_data = -1; |
| 5234 | s->logs.t_close = 0; |
| 5235 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5236 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5237 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5238 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5239 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5240 | |
| 5241 | if (s->pend_pos) |
| 5242 | pendconn_free(s->pend_pos); |
| 5243 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5244 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5245 | if (s->flags & SF_CURR_SESS) { |
| 5246 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5247 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5248 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 5249 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5250 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5253 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5254 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5255 | /* only release our endpoint if we don't intend to reuse the |
| 5256 | * connection. |
| 5257 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5258 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5259 | !si_conn_ready(&s->si[1])) { |
| 5260 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5261 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5262 | } |
| 5263 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5264 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5265 | s->si[1].err_type = SI_ET_NONE; |
| 5266 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5267 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5268 | 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] | 5269 | 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); |
| 5270 | 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] | 5271 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5272 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5273 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5274 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5275 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5276 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5277 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5278 | |
| 5279 | if (prev_status == 401 || prev_status == 407) { |
| 5280 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5281 | * a chance of being able to send the visitor again to the same |
| 5282 | * server over the same connection. This is required by some |
| 5283 | * broken protocols such as NTLM, and anyway whenever there is |
| 5284 | * an opportunity for sending the challenge to the proper place, |
| 5285 | * it's better to do it (at least it helps with debugging). |
| 5286 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5287 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5288 | if (srv_conn) |
| 5289 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5290 | } |
| 5291 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5292 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 5293 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 5294 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5295 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5296 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5297 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5298 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5299 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5300 | s->req.flags |= CF_NEVER_WAIT; |
| 5301 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5302 | } |
| 5303 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5304 | /* if the request buffer is not empty, it means we're |
| 5305 | * about to process another request, so send pending |
| 5306 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5307 | * Just don't do this if the buffer is close to be full, |
| 5308 | * because the request will wait for it to flush a little |
| 5309 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5310 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5311 | if (s->req.buf->i) { |
| 5312 | if (s->res.buf->o && |
| 5313 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5314 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5315 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5316 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5317 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 5318 | /* we're removing the analysers, we MUST re-enable events detection. |
| 5319 | * We don't enable close on the response channel since it's either |
| 5320 | * already closed, or in keep-alive with an idle connection handler. |
| 5321 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5322 | channel_auto_read(&s->req); |
| 5323 | channel_auto_close(&s->req); |
| 5324 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5325 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5326 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5327 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5328 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5329 | if (!srv) |
| 5330 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 5331 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5332 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5333 | else if (prev_flags & TX_NOT_FIRST) |
| 5334 | /* note: we check the request, not the connection, but |
| 5335 | * this is valid for strategies SAFE and AGGR, and in |
| 5336 | * case of ALWS, we don't care anyway. |
| 5337 | */ |
| 5338 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5339 | else |
| 5340 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5341 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 5342 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 5343 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | |
| 5347 | /* This function updates the request state machine according to the response |
| 5348 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5349 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5350 | * it is only used to find when a request/response couple is complete. Both |
| 5351 | * this function and its equivalent should loop until both return zero. It |
| 5352 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5353 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5354 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5355 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5356 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5357 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5358 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5359 | unsigned int old_state = txn->req.msg_state; |
| 5360 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5361 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5362 | return 0; |
| 5363 | |
| 5364 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5365 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5366 | * We can shut the read side unless we want to abort_on_close, |
| 5367 | * or we have a POST request. The issue with POST requests is |
| 5368 | * that some browsers still send a CRLF after the request, and |
| 5369 | * this CRLF must be read so that it does not remain in the kernel |
| 5370 | * buffers, otherwise a close could cause an RST on some systems |
| 5371 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5372 | * Note that if we're using keep-alive on the client side, we'd |
| 5373 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5374 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5375 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5376 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5377 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5378 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5379 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5380 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5381 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5382 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5383 | /* if the server closes the connection, we want to immediately react |
| 5384 | * and close the socket to save packets and syscalls. |
| 5385 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5386 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5387 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 5388 | /* In any case we've finished parsing the request so we must |
| 5389 | * disable Nagle when sending data because 1) we're not going |
| 5390 | * to shut this side, and 2) the server is waiting for us to |
| 5391 | * send pending data. |
| 5392 | */ |
| 5393 | chn->flags |= CF_NEVER_WAIT; |
| 5394 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5395 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5396 | goto wait_other_side; |
| 5397 | |
| 5398 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5399 | /* The server has not finished to respond, so we |
| 5400 | * don't want to move in order not to upset it. |
| 5401 | */ |
| 5402 | goto wait_other_side; |
| 5403 | } |
| 5404 | |
| 5405 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5406 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5407 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5408 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5409 | goto wait_other_side; |
| 5410 | } |
| 5411 | |
| 5412 | /* When we get here, it means that both the request and the |
| 5413 | * response have finished receiving. Depending on the connection |
| 5414 | * mode, we'll have to wait for the last bytes to leave in either |
| 5415 | * direction, and sometimes for a close to be effective. |
| 5416 | */ |
| 5417 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5418 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5419 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5420 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5421 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5422 | } |
| 5423 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5424 | /* Option forceclose is set, or either side wants to close, |
| 5425 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5426 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5427 | * once both states are CLOSED. |
| 5428 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5429 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5430 | channel_shutr_now(chn); |
| 5431 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5432 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5433 | } |
| 5434 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5435 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5436 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5437 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5438 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5439 | channel_auto_read(chn); |
| 5440 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5441 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5444 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5445 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5446 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5447 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5448 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5449 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5450 | goto http_msg_closing; |
| 5451 | } |
| 5452 | else { |
| 5453 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5454 | goto http_msg_closed; |
| 5455 | } |
| 5456 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5457 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5461 | http_msg_closing: |
| 5462 | /* nothing else to forward, just waiting for the output buffer |
| 5463 | * to be empty and for the shutw_now to take effect. |
| 5464 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5465 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5466 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5467 | goto http_msg_closed; |
| 5468 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5469 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5470 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5471 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5472 | goto wait_other_side; |
| 5473 | } |
| 5474 | } |
| 5475 | |
| 5476 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5477 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5478 | /* see above in MSG_DONE why we only do this in these states */ |
| 5479 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5480 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5481 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5482 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5483 | goto wait_other_side; |
| 5484 | } |
| 5485 | |
| 5486 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5487 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | |
| 5491 | /* This function updates the response state machine according to the request |
| 5492 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5493 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5494 | * it is only used to find when a request/response couple is complete. Both |
| 5495 | * this function and its equivalent should loop until both return zero. It |
| 5496 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5497 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5498 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5499 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5500 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5501 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5502 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5503 | unsigned int old_state = txn->rsp.msg_state; |
| 5504 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5505 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5506 | return 0; |
| 5507 | |
| 5508 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5509 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5510 | * still monitor the server connection for a possible close |
| 5511 | * while the request is being uploaded, so we don't disable |
| 5512 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5513 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5514 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5515 | |
| 5516 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5517 | goto wait_other_side; |
| 5518 | |
| 5519 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5520 | /* The client seems to still be sending data, probably |
| 5521 | * because we got an error response during an upload. |
| 5522 | * We have the choice of either breaking the connection |
| 5523 | * or letting it pass through. Let's do the later. |
| 5524 | */ |
| 5525 | goto wait_other_side; |
| 5526 | } |
| 5527 | |
| 5528 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5529 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5530 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5531 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5532 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5533 | goto wait_other_side; |
| 5534 | } |
| 5535 | |
| 5536 | /* When we get here, it means that both the request and the |
| 5537 | * response have finished receiving. Depending on the connection |
| 5538 | * mode, we'll have to wait for the last bytes to leave in either |
| 5539 | * direction, and sometimes for a close to be effective. |
| 5540 | */ |
| 5541 | |
| 5542 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5543 | /* Server-close mode : shut read and wait for the request |
| 5544 | * side to close its output buffer. The caller will detect |
| 5545 | * when we're in DONE and the other is in CLOSED and will |
| 5546 | * catch that for the final cleanup. |
| 5547 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5548 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5549 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5550 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5551 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5552 | /* Option forceclose is set, or either side wants to close, |
| 5553 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5554 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5555 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5556 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5557 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5558 | channel_shutr_now(chn); |
| 5559 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5560 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5561 | } |
| 5562 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5563 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5564 | * need to forward remaining data. Keep-alive will need to monitor |
| 5565 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5566 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5567 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5568 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5569 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5570 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5571 | } |
| 5572 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5573 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5574 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5575 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5576 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5577 | goto http_msg_closing; |
| 5578 | } |
| 5579 | else { |
| 5580 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5581 | goto http_msg_closed; |
| 5582 | } |
| 5583 | } |
| 5584 | goto wait_other_side; |
| 5585 | } |
| 5586 | |
| 5587 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5588 | http_msg_closing: |
| 5589 | /* nothing else to forward, just waiting for the output buffer |
| 5590 | * to be empty and for the shutw_now to take effect. |
| 5591 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5592 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5593 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5594 | goto http_msg_closed; |
| 5595 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5596 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5597 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5598 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5599 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5600 | if (objt_server(s->target)) |
| 5601 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5602 | goto wait_other_side; |
| 5603 | } |
| 5604 | } |
| 5605 | |
| 5606 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5607 | http_msg_closed: |
| 5608 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5609 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5610 | channel_auto_close(chn); |
| 5611 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5612 | goto wait_other_side; |
| 5613 | } |
| 5614 | |
| 5615 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5616 | /* We force the response to leave immediately if we're waiting for the |
| 5617 | * other side, since there is no pending shutdown to push it out. |
| 5618 | */ |
| 5619 | if (!channel_is_empty(chn)) |
| 5620 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5621 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | |
| 5625 | /* Resync the request and response state machines. Return 1 if either state |
| 5626 | * changes. |
| 5627 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5628 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5629 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5630 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5631 | int old_req_state = txn->req.msg_state; |
| 5632 | int old_res_state = txn->rsp.msg_state; |
| 5633 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5634 | http_sync_req_state(s); |
| 5635 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5636 | if (!http_sync_res_state(s)) |
| 5637 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5638 | if (!http_sync_req_state(s)) |
| 5639 | break; |
| 5640 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5641 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5642 | /* OK, both state machines agree on a compatible state. |
| 5643 | * There are a few cases we're interested in : |
| 5644 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5645 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5646 | * directions, so let's simply disable both analysers. |
| 5647 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5648 | * request. |
| 5649 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5650 | * with server-close mode means we've completed one request and we |
| 5651 | * must re-initialize the server connection. |
| 5652 | */ |
| 5653 | |
| 5654 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5655 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5656 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5657 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5658 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5659 | channel_auto_close(&s->req); |
| 5660 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5661 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5662 | channel_auto_close(&s->res); |
| 5663 | channel_auto_read(&s->res); |
Christopher Faulet | 6962f4e | 2016-11-28 14:21:27 +0100 | [diff] [blame] | 5664 | 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] | 5665 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 6962f4e | 2016-11-28 14:21:27 +0100 | [diff] [blame] | 5666 | 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] | 5667 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5668 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5669 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5670 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5671 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5672 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5673 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5674 | channel_auto_close(&s->res); |
| 5675 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5676 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5677 | channel_abort(&s->req); |
| 5678 | channel_auto_close(&s->req); |
| 5679 | channel_auto_read(&s->req); |
| 5680 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5681 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5682 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5683 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5684 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5685 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5686 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5687 | /* server-close/keep-alive: terminate this transaction, |
| 5688 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5689 | * a fresh-new transaction, but only once we're sure there's |
| 5690 | * enough room in the request and response buffer to process |
| 5691 | * another request. The request buffer must not hold any |
| 5692 | * pending output data and the request buffer must not have |
| 5693 | * output data occupying the reserve. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5694 | */ |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 5695 | if (s->req.buf->o) |
| 5696 | s->req.flags |= CF_WAKE_WRITE; |
| 5697 | else if (channel_congested(&s->res)) |
| 5698 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 5699 | else { |
| 5700 | s->req.analysers = AN_REQ_FLT_END; |
| 5701 | s->res.analysers = AN_RES_FLT_END; |
| 5702 | txn->flags |= TX_WAIT_CLEANUP; |
| 5703 | return 1; |
| 5704 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5705 | } |
| 5706 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5707 | return txn->req.msg_state != old_req_state || |
| 5708 | txn->rsp.msg_state != old_res_state; |
| 5709 | } |
| 5710 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5711 | /* This function is an analyser which forwards request body (including chunk |
| 5712 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5713 | * zero byte. The only situation where it must not be called is when we're in |
| 5714 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5715 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5716 | * 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] | 5717 | * 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] | 5718 | * 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] | 5719 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5720 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5721 | 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] | 5722 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5723 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5724 | struct http_txn *txn = s->txn; |
| 5725 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5726 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5727 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5728 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5729 | return 0; |
| 5730 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5731 | 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] | 5732 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5733 | /* Output closed while we were sending data. We must abort and |
| 5734 | * wake the other side up. |
| 5735 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5736 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5737 | msg->msg_state = HTTP_MSG_ERROR; |
| 5738 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5739 | return 1; |
| 5740 | } |
| 5741 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5742 | /* Note that we don't have to send 100-continue back because we don't |
| 5743 | * need the data to complete our job, and it's up to the server to |
| 5744 | * decide whether to return 100, 417 or anything else in return of |
| 5745 | * an "Expect: 100-continue" header. |
| 5746 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5747 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5748 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5749 | ? HTTP_MSG_CHUNK_SIZE |
| 5750 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5751 | |
| 5752 | /* TODO/filters: when http-buffer-request option is set or if a |
| 5753 | * rule on url_param exists, the first chunk size could be |
| 5754 | * already parsed. In that case, msg->next is after the chunk |
| 5755 | * size (including the CRLF after the size). So this case should |
| 5756 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5757 | } |
| 5758 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5759 | /* Some post-connect processing might want us to refrain from starting to |
| 5760 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5761 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5762 | */ |
| 5763 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5764 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5765 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5766 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5767 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5768 | } |
| 5769 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5770 | } |
| 5771 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5772 | /* in most states, we should abort in case of early close */ |
| 5773 | channel_auto_close(req); |
| 5774 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5775 | if (req->to_forward) { |
| 5776 | /* We can't process the buffer's contents yet */ |
| 5777 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5778 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5779 | } |
| 5780 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5781 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5782 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5783 | ? http_msg_forward_chunked_body(s, msg) |
| 5784 | : http_msg_forward_body(s, msg)); |
| 5785 | if (!ret) |
| 5786 | goto missing_data_or_waiting; |
| 5787 | if (ret < 0) |
| 5788 | goto return_bad_req; |
| 5789 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5790 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5791 | /* other states, DONE...TUNNEL */ |
| 5792 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 5793 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5794 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5795 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5796 | if (http_resync_states(s)) { |
| 5797 | /* some state changes occurred, maybe the analyser |
| 5798 | * was disabled too. */ |
| 5799 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5800 | if (req->flags & CF_SHUTW) { |
| 5801 | /* request errors are most likely due to the |
| 5802 | * server aborting the transfer. */ |
| 5803 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5804 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5805 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5806 | 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] | 5807 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5808 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5809 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5810 | } |
| 5811 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5812 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 5813 | * the client's connection and forward any shutdown notification to the |
| 5814 | * server, which will decide whether to close or to go on processing the |
| 5815 | * request. We only do that in tunnel mode, and not in other modes since |
| 5816 | * it can be abused to exhaust source ports. */ |
| 5817 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 5818 | channel_auto_read(req); |
| 5819 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5820 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5821 | s->si[1].flags |= SI_FL_NOLINGER; |
| 5822 | channel_auto_close(req); |
| 5823 | } |
| 5824 | else if (s->txn->meth == HTTP_METH_POST) { |
| 5825 | /* POST requests may require to read extra CRLF sent by broken |
| 5826 | * browsers and which could cause an RST to be sent upon close |
| 5827 | * on some systems (eg: Linux). */ |
| 5828 | channel_auto_read(req); |
| 5829 | } |
| 5830 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5831 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5832 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5833 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5834 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5835 | if (!(s->flags & SF_ERR_MASK)) |
| 5836 | s->flags |= SF_ERR_CLICL; |
| 5837 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5838 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5839 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5840 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5841 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5842 | } |
| 5843 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5844 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5845 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5846 | if (objt_server(s->target)) |
| 5847 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5848 | |
| 5849 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5850 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5851 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5852 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5853 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5854 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5855 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5856 | /* 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] | 5857 | * 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] | 5858 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5859 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5860 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5861 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5862 | /* 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] | 5863 | * 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] | 5864 | * 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] | 5865 | * modes are already handled by the stream sock layer. We must not do |
| 5866 | * this in content-length mode because it could present the MSG_MORE |
| 5867 | * flag with the last block of forwarded data, which would cause an |
| 5868 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5869 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5870 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5871 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5872 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5873 | return 0; |
| 5874 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5875 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5876 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5877 | if (sess->listener->counters) |
| 5878 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5879 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5880 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5881 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5882 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5883 | if (txn->status) { |
| 5884 | /* 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] | 5885 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5886 | } else { |
| 5887 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5888 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5889 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5890 | req->analysers &= AN_REQ_FLT_END; |
| 5891 | 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] | 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_PRXCOND; |
| 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 | } |
| 5901 | return 0; |
| 5902 | |
| 5903 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5904 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5905 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5906 | if (txn->status) { |
| 5907 | /* 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] | 5908 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5909 | } else { |
| 5910 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5911 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5912 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5913 | req->analysers &= AN_REQ_FLT_END; |
| 5914 | 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] | 5915 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5916 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5917 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5918 | if (objt_server(s->target)) |
| 5919 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5920 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5921 | if (!(s->flags & SF_ERR_MASK)) |
| 5922 | s->flags |= SF_ERR_SRVCL; |
| 5923 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5924 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5925 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5926 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5927 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5928 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5929 | return 0; |
| 5930 | } |
| 5931 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5932 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5933 | * processing can continue on next analysers, or zero if it either needs more |
| 5934 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5935 | * 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] | 5936 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5937 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5938 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5939 | 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] | 5940 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5941 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5942 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5943 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5944 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5945 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5946 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5947 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5948 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5949 | 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] | 5950 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5951 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5952 | rep, |
| 5953 | rep->rex, rep->wex, |
| 5954 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5955 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5956 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5957 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5958 | /* |
| 5959 | * Now parse the partial (or complete) lines. |
| 5960 | * We will check the response syntax, and also join multi-line |
| 5961 | * headers. An index of all the lines will be elaborated while |
| 5962 | * parsing. |
| 5963 | * |
| 5964 | * For the parsing, we use a 28 states FSM. |
| 5965 | * |
| 5966 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5967 | * rep->buf->p = beginning of response |
| 5968 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5969 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5970 | * msg->eol = end of current header or line (LF or CRLF) |
| 5971 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5972 | */ |
| 5973 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5974 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5975 | /* There's a protected area at the end of the buffer for rewriting |
| 5976 | * purposes. We don't want to start to parse the request if the |
| 5977 | * protected area is affected, because we may have to move processed |
| 5978 | * data later, which is much more complicated. |
| 5979 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5980 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5981 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5982 | /* some data has still not left the buffer, wake us once that's done */ |
| 5983 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5984 | goto abort_response; |
| 5985 | channel_dont_close(rep); |
| 5986 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5987 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5988 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5989 | } |
| 5990 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5991 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5992 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5993 | buffer_slow_realign(rep->buf); |
| 5994 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5995 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5996 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5997 | } |
| 5998 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5999 | /* 1: we might have to print this header in debug mode */ |
| 6000 | if (unlikely((global.mode & MODE_DEBUG) && |
| 6001 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 6002 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6003 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6004 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6005 | sol = rep->buf->p; |
| 6006 | 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] | 6007 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6008 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6009 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 6010 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6011 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6012 | while (cur_idx) { |
| 6013 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 6014 | debug_hdr("srvhdr", s, sol, eol); |
| 6015 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 6016 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 6017 | } |
| 6018 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6019 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6020 | /* |
| 6021 | * Now we quickly check if we have found a full valid response. |
| 6022 | * If not so, we check the FD and buffer states before leaving. |
| 6023 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6024 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6025 | * responses are checked first. |
| 6026 | * |
| 6027 | * Depending on whether the client is still there or not, we |
| 6028 | * may send an error response back or not. Note that normally |
| 6029 | * we should only check for HTTP status there, and check I/O |
| 6030 | * errors somewhere else. |
| 6031 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6032 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6033 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6034 | /* Invalid response */ |
| 6035 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6036 | /* we detected a parsing error. We want to archive this response |
| 6037 | * in the dedicated proxy area for later troubleshooting. |
| 6038 | */ |
| 6039 | hdr_response_bad: |
| 6040 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6041 | 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] | 6042 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6043 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6044 | if (objt_server(s->target)) { |
| 6045 | objt_server(s->target)->counters.failed_resp++; |
| 6046 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6047 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 6048 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6049 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6050 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6051 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6052 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6053 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6054 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6055 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6056 | if (!(s->flags & SF_ERR_MASK)) |
| 6057 | s->flags |= SF_ERR_PRXCOND; |
| 6058 | if (!(s->flags & SF_FINST_MASK)) |
| 6059 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6060 | |
| 6061 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6062 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6063 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6064 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6065 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 6066 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6067 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6068 | goto hdr_response_bad; |
| 6069 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6070 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6071 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6072 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6073 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6074 | 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] | 6075 | else if (txn->flags & TX_NOT_FIRST) |
| 6076 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6077 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6078 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6079 | if (objt_server(s->target)) { |
| 6080 | objt_server(s->target)->counters.failed_resp++; |
| 6081 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6082 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6083 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6084 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6085 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6086 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6087 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6088 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6089 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 6090 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6091 | if (!(s->flags & SF_ERR_MASK)) |
| 6092 | s->flags |= SF_ERR_SRVCL; |
| 6093 | if (!(s->flags & SF_FINST_MASK)) |
| 6094 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6095 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6096 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6097 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6098 | /* read timeout : return a 504 to the client. */ |
| 6099 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6100 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6101 | 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] | 6102 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6103 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6104 | if (objt_server(s->target)) { |
| 6105 | objt_server(s->target)->counters.failed_resp++; |
| 6106 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6107 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6108 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6109 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6110 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6111 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6112 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6113 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6114 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6115 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6116 | if (!(s->flags & SF_ERR_MASK)) |
| 6117 | s->flags |= SF_ERR_SRVTO; |
| 6118 | if (!(s->flags & SF_FINST_MASK)) |
| 6119 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6120 | return 0; |
| 6121 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6122 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6123 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6124 | 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] | 6125 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6126 | s->be->be_counters.cli_aborts++; |
| 6127 | if (objt_server(s->target)) |
| 6128 | objt_server(s->target)->counters.cli_aborts++; |
| 6129 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6130 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6131 | channel_auto_close(rep); |
| 6132 | |
| 6133 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6134 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6135 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6136 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6137 | if (!(s->flags & SF_ERR_MASK)) |
| 6138 | s->flags |= SF_ERR_CLICL; |
| 6139 | if (!(s->flags & SF_FINST_MASK)) |
| 6140 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6141 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6142 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6143 | return 0; |
| 6144 | } |
| 6145 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6146 | /* 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] | 6147 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6148 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6149 | 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] | 6150 | else if (txn->flags & TX_NOT_FIRST) |
| 6151 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6152 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6153 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6154 | if (objt_server(s->target)) { |
| 6155 | objt_server(s->target)->counters.failed_resp++; |
| 6156 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6157 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6158 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6159 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6160 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6161 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6162 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6163 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6164 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6165 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6166 | if (!(s->flags & SF_ERR_MASK)) |
| 6167 | s->flags |= SF_ERR_SRVCL; |
| 6168 | if (!(s->flags & SF_FINST_MASK)) |
| 6169 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6170 | return 0; |
| 6171 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6172 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6173 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6174 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6175 | if (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 | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6177 | else if (txn->flags & TX_NOT_FIRST) |
| 6178 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6179 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6180 | s->be->be_counters.failed_resp++; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6181 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6182 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6183 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6184 | if (!(s->flags & SF_ERR_MASK)) |
| 6185 | s->flags |= SF_ERR_CLICL; |
| 6186 | if (!(s->flags & SF_FINST_MASK)) |
| 6187 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6188 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6189 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6190 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6191 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6192 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6193 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6194 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6195 | return 0; |
| 6196 | } |
| 6197 | |
| 6198 | /* More interesting part now : we know that we have a complete |
| 6199 | * response which at least looks like HTTP. We have an indicator |
| 6200 | * of each header's length, so we can parse them quickly. |
| 6201 | */ |
| 6202 | |
| 6203 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6204 | 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] | 6205 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6206 | /* |
| 6207 | * 1: get the status code |
| 6208 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6209 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6210 | if (n < 1 || n > 5) |
| 6211 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6212 | /* when the client triggers a 4xx from the server, it's most often due |
| 6213 | * to a missing object or permission. These events should be tracked |
| 6214 | * because if they happen often, it may indicate a brute force or a |
| 6215 | * vulnerability scan. |
| 6216 | */ |
| 6217 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6218 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6219 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6220 | if (objt_server(s->target)) |
| 6221 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6222 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6223 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6224 | * exactly one digit "." one digit. This check may be disabled using |
| 6225 | * option accept-invalid-http-response. |
| 6226 | */ |
| 6227 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6228 | if (msg->sl.st.v_l != 8) { |
| 6229 | msg->err_pos = 0; |
| 6230 | goto hdr_response_bad; |
| 6231 | } |
| 6232 | |
| 6233 | if (rep->buf->p[4] != '/' || |
| 6234 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6235 | rep->buf->p[6] != '.' || |
| 6236 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6237 | msg->err_pos = 4; |
| 6238 | goto hdr_response_bad; |
| 6239 | } |
| 6240 | } |
| 6241 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6242 | /* check if the response is HTTP/1.1 or above */ |
| 6243 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6244 | ((rep->buf->p[5] > '1') || |
| 6245 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6246 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6247 | |
| 6248 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6249 | 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] | 6250 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6251 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6252 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6253 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6254 | 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] | 6255 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6256 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6257 | * and 505 are triggered on demand by client request, so we must not |
| 6258 | * count them as server failures. |
| 6259 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6260 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6261 | 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] | 6262 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6263 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6264 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6265 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6266 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6267 | /* |
| 6268 | * 2: check for cacheability. |
| 6269 | */ |
| 6270 | |
| 6271 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6272 | case 100: |
| 6273 | /* |
| 6274 | * We may be facing a 100-continue response, in which case this |
| 6275 | * is not the right response, and we're waiting for the next one. |
| 6276 | * Let's allow this response to go to the client and wait for the |
| 6277 | * next one. |
| 6278 | */ |
| 6279 | hdr_idx_init(&txn->hdr_idx); |
| 6280 | msg->next -= channel_forward(rep, msg->next); |
| 6281 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6282 | txn->status = 0; |
| 6283 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6284 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6285 | goto next_one; |
| 6286 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6287 | case 200: |
| 6288 | case 203: |
| 6289 | case 206: |
| 6290 | case 300: |
| 6291 | case 301: |
| 6292 | case 410: |
| 6293 | /* RFC2616 @13.4: |
| 6294 | * "A response received with a status code of |
| 6295 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6296 | * by a cache (...) unless a cache-control |
| 6297 | * directive prohibits caching." |
| 6298 | * |
| 6299 | * RFC2616 @9.5: POST method : |
| 6300 | * "Responses to this method are not cacheable, |
| 6301 | * unless the response includes appropriate |
| 6302 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6303 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6304 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6305 | ((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] | 6306 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6307 | break; |
| 6308 | default: |
| 6309 | break; |
| 6310 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6311 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6312 | /* |
| 6313 | * 3: we may need to capture headers |
| 6314 | */ |
| 6315 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6316 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6317 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6318 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6319 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6320 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6321 | * by RFC7230#3.3.3 : |
| 6322 | * |
| 6323 | * The length of a message body is determined by one of the following |
| 6324 | * (in order of precedence): |
| 6325 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6326 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 6327 | * the connection will become a tunnel immediately after the empty |
| 6328 | * line that concludes the header fields. A client MUST ignore |
| 6329 | * any Content-Length or Transfer-Encoding header fields received |
| 6330 | * in such a message. Any 101 response (Switching Protocols) is |
| 6331 | * managed in the same manner. |
| 6332 | * |
| 6333 | * 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] | 6334 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6335 | * code is always terminated by the first empty line after the |
| 6336 | * header fields, regardless of the header fields present in the |
| 6337 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6338 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6339 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6340 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6341 | * body length is determined by reading and decoding the chunked |
| 6342 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6343 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6344 | * If a Transfer-Encoding header field is present in a response and |
| 6345 | * the chunked transfer coding is not the final encoding, the |
| 6346 | * message body length is determined by reading the connection until |
| 6347 | * it is closed by the server. If a Transfer-Encoding header field |
| 6348 | * is present in a request and the chunked transfer coding is not |
| 6349 | * the final encoding, the message body length cannot be determined |
| 6350 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6351 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6352 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6353 | * If a message is received with both a Transfer-Encoding and a |
| 6354 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6355 | * Content-Length. Such a message might indicate an attempt to |
| 6356 | * perform request smuggling (Section 9.5) or response splitting |
| 6357 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6358 | * remove the received Content-Length field prior to forwarding such |
| 6359 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6360 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6361 | * 4. If a message is received without Transfer-Encoding and with |
| 6362 | * either multiple Content-Length header fields having differing |
| 6363 | * field-values or a single Content-Length header field having an |
| 6364 | * invalid value, then the message framing is invalid and the |
| 6365 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6366 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6367 | * status code and then close the connection. If this is a response |
| 6368 | * message received by a proxy, the proxy MUST close the connection |
| 6369 | * to the server, discard the received response, and send a 502 (Bad |
| 6370 | * Gateway) response to the client. If this is a response message |
| 6371 | * received by a user agent, the user agent MUST close the |
| 6372 | * connection to the server and discard the received response. |
| 6373 | * |
| 6374 | * 5. If a valid Content-Length header field is present without |
| 6375 | * Transfer-Encoding, its decimal value defines the expected message |
| 6376 | * body length in octets. If the sender closes the connection or |
| 6377 | * the recipient times out before the indicated number of octets are |
| 6378 | * received, the recipient MUST consider the message to be |
| 6379 | * incomplete and close the connection. |
| 6380 | * |
| 6381 | * 6. If this is a request message and none of the above are true, then |
| 6382 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6383 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6384 | * 7. Otherwise, this is a response message without a declared message |
| 6385 | * body length, so the message body length is determined by the |
| 6386 | * number of octets received prior to the server closing the |
| 6387 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6388 | */ |
| 6389 | |
| 6390 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6391 | * 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] | 6392 | * 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] | 6393 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6394 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6395 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6396 | txn->status == 101)) { |
| 6397 | /* Either we've established an explicit tunnel, or we're |
| 6398 | * switching the protocol. In both cases, we're very unlikely |
| 6399 | * to understand the next protocols. We have to switch to tunnel |
| 6400 | * mode, so that we transfer the request and responses then let |
| 6401 | * this protocol pass unmodified. When we later implement specific |
| 6402 | * parsers for such protocols, we'll want to check the Upgrade |
| 6403 | * header which contains information about that protocol for |
| 6404 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 6405 | */ |
| 6406 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6407 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 6408 | goto end; |
| 6409 | } |
| 6410 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6411 | if (txn->meth == HTTP_METH_HEAD || |
| 6412 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6413 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6414 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6415 | goto skip_content_length; |
| 6416 | } |
| 6417 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6418 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6419 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6420 | 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] | 6421 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6422 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6423 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6424 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6425 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6426 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6427 | break; |
| 6428 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6429 | } |
| 6430 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6431 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6432 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6433 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6434 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6435 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6436 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6437 | 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] | 6438 | signed long long cl; |
| 6439 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6440 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6441 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6442 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6443 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6444 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6445 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6446 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6447 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6448 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6449 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6450 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6451 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6452 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6453 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6454 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6455 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6456 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6457 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6458 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6459 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6460 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6461 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6462 | } |
| 6463 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6464 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6465 | /* Now we have to check if we need to modify the Connection header. |
| 6466 | * This is more difficult on the response than it is on the request, |
| 6467 | * because we can have two different HTTP versions and we don't know |
| 6468 | * how the client will interprete a response. For instance, let's say |
| 6469 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6470 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6471 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6472 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6473 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6474 | * indicating how a request MAY be understood by the client. In case |
| 6475 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6476 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6477 | * will fall back to explicit close. Note that we won't take risks with |
| 6478 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6479 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6480 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6481 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6482 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 6483 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6484 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6485 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6486 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6487 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6488 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6489 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6490 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6491 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6492 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6493 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6494 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6495 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6496 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6497 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6498 | /* now adjust header transformations depending on current state */ |
| 6499 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6500 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6501 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6502 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6503 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6504 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6505 | else { /* SCL / KAL */ |
| 6506 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6507 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6508 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6509 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6510 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6511 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6512 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6513 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6514 | /* Some keep-alive responses are converted to Server-close if |
| 6515 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6516 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6517 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6518 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6519 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6520 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6521 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6522 | } |
| 6523 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 6524 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6525 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6526 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6527 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6528 | /* end of job, return OK */ |
| 6529 | rep->analysers &= ~an_bit; |
| 6530 | rep->analyse_exp = TICK_ETERNITY; |
| 6531 | channel_auto_close(rep); |
| 6532 | return 1; |
| 6533 | |
| 6534 | abort_keep_alive: |
| 6535 | /* A keep-alive request to the server failed on a network error. |
| 6536 | * The client is required to retry. We need to close without returning |
| 6537 | * any other information so that the client retries. |
| 6538 | */ |
| 6539 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6540 | rep->analysers &= AN_RES_FLT_END; |
| 6541 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6542 | channel_auto_close(rep); |
| 6543 | s->logs.logwait = 0; |
| 6544 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6545 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6546 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6547 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6548 | return 0; |
| 6549 | } |
| 6550 | |
| 6551 | /* This function performs all the processing enabled for the current response. |
| 6552 | * 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] | 6553 | * 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] | 6554 | * other functions. It works like process_request (see indications above). |
| 6555 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6556 | 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] | 6557 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6558 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6559 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6560 | struct http_msg *msg = &txn->rsp; |
| 6561 | struct proxy *cur_proxy; |
| 6562 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6563 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6564 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6565 | 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] | 6566 | now_ms, __FUNCTION__, |
| 6567 | s, |
| 6568 | rep, |
| 6569 | rep->rex, rep->wex, |
| 6570 | rep->flags, |
| 6571 | rep->buf->i, |
| 6572 | rep->analysers); |
| 6573 | |
| 6574 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6575 | return 0; |
| 6576 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6577 | /* The stats applet needs to adjust the Connection header but we don't |
| 6578 | * apply any filter there. |
| 6579 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6580 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6581 | rep->analysers &= ~an_bit; |
| 6582 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6583 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6584 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6585 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6586 | /* |
| 6587 | * We will have to evaluate the filters. |
| 6588 | * As opposed to version 1.2, now they will be evaluated in the |
| 6589 | * filters order and not in the header order. This means that |
| 6590 | * each filter has to be validated among all headers. |
| 6591 | * |
| 6592 | * Filters are tried with ->be first, then with ->fe if it is |
| 6593 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6594 | * |
| 6595 | * Maybe we are in resume condiion. In this case I choose the |
| 6596 | * "struct proxy" which contains the rule list matching the resume |
| 6597 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6598 | * the process with the first one. |
| 6599 | * |
| 6600 | * In fact, I check only correspondance betwwen the current list |
| 6601 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6602 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6603 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6604 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6605 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6606 | else |
| 6607 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6608 | while (1) { |
| 6609 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6610 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6611 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6612 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6613 | 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] | 6614 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6615 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6616 | goto return_srv_prx_502; |
| 6617 | |
| 6618 | if (ret == HTTP_RULE_RES_DONE) { |
| 6619 | rep->analysers &= ~an_bit; |
| 6620 | rep->analyse_exp = TICK_ETERNITY; |
| 6621 | return 1; |
| 6622 | } |
| 6623 | } |
| 6624 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6625 | /* we need to be called again. */ |
| 6626 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6627 | channel_dont_close(rep); |
| 6628 | return 0; |
| 6629 | } |
| 6630 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6631 | /* try headers filters */ |
| 6632 | if (rule_set->rsp_exp != NULL) { |
| 6633 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6634 | return_bad_resp: |
| 6635 | if (objt_server(s->target)) { |
| 6636 | objt_server(s->target)->counters.failed_resp++; |
| 6637 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6638 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6639 | s->be->be_counters.failed_resp++; |
| 6640 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6641 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6642 | txn->status = 502; |
| 6643 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6644 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6645 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 6646 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6647 | if (!(s->flags & SF_ERR_MASK)) |
| 6648 | s->flags |= SF_ERR_PRXCOND; |
| 6649 | if (!(s->flags & SF_FINST_MASK)) |
| 6650 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6651 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6652 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6653 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6654 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6655 | /* has the response been denied ? */ |
| 6656 | if (txn->flags & TX_SVDENY) { |
| 6657 | if (objt_server(s->target)) |
| 6658 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6659 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6660 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6661 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6662 | if (sess->listener->counters) |
| 6663 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6664 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6665 | goto return_srv_prx_502; |
| 6666 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6667 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6668 | /* add response headers from the rule sets in the same order */ |
| 6669 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6670 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6671 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6672 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6673 | 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] | 6674 | ret = acl_pass(ret); |
| 6675 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6676 | ret = !ret; |
| 6677 | if (!ret) |
| 6678 | continue; |
| 6679 | } |
| 6680 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6681 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6682 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6683 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6684 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6685 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6686 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6687 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6688 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6689 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6690 | /* After this point, this anayzer can't return yield, so we can |
| 6691 | * remove the bit corresponding to this analyzer from the list. |
| 6692 | * |
| 6693 | * Note that the intermediate returns and goto found previously |
| 6694 | * reset the analyzers. |
| 6695 | */ |
| 6696 | rep->analysers &= ~an_bit; |
| 6697 | rep->analyse_exp = TICK_ETERNITY; |
| 6698 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6699 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6700 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6701 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6702 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6703 | /* |
| 6704 | * Now check for a server cookie. |
| 6705 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6706 | 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] | 6707 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6708 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6709 | /* |
| 6710 | * Check for cache-control or pragma headers if required. |
| 6711 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6712 | 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] | 6713 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6714 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6715 | /* |
| 6716 | * Add server cookie in the response if needed |
| 6717 | */ |
| 6718 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6719 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6720 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6721 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6722 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6723 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6724 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6725 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6726 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6727 | /* the server is known, it's not the one the client requested, or the |
| 6728 | * cookie's last seen date needs to be refreshed. We have to |
| 6729 | * insert a set-cookie here, except if we want to insert only on POST |
| 6730 | * requests and this one isn't. Note that servers which don't have cookies |
| 6731 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6732 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6733 | if (!objt_server(s->target)->cookie) { |
| 6734 | chunk_printf(&trash, |
| 6735 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6736 | s->be->cookie_name); |
| 6737 | } |
| 6738 | else { |
| 6739 | 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] | 6740 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6741 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6742 | /* emit last_date, which is mandatory */ |
| 6743 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6744 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6745 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6746 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6747 | if (s->be->cookie_maxlife) { |
| 6748 | /* emit first_date, which is either the original one or |
| 6749 | * the current date. |
| 6750 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6751 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6752 | s30tob64(txn->cookie_first_date ? |
| 6753 | txn->cookie_first_date >> 2 : |
| 6754 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6755 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6756 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6757 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6758 | chunk_appendf(&trash, "; path=/"); |
| 6759 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6760 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6761 | if (s->be->cookie_domain) |
| 6762 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6763 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6764 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6765 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6766 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6767 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6768 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6769 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6770 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6771 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6772 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6773 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6774 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6775 | /* the server did not change, only the date was updated */ |
| 6776 | txn->flags |= TX_SCK_UPDATED; |
| 6777 | else |
| 6778 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6779 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6780 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6781 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6782 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6783 | * 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] | 6784 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6785 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6786 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6787 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6788 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6789 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6790 | "Cache-control: private", 22) < 0)) |
| 6791 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6792 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6793 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6794 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6795 | /* |
| 6796 | * Check if result will be cacheable with a cookie. |
| 6797 | * We'll block the response if security checks have caught |
| 6798 | * nasty things such as a cacheable cookie. |
| 6799 | */ |
| 6800 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6801 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6802 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6803 | /* we're in presence of a cacheable response containing |
| 6804 | * a set-cookie header. We'll block it as requested by |
| 6805 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6806 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6807 | if (objt_server(s->target)) |
| 6808 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6809 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6810 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6811 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6812 | if (sess->listener->counters) |
| 6813 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6814 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6815 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6816 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6817 | send_log(s->be, LOG_ALERT, |
| 6818 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6819 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6820 | goto return_srv_prx_502; |
| 6821 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6822 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6823 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6824 | /* |
| 6825 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6826 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6827 | * 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] | 6828 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6829 | * want to touch any 101 response either since it's switching to another |
| 6830 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6831 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6832 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6833 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6834 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6835 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6836 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6837 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6838 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6839 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6840 | /* we want a keep-alive response here. Keep-alive header |
| 6841 | * required if either side is not 1.1. |
| 6842 | */ |
| 6843 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6844 | want_flags |= TX_CON_KAL_SET; |
| 6845 | } |
| 6846 | else { |
| 6847 | /* we want a close response here. Close header required if |
| 6848 | * the server is 1.1, regardless of the client. |
| 6849 | */ |
| 6850 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6851 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6852 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6853 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6854 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6855 | http_change_connection_header(txn, msg, want_flags); |
| 6856 | } |
| 6857 | |
| 6858 | skip_header_mangling: |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6859 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, rep) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6860 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6861 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6862 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6863 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6864 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6865 | /* if the user wants to log as soon as possible, without counting |
| 6866 | * bytes from the server, then this is the right moment. We have |
| 6867 | * to temporarily assign bytes_out to log what we currently have. |
| 6868 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6869 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6870 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6871 | s->logs.bytes_out = txn->rsp.eoh; |
| 6872 | s->do_log(s); |
| 6873 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6874 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6875 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6876 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6877 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6878 | /* This function is an analyser which forwards response body (including chunk |
| 6879 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6880 | * zero byte. The only situation where it must not be called is when we're in |
| 6881 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6882 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6883 | * 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] | 6884 | * 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] | 6885 | * |
| 6886 | * It is capable of compressing response data both in content-length mode and |
| 6887 | * in chunked mode. The state machines follows different flows depending on |
| 6888 | * whether content-length and chunked modes are used, since there are no |
| 6889 | * trailers in content-length : |
| 6890 | * |
| 6891 | * chk-mode cl-mode |
| 6892 | * ,----- BODY -----. |
| 6893 | * / \ |
| 6894 | * V size > 0 V chk-mode |
| 6895 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6896 | * | | size == 0 | last byte | |
| 6897 | * | v final crlf v inspected | |
| 6898 | * | TRAILERS -----------> DONE | |
| 6899 | * | | |
| 6900 | * `----------------------------------------------' |
| 6901 | * |
| 6902 | * Compression only happens in the DATA state, and must be flushed in final |
| 6903 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6904 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6905 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6906 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6907 | 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] | 6908 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6909 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6910 | struct http_txn *txn = s->txn; |
| 6911 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6912 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6913 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6914 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6915 | return 0; |
| 6916 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6917 | 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] | 6918 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6919 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6920 | /* Output closed while we were sending data. We must abort and |
| 6921 | * wake the other side up. |
| 6922 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6923 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6924 | msg->msg_state = HTTP_MSG_ERROR; |
| 6925 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6926 | return 1; |
| 6927 | } |
| 6928 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6929 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6930 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6931 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6932 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6933 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6934 | ? HTTP_MSG_CHUNK_SIZE |
| 6935 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6936 | } |
| 6937 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6938 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6939 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6940 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6941 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6942 | } |
| 6943 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6944 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6945 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6946 | ? http_msg_forward_chunked_body(s, msg) |
| 6947 | : http_msg_forward_body(s, msg)); |
| 6948 | if (!ret) |
| 6949 | goto missing_data_or_waiting; |
| 6950 | if (ret < 0) |
| 6951 | goto return_bad_res; |
| 6952 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6953 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6954 | /* other states, DONE...TUNNEL */ |
| 6955 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6956 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6957 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6958 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6959 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6960 | if (http_resync_states(s)) { |
| 6961 | /* some state changes occurred, maybe the analyser was disabled |
| 6962 | * too. */ |
| 6963 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6964 | if (res->flags & CF_SHUTW) { |
| 6965 | /* response errors are most likely due to the |
| 6966 | * client aborting the transfer. */ |
| 6967 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6968 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6969 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6970 | 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] | 6971 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6972 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6973 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6974 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6975 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6976 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6977 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6978 | if (res->flags & CF_SHUTW) |
| 6979 | goto aborted_xfer; |
| 6980 | |
| 6981 | /* stop waiting for data if the input is closed before the end. If the |
| 6982 | * client side was already closed, it means that the client has aborted, |
| 6983 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6984 | * server abort. |
| 6985 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6986 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6987 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6988 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6989 | /* If we have some pending data, we continue the processing */ |
| 6990 | if (!buffer_pending(res->buf)) { |
| 6991 | if (!(s->flags & SF_ERR_MASK)) |
| 6992 | s->flags |= SF_ERR_SRVCL; |
| 6993 | s->be->be_counters.srv_aborts++; |
| 6994 | if (objt_server(s->target)) |
| 6995 | objt_server(s->target)->counters.srv_aborts++; |
| 6996 | goto return_bad_res_stats_ok; |
| 6997 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6998 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6999 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7000 | /* 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] | 7001 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7002 | goto return_bad_res; |
| 7003 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7004 | /* When TE: chunked is used, we need to get there again to parse |
| 7005 | * remaining chunks even if the server has closed, so we don't want to |
| 7006 | * set CF_DONTCLOSE. Similarly, if the body length is undefined, if |
| 7007 | * keep-alive is set on the client side or if there are filters |
| 7008 | * registered on the stream, we don't want to forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7009 | */ |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 7010 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || !(msg->flags & HTTP_MSGF_XFER_LEN) || |
| 7011 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7012 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7013 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7014 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7015 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7016 | /* 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] | 7017 | * 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] | 7018 | * 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] | 7019 | * modes are already handled by the stream sock layer. We must not do |
| 7020 | * this in content-length mode because it could present the MSG_MORE |
| 7021 | * flag with the last block of forwarded data, which would cause an |
| 7022 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7023 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 7024 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7025 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7026 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7027 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7028 | return 0; |
| 7029 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7030 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7031 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7032 | if (objt_server(s->target)) |
| 7033 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7034 | |
| 7035 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 7036 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7037 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7038 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 7039 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 7040 | res->analysers &= AN_RES_FLT_END; |
| 7041 | 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] | 7042 | if (objt_server(s->target)) |
| 7043 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7044 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7045 | if (!(s->flags & SF_ERR_MASK)) |
| 7046 | s->flags |= SF_ERR_PRXCOND; |
| 7047 | if (!(s->flags & SF_FINST_MASK)) |
| 7048 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7049 | return 0; |
| 7050 | |
| 7051 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 7052 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7053 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7054 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 7055 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 7056 | res->analysers &= AN_RES_FLT_END; |
| 7057 | 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] | 7058 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7059 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7060 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7061 | if (objt_server(s->target)) |
| 7062 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7063 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7064 | if (!(s->flags & SF_ERR_MASK)) |
| 7065 | s->flags |= SF_ERR_CLICL; |
| 7066 | if (!(s->flags & SF_FINST_MASK)) |
| 7067 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7068 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | |
| 7072 | static inline int |
| 7073 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 7074 | { |
| 7075 | struct channel *chn = msg->chn; |
| 7076 | int ret; |
| 7077 | |
| 7078 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 7079 | |
| 7080 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 7081 | goto ending; |
| 7082 | |
| 7083 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 7084 | * read the body until the server connection is closed. In that case, we |
| 7085 | * eat data as they come. Of course, this happens for response only. */ |
| 7086 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 7087 | unsigned long long len = (chn->buf->i - msg->next); |
| 7088 | msg->chunk_len += len; |
| 7089 | msg->body_len += len; |
| 7090 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7091 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7092 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7093 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7094 | msg->next += ret; |
| 7095 | msg->chunk_len -= ret; |
| 7096 | if (msg->chunk_len) { |
| 7097 | /* input empty or output full */ |
| 7098 | if (chn->buf->i > msg->next) |
| 7099 | chn->flags |= CF_WAKE_WRITE; |
| 7100 | goto missing_data_or_waiting; |
| 7101 | } |
| 7102 | |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 7103 | /* The server still sending data that should be filtered */ |
| 7104 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7105 | goto missing_data_or_waiting; |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 7106 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7107 | msg->msg_state = HTTP_MSG_ENDING; |
| 7108 | |
| 7109 | ending: |
| 7110 | /* we may have some pending data starting at res->buf->p such as a last |
| 7111 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7112 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7113 | /* default_ret */ msg->next, |
| 7114 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7115 | b_adv(chn->buf, ret); |
| 7116 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7117 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7118 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7119 | if (msg->next) |
| 7120 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7121 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7122 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 7123 | /* default_ret */ 1, |
| 7124 | /* on_error */ goto error, |
| 7125 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7126 | msg->msg_state = HTTP_MSG_DONE; |
| 7127 | return 1; |
| 7128 | |
| 7129 | missing_data_or_waiting: |
| 7130 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7131 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 7132 | /* default_ret */ msg->next, |
| 7133 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7134 | b_adv(chn->buf, ret); |
| 7135 | msg->next -= ret; |
| 7136 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7137 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7138 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7139 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7140 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7141 | return 0; |
| 7142 | error: |
| 7143 | return -1; |
| 7144 | } |
| 7145 | |
| 7146 | static inline int |
| 7147 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 7148 | { |
| 7149 | struct channel *chn = msg->chn; |
| 7150 | int ret; |
| 7151 | |
| 7152 | /* Here we have the guarantee to be in one of the following state: |
| 7153 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 7154 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 7155 | |
| 7156 | switch_states: |
| 7157 | switch (msg->msg_state) { |
| 7158 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7159 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 7160 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 7161 | /* on_error */ goto error); |
| 7162 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7163 | msg->chunk_len -= ret; |
| 7164 | if (msg->chunk_len) { |
| 7165 | /* input empty or output full */ |
| 7166 | if (chn->buf->i > msg->next) |
| 7167 | chn->flags |= CF_WAKE_WRITE; |
| 7168 | goto missing_data_or_waiting; |
| 7169 | } |
| 7170 | |
| 7171 | /* nothing left to forward for this chunk*/ |
| 7172 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 7173 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7174 | |
| 7175 | case HTTP_MSG_CHUNK_CRLF: |
| 7176 | /* we want the CRLF after the data */ |
| 7177 | ret = http_skip_chunk_crlf(msg); |
| 7178 | if (ret == 0) |
| 7179 | goto missing_data_or_waiting; |
| 7180 | if (ret < 0) |
| 7181 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7182 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7183 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 7184 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 7185 | |
| 7186 | case HTTP_MSG_CHUNK_SIZE: |
| 7187 | /* read the chunk size and assign it to ->chunk_len, |
| 7188 | * then set ->next to point to the body and switch to |
| 7189 | * DATA or TRAILERS state. |
| 7190 | */ |
| 7191 | ret = http_parse_chunk_size(msg); |
| 7192 | if (ret == 0) |
| 7193 | goto missing_data_or_waiting; |
| 7194 | if (ret < 0) |
| 7195 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 7196 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7197 | if (msg->chunk_len) { |
| 7198 | msg->msg_state = HTTP_MSG_DATA; |
| 7199 | goto switch_states; |
| 7200 | } |
| 7201 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 7202 | /* fall through for HTTP_MSG_TRAILERS */ |
| 7203 | |
| 7204 | case HTTP_MSG_TRAILERS: |
| 7205 | ret = http_forward_trailers(msg); |
| 7206 | if (ret < 0) |
| 7207 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7208 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 7209 | /* default_ret */ 1, |
| 7210 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7211 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7212 | if (!ret) |
| 7213 | goto missing_data_or_waiting; |
| 7214 | break; |
| 7215 | |
| 7216 | case HTTP_MSG_ENDING: |
| 7217 | goto ending; |
| 7218 | |
| 7219 | default: |
| 7220 | /* This should no happen in this function */ |
| 7221 | goto error; |
| 7222 | } |
| 7223 | |
| 7224 | msg->msg_state = HTTP_MSG_ENDING; |
| 7225 | ending: |
| 7226 | /* we may have some pending data starting at res->buf->p such as a last |
| 7227 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7228 | 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] | 7229 | /* default_ret */ msg->next, |
| 7230 | /* on_error */ goto error); |
| 7231 | b_adv(chn->buf, ret); |
| 7232 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7233 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 7234 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7235 | if (msg->next) |
| 7236 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 7237 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7238 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7239 | /* default_ret */ 1, |
| 7240 | /* on_error */ goto error, |
| 7241 | /* on_wait */ goto waiting); |
| 7242 | msg->msg_state = HTTP_MSG_DONE; |
| 7243 | return 1; |
| 7244 | |
| 7245 | missing_data_or_waiting: |
| 7246 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 7247 | 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] | 7248 | /* default_ret */ msg->next, |
| 7249 | /* on_error */ goto error); |
| 7250 | b_adv(chn->buf, ret); |
| 7251 | msg->next -= ret; |
| 7252 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 7253 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 7254 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7255 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 7256 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7257 | return 0; |
| 7258 | |
| 7259 | chunk_parsing_error: |
| 7260 | if (msg->err_pos >= 0) { |
| 7261 | if (chn->flags & CF_ISRESP) |
| 7262 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 7263 | msg->msg_state, strm_fe(s)); |
| 7264 | else |
| 7265 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 7266 | msg, msg->msg_state, s->be); |
| 7267 | } |
| 7268 | error: |
| 7269 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7270 | } |
| 7271 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 7272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7273 | /* Iterate the same filter through all request headers. |
| 7274 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7275 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7276 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7277 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7278 | 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] | 7279 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7280 | char *cur_ptr, *cur_end, *cur_next; |
| 7281 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7282 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7283 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7284 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7285 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7286 | last_hdr = 0; |
| 7287 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7288 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7289 | old_idx = 0; |
| 7290 | |
| 7291 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7292 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7293 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7294 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7295 | (exp->action == ACT_ALLOW || |
| 7296 | exp->action == ACT_DENY || |
| 7297 | exp->action == ACT_TARPIT)) |
| 7298 | return 0; |
| 7299 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7300 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7301 | if (!cur_idx) |
| 7302 | break; |
| 7303 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7304 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7305 | cur_ptr = cur_next; |
| 7306 | cur_end = cur_ptr + cur_hdr->len; |
| 7307 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7308 | |
| 7309 | /* Now we have one header between cur_ptr and cur_end, |
| 7310 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7311 | */ |
| 7312 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7313 | 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] | 7314 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7315 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7316 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7317 | last_hdr = 1; |
| 7318 | break; |
| 7319 | |
| 7320 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7321 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7322 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7323 | break; |
| 7324 | |
| 7325 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7326 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7327 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7328 | break; |
| 7329 | |
| 7330 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7331 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7332 | if (trash.len < 0) |
| 7333 | return -1; |
| 7334 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7335 | 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] | 7336 | /* FIXME: if the user adds a newline in the replacement, the |
| 7337 | * index will not be recalculated for now, and the new line |
| 7338 | * will not be counted as a new header. |
| 7339 | */ |
| 7340 | |
| 7341 | cur_end += delta; |
| 7342 | cur_next += delta; |
| 7343 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7344 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7345 | break; |
| 7346 | |
| 7347 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7348 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7349 | cur_next += delta; |
| 7350 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7351 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7352 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7353 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7354 | cur_hdr->len = 0; |
| 7355 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7356 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7357 | break; |
| 7358 | |
| 7359 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7360 | } |
| 7361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7362 | /* keep the link from this header to next one in case of later |
| 7363 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7364 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7365 | old_idx = cur_idx; |
| 7366 | } |
| 7367 | return 0; |
| 7368 | } |
| 7369 | |
| 7370 | |
| 7371 | /* Apply the filter to the request line. |
| 7372 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7373 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7374 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7375 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7376 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7377 | 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] | 7378 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7379 | char *cur_ptr, *cur_end; |
| 7380 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7381 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7382 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7383 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7384 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7385 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7386 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7387 | (exp->action == ACT_ALLOW || |
| 7388 | exp->action == ACT_DENY || |
| 7389 | exp->action == ACT_TARPIT)) |
| 7390 | return 0; |
| 7391 | else if (exp->action == ACT_REMOVE) |
| 7392 | return 0; |
| 7393 | |
| 7394 | done = 0; |
| 7395 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7396 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7397 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7398 | |
| 7399 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7400 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7401 | 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] | 7402 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7403 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7404 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7405 | done = 1; |
| 7406 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7408 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7409 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7410 | done = 1; |
| 7411 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7413 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7414 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7415 | done = 1; |
| 7416 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7418 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7419 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7420 | if (trash.len < 0) |
| 7421 | return -1; |
| 7422 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7423 | 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] | 7424 | /* FIXME: if the user adds a newline in the replacement, the |
| 7425 | * index will not be recalculated for now, and the new line |
| 7426 | * will not be counted as a new header. |
| 7427 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7428 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7429 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7430 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7431 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7432 | HTTP_MSG_RQMETH, |
| 7433 | cur_ptr, cur_end + 1, |
| 7434 | NULL, NULL); |
| 7435 | if (unlikely(!cur_end)) |
| 7436 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7437 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7438 | /* we have a full request and we know that we have either a CR |
| 7439 | * or an LF at <ptr>. |
| 7440 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7441 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7442 | 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] | 7443 | /* there is no point trying this regex on headers */ |
| 7444 | return 1; |
| 7445 | } |
| 7446 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7447 | return done; |
| 7448 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7449 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7450 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7451 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7452 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7453 | * 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] | 7454 | * 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] | 7455 | * unparsable request. Since it can manage the switch to another backend, it |
| 7456 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7457 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7458 | 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] | 7459 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7460 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7461 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7462 | struct hdr_exp *exp; |
| 7463 | |
| 7464 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7465 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7466 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7467 | /* |
| 7468 | * The interleaving of transformations and verdicts |
| 7469 | * makes it difficult to decide to continue or stop |
| 7470 | * the evaluation. |
| 7471 | */ |
| 7472 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7473 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7474 | break; |
| 7475 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7476 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7477 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7478 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7479 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7480 | |
| 7481 | /* if this filter had a condition, evaluate it now and skip to |
| 7482 | * next filter if the condition does not match. |
| 7483 | */ |
| 7484 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7485 | 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] | 7486 | ret = acl_pass(ret); |
| 7487 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7488 | ret = !ret; |
| 7489 | |
| 7490 | if (!ret) |
| 7491 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7492 | } |
| 7493 | |
| 7494 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7495 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7496 | if (unlikely(ret < 0)) |
| 7497 | return -1; |
| 7498 | |
| 7499 | if (likely(ret == 0)) { |
| 7500 | /* The filter did not match the request, it can be |
| 7501 | * iterated through all headers. |
| 7502 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7503 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7504 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7505 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7506 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7507 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7508 | } |
| 7509 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7510 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7511 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7512 | * same way as with headers above except that the semi-colon also ends a token. |
| 7513 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7514 | * return a valid result. |
| 7515 | */ |
| 7516 | char *find_cookie_value_end(char *s, const char *e) |
| 7517 | { |
| 7518 | int quoted, qdpair; |
| 7519 | |
| 7520 | quoted = qdpair = 0; |
| 7521 | for (; s < e; s++) { |
| 7522 | if (qdpair) qdpair = 0; |
| 7523 | else if (quoted) { |
| 7524 | if (*s == '\\') qdpair = 1; |
| 7525 | else if (*s == '"') quoted = 0; |
| 7526 | } |
| 7527 | else if (*s == '"') quoted = 1; |
| 7528 | else if (*s == ',' || *s == ';') return s; |
| 7529 | } |
| 7530 | return s; |
| 7531 | } |
| 7532 | |
| 7533 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7534 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7535 | * the first delimiter is updated if required. The function tries as much as |
| 7536 | * possible to respect the following principles : |
| 7537 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7538 | * colon, in which case <next> is simply removed |
| 7539 | * - set exactly one space character after the new first delimiter, unless |
| 7540 | * there are not enough characters in the block being moved to do so. |
| 7541 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7542 | * one. |
| 7543 | * |
| 7544 | * It is the caller's responsibility to ensure that : |
| 7545 | * - <from> points to a valid delimiter or the colon ; |
| 7546 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7547 | * - there are non-space chars before <from> ; |
| 7548 | * - there is a CR/LF at or after <next>. |
| 7549 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7550 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7551 | { |
| 7552 | char *prev = *from; |
| 7553 | |
| 7554 | if (*prev == ':') { |
| 7555 | /* We're removing the first value, preserve the colon and add a |
| 7556 | * space if possible. |
| 7557 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7558 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7559 | next++; |
| 7560 | prev++; |
| 7561 | if (prev < next) |
| 7562 | *prev++ = ' '; |
| 7563 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7564 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7565 | next++; |
| 7566 | } else { |
| 7567 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7568 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7569 | prev--; |
| 7570 | *from = prev; |
| 7571 | |
| 7572 | /* copy the delimiter and if possible a space if we're |
| 7573 | * not at the end of the line. |
| 7574 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7575 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7576 | *prev++ = *next++; |
| 7577 | if (prev + 1 < next) |
| 7578 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7579 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7580 | next++; |
| 7581 | } |
| 7582 | } |
| 7583 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7584 | } |
| 7585 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7586 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7587 | * 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] | 7588 | * desirable to call it only when needed. This code is quite complex because |
| 7589 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7590 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7591 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7592 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7593 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7594 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7595 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7596 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7597 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7598 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7599 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
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 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7602 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7603 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7604 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7605 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7606 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7607 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7608 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7609 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7610 | hdr_beg = hdr_next; |
| 7611 | hdr_end = hdr_beg + cur_hdr->len; |
| 7612 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7613 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7614 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7615 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7616 | * "Cookie:" headers. |
| 7617 | */ |
| 7618 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7619 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7620 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7621 | old_idx = cur_idx; |
| 7622 | continue; |
| 7623 | } |
| 7624 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7625 | del_from = NULL; /* nothing to be deleted */ |
| 7626 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7627 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7628 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7629 | * attributes whose name begin with a '$', and associate them with |
| 7630 | * the right cookie, if we want to delete this cookie. |
| 7631 | * So there are 3 cases for each cookie read : |
| 7632 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7633 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7634 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7635 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7636 | * "special" cookie. |
| 7637 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7638 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7639 | * *MUST* delete it. |
| 7640 | * |
| 7641 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7642 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7643 | * the RFC explicitly allows spaces before it, and not within the |
| 7644 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7645 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7646 | * after the equal sign too, resulting in some (rare) buggy |
| 7647 | * implementations trying to do that. So let's do what servers do. |
| 7648 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7649 | * allowed quoted strings in values, with any possible character |
| 7650 | * after a backslash, including control chars and delimitors, which |
| 7651 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7652 | * within values even without quotes. |
| 7653 | * |
| 7654 | * We have to keep multiple pointers in order to support cookie |
| 7655 | * removal at the beginning, middle or end of header without |
| 7656 | * corrupting the header. All of these headers are valid : |
| 7657 | * |
| 7658 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7659 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7660 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7661 | * | | | | | | | | | |
| 7662 | * | | | | | | | | hdr_end <--+ |
| 7663 | * | | | | | | | +--> next |
| 7664 | * | | | | | | +----> val_end |
| 7665 | * | | | | | +-----------> val_beg |
| 7666 | * | | | | +--------------> equal |
| 7667 | * | | | +----------------> att_end |
| 7668 | * | | +---------------------> att_beg |
| 7669 | * | +--------------------------> prev |
| 7670 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7671 | */ |
| 7672 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7673 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7674 | /* Iterate through all cookies on this line */ |
| 7675 | |
| 7676 | /* find att_beg */ |
| 7677 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7678 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7679 | att_beg++; |
| 7680 | |
| 7681 | /* find att_end : this is the first character after the last non |
| 7682 | * space before the equal. It may be equal to hdr_end. |
| 7683 | */ |
| 7684 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7685 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7686 | while (equal < hdr_end) { |
| 7687 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7688 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7689 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7690 | continue; |
| 7691 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7692 | } |
| 7693 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7694 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7695 | * is between <att_beg> and <equal>, both may be identical. |
| 7696 | */ |
| 7697 | |
| 7698 | /* look for end of cookie if there is an equal sign */ |
| 7699 | if (equal < hdr_end && *equal == '=') { |
| 7700 | /* look for the beginning of the value */ |
| 7701 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7702 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7703 | val_beg++; |
| 7704 | |
| 7705 | /* find the end of the value, respecting quotes */ |
| 7706 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7707 | |
| 7708 | /* make val_end point to the first white space or delimitor after the value */ |
| 7709 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7710 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7711 | val_end--; |
| 7712 | } else { |
| 7713 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7714 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7715 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7716 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7717 | * they will automatically be removed if a header before them is removed, |
| 7718 | * since they're supposed to be linked together. |
| 7719 | */ |
| 7720 | if (*att_beg == '$') |
| 7721 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7722 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7723 | /* Ignore cookies with no equal sign */ |
| 7724 | if (equal == next) { |
| 7725 | /* This is not our cookie, so we must preserve it. But if we already |
| 7726 | * scheduled another cookie for removal, we cannot remove the |
| 7727 | * complete header, but we can remove the previous block itself. |
| 7728 | */ |
| 7729 | preserve_hdr = 1; |
| 7730 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7731 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7732 | val_end += delta; |
| 7733 | next += delta; |
| 7734 | hdr_end += delta; |
| 7735 | hdr_next += delta; |
| 7736 | cur_hdr->len += delta; |
| 7737 | http_msg_move_end(&txn->req, delta); |
| 7738 | prev = del_from; |
| 7739 | del_from = NULL; |
| 7740 | } |
| 7741 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7742 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7743 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7744 | /* if there are spaces around the equal sign, we need to |
| 7745 | * strip them otherwise we'll get trouble for cookie captures, |
| 7746 | * or even for rewrites. Since this happens extremely rarely, |
| 7747 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7748 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7749 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7750 | int stripped_before = 0; |
| 7751 | int stripped_after = 0; |
| 7752 | |
| 7753 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7754 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7755 | equal += stripped_before; |
| 7756 | val_beg += stripped_before; |
| 7757 | } |
| 7758 | |
| 7759 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7760 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7761 | val_beg += stripped_after; |
| 7762 | stripped_before += stripped_after; |
| 7763 | } |
| 7764 | |
| 7765 | val_end += stripped_before; |
| 7766 | next += stripped_before; |
| 7767 | hdr_end += stripped_before; |
| 7768 | hdr_next += stripped_before; |
| 7769 | cur_hdr->len += stripped_before; |
| 7770 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7771 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7772 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7773 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7774 | /* First, let's see if we want to capture this cookie. We check |
| 7775 | * that we don't already have a client side cookie, because we |
| 7776 | * can only capture one. Also as an optimisation, we ignore |
| 7777 | * cookies shorter than the declared name. |
| 7778 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7779 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7780 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7781 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7782 | int log_len = val_end - att_beg; |
| 7783 | |
| 7784 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7785 | Alert("HTTP logging : out of memory.\n"); |
| 7786 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7787 | if (log_len > sess->fe->capture_len) |
| 7788 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7789 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7790 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7791 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7792 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7793 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7794 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7795 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7796 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7797 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7798 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7799 | * For cookies in prefix mode, the form is : |
| 7800 | * |
| 7801 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7802 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7803 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7804 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7805 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7806 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7807 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7808 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7809 | * have the server ID between val_beg and delim, and the original cookie between |
| 7810 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7811 | * |
| 7812 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7813 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7814 | * | || || | |+-> next |
| 7815 | * | || || | +--> val_end |
| 7816 | * | || || +---------> delim |
| 7817 | * | || |+------------> val_beg |
| 7818 | * | || +-------------> att_end = equal |
| 7819 | * | |+-----------------> att_beg |
| 7820 | * | +------------------> prev |
| 7821 | * +-------------------------> hdr_beg |
| 7822 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7823 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7824 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7825 | for (delim = val_beg; delim < val_end; delim++) |
| 7826 | if (*delim == COOKIE_DELIM) |
| 7827 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7828 | } else { |
| 7829 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7830 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7831 | /* Now check if the cookie contains a date field, which would |
| 7832 | * appear after a vertical bar ('|') just after the server name |
| 7833 | * and before the delimiter. |
| 7834 | */ |
| 7835 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7836 | if (vbar1) { |
| 7837 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7838 | * right is the last seen date. It is a base64 encoded |
| 7839 | * 30-bit value representing the UNIX date since the |
| 7840 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7841 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7842 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7843 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7844 | if (val_end - vbar1 >= 5) { |
| 7845 | val = b64tos30(vbar1); |
| 7846 | if (val > 0) |
| 7847 | txn->cookie_last_date = val << 2; |
| 7848 | } |
| 7849 | /* look for a second vertical bar */ |
| 7850 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7851 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7852 | val = b64tos30(vbar1 + 1); |
| 7853 | if (val > 0) |
| 7854 | txn->cookie_first_date = val << 2; |
| 7855 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7856 | } |
| 7857 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7858 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7859 | /* if the cookie has an expiration date and the proxy wants to check |
| 7860 | * it, then we do that now. We first check if the cookie is too old, |
| 7861 | * then only if it has expired. We detect strict overflow because the |
| 7862 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7863 | * in the future are ignored if their offset is beyond one day. This |
| 7864 | * allows an admin to fix timezone issues without expiring everyone |
| 7865 | * and at the same time avoids keeping unwanted side effects for too |
| 7866 | * long. |
| 7867 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7868 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7869 | (((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] | 7870 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7871 | txn->flags &= ~TX_CK_MASK; |
| 7872 | txn->flags |= TX_CK_OLD; |
| 7873 | delim = val_beg; // let's pretend we have not found the cookie |
| 7874 | txn->cookie_first_date = 0; |
| 7875 | txn->cookie_last_date = 0; |
| 7876 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7877 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7878 | (((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] | 7879 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7880 | txn->flags &= ~TX_CK_MASK; |
| 7881 | txn->flags |= TX_CK_EXPIRED; |
| 7882 | delim = val_beg; // let's pretend we have not found the cookie |
| 7883 | txn->cookie_first_date = 0; |
| 7884 | txn->cookie_last_date = 0; |
| 7885 | } |
| 7886 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7887 | /* Here, we'll look for the first running server which supports the cookie. |
| 7888 | * This allows to share a same cookie between several servers, for example |
| 7889 | * to dedicate backup servers to specific servers only. |
| 7890 | * However, to prevent clients from sticking to cookie-less backup server |
| 7891 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7892 | * empty cookies and mark them as invalid. |
| 7893 | * The same behaviour is applied when persistence must be ignored. |
| 7894 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7895 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7896 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7897 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7898 | while (srv) { |
| 7899 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7900 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7901 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7902 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7903 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7904 | /* we found the server and we can use it */ |
| 7905 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7906 | 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] | 7907 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7908 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7909 | break; |
| 7910 | } else { |
| 7911 | /* we found a server, but it's down, |
| 7912 | * mark it as such and go on in case |
| 7913 | * another one is available. |
| 7914 | */ |
| 7915 | txn->flags &= ~TX_CK_MASK; |
| 7916 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7917 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7918 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7919 | srv = srv->next; |
| 7920 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7921 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7922 | 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] | 7923 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7924 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7925 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7926 | txn->flags |= TX_CK_UNUSED; |
| 7927 | else |
| 7928 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7929 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7930 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7931 | /* depending on the cookie mode, we may have to either : |
| 7932 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7933 | * the server never sees it ; |
| 7934 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7935 | * application cookie so that it does not get accidentely removed later, |
| 7936 | * if we're in cookie prefix mode |
| 7937 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7938 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7939 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7940 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7941 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7942 | val_end += delta; |
| 7943 | next += delta; |
| 7944 | hdr_end += delta; |
| 7945 | hdr_next += delta; |
| 7946 | cur_hdr->len += delta; |
| 7947 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7948 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7949 | del_from = NULL; |
| 7950 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7951 | } |
| 7952 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7953 | (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] | 7954 | del_from = prev; |
| 7955 | } |
| 7956 | } else { |
| 7957 | /* This is not our cookie, so we must preserve it. But if we already |
| 7958 | * scheduled another cookie for removal, we cannot remove the |
| 7959 | * complete header, but we can remove the previous block itself. |
| 7960 | */ |
| 7961 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7962 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7963 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7964 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7965 | if (att_beg >= del_from) |
| 7966 | att_beg += delta; |
| 7967 | if (att_end >= del_from) |
| 7968 | att_end += delta; |
| 7969 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7970 | val_end += delta; |
| 7971 | next += delta; |
| 7972 | hdr_end += delta; |
| 7973 | hdr_next += delta; |
| 7974 | cur_hdr->len += delta; |
| 7975 | http_msg_move_end(&txn->req, delta); |
| 7976 | prev = del_from; |
| 7977 | del_from = NULL; |
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 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7980 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7981 | /* continue with next cookie on this header line */ |
| 7982 | att_beg = next; |
| 7983 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7984 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7985 | /* There are no more cookies on this line. |
| 7986 | * We may still have one (or several) marked for deletion at the |
| 7987 | * end of the line. We must do this now in two ways : |
| 7988 | * - if some cookies must be preserved, we only delete from the |
| 7989 | * mark to the end of line ; |
| 7990 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7991 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7992 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7993 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7994 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7995 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7996 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7997 | cur_hdr->len += delta; |
| 7998 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7999 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8000 | |
| 8001 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 8002 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8003 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8004 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8005 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8006 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 8007 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8008 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8009 | } |
| 8010 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 8011 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8012 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 8013 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8014 | } |
| 8015 | |
| 8016 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8017 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 8018 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 8019 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8020 | 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] | 8021 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8022 | char *cur_ptr, *cur_end, *cur_next; |
| 8023 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8024 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8025 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8026 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8027 | |
| 8028 | last_hdr = 0; |
| 8029 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8030 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8031 | old_idx = 0; |
| 8032 | |
| 8033 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8034 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8035 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8036 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8037 | (exp->action == ACT_ALLOW || |
| 8038 | exp->action == ACT_DENY)) |
| 8039 | return 0; |
| 8040 | |
| 8041 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 8042 | if (!cur_idx) |
| 8043 | break; |
| 8044 | |
| 8045 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8046 | cur_ptr = cur_next; |
| 8047 | cur_end = cur_ptr + cur_hdr->len; |
| 8048 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8049 | |
| 8050 | /* Now we have one header between cur_ptr and cur_end, |
| 8051 | * and the next header starts at cur_next. |
| 8052 | */ |
| 8053 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8054 | 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] | 8055 | switch (exp->action) { |
| 8056 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8057 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8058 | last_hdr = 1; |
| 8059 | break; |
| 8060 | |
| 8061 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8062 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8063 | last_hdr = 1; |
| 8064 | break; |
| 8065 | |
| 8066 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8067 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8068 | if (trash.len < 0) |
| 8069 | return -1; |
| 8070 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8071 | 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] | 8072 | /* FIXME: if the user adds a newline in the replacement, the |
| 8073 | * index will not be recalculated for now, and the new line |
| 8074 | * will not be counted as a new header. |
| 8075 | */ |
| 8076 | |
| 8077 | cur_end += delta; |
| 8078 | cur_next += delta; |
| 8079 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8080 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8081 | break; |
| 8082 | |
| 8083 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8084 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8085 | cur_next += delta; |
| 8086 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8087 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8088 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8089 | txn->hdr_idx.used--; |
| 8090 | cur_hdr->len = 0; |
| 8091 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8092 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8093 | break; |
| 8094 | |
| 8095 | } |
| 8096 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8097 | |
| 8098 | /* keep the link from this header to next one in case of later |
| 8099 | * removal of next header. |
| 8100 | */ |
| 8101 | old_idx = cur_idx; |
| 8102 | } |
| 8103 | return 0; |
| 8104 | } |
| 8105 | |
| 8106 | |
| 8107 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8108 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8109 | * or -1 if a replacement resulted in an invalid status line. |
| 8110 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8111 | 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] | 8112 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8113 | char *cur_ptr, *cur_end; |
| 8114 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8115 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8116 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8117 | |
| 8118 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8119 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8120 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8121 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8122 | (exp->action == ACT_ALLOW || |
| 8123 | exp->action == ACT_DENY)) |
| 8124 | return 0; |
| 8125 | else if (exp->action == ACT_REMOVE) |
| 8126 | return 0; |
| 8127 | |
| 8128 | done = 0; |
| 8129 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8130 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8131 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8132 | |
| 8133 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8134 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8135 | 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] | 8136 | switch (exp->action) { |
| 8137 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8138 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8139 | done = 1; |
| 8140 | break; |
| 8141 | |
| 8142 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8143 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8144 | done = 1; |
| 8145 | break; |
| 8146 | |
| 8147 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8148 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8149 | if (trash.len < 0) |
| 8150 | return -1; |
| 8151 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8152 | 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] | 8153 | /* FIXME: if the user adds a newline in the replacement, the |
| 8154 | * index will not be recalculated for now, and the new line |
| 8155 | * will not be counted as a new header. |
| 8156 | */ |
| 8157 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8158 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8159 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8160 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8161 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8162 | cur_ptr, cur_end + 1, |
| 8163 | NULL, NULL); |
| 8164 | if (unlikely(!cur_end)) |
| 8165 | return -1; |
| 8166 | |
| 8167 | /* we have a full respnse and we know that we have either a CR |
| 8168 | * or an LF at <ptr>. |
| 8169 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8170 | 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] | 8171 | 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] | 8172 | /* there is no point trying this regex on headers */ |
| 8173 | return 1; |
| 8174 | } |
| 8175 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8176 | return done; |
| 8177 | } |
| 8178 | |
| 8179 | |
| 8180 | |
| 8181 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8182 | * 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] | 8183 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8184 | * unparsable response. |
| 8185 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8186 | 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] | 8187 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8188 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8189 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8190 | struct hdr_exp *exp; |
| 8191 | |
| 8192 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8193 | int ret; |
| 8194 | |
| 8195 | /* |
| 8196 | * The interleaving of transformations and verdicts |
| 8197 | * makes it difficult to decide to continue or stop |
| 8198 | * the evaluation. |
| 8199 | */ |
| 8200 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8201 | if (txn->flags & TX_SVDENY) |
| 8202 | break; |
| 8203 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8204 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8205 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8206 | exp->action == ACT_PASS)) { |
| 8207 | exp = exp->next; |
| 8208 | continue; |
| 8209 | } |
| 8210 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8211 | /* if this filter had a condition, evaluate it now and skip to |
| 8212 | * next filter if the condition does not match. |
| 8213 | */ |
| 8214 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8215 | 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] | 8216 | ret = acl_pass(ret); |
| 8217 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8218 | ret = !ret; |
| 8219 | if (!ret) |
| 8220 | continue; |
| 8221 | } |
| 8222 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8223 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8224 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | if (unlikely(ret < 0)) |
| 8226 | return -1; |
| 8227 | |
| 8228 | if (likely(ret == 0)) { |
| 8229 | /* The filter did not match the response, it can be |
| 8230 | * iterated through all headers. |
| 8231 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8232 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8233 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8234 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8235 | } |
| 8236 | return 0; |
| 8237 | } |
| 8238 | |
| 8239 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8240 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8241 | * 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] | 8242 | * desirable to call it only when needed. This function is also used when we |
| 8243 | * 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] | 8244 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8245 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8246 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8247 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8248 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8249 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8250 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8251 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8252 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8253 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8254 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8255 | /* Iterate through the headers. |
| 8256 | * we start with the start line. |
| 8257 | */ |
| 8258 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8259 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8260 | |
| 8261 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8262 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8263 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8264 | |
| 8265 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8266 | hdr_beg = hdr_next; |
| 8267 | hdr_end = hdr_beg + cur_hdr->len; |
| 8268 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8269 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8270 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8271 | * next header starts at hdr_next. We're only interested in |
| 8272 | * "Set-Cookie" and "Set-Cookie2" headers. |
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 | is_cookie2 = 0; |
| 8276 | prev = hdr_beg + 10; |
| 8277 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8278 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8279 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8280 | if (!val) { |
| 8281 | old_idx = cur_idx; |
| 8282 | continue; |
| 8283 | } |
| 8284 | is_cookie2 = 1; |
| 8285 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8286 | } |
| 8287 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8288 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8289 | * <prev> points to the colon. |
| 8290 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8291 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8292 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8293 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8294 | * check-cache is enabled) and we are not interested in checking |
| 8295 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8296 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8297 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8298 | return; |
| 8299 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8300 | /* OK so now we know we have to process this response cookie. |
| 8301 | * The format of the Set-Cookie header is slightly different |
| 8302 | * from the format of the Cookie header in that it does not |
| 8303 | * support the comma as a cookie delimiter (thus the header |
| 8304 | * cannot be folded) because the Expires attribute described in |
| 8305 | * the original Netscape's spec may contain an unquoted date |
| 8306 | * with a comma inside. We have to live with this because |
| 8307 | * many browsers don't support Max-Age and some browsers don't |
| 8308 | * support quoted strings. However the Set-Cookie2 header is |
| 8309 | * clean. |
| 8310 | * |
| 8311 | * We have to keep multiple pointers in order to support cookie |
| 8312 | * removal at the beginning, middle or end of header without |
| 8313 | * corrupting the header (in case of set-cookie2). A special |
| 8314 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8315 | * fields after the first semi-colon. The <next> pointer points |
| 8316 | * either to the end of line (set-cookie) or next unquoted comma |
| 8317 | * (set-cookie2). All of these headers are valid : |
| 8318 | * |
| 8319 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8320 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8321 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8322 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8323 | * | | | | | | | | | | |
| 8324 | * | | | | | | | | +-> next hdr_end <--+ |
| 8325 | * | | | | | | | +------------> scav |
| 8326 | * | | | | | | +--------------> val_end |
| 8327 | * | | | | | +--------------------> val_beg |
| 8328 | * | | | | +----------------------> equal |
| 8329 | * | | | +------------------------> att_end |
| 8330 | * | | +----------------------------> att_beg |
| 8331 | * | +------------------------------> prev |
| 8332 | * +-----------------------------------------> hdr_beg |
| 8333 | */ |
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 | for (; prev < hdr_end; prev = next) { |
| 8336 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8337 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8338 | /* find att_beg */ |
| 8339 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8340 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8341 | att_beg++; |
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 | /* find att_end : this is the first character after the last non |
| 8344 | * space before the equal. It may be equal to hdr_end. |
| 8345 | */ |
| 8346 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8347 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8348 | while (equal < hdr_end) { |
| 8349 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8350 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8351 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8352 | continue; |
| 8353 | att_end = equal; |
| 8354 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8355 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8356 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8357 | * is between <att_beg> and <equal>, both may be identical. |
| 8358 | */ |
| 8359 | |
| 8360 | /* look for end of cookie if there is an equal sign */ |
| 8361 | if (equal < hdr_end && *equal == '=') { |
| 8362 | /* look for the beginning of the value */ |
| 8363 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8364 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8365 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8366 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8367 | /* find the end of the value, respecting quotes */ |
| 8368 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8369 | |
| 8370 | /* make val_end point to the first white space or delimitor after the value */ |
| 8371 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 8372 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8373 | val_end--; |
| 8374 | } else { |
| 8375 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8376 | val_beg = val_end = next = equal; |
| 8377 | } |
| 8378 | |
| 8379 | if (next < hdr_end) { |
| 8380 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8381 | * a colon or semi-colon before the end. So skip all attr-value |
| 8382 | * pairs and look for the next comma. For Set-Cookie, since |
| 8383 | * commas are permitted in values, skip to the end. |
| 8384 | */ |
| 8385 | if (is_cookie2) |
| 8386 | next = find_hdr_value_end(next, hdr_end); |
| 8387 | else |
| 8388 | next = hdr_end; |
| 8389 | } |
| 8390 | |
| 8391 | /* Now everything is as on the diagram above */ |
| 8392 | |
| 8393 | /* Ignore cookies with no equal sign */ |
| 8394 | if (equal == val_end) |
| 8395 | continue; |
| 8396 | |
| 8397 | /* If there are spaces around the equal sign, we need to |
| 8398 | * strip them otherwise we'll get trouble for cookie captures, |
| 8399 | * or even for rewrites. Since this happens extremely rarely, |
| 8400 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8401 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8402 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8403 | int stripped_before = 0; |
| 8404 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8405 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8406 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8407 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8408 | equal += stripped_before; |
| 8409 | val_beg += stripped_before; |
| 8410 | } |
| 8411 | |
| 8412 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8413 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8414 | val_beg += stripped_after; |
| 8415 | stripped_before += stripped_after; |
| 8416 | } |
| 8417 | |
| 8418 | val_end += stripped_before; |
| 8419 | next += stripped_before; |
| 8420 | hdr_end += stripped_before; |
| 8421 | hdr_next += stripped_before; |
| 8422 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8423 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8424 | } |
| 8425 | |
| 8426 | /* First, let's see if we want to capture this cookie. We check |
| 8427 | * that we don't already have a server side cookie, because we |
| 8428 | * can only capture one. Also as an optimisation, we ignore |
| 8429 | * cookies shorter than the declared name. |
| 8430 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8431 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8432 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8433 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8434 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8435 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8436 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8437 | Alert("HTTP logging : out of memory.\n"); |
| 8438 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8439 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8440 | if (log_len > sess->fe->capture_len) |
| 8441 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8442 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8443 | txn->srv_cookie[log_len] = 0; |
| 8444 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8445 | } |
| 8446 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8447 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8448 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8449 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8450 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8451 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8452 | /* assume passive cookie by default */ |
| 8453 | txn->flags &= ~TX_SCK_MASK; |
| 8454 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8455 | |
| 8456 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8457 | * this occurrence because we'll insert another one later. |
| 8458 | * 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] | 8459 | * a direct access. |
| 8460 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8461 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8462 | /* The "preserve" flag was set, we don't want to touch the |
| 8463 | * server's cookie. |
| 8464 | */ |
| 8465 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8466 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8467 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8468 | /* this cookie must be deleted */ |
| 8469 | if (*prev == ':' && next == hdr_end) { |
| 8470 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8471 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8472 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8473 | txn->hdr_idx.used--; |
| 8474 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8475 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8476 | hdr_next += delta; |
| 8477 | http_msg_move_end(&txn->rsp, delta); |
| 8478 | /* note: while both invalid now, <next> and <hdr_end> |
| 8479 | * are still equal, so the for() will stop as expected. |
| 8480 | */ |
| 8481 | } else { |
| 8482 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8483 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8484 | next = prev; |
| 8485 | hdr_end += delta; |
| 8486 | hdr_next += delta; |
| 8487 | cur_hdr->len += delta; |
| 8488 | http_msg_move_end(&txn->rsp, delta); |
| 8489 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8490 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8491 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8492 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8493 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8494 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8495 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8496 | * with this server since we know it. |
| 8497 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8498 | 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] | 8499 | next += delta; |
| 8500 | hdr_end += delta; |
| 8501 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8502 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8503 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8504 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8505 | txn->flags &= ~TX_SCK_MASK; |
| 8506 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8507 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8508 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8509 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8510 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8511 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8512 | 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] | 8513 | next += delta; |
| 8514 | hdr_end += delta; |
| 8515 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8516 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8517 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8518 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8519 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8520 | txn->flags &= ~TX_SCK_MASK; |
| 8521 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8522 | } |
| 8523 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8524 | /* that's done for this cookie, check the next one on the same |
| 8525 | * line when next != hdr_end (only if is_cookie2). |
| 8526 | */ |
| 8527 | } |
| 8528 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8529 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8530 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8534 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8535 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8536 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8537 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8538 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8539 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8540 | char *p1, *p2; |
| 8541 | |
| 8542 | char *cur_ptr, *cur_end, *cur_next; |
| 8543 | int cur_idx; |
| 8544 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8545 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8546 | return; |
| 8547 | |
| 8548 | /* Iterate through the headers. |
| 8549 | * we start with the start line. |
| 8550 | */ |
| 8551 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8552 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8553 | |
| 8554 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8555 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8556 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8557 | |
| 8558 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8559 | cur_ptr = cur_next; |
| 8560 | cur_end = cur_ptr + cur_hdr->len; |
| 8561 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8562 | |
| 8563 | /* We have one full header between cur_ptr and cur_end, and the |
| 8564 | * next header starts at cur_next. We're only interested in |
| 8565 | * "Cookie:" headers. |
| 8566 | */ |
| 8567 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8568 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8569 | if (val) { |
| 8570 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8571 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8572 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8573 | return; |
| 8574 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8575 | } |
| 8576 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8577 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8578 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8579 | continue; |
| 8580 | |
| 8581 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8582 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8583 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8584 | |
| 8585 | if (p1 >= cur_end) /* no more info */ |
| 8586 | continue; |
| 8587 | |
| 8588 | /* p1 is at the beginning of the value */ |
| 8589 | p2 = p1; |
| 8590 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8591 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8592 | p2++; |
| 8593 | |
| 8594 | /* we have a complete value between p1 and p2 */ |
| 8595 | if (p2 < cur_end && *p2 == '=') { |
| 8596 | /* we have something of the form no-cache="set-cookie" */ |
| 8597 | if ((cur_end - p1 >= 21) && |
| 8598 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8599 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8600 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8601 | continue; |
| 8602 | } |
| 8603 | |
| 8604 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8605 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8606 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8607 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8608 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8609 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8610 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8611 | return; |
| 8612 | } |
| 8613 | |
| 8614 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8615 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8616 | continue; |
| 8617 | } |
| 8618 | } |
| 8619 | } |
| 8620 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8621 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8622 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8623 | * 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] | 8624 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8625 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8626 | * 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] | 8627 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8628 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8629 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8630 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8631 | 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] | 8632 | { |
| 8633 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8634 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8635 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8636 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8637 | if (!uri_auth) |
| 8638 | return 0; |
| 8639 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8640 | 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] | 8641 | return 0; |
| 8642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8643 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8644 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8645 | return 0; |
| 8646 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8647 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8648 | return 0; |
| 8649 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8650 | return 1; |
| 8651 | } |
| 8652 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8653 | /* |
| 8654 | * 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] | 8655 | * By default it tries to report the error position as msg->err_pos. However if |
| 8656 | * this one is not set, it will then report msg->next, which is the last known |
| 8657 | * 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] | 8658 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8659 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8660 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8661 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8662 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8663 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8664 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8665 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8666 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8667 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8668 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8669 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8670 | len1 = MIN(len1, es->len); |
| 8671 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8672 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 8673 | if (!es->buf) |
| 8674 | es->buf = malloc(global.tune.bufsize); |
| 8675 | |
| 8676 | if (es->buf) { |
| 8677 | memcpy(es->buf, chn->buf->p, len1); |
| 8678 | if (len2) |
| 8679 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 8680 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8681 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8682 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8683 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8684 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8685 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8686 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8687 | es->when = date; // user-visible date |
| 8688 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8689 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8690 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8691 | if (objt_conn(sess->origin)) |
| 8692 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8693 | else |
| 8694 | memset(&es->src, 0, sizeof(es->src)); |
| 8695 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8696 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8697 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8698 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8699 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8700 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8701 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8702 | es->b_out = chn->buf->o; |
| 8703 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8704 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8705 | es->m_clen = msg->chunk_len; |
| 8706 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8707 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8708 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8709 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8710 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8711 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8712 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8713 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8714 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8715 | * 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] | 8716 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8717 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8718 | * 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] | 8719 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8720 | 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] | 8721 | struct hdr_idx *idx, int occ, |
| 8722 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8723 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8724 | struct hdr_ctx local_ctx; |
| 8725 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8726 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8727 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8728 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8729 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8730 | if (!ctx) { |
| 8731 | local_ctx.idx = 0; |
| 8732 | ctx = &local_ctx; |
| 8733 | } |
| 8734 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8735 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8736 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8737 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8738 | occ--; |
| 8739 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8740 | *vptr = ctx->line + ctx->val; |
| 8741 | *vlen = ctx->vlen; |
| 8742 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8743 | } |
| 8744 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8745 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | /* negative occurrence, we scan all the list then walk back */ |
| 8749 | if (-occ > MAX_HDR_HISTORY) |
| 8750 | return 0; |
| 8751 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8752 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8753 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8754 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8755 | len_hist[hist_ptr] = ctx->vlen; |
| 8756 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8757 | hist_ptr = 0; |
| 8758 | found++; |
| 8759 | } |
| 8760 | if (-occ > found) |
| 8761 | return 0; |
| 8762 | /* 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] | 8763 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8764 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8765 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8766 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8767 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8768 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8769 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8770 | *vptr = ptr_hist[hist_ptr]; |
| 8771 | *vlen = len_hist[hist_ptr]; |
| 8772 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8773 | } |
| 8774 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8775 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8776 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8777 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8778 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8779 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8780 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8781 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8782 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8783 | * line header values and does not stop at commas. |
| 8784 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8785 | */ |
| 8786 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8787 | struct hdr_idx *idx, int occ, |
| 8788 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8789 | { |
| 8790 | struct hdr_ctx local_ctx; |
| 8791 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8792 | int len_hist[MAX_HDR_HISTORY]; |
| 8793 | unsigned int hist_ptr; |
| 8794 | int found; |
| 8795 | |
| 8796 | if (!ctx) { |
| 8797 | local_ctx.idx = 0; |
| 8798 | ctx = &local_ctx; |
| 8799 | } |
| 8800 | |
| 8801 | if (occ >= 0) { |
| 8802 | /* search from the beginning */ |
| 8803 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8804 | occ--; |
| 8805 | if (occ <= 0) { |
| 8806 | *vptr = ctx->line + ctx->val; |
| 8807 | *vlen = ctx->vlen; |
| 8808 | return 1; |
| 8809 | } |
| 8810 | } |
| 8811 | return 0; |
| 8812 | } |
| 8813 | |
| 8814 | /* negative occurrence, we scan all the list then walk back */ |
| 8815 | if (-occ > MAX_HDR_HISTORY) |
| 8816 | return 0; |
| 8817 | |
| 8818 | found = hist_ptr = 0; |
| 8819 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8820 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8821 | len_hist[hist_ptr] = ctx->vlen; |
| 8822 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8823 | hist_ptr = 0; |
| 8824 | found++; |
| 8825 | } |
| 8826 | if (-occ > found) |
| 8827 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8828 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8829 | /* 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] | 8830 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8831 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8832 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8833 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8834 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8835 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8836 | hist_ptr -= MAX_HDR_HISTORY; |
| 8837 | *vptr = ptr_hist[hist_ptr]; |
| 8838 | *vlen = len_hist[hist_ptr]; |
| 8839 | return 1; |
| 8840 | } |
| 8841 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8842 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8843 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8844 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8845 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8846 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8847 | 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] | 8848 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8849 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8850 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8851 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8852 | 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] | 8853 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8854 | 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] | 8855 | 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] | 8856 | |
| 8857 | for (max = 0; start + max < end; max++) |
| 8858 | if (start[max] == '\r' || start[max] == '\n') |
| 8859 | break; |
| 8860 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8861 | UBOUND(max, trash.size - trash.len - 3); |
| 8862 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8863 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8864 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8865 | } |
| 8866 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8867 | |
| 8868 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8869 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8870 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8871 | * assigned to the stream and returned. |
| 8872 | */ |
| 8873 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8874 | { |
| 8875 | struct http_txn *txn = s->txn; |
| 8876 | |
| 8877 | if (txn) |
| 8878 | return txn; |
| 8879 | |
| 8880 | txn = pool_alloc2(pool2_http_txn); |
| 8881 | if (!txn) |
| 8882 | return txn; |
| 8883 | |
| 8884 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8885 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8886 | if (!txn->hdr_idx.v) { |
| 8887 | pool_free2(pool2_http_txn, txn); |
| 8888 | return NULL; |
| 8889 | } |
| 8890 | |
| 8891 | s->txn = txn; |
| 8892 | return txn; |
| 8893 | } |
| 8894 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8895 | void http_txn_reset_req(struct http_txn *txn) |
| 8896 | { |
| 8897 | txn->req.flags = 0; |
| 8898 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8899 | txn->req.next = 0; |
| 8900 | txn->req.chunk_len = 0LL; |
| 8901 | txn->req.body_len = 0LL; |
| 8902 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8903 | } |
| 8904 | |
| 8905 | void http_txn_reset_res(struct http_txn *txn) |
| 8906 | { |
| 8907 | txn->rsp.flags = 0; |
| 8908 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8909 | txn->rsp.next = 0; |
| 8910 | txn->rsp.chunk_len = 0LL; |
| 8911 | txn->rsp.body_len = 0LL; |
| 8912 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8913 | } |
| 8914 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8915 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8916 | * 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] | 8917 | * the required fields are properly allocated and that we only need to (re)init |
| 8918 | * them. This should be used before processing any new request. |
| 8919 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8920 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8921 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8922 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8923 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8924 | |
| 8925 | txn->flags = 0; |
| 8926 | txn->status = -1; |
| 8927 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8928 | txn->cookie_first_date = 0; |
| 8929 | txn->cookie_last_date = 0; |
| 8930 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8931 | txn->srv_cookie = NULL; |
| 8932 | txn->cli_cookie = NULL; |
| 8933 | txn->uri = NULL; |
| 8934 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8935 | http_txn_reset_req(txn); |
| 8936 | http_txn_reset_res(txn); |
| 8937 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8938 | txn->req.chn = &s->req; |
| 8939 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8940 | |
| 8941 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8942 | |
| 8943 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8944 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8945 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8946 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8947 | if (txn->hdr_idx.v) |
| 8948 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8949 | |
| 8950 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8951 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8952 | } |
| 8953 | |
| 8954 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8955 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8956 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8957 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8958 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8959 | |
| 8960 | /* these ones will have been dynamically allocated */ |
| 8961 | pool_free2(pool2_requri, txn->uri); |
| 8962 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8963 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8964 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8965 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8966 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8967 | txn->uri = NULL; |
| 8968 | txn->srv_cookie = NULL; |
| 8969 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8970 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8971 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8972 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8973 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8974 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8975 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8976 | } |
| 8977 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8978 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8979 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8980 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8981 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8982 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8983 | } |
| 8984 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8985 | vars_prune(&s->vars_txn, s->sess, s); |
| 8986 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8987 | } |
| 8988 | |
| 8989 | /* 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] | 8990 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8991 | { |
| 8992 | http_end_txn(s); |
| 8993 | http_init_txn(s); |
| 8994 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8995 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8996 | * any rulelist match the NULL pointer. |
| 8997 | */ |
| 8998 | s->current_rule_list = NULL; |
| 8999 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9000 | s->be = strm_fe(s); |
| 9001 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 9002 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9003 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 9004 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 9005 | /* re-init store persistence */ |
| 9006 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 9007 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 9008 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9009 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9010 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9011 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9012 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9013 | /* We must trim any excess data from the response buffer, because we |
| 9014 | * may have blocked an invalid response from a server that we don't |
| 9015 | * want to accidentely forward once we disable the analysers, nor do |
| 9016 | * we want those data to come along with next response. A typical |
| 9017 | * example of such data would be from a buggy server responding to |
| 9018 | * a HEAD with some data, or sending more than the advertised |
| 9019 | * content-length. |
| 9020 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9021 | if (unlikely(s->res.buf->i)) |
| 9022 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 9023 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9024 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9025 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9026 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9027 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 9028 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9029 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9030 | s->req.rex = TICK_ETERNITY; |
| 9031 | s->req.wex = TICK_ETERNITY; |
| 9032 | s->req.analyse_exp = TICK_ETERNITY; |
| 9033 | s->res.rex = TICK_ETERNITY; |
| 9034 | s->res.wex = TICK_ETERNITY; |
| 9035 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 9036 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9037 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9038 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9039 | void free_http_res_rules(struct list *r) |
| 9040 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9041 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9042 | |
| 9043 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9044 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9045 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9046 | free(pr); |
| 9047 | } |
| 9048 | } |
| 9049 | |
| 9050 | void free_http_req_rules(struct list *r) |
| 9051 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9052 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9053 | |
| 9054 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9055 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9056 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9057 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9058 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9059 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9060 | free(pr); |
| 9061 | } |
| 9062 | } |
| 9063 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9064 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9065 | 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] | 9066 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9067 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9068 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9069 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9070 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9071 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 9072 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9073 | if (!rule) { |
| 9074 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9075 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9076 | } |
| 9077 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9078 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9079 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9080 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9081 | } 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] | 9082 | int code; |
| 9083 | int hc; |
| 9084 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9085 | if (!strcmp(args[0], "tarpit")) { |
| 9086 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 9087 | rule->deny_status = HTTP_ERR_500; |
| 9088 | } |
| 9089 | else { |
| 9090 | rule->action = ACT_ACTION_DENY; |
| 9091 | rule->deny_status = HTTP_ERR_403; |
| 9092 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9093 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9094 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9095 | cur_arg++; |
| 9096 | if (!args[cur_arg]) { |
| 9097 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9098 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9099 | goto out_err; |
| 9100 | } |
| 9101 | |
| 9102 | code = atol(args[cur_arg]); |
| 9103 | cur_arg++; |
| 9104 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9105 | if (http_err_codes[hc] == code) { |
| 9106 | rule->deny_status = hc; |
| 9107 | break; |
| 9108 | } |
| 9109 | } |
| 9110 | |
| 9111 | if (hc >= HTTP_ERR_SIZE) { |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 9112 | Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 9113 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9114 | } |
| 9115 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9116 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9117 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9118 | cur_arg = 1; |
| 9119 | |
| 9120 | while(*args[cur_arg]) { |
| 9121 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9122 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9123 | cur_arg+=2; |
| 9124 | continue; |
| 9125 | } else |
| 9126 | break; |
| 9127 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9128 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9129 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9130 | cur_arg = 1; |
| 9131 | |
| 9132 | if (!*args[cur_arg] || |
| 9133 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9134 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9135 | file, linenum, args[0]); |
| 9136 | goto out_err; |
| 9137 | } |
| 9138 | rule->arg.nice = atoi(args[cur_arg]); |
| 9139 | if (rule->arg.nice < -1024) |
| 9140 | rule->arg.nice = -1024; |
| 9141 | else if (rule->arg.nice > 1024) |
| 9142 | rule->arg.nice = 1024; |
| 9143 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9144 | } else if (!strcmp(args[0], "set-tos")) { |
| 9145 | #ifdef IP_TOS |
| 9146 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9147 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9148 | cur_arg = 1; |
| 9149 | |
| 9150 | if (!*args[cur_arg] || |
| 9151 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9152 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9153 | file, linenum, args[0]); |
| 9154 | goto out_err; |
| 9155 | } |
| 9156 | |
| 9157 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9158 | if (err && *err != '\0') { |
| 9159 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9160 | file, linenum, err, args[0]); |
| 9161 | goto out_err; |
| 9162 | } |
| 9163 | cur_arg++; |
| 9164 | #else |
| 9165 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9166 | goto out_err; |
| 9167 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9168 | } else if (!strcmp(args[0], "set-mark")) { |
| 9169 | #ifdef SO_MARK |
| 9170 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9171 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9172 | cur_arg = 1; |
| 9173 | |
| 9174 | if (!*args[cur_arg] || |
| 9175 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9176 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9177 | file, linenum, args[0]); |
| 9178 | goto out_err; |
| 9179 | } |
| 9180 | |
| 9181 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9182 | if (err && *err != '\0') { |
| 9183 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9184 | file, linenum, err, args[0]); |
| 9185 | goto out_err; |
| 9186 | } |
| 9187 | cur_arg++; |
| 9188 | global.last_checks |= LSTCHK_NETADM; |
| 9189 | #else |
| 9190 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9191 | goto out_err; |
| 9192 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9193 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9194 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9195 | cur_arg = 1; |
| 9196 | |
| 9197 | if (!*args[cur_arg] || |
| 9198 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9199 | bad_log_level: |
| 9200 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9201 | file, linenum, args[0]); |
| 9202 | goto out_err; |
| 9203 | } |
| 9204 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9205 | rule->arg.loglevel = -1; |
| 9206 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9207 | goto bad_log_level; |
| 9208 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9209 | } 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] | 9210 | 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] | 9211 | cur_arg = 1; |
| 9212 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9213 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9214 | (*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] | 9215 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9216 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9217 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9218 | } |
| 9219 | |
| 9220 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9221 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9222 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9223 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9224 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9225 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9226 | 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] | 9227 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9228 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9229 | file, linenum, args[0], error); |
| 9230 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9231 | goto out_err; |
| 9232 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9233 | free(proxy->conf.lfs_file); |
| 9234 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9235 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9236 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9237 | } 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] | 9238 | 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] | 9239 | cur_arg = 1; |
| 9240 | |
| 9241 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9242 | (*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] | 9243 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9244 | file, linenum, args[0]); |
| 9245 | goto out_err; |
| 9246 | } |
| 9247 | |
| 9248 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9249 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9250 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9251 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9252 | error = NULL; |
| 9253 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9254 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9255 | args[cur_arg + 1], error); |
| 9256 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9257 | goto out_err; |
| 9258 | } |
| 9259 | |
| 9260 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9261 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9262 | 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] | 9263 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9264 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9265 | file, linenum, args[0], error); |
| 9266 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9267 | goto out_err; |
| 9268 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9269 | |
| 9270 | free(proxy->conf.lfs_file); |
| 9271 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9272 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9273 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9274 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9275 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9276 | cur_arg = 1; |
| 9277 | |
| 9278 | if (!*args[cur_arg] || |
| 9279 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9280 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9281 | file, linenum, args[0]); |
| 9282 | goto out_err; |
| 9283 | } |
| 9284 | |
| 9285 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9286 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9287 | |
| 9288 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9289 | free(proxy->conf.lfs_file); |
| 9290 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9291 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9292 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9293 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9294 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9295 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9296 | struct sample_expr *expr; |
| 9297 | unsigned int where; |
| 9298 | char *err = NULL; |
| 9299 | |
| 9300 | cur_arg = 1; |
| 9301 | proxy->conf.args.ctx = ARGC_TRK; |
| 9302 | |
| 9303 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9304 | if (!expr) { |
| 9305 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9306 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9307 | free(err); |
| 9308 | goto out_err; |
| 9309 | } |
| 9310 | |
| 9311 | where = 0; |
| 9312 | if (proxy->cap & PR_CAP_FE) |
| 9313 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9314 | if (proxy->cap & PR_CAP_BE) |
| 9315 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9316 | |
| 9317 | if (!(expr->fetch->val & where)) { |
| 9318 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9319 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9320 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9321 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9322 | free(expr); |
| 9323 | goto out_err; |
| 9324 | } |
| 9325 | |
| 9326 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9327 | cur_arg++; |
| 9328 | if (!args[cur_arg]) { |
| 9329 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9330 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9331 | free(expr); |
| 9332 | goto out_err; |
| 9333 | } |
| 9334 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9335 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9336 | cur_arg++; |
| 9337 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9338 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9339 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9340 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9341 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9342 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9343 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9344 | 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] | 9345 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9346 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9347 | goto out_err; |
| 9348 | } |
| 9349 | |
| 9350 | /* this redirect rule might already contain a parsed condition which |
| 9351 | * we'll pass to the http-request rule. |
| 9352 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9353 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9354 | rule->arg.redir = redir; |
| 9355 | rule->cond = redir->cond; |
| 9356 | redir->cond = NULL; |
| 9357 | cur_arg = 2; |
| 9358 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9359 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9360 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9361 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9362 | /* |
| 9363 | * '+ 8' for 'add-acl(' |
| 9364 | * '- 9' for 'add-acl(' + trailing ')' |
| 9365 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9366 | 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] | 9367 | |
| 9368 | cur_arg = 1; |
| 9369 | |
| 9370 | if (!*args[cur_arg] || |
| 9371 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9372 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9373 | file, linenum, args[0]); |
| 9374 | goto out_err; |
| 9375 | } |
| 9376 | |
| 9377 | LIST_INIT(&rule->arg.map.key); |
| 9378 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9379 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9380 | 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] | 9381 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9382 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9383 | file, linenum, args[0], error); |
| 9384 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9385 | goto out_err; |
| 9386 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9387 | free(proxy->conf.lfs_file); |
| 9388 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9389 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9390 | cur_arg += 1; |
| 9391 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9392 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9393 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9394 | /* |
| 9395 | * '+ 8' for 'del-acl(' |
| 9396 | * '- 9' for 'del-acl(' + trailing ')' |
| 9397 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9398 | 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] | 9399 | |
| 9400 | cur_arg = 1; |
| 9401 | |
| 9402 | if (!*args[cur_arg] || |
| 9403 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9404 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9405 | file, linenum, args[0]); |
| 9406 | goto out_err; |
| 9407 | } |
| 9408 | |
| 9409 | LIST_INIT(&rule->arg.map.key); |
| 9410 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9411 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9412 | 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] | 9413 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9414 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9415 | file, linenum, args[0], error); |
| 9416 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9417 | goto out_err; |
| 9418 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9419 | free(proxy->conf.lfs_file); |
| 9420 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9421 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9422 | cur_arg += 1; |
| 9423 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9424 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9425 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9426 | /* |
| 9427 | * '+ 8' for 'del-map(' |
| 9428 | * '- 9' for 'del-map(' + trailing ')' |
| 9429 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9430 | 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] | 9431 | |
| 9432 | cur_arg = 1; |
| 9433 | |
| 9434 | if (!*args[cur_arg] || |
| 9435 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9436 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9437 | file, linenum, args[0]); |
| 9438 | goto out_err; |
| 9439 | } |
| 9440 | |
| 9441 | LIST_INIT(&rule->arg.map.key); |
| 9442 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9443 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9444 | 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] | 9445 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9446 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 9447 | file, linenum, args[0], error); |
| 9448 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9449 | goto out_err; |
| 9450 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9451 | free(proxy->conf.lfs_file); |
| 9452 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9453 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9454 | cur_arg += 1; |
| 9455 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9456 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9457 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9458 | /* |
| 9459 | * '+ 8' for 'set-map(' |
| 9460 | * '- 9' for 'set-map(' + trailing ')' |
| 9461 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9462 | 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] | 9463 | |
| 9464 | cur_arg = 1; |
| 9465 | |
| 9466 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9467 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9468 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9469 | file, linenum, args[0]); |
| 9470 | goto out_err; |
| 9471 | } |
| 9472 | |
| 9473 | LIST_INIT(&rule->arg.map.key); |
| 9474 | LIST_INIT(&rule->arg.map.value); |
| 9475 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9476 | |
| 9477 | /* key 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], proxy, &rule->arg.map.key, 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' key: %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 | |
| 9487 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9488 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9489 | 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] | 9490 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 9491 | Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 9492 | file, linenum, args[0], error); |
| 9493 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9494 | goto out_err; |
| 9495 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9496 | free(proxy->conf.lfs_file); |
| 9497 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9498 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9499 | |
| 9500 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9501 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9502 | char *errmsg = NULL; |
| 9503 | cur_arg = 1; |
| 9504 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9505 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9506 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9507 | 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] | 9508 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9509 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9510 | free(errmsg); |
| 9511 | goto out_err; |
| 9512 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9513 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9514 | action_build_list(&http_req_keywords.list, &trash); |
| 9515 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9516 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9517 | "'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] | 9518 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9519 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9520 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9521 | } |
| 9522 | |
| 9523 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9524 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9525 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9526 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9527 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9528 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9529 | file, linenum, args[0], errmsg); |
| 9530 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9531 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9532 | } |
| 9533 | rule->cond = cond; |
| 9534 | } |
| 9535 | else if (*args[cur_arg]) { |
| 9536 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9537 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9538 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9539 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9540 | } |
| 9541 | |
| 9542 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9543 | out_err: |
| 9544 | free(rule); |
| 9545 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9546 | } |
| 9547 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9548 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9549 | 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] | 9550 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9551 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9552 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9553 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9554 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9555 | |
| 9556 | rule = calloc(1, sizeof(*rule)); |
| 9557 | if (!rule) { |
| 9558 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9559 | goto out_err; |
| 9560 | } |
| 9561 | |
| 9562 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9563 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9564 | cur_arg = 1; |
| 9565 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9566 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9567 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9568 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9569 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9570 | cur_arg = 1; |
| 9571 | |
| 9572 | if (!*args[cur_arg] || |
| 9573 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9574 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9575 | file, linenum, args[0]); |
| 9576 | goto out_err; |
| 9577 | } |
| 9578 | rule->arg.nice = atoi(args[cur_arg]); |
| 9579 | if (rule->arg.nice < -1024) |
| 9580 | rule->arg.nice = -1024; |
| 9581 | else if (rule->arg.nice > 1024) |
| 9582 | rule->arg.nice = 1024; |
| 9583 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9584 | } else if (!strcmp(args[0], "set-tos")) { |
| 9585 | #ifdef IP_TOS |
| 9586 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9587 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9588 | cur_arg = 1; |
| 9589 | |
| 9590 | if (!*args[cur_arg] || |
| 9591 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9592 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9593 | file, linenum, args[0]); |
| 9594 | goto out_err; |
| 9595 | } |
| 9596 | |
| 9597 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9598 | if (err && *err != '\0') { |
| 9599 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9600 | file, linenum, err, args[0]); |
| 9601 | goto out_err; |
| 9602 | } |
| 9603 | cur_arg++; |
| 9604 | #else |
| 9605 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9606 | goto out_err; |
| 9607 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9608 | } else if (!strcmp(args[0], "set-mark")) { |
| 9609 | #ifdef SO_MARK |
| 9610 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9611 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9612 | cur_arg = 1; |
| 9613 | |
| 9614 | if (!*args[cur_arg] || |
| 9615 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9616 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9617 | file, linenum, args[0]); |
| 9618 | goto out_err; |
| 9619 | } |
| 9620 | |
| 9621 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9622 | if (err && *err != '\0') { |
| 9623 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9624 | file, linenum, err, args[0]); |
| 9625 | goto out_err; |
| 9626 | } |
| 9627 | cur_arg++; |
| 9628 | global.last_checks |= LSTCHK_NETADM; |
| 9629 | #else |
| 9630 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9631 | goto out_err; |
| 9632 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9633 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9634 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9635 | cur_arg = 1; |
| 9636 | |
| 9637 | if (!*args[cur_arg] || |
| 9638 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9639 | bad_log_level: |
| 9640 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9641 | file, linenum, args[0]); |
| 9642 | goto out_err; |
| 9643 | } |
| 9644 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9645 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 9646 | 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] | 9647 | goto bad_log_level; |
| 9648 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9649 | } 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] | 9650 | 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] | 9651 | cur_arg = 1; |
| 9652 | |
| 9653 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9654 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9655 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9656 | file, linenum, args[0]); |
| 9657 | goto out_err; |
| 9658 | } |
| 9659 | |
| 9660 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9661 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9662 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9663 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9664 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9665 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9666 | 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] | 9667 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9668 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9669 | file, linenum, args[0], error); |
| 9670 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9671 | goto out_err; |
| 9672 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9673 | free(proxy->conf.lfs_file); |
| 9674 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9675 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9676 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9677 | } 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] | 9678 | 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] | 9679 | cur_arg = 1; |
| 9680 | |
| 9681 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9682 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9683 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9684 | file, linenum, args[0]); |
| 9685 | goto out_err; |
| 9686 | } |
| 9687 | |
| 9688 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9689 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9690 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9691 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9692 | error = NULL; |
| 9693 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9694 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9695 | args[cur_arg + 1], error); |
| 9696 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9697 | goto out_err; |
| 9698 | } |
| 9699 | |
| 9700 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9701 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9702 | 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] | 9703 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9704 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9705 | file, linenum, args[0], error); |
| 9706 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9707 | goto out_err; |
| 9708 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9709 | |
| 9710 | free(proxy->conf.lfs_file); |
| 9711 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9712 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9713 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9714 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9715 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9716 | cur_arg = 1; |
| 9717 | |
| 9718 | if (!*args[cur_arg] || |
| 9719 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9720 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9721 | file, linenum, args[0]); |
| 9722 | goto out_err; |
| 9723 | } |
| 9724 | |
| 9725 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9726 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9727 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9728 | proxy->conf.args.ctx = ARGC_HRS; |
| 9729 | free(proxy->conf.lfs_file); |
| 9730 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9731 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9732 | cur_arg += 1; |
| 9733 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9734 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9735 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9736 | /* |
| 9737 | * '+ 8' for 'add-acl(' |
| 9738 | * '- 9' for 'add-acl(' + trailing ')' |
| 9739 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9740 | 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] | 9741 | |
| 9742 | cur_arg = 1; |
| 9743 | |
| 9744 | if (!*args[cur_arg] || |
| 9745 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9746 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9747 | file, linenum, args[0]); |
| 9748 | goto out_err; |
| 9749 | } |
| 9750 | |
| 9751 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9752 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9753 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9754 | 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] | 9755 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9756 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9757 | file, linenum, args[0], error); |
| 9758 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9759 | goto out_err; |
| 9760 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9761 | free(proxy->conf.lfs_file); |
| 9762 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9763 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9764 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9765 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9766 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9767 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9768 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9769 | /* |
| 9770 | * '+ 8' for 'del-acl(' |
| 9771 | * '- 9' for 'del-acl(' + trailing ')' |
| 9772 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9773 | 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] | 9774 | |
| 9775 | cur_arg = 1; |
| 9776 | |
| 9777 | if (!*args[cur_arg] || |
| 9778 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9779 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9780 | file, linenum, args[0]); |
| 9781 | goto out_err; |
| 9782 | } |
| 9783 | |
| 9784 | LIST_INIT(&rule->arg.map.key); |
| 9785 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9786 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9787 | 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] | 9788 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9789 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9790 | file, linenum, args[0], error); |
| 9791 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9792 | goto out_err; |
| 9793 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9794 | free(proxy->conf.lfs_file); |
| 9795 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9796 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9797 | cur_arg += 1; |
| 9798 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9799 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9800 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9801 | /* |
| 9802 | * '+ 8' for 'del-map(' |
| 9803 | * '- 9' for 'del-map(' + trailing ')' |
| 9804 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9805 | 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] | 9806 | |
| 9807 | cur_arg = 1; |
| 9808 | |
| 9809 | if (!*args[cur_arg] || |
| 9810 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9811 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9812 | file, linenum, args[0]); |
| 9813 | goto out_err; |
| 9814 | } |
| 9815 | |
| 9816 | LIST_INIT(&rule->arg.map.key); |
| 9817 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9818 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9819 | 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] | 9820 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9821 | Alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9822 | file, linenum, args[0], error); |
| 9823 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9824 | goto out_err; |
| 9825 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9826 | free(proxy->conf.lfs_file); |
| 9827 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9828 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9829 | cur_arg += 1; |
| 9830 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9831 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9832 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9833 | /* |
| 9834 | * '+ 8' for 'set-map(' |
| 9835 | * '- 9' for 'set-map(' + trailing ')' |
| 9836 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9837 | 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] | 9838 | |
| 9839 | cur_arg = 1; |
| 9840 | |
| 9841 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9842 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9843 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9844 | file, linenum, args[0]); |
| 9845 | goto out_err; |
| 9846 | } |
| 9847 | |
| 9848 | LIST_INIT(&rule->arg.map.key); |
| 9849 | LIST_INIT(&rule->arg.map.value); |
| 9850 | |
| 9851 | proxy->conf.args.ctx = ARGC_HRS; |
| 9852 | |
| 9853 | /* key 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], proxy, &rule->arg.map.key, 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' name: %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 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9864 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9865 | 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] | 9866 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 9867 | Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9868 | file, linenum, args[0], error); |
| 9869 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9870 | goto out_err; |
| 9871 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9872 | |
| 9873 | free(proxy->conf.lfs_file); |
| 9874 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9875 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9876 | |
| 9877 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9878 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9879 | struct redirect_rule *redir; |
| 9880 | char *errmsg = NULL; |
| 9881 | |
| 9882 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9883 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9884 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9885 | goto out_err; |
| 9886 | } |
| 9887 | |
| 9888 | /* this redirect rule might already contain a parsed condition which |
| 9889 | * we'll pass to the http-request rule. |
| 9890 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9891 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9892 | rule->arg.redir = redir; |
| 9893 | rule->cond = redir->cond; |
| 9894 | redir->cond = NULL; |
| 9895 | cur_arg = 2; |
| 9896 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9897 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9898 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 9899 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 9900 | struct sample_expr *expr; |
| 9901 | unsigned int where; |
| 9902 | char *err = NULL; |
| 9903 | |
| 9904 | cur_arg = 1; |
| 9905 | proxy->conf.args.ctx = ARGC_TRK; |
| 9906 | |
| 9907 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9908 | if (!expr) { |
| 9909 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9910 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9911 | free(err); |
| 9912 | goto out_err; |
| 9913 | } |
| 9914 | |
| 9915 | where = 0; |
| 9916 | if (proxy->cap & PR_CAP_FE) |
| 9917 | where |= SMP_VAL_FE_HRS_HDR; |
| 9918 | if (proxy->cap & PR_CAP_BE) |
| 9919 | where |= SMP_VAL_BE_HRS_HDR; |
| 9920 | |
| 9921 | if (!(expr->fetch->val & where)) { |
| 9922 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9923 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9924 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9925 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9926 | free(expr); |
| 9927 | goto out_err; |
| 9928 | } |
| 9929 | |
| 9930 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9931 | cur_arg++; |
| 9932 | if (!args[cur_arg]) { |
| 9933 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9934 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9935 | free(expr); |
| 9936 | goto out_err; |
| 9937 | } |
| 9938 | /* we copy the table name for now, it will be resolved later */ |
| 9939 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9940 | cur_arg++; |
| 9941 | } |
| 9942 | rule->arg.trk_ctr.expr = expr; |
| 9943 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9944 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9945 | char *errmsg = NULL; |
| 9946 | cur_arg = 1; |
| 9947 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9948 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9949 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9950 | 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] | 9951 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9952 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9953 | free(errmsg); |
| 9954 | goto out_err; |
| 9955 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9956 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9957 | action_build_list(&http_res_keywords.list, &trash); |
| 9958 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9959 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9960 | "'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] | 9961 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9962 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9963 | goto out_err; |
| 9964 | } |
| 9965 | |
| 9966 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9967 | struct acl_cond *cond; |
| 9968 | char *errmsg = NULL; |
| 9969 | |
| 9970 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9971 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9972 | file, linenum, args[0], errmsg); |
| 9973 | free(errmsg); |
| 9974 | goto out_err; |
| 9975 | } |
| 9976 | rule->cond = cond; |
| 9977 | } |
| 9978 | else if (*args[cur_arg]) { |
| 9979 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9980 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9981 | file, linenum, args[0], args[cur_arg]); |
| 9982 | goto out_err; |
| 9983 | } |
| 9984 | |
| 9985 | return rule; |
| 9986 | out_err: |
| 9987 | free(rule); |
| 9988 | return NULL; |
| 9989 | } |
| 9990 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9991 | /* 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] | 9992 | * 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] | 9993 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9994 | * 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] | 9995 | */ |
| 9996 | 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] | 9997 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9998 | { |
| 9999 | struct redirect_rule *rule; |
| 10000 | int cur_arg; |
| 10001 | int type = REDIRECT_TYPE_NONE; |
| 10002 | int code = 302; |
| 10003 | const char *destination = NULL; |
| 10004 | const char *cookie = NULL; |
| 10005 | int cookie_set = 0; |
| 10006 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 10007 | struct acl_cond *cond = NULL; |
| 10008 | |
| 10009 | cur_arg = 0; |
| 10010 | while (*(args[cur_arg])) { |
| 10011 | if (strcmp(args[cur_arg], "location") == 0) { |
| 10012 | if (!*args[cur_arg + 1]) |
| 10013 | goto missing_arg; |
| 10014 | |
| 10015 | type = REDIRECT_TYPE_LOCATION; |
| 10016 | cur_arg++; |
| 10017 | destination = args[cur_arg]; |
| 10018 | } |
| 10019 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 10020 | if (!*args[cur_arg + 1]) |
| 10021 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10022 | type = REDIRECT_TYPE_PREFIX; |
| 10023 | cur_arg++; |
| 10024 | destination = args[cur_arg]; |
| 10025 | } |
| 10026 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 10027 | if (!*args[cur_arg + 1]) |
| 10028 | goto missing_arg; |
| 10029 | |
| 10030 | type = REDIRECT_TYPE_SCHEME; |
| 10031 | cur_arg++; |
| 10032 | destination = args[cur_arg]; |
| 10033 | } |
| 10034 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 10035 | if (!*args[cur_arg + 1]) |
| 10036 | goto missing_arg; |
| 10037 | |
| 10038 | cur_arg++; |
| 10039 | cookie = args[cur_arg]; |
| 10040 | cookie_set = 1; |
| 10041 | } |
| 10042 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 10043 | if (!*args[cur_arg + 1]) |
| 10044 | goto missing_arg; |
| 10045 | |
| 10046 | cur_arg++; |
| 10047 | cookie = args[cur_arg]; |
| 10048 | cookie_set = 0; |
| 10049 | } |
| 10050 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 10051 | if (!*args[cur_arg + 1]) |
| 10052 | goto missing_arg; |
| 10053 | |
| 10054 | cur_arg++; |
| 10055 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10056 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10057 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 10058 | "'%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] | 10059 | args[cur_arg - 1], args[cur_arg]); |
| 10060 | return NULL; |
| 10061 | } |
| 10062 | } |
| 10063 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 10064 | flags |= REDIRECT_FLAG_DROP_QS; |
| 10065 | } |
| 10066 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 10067 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 10068 | } |
| 10069 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 10070 | strcmp(args[cur_arg], "unless") == 0) { |
| 10071 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 10072 | if (!cond) { |
| 10073 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 10074 | return NULL; |
| 10075 | } |
| 10076 | break; |
| 10077 | } |
| 10078 | else { |
| 10079 | memprintf(errmsg, |
| 10080 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 10081 | args[cur_arg]); |
| 10082 | return NULL; |
| 10083 | } |
| 10084 | cur_arg++; |
| 10085 | } |
| 10086 | |
| 10087 | if (type == REDIRECT_TYPE_NONE) { |
| 10088 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 10089 | return NULL; |
| 10090 | } |
| 10091 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 10092 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 10093 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 10094 | return NULL; |
| 10095 | } |
| 10096 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10097 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10098 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 10099 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10100 | |
| 10101 | if (!use_fmt) { |
| 10102 | /* old-style static redirect rule */ |
| 10103 | rule->rdr_str = strdup(destination); |
| 10104 | rule->rdr_len = strlen(destination); |
| 10105 | } |
| 10106 | else { |
| 10107 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10108 | |
| 10109 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 10110 | * if prefix == "/", we don't want to add anything, otherwise it |
| 10111 | * makes it hard for the user to configure a self-redirection. |
| 10112 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 10113 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10114 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10115 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 10116 | 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] | 10117 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 10118 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 10119 | return NULL; |
| 10120 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 10121 | free(curproxy->conf.lfs_file); |
| 10122 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10123 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10124 | } |
| 10125 | } |
| 10126 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10127 | if (cookie) { |
| 10128 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10129 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10130 | */ |
| 10131 | rule->cookie_len = strlen(cookie); |
| 10132 | if (cookie_set) { |
| 10133 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10134 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10135 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10136 | rule->cookie_len += 9; |
| 10137 | } else { |
| 10138 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10139 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10140 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10141 | rule->cookie_len += 20; |
| 10142 | } |
| 10143 | } |
| 10144 | rule->type = type; |
| 10145 | rule->code = code; |
| 10146 | rule->flags = flags; |
| 10147 | LIST_INIT(&rule->list); |
| 10148 | return rule; |
| 10149 | |
| 10150 | missing_arg: |
| 10151 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10152 | return NULL; |
| 10153 | } |
| 10154 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10155 | /************************************************************************/ |
| 10156 | /* The code below is dedicated to ACL parsing and matching */ |
| 10157 | /************************************************************************/ |
| 10158 | |
| 10159 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10160 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10161 | * which means that a request or response is ready. If some data is missing, |
| 10162 | * 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] | 10163 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10164 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10165 | * |
| 10166 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10167 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10168 | * decide whether or not an HTTP message is present ; |
| 10169 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10170 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10171 | * 1 if an HTTP message is ready |
| 10172 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 10173 | 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] | 10174 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10175 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10176 | struct http_txn *txn; |
| 10177 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10178 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10179 | /* Note: it is possible that <s> is NULL when called before stream |
| 10180 | * initialization (eg: tcp-request connection), so this function is the |
| 10181 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10182 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10183 | if (!s) |
| 10184 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10185 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 10186 | if (!s->txn) { |
| 10187 | if (unlikely(!http_alloc_txn(s))) |
| 10188 | return 0; /* not enough memory */ |
| 10189 | http_init_txn(s); |
| 10190 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10191 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10192 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10193 | |
| 10194 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10195 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10196 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10197 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10198 | /* If the buffer does not leave enough free space at the end, |
| 10199 | * we must first realign it. |
| 10200 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10201 | if (s->req.buf->p > s->req.buf->data && |
| 10202 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10203 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10204 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10205 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10206 | if (msg->msg_state == HTTP_MSG_ERROR) |
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 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10210 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10211 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10212 | |
| 10213 | /* Still no valid request ? */ |
| 10214 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10215 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10216 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10217 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10218 | } |
| 10219 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10220 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10221 | return 0; |
| 10222 | } |
| 10223 | |
| 10224 | /* OK we just got a valid HTTP request. We have some minor |
| 10225 | * preparation to perform so that further checks can rely |
| 10226 | * on HTTP tests. |
| 10227 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10228 | |
| 10229 | /* If the request was parsed but was too large, we must absolutely |
| 10230 | * return an error so that it is not processed. At the moment this |
| 10231 | * cannot happen, but if the parsers are to change in the future, |
| 10232 | * we want this check to be maintained. |
| 10233 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10234 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10235 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 10236 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10237 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10238 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10239 | return 1; |
| 10240 | } |
| 10241 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10242 | 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] | 10243 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10244 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10245 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10246 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10247 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10248 | } |
| 10249 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10250 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10251 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10252 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10253 | |
| 10254 | /* otherwise everything's ready for the request */ |
| 10255 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10256 | else { |
| 10257 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10258 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10259 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10260 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10261 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10262 | } |
| 10263 | |
| 10264 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10265 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10266 | return 1; |
| 10267 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10268 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10269 | /* 1. Check on METHOD |
| 10270 | * We use the pre-parsed method if it is known, and store its number as an |
| 10271 | * integer. If it is unknown, we use the pointer and the length. |
| 10272 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10273 | 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] | 10274 | { |
| 10275 | int len, meth; |
| 10276 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10277 | len = strlen(text); |
| 10278 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10279 | |
| 10280 | pattern->val.i = meth; |
| 10281 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10282 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10283 | pattern->len = len; |
| 10284 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10285 | else { |
| 10286 | pattern->ptr.str = NULL; |
| 10287 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10288 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10289 | return 1; |
| 10290 | } |
| 10291 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10292 | /* This function fetches the method of current HTTP request and stores |
| 10293 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10294 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10295 | * in <len> and <ptr> is NULL ; |
| 10296 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10297 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10298 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10299 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10300 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10301 | 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] | 10302 | { |
| 10303 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10304 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10305 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10306 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10307 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10308 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10309 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10310 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10311 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10312 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10313 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10314 | /* ensure the indexes are not affected */ |
| 10315 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10316 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10317 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10318 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10319 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10320 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10321 | return 1; |
| 10322 | } |
| 10323 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10324 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10325 | 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] | 10326 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10327 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10328 | struct pattern_list *lst; |
| 10329 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10330 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10331 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10332 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10333 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10334 | /* well-known method */ |
| 10335 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10336 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10337 | return pattern; |
| 10338 | else |
| 10339 | continue; |
| 10340 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10341 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10342 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10343 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10344 | continue; |
| 10345 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10346 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10347 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10348 | (!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] | 10349 | return pattern; |
| 10350 | } |
| 10351 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10352 | } |
| 10353 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10354 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10355 | 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] | 10356 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10357 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10358 | char *ptr; |
| 10359 | int len; |
| 10360 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10361 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10362 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10363 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10364 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10365 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10366 | |
| 10367 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10368 | if (len <= 0) |
| 10369 | return 0; |
| 10370 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10371 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10372 | smp->data.u.str.str = ptr; |
| 10373 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10374 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10375 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10376 | return 1; |
| 10377 | } |
| 10378 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10379 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10380 | 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] | 10381 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10382 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10383 | char *ptr; |
| 10384 | int len; |
| 10385 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10386 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10387 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10388 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10389 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10390 | return 0; |
| 10391 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10392 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10393 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10394 | |
| 10395 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10396 | if (len <= 0) |
| 10397 | return 0; |
| 10398 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10399 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10400 | smp->data.u.str.str = ptr; |
| 10401 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10402 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10403 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10404 | return 1; |
| 10405 | } |
| 10406 | |
| 10407 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10408 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10409 | 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] | 10410 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10411 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10412 | char *ptr; |
| 10413 | int len; |
| 10414 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10415 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10416 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10417 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10418 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10419 | return 0; |
| 10420 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10421 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10422 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10423 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10424 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10425 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10426 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10427 | return 1; |
| 10428 | } |
| 10429 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 10430 | static int |
| 10431 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10432 | { |
| 10433 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 10434 | return 0; |
| 10435 | |
| 10436 | if (!smp->strm->unique_id) { |
| 10437 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 10438 | return 0; |
| 10439 | smp->strm->unique_id[0] = '\0'; |
| 10440 | } |
| 10441 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 10442 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 10443 | |
| 10444 | smp->data.type = SMP_T_STR; |
| 10445 | smp->data.u.str.str = smp->strm->unique_id; |
| 10446 | smp->flags = SMP_F_CONST; |
| 10447 | return 1; |
| 10448 | } |
| 10449 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10450 | /* returns the longest available part of the body. This requires that the body |
| 10451 | * has been waited for using http-buffer-request. |
| 10452 | */ |
| 10453 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10454 | 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] | 10455 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10456 | struct http_msg *msg; |
| 10457 | unsigned long len; |
| 10458 | unsigned long block1; |
| 10459 | char *body; |
| 10460 | struct chunk *temp; |
| 10461 | |
| 10462 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10463 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10464 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10465 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10466 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10467 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10468 | |
| 10469 | len = http_body_bytes(msg); |
| 10470 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10471 | |
| 10472 | block1 = len; |
| 10473 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10474 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10475 | |
| 10476 | if (block1 == len) { |
| 10477 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10478 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10479 | smp->data.u.str.str = body; |
| 10480 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10481 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10482 | } |
| 10483 | else { |
| 10484 | /* buffer is wrapped, we need to defragment it */ |
| 10485 | temp = get_trash_chunk(); |
| 10486 | memcpy(temp->str, body, block1); |
| 10487 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10488 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10489 | smp->data.u.str.str = temp->str; |
| 10490 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10491 | smp->flags = SMP_F_VOL_TEST; |
| 10492 | } |
| 10493 | return 1; |
| 10494 | } |
| 10495 | |
| 10496 | |
| 10497 | /* returns the available length of the body. This requires that the body |
| 10498 | * has been waited for using http-buffer-request. |
| 10499 | */ |
| 10500 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10501 | 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] | 10502 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10503 | struct http_msg *msg; |
| 10504 | |
| 10505 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10506 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10507 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10508 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10509 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10510 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10511 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10512 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10513 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10514 | |
| 10515 | smp->flags = SMP_F_VOL_TEST; |
| 10516 | return 1; |
| 10517 | } |
| 10518 | |
| 10519 | |
| 10520 | /* returns the advertised length of the body, or the advertised size of the |
| 10521 | * chunks available in the buffer. This requires that the body has been waited |
| 10522 | * for using http-buffer-request. |
| 10523 | */ |
| 10524 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10525 | 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] | 10526 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10527 | struct http_msg *msg; |
| 10528 | |
| 10529 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10530 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10531 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10532 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10533 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 10534 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10535 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10536 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10537 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10538 | |
| 10539 | smp->flags = SMP_F_VOL_TEST; |
| 10540 | return 1; |
| 10541 | } |
| 10542 | |
| 10543 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10544 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10545 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10546 | 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] | 10547 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10548 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10549 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10550 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10551 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10552 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10553 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10554 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10555 | 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] | 10556 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10557 | return 1; |
| 10558 | } |
| 10559 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10560 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10561 | 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] | 10562 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10563 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10564 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10565 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10566 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10567 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10568 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10569 | 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] | 10570 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10571 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10572 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10573 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10574 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10575 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10576 | return 1; |
| 10577 | } |
| 10578 | |
| 10579 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10580 | 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] | 10581 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10582 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10583 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10584 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10585 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10586 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10587 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10588 | 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] | 10589 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10590 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10591 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10592 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10593 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10594 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10595 | return 1; |
| 10596 | } |
| 10597 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10598 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10599 | * Accepts an optional argument of type string containing the header field name, |
| 10600 | * and an optional argument of type signed or unsigned integer to request an |
| 10601 | * 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] | 10602 | * headers are considered from the first one. It does not stop on commas and |
| 10603 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10604 | */ |
| 10605 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10606 | 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] | 10607 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10608 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10609 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10610 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10611 | int occ = 0; |
| 10612 | const char *name_str = NULL; |
| 10613 | int name_len = 0; |
| 10614 | |
| 10615 | if (!ctx) { |
| 10616 | /* first call */ |
| 10617 | ctx = &static_hdr_ctx; |
| 10618 | ctx->idx = 0; |
| 10619 | smp->ctx.a[0] = ctx; |
| 10620 | } |
| 10621 | |
| 10622 | if (args) { |
| 10623 | if (args[0].type != ARGT_STR) |
| 10624 | return 0; |
| 10625 | name_str = args[0].data.str.str; |
| 10626 | name_len = args[0].data.str.len; |
| 10627 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10628 | if (args[1].type == ARGT_SINT) |
| 10629 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10630 | } |
| 10631 | |
| 10632 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10633 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10634 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10635 | 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] | 10636 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10637 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10638 | /* search for header from the beginning */ |
| 10639 | ctx->idx = 0; |
| 10640 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10641 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10642 | /* no explicit occurrence and single fetch => last header by default */ |
| 10643 | occ = -1; |
| 10644 | |
| 10645 | if (!occ) |
| 10646 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10647 | smp->flags |= SMP_F_NOT_LAST; |
| 10648 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10649 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10650 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10651 | 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] | 10652 | return 1; |
| 10653 | |
| 10654 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10655 | return 0; |
| 10656 | } |
| 10657 | |
| 10658 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10659 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10660 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10661 | */ |
| 10662 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10663 | 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] | 10664 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10665 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10666 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10667 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10668 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10669 | const char *name = NULL; |
| 10670 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10671 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10672 | if (args && args->type == ARGT_STR) { |
| 10673 | name = args->data.str.str; |
| 10674 | len = args->data.str.len; |
| 10675 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10676 | |
| 10677 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10678 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10679 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10680 | 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] | 10681 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10682 | ctx.idx = 0; |
| 10683 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10684 | 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] | 10685 | cnt++; |
| 10686 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10687 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10688 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10689 | smp->flags = SMP_F_VOL_HDR; |
| 10690 | return 1; |
| 10691 | } |
| 10692 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10693 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10694 | 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] | 10695 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10696 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10697 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10698 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10699 | struct chunk *temp; |
| 10700 | char del = ','; |
| 10701 | |
| 10702 | if (args && args->type == ARGT_STR) |
| 10703 | del = *args[0].data.str.str; |
| 10704 | |
| 10705 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10706 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10707 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10708 | 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] | 10709 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10710 | temp = get_trash_chunk(); |
| 10711 | |
| 10712 | ctx.idx = 0; |
| 10713 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10714 | if (temp->len) |
| 10715 | temp->str[temp->len++] = del; |
| 10716 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10717 | temp->len += ctx.del; |
| 10718 | } |
| 10719 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10720 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10721 | smp->data.u.str.str = temp->str; |
| 10722 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10723 | smp->flags = SMP_F_VOL_HDR; |
| 10724 | return 1; |
| 10725 | } |
| 10726 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10727 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10728 | * Accepts an optional argument of type string containing the header field name, |
| 10729 | * and an optional argument of type signed or unsigned integer to request an |
| 10730 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10731 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10732 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10733 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10734 | 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] | 10735 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10736 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10737 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10738 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10739 | int occ = 0; |
| 10740 | const char *name_str = NULL; |
| 10741 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10742 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10743 | if (!ctx) { |
| 10744 | /* first call */ |
| 10745 | ctx = &static_hdr_ctx; |
| 10746 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10747 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10748 | } |
| 10749 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10750 | if (args) { |
| 10751 | if (args[0].type != ARGT_STR) |
| 10752 | return 0; |
| 10753 | name_str = args[0].data.str.str; |
| 10754 | name_len = args[0].data.str.len; |
| 10755 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10756 | if (args[1].type == ARGT_SINT) |
| 10757 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10758 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10759 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10760 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10761 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10762 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10763 | 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] | 10764 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10765 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10766 | /* search for header from the beginning */ |
| 10767 | ctx->idx = 0; |
| 10768 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10769 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10770 | /* no explicit occurrence and single fetch => last header by default */ |
| 10771 | occ = -1; |
| 10772 | |
| 10773 | if (!occ) |
| 10774 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10775 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10776 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10777 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10778 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10779 | 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] | 10780 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10781 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10782 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10783 | return 0; |
| 10784 | } |
| 10785 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10786 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10787 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10788 | */ |
| 10789 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10790 | 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] | 10791 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10792 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10793 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10794 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10795 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10796 | const char *name = NULL; |
| 10797 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10798 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10799 | if (args && args->type == ARGT_STR) { |
| 10800 | name = args->data.str.str; |
| 10801 | len = args->data.str.len; |
| 10802 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10803 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10804 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10805 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10806 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10807 | 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] | 10808 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10809 | ctx.idx = 0; |
| 10810 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10811 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10812 | cnt++; |
| 10813 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10814 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10815 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10816 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10817 | return 1; |
| 10818 | } |
| 10819 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10820 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10821 | * takes a mandatory argument of type string and an optional one of type int |
| 10822 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10823 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10824 | */ |
| 10825 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10826 | 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] | 10827 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10828 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10829 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10830 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10831 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10832 | 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] | 10833 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10834 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10835 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10836 | } |
| 10837 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10838 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10839 | * and an optional one of type int to designate a specific occurrence. |
| 10840 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10841 | */ |
| 10842 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10843 | 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] | 10844 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10845 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10846 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10847 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10848 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10849 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10850 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10851 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10852 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10853 | if (smp->data.u.str.len < temp->size - 1) { |
| 10854 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10855 | temp->str[smp->data.u.str.len] = '\0'; |
| 10856 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10857 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10858 | break; |
| 10859 | } |
| 10860 | } |
| 10861 | } |
| 10862 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10863 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10864 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10865 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10866 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10867 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10868 | } |
| 10869 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10870 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10871 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10872 | */ |
| 10873 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10874 | 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] | 10875 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10876 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10877 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10878 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10879 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10880 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10881 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10882 | 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] | 10883 | ptr = http_get_path(txn); |
| 10884 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10885 | return 0; |
| 10886 | |
| 10887 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10888 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10889 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10890 | |
| 10891 | while (ptr < end && *ptr != '?') |
| 10892 | ptr++; |
| 10893 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10894 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10895 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10896 | return 1; |
| 10897 | } |
| 10898 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10899 | /* This produces a concatenation of the first occurrence of the Host header |
| 10900 | * followed by the path component if it begins with a slash ('/'). This means |
| 10901 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10902 | * If no Host header is found, then the path is returned as-is. The returned |
| 10903 | * 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] | 10904 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10905 | */ |
| 10906 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10907 | 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] | 10908 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10909 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10910 | char *ptr, *end, *beg; |
| 10911 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10912 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10913 | |
| 10914 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10915 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10916 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10917 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10918 | 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] | 10919 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10920 | |
| 10921 | /* 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] | 10922 | temp = get_trash_chunk(); |
| 10923 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10924 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10925 | smp->data.u.str.str = temp->str; |
| 10926 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10927 | |
| 10928 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10929 | 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] | 10930 | beg = http_get_path(txn); |
| 10931 | if (!beg) |
| 10932 | beg = end; |
| 10933 | |
| 10934 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10935 | |
| 10936 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10937 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10938 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10939 | } |
| 10940 | |
| 10941 | smp->flags = SMP_F_VOL_1ST; |
| 10942 | return 1; |
| 10943 | } |
| 10944 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10945 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10946 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10947 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10948 | * 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] | 10949 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10950 | * 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] | 10951 | * high-traffic sites without having to store whole paths. |
| 10952 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10953 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10954 | 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] | 10955 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10956 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10957 | struct hdr_ctx ctx; |
| 10958 | unsigned int hash = 0; |
| 10959 | char *ptr, *beg, *end; |
| 10960 | int len; |
| 10961 | |
| 10962 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10963 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10964 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10965 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10966 | 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] | 10967 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10968 | ptr = ctx.line + ctx.val; |
| 10969 | len = ctx.vlen; |
| 10970 | while (len--) |
| 10971 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10972 | } |
| 10973 | |
| 10974 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10975 | 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] | 10976 | beg = http_get_path(txn); |
| 10977 | if (!beg) |
| 10978 | beg = end; |
| 10979 | |
| 10980 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10981 | |
| 10982 | if (beg < ptr && *beg == '/') { |
| 10983 | while (beg < ptr) |
| 10984 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10985 | } |
| 10986 | hash = full_hash(hash); |
| 10987 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10988 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10989 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10990 | smp->flags = SMP_F_VOL_1ST; |
| 10991 | return 1; |
| 10992 | } |
| 10993 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10994 | /* 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] | 10995 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10996 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10997 | * 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] | 10998 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10999 | * 8 bytes would still work. |
| 11000 | */ |
| 11001 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11002 | 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] | 11003 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11004 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11005 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11006 | |
| 11007 | if (!cli_conn) |
| 11008 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11009 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11010 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11011 | return 0; |
| 11012 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 11013 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11014 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 11015 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11016 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11017 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11018 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11019 | 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] | 11020 | temp->len += 4; |
| 11021 | break; |
| 11022 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11023 | 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] | 11024 | temp->len += 16; |
| 11025 | break; |
| 11026 | default: |
| 11027 | return 0; |
| 11028 | } |
| 11029 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11030 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11031 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 11032 | return 1; |
| 11033 | } |
| 11034 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11035 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 11036 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 11037 | * of type string carrying the whole query string. |
| 11038 | */ |
| 11039 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11040 | 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] | 11041 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11042 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11043 | char *ptr, *end; |
| 11044 | |
| 11045 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11046 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11047 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11048 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 11049 | end = ptr + txn->req.sl.rq.u_l; |
| 11050 | |
| 11051 | /* look up the '?' */ |
| 11052 | do { |
| 11053 | if (ptr == end) |
| 11054 | return 0; |
| 11055 | } while (*ptr++ != '?'); |
| 11056 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11057 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11058 | smp->data.u.str.str = ptr; |
| 11059 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 11060 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11061 | return 1; |
| 11062 | } |
| 11063 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11064 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11065 | 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] | 11066 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11067 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 11068 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 11069 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11070 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11071 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11072 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11073 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11074 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 11075 | return 1; |
| 11076 | } |
| 11077 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11078 | /* return a valid test if the current request is the first one on the connection */ |
| 11079 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11080 | 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] | 11081 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11082 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11083 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 11084 | return 1; |
| 11085 | } |
| 11086 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11087 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11088 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11089 | 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] | 11090 | { |
| 11091 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11092 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11093 | return 0; |
| 11094 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 11095 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11096 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11097 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11098 | return 0; |
| 11099 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11100 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11101 | 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] | 11102 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 11103 | return 1; |
| 11104 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11105 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11106 | /* Accepts exactly 1 argument of type userlist */ |
| 11107 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11108 | 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] | 11109 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11110 | if (!args || args->type != ARGT_USR) |
| 11111 | return 0; |
| 11112 | |
| 11113 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11114 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11115 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11116 | return 0; |
| 11117 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11118 | /* if the user does not belong to the userlist or has a wrong password, |
| 11119 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11120 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11121 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11122 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 11123 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 11124 | return 0; |
| 11125 | |
| 11126 | /* pat_match_auth() will need the user list */ |
| 11127 | smp->ctx.a[0] = args->data.usr; |
| 11128 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11129 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11130 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11131 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 11132 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 11133 | |
| 11134 | return 1; |
| 11135 | } |
| 11136 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11137 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 11138 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 11139 | * the cookie value is returned into *value and *value_l, and the function |
| 11140 | * returns a pointer to the next pointer to search from if the value was found. |
| 11141 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11142 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11143 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11144 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11145 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11146 | * list is expected. |
| 11147 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 11148 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11149 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11150 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11151 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11152 | { |
| 11153 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11154 | char *next; |
| 11155 | |
| 11156 | /* we search at least a cookie name followed by an equal, and more |
| 11157 | * generally something like this : |
| 11158 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11159 | */ |
| 11160 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11161 | /* Iterate through all cookies on this line */ |
| 11162 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11163 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11164 | att_beg++; |
| 11165 | |
| 11166 | /* find att_end : this is the first character after the last non |
| 11167 | * space before the equal. It may be equal to hdr_end. |
| 11168 | */ |
| 11169 | equal = att_end = att_beg; |
| 11170 | |
| 11171 | while (equal < hdr_end) { |
| 11172 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11173 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11174 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11175 | continue; |
| 11176 | att_end = equal; |
| 11177 | } |
| 11178 | |
| 11179 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11180 | * is between <att_beg> and <equal>, both may be identical. |
| 11181 | */ |
| 11182 | |
| 11183 | /* look for end of cookie if there is an equal sign */ |
| 11184 | if (equal < hdr_end && *equal == '=') { |
| 11185 | /* look for the beginning of the value */ |
| 11186 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11187 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11188 | val_beg++; |
| 11189 | |
| 11190 | /* find the end of the value, respecting quotes */ |
| 11191 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11192 | |
| 11193 | /* make val_end point to the first white space or delimitor after the value */ |
| 11194 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 11195 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11196 | val_end--; |
| 11197 | } else { |
| 11198 | val_beg = val_end = next = equal; |
| 11199 | } |
| 11200 | |
| 11201 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11202 | * they will automatically be removed if a header before them is removed, |
| 11203 | * since they're supposed to be linked together. |
| 11204 | */ |
| 11205 | if (*att_beg == '$') |
| 11206 | continue; |
| 11207 | |
| 11208 | /* Ignore cookies with no equal sign */ |
| 11209 | if (equal == next) |
| 11210 | continue; |
| 11211 | |
| 11212 | /* Now we have the cookie name between att_beg and att_end, and |
| 11213 | * its value between val_beg and val_end. |
| 11214 | */ |
| 11215 | |
| 11216 | if (att_end - att_beg == cookie_name_l && |
| 11217 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11218 | /* let's return this value and indicate where to go on from */ |
| 11219 | *value = val_beg; |
| 11220 | *value_l = val_end - val_beg; |
| 11221 | return next + 1; |
| 11222 | } |
| 11223 | |
| 11224 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11225 | if (!list) |
| 11226 | break; |
| 11227 | } |
| 11228 | |
| 11229 | return NULL; |
| 11230 | } |
| 11231 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11232 | /* Fetch a captured HTTP request header. The index is the position of |
| 11233 | * the "capture" option in the configuration file |
| 11234 | */ |
| 11235 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11236 | 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] | 11237 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11238 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11239 | int idx; |
| 11240 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11241 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11242 | return 0; |
| 11243 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11244 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11245 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11246 | 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] | 11247 | return 0; |
| 11248 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11249 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11250 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11251 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 11252 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11253 | |
| 11254 | return 1; |
| 11255 | } |
| 11256 | |
| 11257 | /* Fetch a captured HTTP response header. The index is the position of |
| 11258 | * the "capture" option in the configuration file |
| 11259 | */ |
| 11260 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11261 | 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] | 11262 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11263 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11264 | int idx; |
| 11265 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11266 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11267 | return 0; |
| 11268 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11269 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11270 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11271 | 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] | 11272 | return 0; |
| 11273 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11274 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11275 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11276 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 11277 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11278 | |
| 11279 | return 1; |
| 11280 | } |
| 11281 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11282 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11283 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11284 | 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] | 11285 | { |
| 11286 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11287 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11288 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11289 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11290 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11291 | return 0; |
| 11292 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11293 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11294 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11295 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11296 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11297 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11298 | temp = get_trash_chunk(); |
| 11299 | temp->str = txn->uri; |
| 11300 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11301 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11302 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11303 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11304 | |
| 11305 | return 1; |
| 11306 | |
| 11307 | } |
| 11308 | |
| 11309 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11310 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11311 | 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] | 11312 | { |
| 11313 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11314 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11315 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11316 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11317 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11318 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11319 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11320 | ptr = txn->uri; |
| 11321 | |
| 11322 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11323 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11324 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11325 | if (!*ptr) |
| 11326 | return 0; |
| 11327 | |
| 11328 | ptr++; /* skip the space */ |
| 11329 | |
| 11330 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11331 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11332 | if (!ptr) |
| 11333 | return 0; |
| 11334 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11335 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11336 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11337 | smp->data.u.str = *temp; |
| 11338 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11339 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11340 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11341 | |
| 11342 | return 1; |
| 11343 | } |
| 11344 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11345 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11346 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11347 | */ |
| 11348 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11349 | 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] | 11350 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11351 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11352 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11353 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11354 | return 0; |
| 11355 | |
| 11356 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11357 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11358 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11359 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11360 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11361 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11362 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11363 | smp->flags = SMP_F_CONST; |
| 11364 | return 1; |
| 11365 | |
| 11366 | } |
| 11367 | |
| 11368 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11369 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11370 | */ |
| 11371 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11372 | 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] | 11373 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11374 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11375 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11376 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11377 | return 0; |
| 11378 | |
| 11379 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11380 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11381 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11382 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11383 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11384 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11385 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11386 | smp->flags = SMP_F_CONST; |
| 11387 | return 1; |
| 11388 | |
| 11389 | } |
| 11390 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11391 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11392 | /* 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] | 11393 | * 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] | 11394 | * 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] | 11395 | * 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] | 11396 | * 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] | 11397 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11398 | * 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] | 11399 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11400 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11401 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11402 | 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] | 11403 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11404 | struct http_txn *txn; |
| 11405 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11406 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11407 | const struct http_msg *msg; |
| 11408 | const char *hdr_name; |
| 11409 | int hdr_name_len; |
| 11410 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11411 | int occ = 0; |
| 11412 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11413 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11414 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11415 | return 0; |
| 11416 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11417 | if (!ctx) { |
| 11418 | /* first call */ |
| 11419 | ctx = &static_hdr_ctx; |
| 11420 | ctx->idx = 0; |
| 11421 | smp->ctx.a[2] = ctx; |
| 11422 | } |
| 11423 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11424 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11425 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11426 | txn = smp->strm->txn; |
| 11427 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11428 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11429 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11430 | msg = &txn->req; |
| 11431 | hdr_name = "Cookie"; |
| 11432 | hdr_name_len = 6; |
| 11433 | } else { |
| 11434 | msg = &txn->rsp; |
| 11435 | hdr_name = "Set-Cookie"; |
| 11436 | hdr_name_len = 10; |
| 11437 | } |
| 11438 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11439 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11440 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11441 | occ = -1; |
| 11442 | |
| 11443 | /* OK so basically here, either we want only one value and it's the |
| 11444 | * last one, or we want to iterate over all of them and we fetch the |
| 11445 | * next one. |
| 11446 | */ |
| 11447 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11448 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11449 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11450 | /* search for the header from the beginning, we must first initialize |
| 11451 | * the search parameters. |
| 11452 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11453 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11454 | ctx->idx = 0; |
| 11455 | } |
| 11456 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11457 | smp->flags |= SMP_F_VOL_HDR; |
| 11458 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11459 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11460 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11461 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11462 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11463 | goto out; |
| 11464 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11465 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11466 | continue; |
| 11467 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11468 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11469 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11470 | } |
| 11471 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11472 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11473 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11474 | 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] | 11475 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11476 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11477 | &smp->data.u.str.str, |
| 11478 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11479 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11480 | found = 1; |
| 11481 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11482 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11483 | smp->flags |= SMP_F_NOT_LAST; |
| 11484 | return 1; |
| 11485 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11486 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11487 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11488 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11489 | /* all cookie headers and values were scanned. If we're looking for the |
| 11490 | * last occurrence, we may return it now. |
| 11491 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11492 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11493 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11494 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11495 | } |
| 11496 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11497 | /* 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] | 11498 | * 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] | 11499 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11500 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11501 | */ |
| 11502 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11503 | 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] | 11504 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11505 | struct http_txn *txn; |
| 11506 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11507 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11508 | const struct http_msg *msg; |
| 11509 | const char *hdr_name; |
| 11510 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11511 | int cnt; |
| 11512 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11513 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11514 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11515 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11516 | return 0; |
| 11517 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11518 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11519 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11520 | txn = smp->strm->txn; |
| 11521 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11522 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11523 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11524 | msg = &txn->req; |
| 11525 | hdr_name = "Cookie"; |
| 11526 | hdr_name_len = 6; |
| 11527 | } else { |
| 11528 | msg = &txn->rsp; |
| 11529 | hdr_name = "Set-Cookie"; |
| 11530 | hdr_name_len = 10; |
| 11531 | } |
| 11532 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11533 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11534 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11535 | ctx.idx = 0; |
| 11536 | cnt = 0; |
| 11537 | |
| 11538 | while (1) { |
| 11539 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11540 | if (!val_beg) { |
| 11541 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11542 | break; |
| 11543 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11544 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11545 | continue; |
| 11546 | |
| 11547 | val_beg = ctx.line + ctx.val; |
| 11548 | val_end = val_beg + ctx.vlen; |
| 11549 | } |
| 11550 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11551 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11552 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11553 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11554 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11555 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11556 | &smp->data.u.str.str, |
| 11557 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11558 | cnt++; |
| 11559 | } |
| 11560 | } |
| 11561 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11562 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11563 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11564 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11565 | return 1; |
| 11566 | } |
| 11567 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11568 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11569 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11570 | */ |
| 11571 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11572 | 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] | 11573 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11574 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11575 | |
| 11576 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11577 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11578 | 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] | 11579 | } |
| 11580 | |
| 11581 | return ret; |
| 11582 | } |
| 11583 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11584 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11585 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11586 | /************************************************************************/ |
| 11587 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11588 | /* |
| 11589 | * Given a path string and its length, find the position of beginning of the |
| 11590 | * query string. Returns NULL if no query string is found in the path. |
| 11591 | * |
| 11592 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11593 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11594 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11595 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11596 | 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] | 11597 | { |
| 11598 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11599 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11600 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11601 | return p ? p + 1 : NULL; |
| 11602 | } |
| 11603 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11604 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11605 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11606 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11607 | } |
| 11608 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11609 | /* after increasing a pointer value, it can exceed the first buffer |
| 11610 | * size. This function transform the value of <ptr> according with |
| 11611 | * the expected position. <chunks> is an array of the one or two |
| 11612 | * avalaible chunks. The first value is the start of the first chunk, |
| 11613 | * the second value if the end+1 of the first chunks. The third value |
| 11614 | * is NULL or the start of the second chunk and the fourth value is |
| 11615 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11616 | * wrap, else returns 0. |
| 11617 | */ |
| 11618 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11619 | { |
| 11620 | if (*ptr < chunks[1]) |
| 11621 | return 0; |
| 11622 | if (!chunks[2]) |
| 11623 | return 0; |
| 11624 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11625 | return 1; |
| 11626 | } |
| 11627 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11628 | /* |
| 11629 | * Given a url parameter, find the starting position of the first occurence, |
| 11630 | * or NULL if the parameter is not found. |
| 11631 | * |
| 11632 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11633 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11634 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11635 | * 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] | 11636 | * or the second chunk. The caller must be check the position before using the |
| 11637 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11638 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11639 | static const char * |
| 11640 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11641 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11642 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11643 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11644 | const char *pos, *last, *equal; |
| 11645 | const char **bufs = chunks; |
| 11646 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11647 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11648 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11649 | pos = bufs[0]; |
| 11650 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11651 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11652 | /* Check the equal. */ |
| 11653 | equal = pos + url_param_name_l; |
| 11654 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11655 | if (equal >= chunks[3]) |
| 11656 | return NULL; |
| 11657 | } else { |
| 11658 | if (equal >= chunks[1]) |
| 11659 | return NULL; |
| 11660 | } |
| 11661 | if (*equal == '=') { |
| 11662 | if (pos + url_param_name_l > last) { |
| 11663 | /* process wrap case, we detect a wrap. In this case, the |
| 11664 | * comparison is performed in two parts. |
| 11665 | */ |
| 11666 | |
| 11667 | /* This is the end, we dont have any other chunk. */ |
| 11668 | if (bufs != chunks || !bufs[2]) |
| 11669 | return NULL; |
| 11670 | |
| 11671 | /* Compute the length of each part of the comparison. */ |
| 11672 | l1 = last - pos; |
| 11673 | l2 = url_param_name_l - l1; |
| 11674 | |
| 11675 | /* The second buffer is too short to contain the compared string. */ |
| 11676 | if (bufs[2] + l2 > bufs[3]) |
| 11677 | return NULL; |
| 11678 | |
| 11679 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11680 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11681 | return pos; |
| 11682 | |
| 11683 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11684 | bufs += 2; |
| 11685 | pos = bufs[0] + l2; |
| 11686 | last = bufs[1]; |
| 11687 | |
| 11688 | } else { |
| 11689 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11690 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11691 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11692 | pos += url_param_name_l + 1; |
| 11693 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11694 | last = bufs[2]; |
| 11695 | } |
| 11696 | } |
| 11697 | |
| 11698 | while (1) { |
| 11699 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11700 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11701 | pos++; |
| 11702 | if (pos < last) |
| 11703 | break; |
| 11704 | /* process buffer wrapping. */ |
| 11705 | if (bufs != chunks || !bufs[2]) |
| 11706 | return NULL; |
| 11707 | bufs += 2; |
| 11708 | pos = bufs[0]; |
| 11709 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11710 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11711 | pos++; |
| 11712 | } |
| 11713 | return NULL; |
| 11714 | } |
| 11715 | |
| 11716 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11717 | * Given a url parameter name and a query string, find the next value. |
| 11718 | * An empty url_param_name matches the first available parameter. |
| 11719 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11720 | * respectively provide a pointer to the value and its end. |
| 11721 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11722 | */ |
| 11723 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11724 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11725 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11726 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11727 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11728 | const char *arg_start, *qs_end; |
| 11729 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11730 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11731 | arg_start = chunks[0]; |
| 11732 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11733 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11734 | /* Looks for an argument name. */ |
| 11735 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11736 | url_param_name, url_param_name_l, |
| 11737 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11738 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11739 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11740 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11741 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11742 | if (!arg_start) |
| 11743 | return 0; |
| 11744 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11745 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11746 | while (1) { |
| 11747 | /* looks for the first argument. */ |
| 11748 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11749 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11750 | /* Check for wrapping. */ |
| 11751 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11752 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11753 | chunks[2]) { |
| 11754 | arg_start = chunks[2]; |
| 11755 | qs_end = chunks[3]; |
| 11756 | continue; |
| 11757 | } |
| 11758 | return 0; |
| 11759 | } |
| 11760 | break; |
| 11761 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11762 | value_start++; |
| 11763 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11764 | else { |
| 11765 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11766 | value_start = arg_start + url_param_name_l + 1; |
| 11767 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11768 | /* Check for pointer wrapping. */ |
| 11769 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11770 | /* Update the end pointer. */ |
| 11771 | qs_end = chunks[3]; |
| 11772 | |
| 11773 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11774 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11775 | return 0; |
| 11776 | } |
| 11777 | } |
| 11778 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11779 | value_end = value_start; |
| 11780 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11781 | while (1) { |
| 11782 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11783 | value_end++; |
| 11784 | if (value_end < qs_end) |
| 11785 | break; |
| 11786 | /* process buffer wrapping. */ |
| 11787 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11788 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11789 | chunks[2]) { |
| 11790 | value_end = chunks[2]; |
| 11791 | qs_end = chunks[3]; |
| 11792 | continue; |
| 11793 | } |
| 11794 | break; |
| 11795 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11796 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11797 | *vstart = value_start; |
| 11798 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11799 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11800 | } |
| 11801 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11802 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11803 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11804 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11805 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11806 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11807 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11808 | 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] | 11809 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11810 | const char *vstart, *vend; |
| 11811 | struct chunk *temp; |
| 11812 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11813 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11814 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11815 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11816 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11817 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11818 | return 0; |
| 11819 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11820 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11821 | * if the value is wrapped, copy-it in a buffer. |
| 11822 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11823 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11824 | if (chunks[2] && |
| 11825 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11826 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11827 | /* Wrapped case. */ |
| 11828 | temp = get_trash_chunk(); |
| 11829 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11830 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11831 | smp->data.u.str.str = temp->str; |
| 11832 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11833 | } else { |
| 11834 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11835 | smp->data.u.str.str = (char *)vstart; |
| 11836 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11837 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11838 | } |
| 11839 | |
| 11840 | /* Update context, check wrapping. */ |
| 11841 | chunks[0] = vend; |
| 11842 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11843 | chunks[1] = chunks[3]; |
| 11844 | chunks[2] = NULL; |
| 11845 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11846 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11847 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11848 | smp->flags |= SMP_F_NOT_LAST; |
| 11849 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11850 | return 1; |
| 11851 | } |
| 11852 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11853 | /* This function iterates over each parameter of the query string. It uses |
| 11854 | * 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] | 11855 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11856 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11857 | * considered. It supports an optional delimiter argument for the beginning of |
| 11858 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11859 | */ |
| 11860 | static int |
| 11861 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11862 | { |
| 11863 | struct http_msg *msg; |
| 11864 | char delim = '?'; |
| 11865 | const char *name; |
| 11866 | int name_len; |
| 11867 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11868 | if (!args || |
| 11869 | (args[0].type && args[0].type != ARGT_STR) || |
| 11870 | (args[1].type && args[1].type != ARGT_STR)) |
| 11871 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11872 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11873 | name = ""; |
| 11874 | name_len = 0; |
| 11875 | if (args->type == ARGT_STR) { |
| 11876 | name = args->data.str.str; |
| 11877 | name_len = args->data.str.len; |
| 11878 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11879 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11880 | if (args[1].type) |
| 11881 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11882 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11883 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11884 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11885 | |
| 11886 | msg = &smp->strm->txn->req; |
| 11887 | |
| 11888 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11889 | msg->sl.rq.u_l, delim); |
| 11890 | if (!smp->ctx.a[0]) |
| 11891 | return 0; |
| 11892 | |
| 11893 | 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] | 11894 | |
| 11895 | /* Assume that the context is filled with NULL pointer |
| 11896 | * before the first call. |
| 11897 | * smp->ctx.a[2] = NULL; |
| 11898 | * smp->ctx.a[3] = NULL; |
| 11899 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11900 | } |
| 11901 | |
| 11902 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11903 | } |
| 11904 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11905 | /* This function iterates over each parameter of the body. This requires |
| 11906 | * 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] | 11907 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11908 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11909 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11910 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11911 | */ |
| 11912 | static int |
| 11913 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11914 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11915 | struct http_msg *msg; |
| 11916 | unsigned long len; |
| 11917 | unsigned long block1; |
| 11918 | char *body; |
| 11919 | const char *name; |
| 11920 | int name_len; |
| 11921 | |
| 11922 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11923 | return 0; |
| 11924 | |
| 11925 | name = ""; |
| 11926 | name_len = 0; |
| 11927 | if (args[0].type == ARGT_STR) { |
| 11928 | name = args[0].data.str.str; |
| 11929 | name_len = args[0].data.str.len; |
| 11930 | } |
| 11931 | |
| 11932 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11933 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11934 | |
| 11935 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11936 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11937 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11938 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11939 | |
| 11940 | len = http_body_bytes(msg); |
| 11941 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11942 | |
| 11943 | block1 = len; |
| 11944 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11945 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11946 | |
| 11947 | if (block1 == len) { |
| 11948 | /* buffer is not wrapped (or empty) */ |
| 11949 | smp->ctx.a[0] = body; |
| 11950 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11951 | |
| 11952 | /* Assume that the context is filled with NULL pointer |
| 11953 | * before the first call. |
| 11954 | * smp->ctx.a[2] = NULL; |
| 11955 | * smp->ctx.a[3] = NULL; |
| 11956 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11957 | } |
| 11958 | else { |
| 11959 | /* buffer is wrapped, we need to defragment it */ |
| 11960 | smp->ctx.a[0] = body; |
| 11961 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11962 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11963 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11964 | } |
| 11965 | } |
| 11966 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11967 | } |
| 11968 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11969 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11970 | * above). |
| 11971 | */ |
| 11972 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11973 | 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] | 11974 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11975 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11976 | |
| 11977 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11978 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11979 | 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] | 11980 | } |
| 11981 | |
| 11982 | return ret; |
| 11983 | } |
| 11984 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11985 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11986 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11987 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11988 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11989 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11990 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11991 | * high-traffic sites without having to store whole paths. |
| 11992 | * this differs from the base32 functions in that it includes the url parameters |
| 11993 | * as well as the path |
| 11994 | */ |
| 11995 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11996 | 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] | 11997 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11998 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11999 | struct hdr_ctx ctx; |
| 12000 | unsigned int hash = 0; |
| 12001 | char *ptr, *beg, *end; |
| 12002 | int len; |
| 12003 | |
| 12004 | CHECK_HTTP_MESSAGE_FIRST(); |
| 12005 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12006 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12007 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12008 | 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] | 12009 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 12010 | ptr = ctx.line + ctx.val; |
| 12011 | len = ctx.vlen; |
| 12012 | while (len--) |
| 12013 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 12014 | } |
| 12015 | |
| 12016 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 12017 | 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] | 12018 | beg = http_get_path(txn); |
| 12019 | if (!beg) |
| 12020 | beg = end; |
| 12021 | |
| 12022 | for (ptr = beg; ptr < end ; ptr++); |
| 12023 | |
| 12024 | if (beg < ptr && *beg == '/') { |
| 12025 | while (beg < ptr) |
| 12026 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 12027 | } |
| 12028 | hash = full_hash(hash); |
| 12029 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12030 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12031 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12032 | smp->flags = SMP_F_VOL_1ST; |
| 12033 | return 1; |
| 12034 | } |
| 12035 | |
| 12036 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 12037 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 12038 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 12039 | * on the source address length. The URL hash is stored before the address so |
| 12040 | * that in environments where IPv6 is insignificant, truncating the output to |
| 12041 | * 8 bytes would still work. |
| 12042 | */ |
| 12043 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12044 | 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] | 12045 | { |
| 12046 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12047 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12048 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 12049 | if (!cli_conn) |
| 12050 | return 0; |
| 12051 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 12052 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12053 | return 0; |
| 12054 | |
| 12055 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 12056 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 12057 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12058 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12059 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12060 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12061 | 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] | 12062 | temp->len += 4; |
| 12063 | break; |
| 12064 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 12065 | 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] | 12066 | temp->len += 16; |
| 12067 | break; |
| 12068 | default: |
| 12069 | return 0; |
| 12070 | } |
| 12071 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12072 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12073 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12074 | return 1; |
| 12075 | } |
| 12076 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12077 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 12078 | * keyword. These keywords support an optional positive or negative occurrence |
| 12079 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 12080 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 12081 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 12082 | * error message in case of error, that the caller is responsible for freeing. |
| 12083 | * The initial location must either be freeable or NULL. |
| 12084 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 12085 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12086 | { |
| 12087 | 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] | 12088 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 12089 | return 0; |
| 12090 | } |
| 12091 | return 1; |
| 12092 | } |
| 12093 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12094 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 12095 | * adds an optional offset found in args[0] and emits a string representing |
| 12096 | * the date in RFC-1123/5322 format. |
| 12097 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12098 | 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] | 12099 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 12100 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12101 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 12102 | struct chunk *temp; |
| 12103 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12104 | /* 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] | 12105 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12106 | |
| 12107 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12108 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12109 | curr_date += args[0].data.sint; |
| 12110 | |
| 12111 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 12112 | if (!tm) |
| 12113 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12114 | |
| 12115 | temp = get_trash_chunk(); |
| 12116 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 12117 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 12118 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 12119 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 12120 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12121 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 12122 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12123 | return 1; |
| 12124 | } |
| 12125 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12126 | /* Match language range with language tag. RFC2616 14.4: |
| 12127 | * |
| 12128 | * A language-range matches a language-tag if it exactly equals |
| 12129 | * the tag, or if it exactly equals a prefix of the tag such |
| 12130 | * that the first tag character following the prefix is "-". |
| 12131 | * |
| 12132 | * Return 1 if the strings match, else return 0. |
| 12133 | */ |
| 12134 | static inline int language_range_match(const char *range, int range_len, |
| 12135 | const char *tag, int tag_len) |
| 12136 | { |
| 12137 | const char *end = range + range_len; |
| 12138 | const char *tend = tag + tag_len; |
| 12139 | while (range < end) { |
| 12140 | if (*range == '-' && tag == tend) |
| 12141 | return 1; |
| 12142 | if (*range != *tag || tag == tend) |
| 12143 | return 0; |
| 12144 | range++; |
| 12145 | tag++; |
| 12146 | } |
| 12147 | /* Return true only if the last char of the tag is matched. */ |
| 12148 | return tag == tend; |
| 12149 | } |
| 12150 | |
| 12151 | /* 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] | 12152 | 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] | 12153 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12154 | const char *al = smp->data.u.str.str; |
| 12155 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12156 | const char *token; |
| 12157 | int toklen; |
| 12158 | int qvalue; |
| 12159 | const char *str; |
| 12160 | const char *w; |
| 12161 | int best_q = 0; |
| 12162 | |
| 12163 | /* Set the constant to the sample, because the output of the |
| 12164 | * function will be peek in the constant configuration string. |
| 12165 | */ |
| 12166 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12167 | smp->data.u.str.size = 0; |
| 12168 | smp->data.u.str.str = ""; |
| 12169 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12170 | |
| 12171 | /* Parse the accept language */ |
| 12172 | while (1) { |
| 12173 | |
| 12174 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12175 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12176 | al++; |
| 12177 | if (al >= end) |
| 12178 | break; |
| 12179 | |
| 12180 | /* Start of the fisrt word. */ |
| 12181 | token = al; |
| 12182 | |
| 12183 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12184 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12185 | al++; |
| 12186 | if (al == token) |
| 12187 | goto expect_comma; |
| 12188 | |
| 12189 | /* Length of the token. */ |
| 12190 | toklen = al - token; |
| 12191 | qvalue = 1000; |
| 12192 | |
| 12193 | /* Check if the token exists in the list. If the token not exists, |
| 12194 | * jump to the next token. |
| 12195 | */ |
| 12196 | str = args[0].data.str.str; |
| 12197 | w = str; |
| 12198 | while (1) { |
| 12199 | if (*str == ';' || *str == '\0') { |
| 12200 | if (language_range_match(token, toklen, w, str-w)) |
| 12201 | goto look_for_q; |
| 12202 | if (*str == '\0') |
| 12203 | goto expect_comma; |
| 12204 | w = str + 1; |
| 12205 | } |
| 12206 | str++; |
| 12207 | } |
| 12208 | goto expect_comma; |
| 12209 | |
| 12210 | look_for_q: |
| 12211 | |
| 12212 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12213 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12214 | al++; |
| 12215 | if (al >= end) |
| 12216 | goto process_value; |
| 12217 | |
| 12218 | /* If ',' is found, process the result */ |
| 12219 | if (*al == ',') |
| 12220 | goto process_value; |
| 12221 | |
| 12222 | /* If the character is different from ';', look |
| 12223 | * for the end of the header part in best effort. |
| 12224 | */ |
| 12225 | if (*al != ';') |
| 12226 | goto expect_comma; |
| 12227 | |
| 12228 | /* Assumes that the char is ';', now expect "q=". */ |
| 12229 | al++; |
| 12230 | |
| 12231 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12232 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12233 | al++; |
| 12234 | if (al >= end) |
| 12235 | goto process_value; |
| 12236 | |
| 12237 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12238 | if (*al != 'q') |
| 12239 | goto process_value; |
| 12240 | al++; |
| 12241 | |
| 12242 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12243 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12244 | al++; |
| 12245 | if (al >= end) |
| 12246 | goto process_value; |
| 12247 | |
| 12248 | /* Expect '='. If no '=', continue in best effort */ |
| 12249 | if (*al != '=') |
| 12250 | goto process_value; |
| 12251 | al++; |
| 12252 | |
| 12253 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12254 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12255 | al++; |
| 12256 | if (al >= end) |
| 12257 | goto process_value; |
| 12258 | |
| 12259 | /* Parse the q value. */ |
| 12260 | qvalue = parse_qvalue(al, &al); |
| 12261 | |
| 12262 | process_value: |
| 12263 | |
| 12264 | /* If the new q value is the best q value, then store the associated |
| 12265 | * language in the response. If qvalue is the biggest value (1000), |
| 12266 | * break the process. |
| 12267 | */ |
| 12268 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12269 | smp->data.u.str.str = (char *)w; |
| 12270 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12271 | if (qvalue >= 1000) |
| 12272 | break; |
| 12273 | best_q = qvalue; |
| 12274 | } |
| 12275 | |
| 12276 | expect_comma: |
| 12277 | |
| 12278 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12279 | while (al < end && *al != ',') |
| 12280 | al++; |
| 12281 | if (al >= end) |
| 12282 | break; |
| 12283 | |
| 12284 | /* Comma is found, jump it and restart the analyzer. */ |
| 12285 | al++; |
| 12286 | } |
| 12287 | |
| 12288 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12289 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12290 | smp->data.u.str.str = args[1].data.str.str; |
| 12291 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12292 | } |
| 12293 | |
| 12294 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12295 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12296 | } |
| 12297 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12298 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12299 | 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] | 12300 | { |
| 12301 | /* If the constant flag is set or if not size is avalaible at |
| 12302 | * the end of the buffer, copy the string in other buffer |
| 12303 | * before decoding. |
| 12304 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12305 | 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] | 12306 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12307 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12308 | smp->data.u.str.str = str->str; |
| 12309 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12310 | smp->flags &= ~SMP_F_CONST; |
| 12311 | } |
| 12312 | |
| 12313 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12314 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12315 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12316 | return 1; |
| 12317 | } |
| 12318 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12319 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12320 | { |
| 12321 | struct proxy *fe = strm_fe(smp->strm); |
| 12322 | int idx, i; |
| 12323 | struct cap_hdr *hdr; |
| 12324 | int len; |
| 12325 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12326 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12327 | return 0; |
| 12328 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12329 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12330 | |
| 12331 | /* Check the availibity of the capture id. */ |
| 12332 | if (idx > fe->nb_req_cap - 1) |
| 12333 | return 0; |
| 12334 | |
| 12335 | /* Look for the original configuration. */ |
| 12336 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12337 | hdr != NULL && i != idx ; |
| 12338 | i--, hdr = hdr->next); |
| 12339 | if (!hdr) |
| 12340 | return 0; |
| 12341 | |
| 12342 | /* check for the memory allocation */ |
| 12343 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12344 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12345 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12346 | return 0; |
| 12347 | |
| 12348 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12349 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12350 | if (len > hdr->len) |
| 12351 | len = hdr->len; |
| 12352 | |
| 12353 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12354 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12355 | smp->strm->req_cap[idx][len] = '\0'; |
| 12356 | |
| 12357 | return 1; |
| 12358 | } |
| 12359 | |
| 12360 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12361 | { |
| 12362 | struct proxy *fe = strm_fe(smp->strm); |
| 12363 | int idx, i; |
| 12364 | struct cap_hdr *hdr; |
| 12365 | int len; |
| 12366 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12367 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12368 | return 0; |
| 12369 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12370 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12371 | |
| 12372 | /* Check the availibity of the capture id. */ |
| 12373 | if (idx > fe->nb_rsp_cap - 1) |
| 12374 | return 0; |
| 12375 | |
| 12376 | /* Look for the original configuration. */ |
| 12377 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12378 | hdr != NULL && i != idx ; |
| 12379 | i--, hdr = hdr->next); |
| 12380 | if (!hdr) |
| 12381 | return 0; |
| 12382 | |
| 12383 | /* check for the memory allocation */ |
| 12384 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12385 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12386 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12387 | return 0; |
| 12388 | |
| 12389 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12390 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12391 | if (len > hdr->len) |
| 12392 | len = hdr->len; |
| 12393 | |
| 12394 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12395 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12396 | smp->strm->res_cap[idx][len] = '\0'; |
| 12397 | |
| 12398 | return 1; |
| 12399 | } |
| 12400 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12401 | /* 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] | 12402 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12403 | * the relevant part of the request line accordingly. Then it updates various |
| 12404 | * pointers to the next elements which were moved, and the total buffer length. |
| 12405 | * 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] | 12406 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12407 | * error, though this can be revisited when this code is finally exploited. |
| 12408 | * |
| 12409 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12410 | * query string and 3 to replace uri. |
| 12411 | * |
| 12412 | * In query string case, the mark question '?' must be set at the start of the |
| 12413 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12414 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12415 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12416 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12417 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12418 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12419 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12420 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12421 | int delta; |
| 12422 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12423 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12424 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12425 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12426 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12427 | |
| 12428 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12429 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12430 | txn->req.sl.rq.m_l += delta; |
| 12431 | txn->req.sl.rq.u += delta; |
| 12432 | txn->req.sl.rq.v += delta; |
| 12433 | break; |
| 12434 | |
| 12435 | case 1: // path |
| 12436 | cur_ptr = http_get_path(txn); |
| 12437 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12438 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12439 | |
| 12440 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12441 | 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] | 12442 | cur_end++; |
| 12443 | |
| 12444 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12445 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12446 | txn->req.sl.rq.u_l += delta; |
| 12447 | txn->req.sl.rq.v += delta; |
| 12448 | break; |
| 12449 | |
| 12450 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12451 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12452 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12453 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12454 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12455 | cur_ptr++; |
| 12456 | |
| 12457 | /* skip the question mark or indicate that we must insert it |
| 12458 | * (but only if the format string is not empty then). |
| 12459 | */ |
| 12460 | if (cur_ptr < cur_end) |
| 12461 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12462 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12463 | offset = 0; |
| 12464 | |
| 12465 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12466 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12467 | txn->req.sl.rq.u_l += delta; |
| 12468 | txn->req.sl.rq.v += delta; |
| 12469 | break; |
| 12470 | |
| 12471 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12472 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12473 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12474 | |
| 12475 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12476 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12477 | txn->req.sl.rq.u_l += delta; |
| 12478 | txn->req.sl.rq.v += delta; |
| 12479 | break; |
| 12480 | |
| 12481 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12482 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12483 | } |
| 12484 | |
| 12485 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12486 | 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] | 12487 | txn->req.sl.rq.l += delta; |
| 12488 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12489 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12490 | return 0; |
| 12491 | } |
| 12492 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12493 | /* This function replace the HTTP status code and the associated message. The |
| 12494 | * variable <status> contains the new status code. This function never fails. |
| 12495 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12496 | 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] | 12497 | { |
| 12498 | struct http_txn *txn = s->txn; |
| 12499 | char *cur_ptr, *cur_end; |
| 12500 | int delta; |
| 12501 | char *res; |
| 12502 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12503 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12504 | int msg_len; |
| 12505 | |
| 12506 | chunk_reset(&trash); |
| 12507 | |
| 12508 | res = ultoa_o(status, trash.str, trash.size); |
| 12509 | c_l = res - trash.str; |
| 12510 | |
| 12511 | trash.str[c_l] = ' '; |
| 12512 | trash.len = c_l + 1; |
| 12513 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12514 | /* Do we have a custom reason format string? */ |
| 12515 | if (msg == NULL) |
| 12516 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12517 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12518 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12519 | trash.len += msg_len; |
| 12520 | |
| 12521 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12522 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12523 | |
| 12524 | /* commit changes and adjust message */ |
| 12525 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12526 | |
| 12527 | /* adjust res line offsets and lengths */ |
| 12528 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12529 | txn->rsp.sl.st.c_l = c_l; |
| 12530 | txn->rsp.sl.st.r_l = msg_len; |
| 12531 | |
| 12532 | delta = trash.len - (cur_end - cur_ptr); |
| 12533 | txn->rsp.sl.st.l += delta; |
| 12534 | txn->hdr_idx.v[0].len += delta; |
| 12535 | http_msg_move_end(&txn->rsp, delta); |
| 12536 | } |
| 12537 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12538 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12539 | * 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] | 12540 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12541 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12542 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12543 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12544 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12545 | 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] | 12546 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12547 | { |
| 12548 | chunk_reset(&trash); |
| 12549 | |
| 12550 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12551 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12552 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12553 | 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] | 12554 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12555 | 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] | 12556 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12557 | } |
| 12558 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12559 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12560 | 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] | 12561 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12562 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12563 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12564 | return ACT_RET_CONT; |
| 12565 | } |
| 12566 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12567 | /* parse an http-request action among : |
| 12568 | * set-method |
| 12569 | * set-path |
| 12570 | * set-query |
| 12571 | * set-uri |
| 12572 | * |
| 12573 | * All of them accept a single argument of type string representing a log-format. |
| 12574 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12575 | * 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] | 12576 | * 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] | 12577 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12578 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12579 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12580 | { |
| 12581 | int cur_arg = *orig_arg; |
| 12582 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12583 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12584 | |
| 12585 | switch (args[0][4]) { |
| 12586 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12587 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12588 | rule->action_ptr = http_action_set_req_line; |
| 12589 | break; |
| 12590 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12591 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12592 | rule->action_ptr = http_action_set_req_line; |
| 12593 | break; |
| 12594 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12595 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12596 | rule->action_ptr = http_action_set_req_line; |
| 12597 | break; |
| 12598 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12599 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12600 | rule->action_ptr = http_action_set_req_line; |
| 12601 | break; |
| 12602 | default: |
| 12603 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12604 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12605 | } |
| 12606 | |
| 12607 | if (!*args[cur_arg] || |
| 12608 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12609 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12610 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12611 | } |
| 12612 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12613 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12614 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 12615 | 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] | 12616 | (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] | 12617 | return ACT_RET_PRS_ERR; |
| 12618 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12619 | |
| 12620 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12621 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12622 | } |
| 12623 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12624 | /* parse set-status action: |
| 12625 | * This action accepts a single argument of type int representing |
| 12626 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12627 | * ACT_RET_PRS_ERR on error. |
| 12628 | */ |
| 12629 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12630 | struct act_rule *rule, char **err) |
| 12631 | { |
| 12632 | char *error; |
| 12633 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12634 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12635 | rule->action_ptr = action_http_set_status; |
| 12636 | |
| 12637 | /* Check if an argument is available */ |
| 12638 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12639 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12640 | return ACT_RET_PRS_ERR; |
| 12641 | } |
| 12642 | |
| 12643 | /* convert status code as integer */ |
| 12644 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12645 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12646 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12647 | return ACT_RET_PRS_ERR; |
| 12648 | } |
| 12649 | |
| 12650 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12651 | |
| 12652 | /* set custom reason string */ |
| 12653 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 12654 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 12655 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 12656 | (*orig_arg)++; |
| 12657 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 12658 | (*orig_arg)++; |
| 12659 | } |
| 12660 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12661 | return ACT_RET_PRS_OK; |
| 12662 | } |
| 12663 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12664 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12665 | * turns the result into a string and puts it in a capture slot. It always |
| 12666 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12667 | * processing continues. |
| 12668 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12669 | 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] | 12670 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12671 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12672 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12673 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12674 | char **cap = s->req_cap; |
| 12675 | int len; |
| 12676 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12677 | 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] | 12678 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12679 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12680 | |
| 12681 | if (cap[h->index] == NULL) |
| 12682 | cap[h->index] = pool_alloc2(h->pool); |
| 12683 | |
| 12684 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12685 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12686 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12687 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12688 | if (len > h->len) |
| 12689 | len = h->len; |
| 12690 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12691 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12692 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12693 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12694 | } |
| 12695 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12696 | /* This function executes the "capture" action and store the result in a |
| 12697 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12698 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12699 | * error occurs the action is cancelled, but the rule processing continues. |
| 12700 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12701 | 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] | 12702 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12703 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12704 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12705 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12706 | char **cap = s->req_cap; |
| 12707 | struct proxy *fe = strm_fe(s); |
| 12708 | int len; |
| 12709 | int i; |
| 12710 | |
| 12711 | /* Look for the original configuration. */ |
| 12712 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12713 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12714 | i--, h = h->next); |
| 12715 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12716 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12717 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12718 | 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] | 12719 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12720 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12721 | |
| 12722 | if (cap[h->index] == NULL) |
| 12723 | cap[h->index] = pool_alloc2(h->pool); |
| 12724 | |
| 12725 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12726 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12727 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12728 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12729 | if (len > h->len) |
| 12730 | len = h->len; |
| 12731 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12732 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12733 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12734 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12735 | } |
| 12736 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12737 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12738 | * 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] | 12739 | * 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] | 12740 | * 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] | 12741 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12742 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12743 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12744 | { |
| 12745 | struct sample_expr *expr; |
| 12746 | struct cap_hdr *hdr; |
| 12747 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12748 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12749 | |
| 12750 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12751 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12752 | strcmp(args[cur_arg], "unless") == 0) |
| 12753 | break; |
| 12754 | |
| 12755 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12756 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12757 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12758 | } |
| 12759 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12760 | cur_arg = *orig_arg; |
| 12761 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12762 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12763 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12764 | |
| 12765 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12766 | memprintf(err, |
| 12767 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12768 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12769 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12770 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12771 | } |
| 12772 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12773 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12774 | memprintf(err, "expects 'len or 'id'"); |
| 12775 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12776 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12777 | } |
| 12778 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12779 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12780 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12781 | |
| 12782 | if (!(px->cap & PR_CAP_FE)) { |
| 12783 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12784 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12785 | } |
| 12786 | |
| 12787 | proxy->conf.args.ctx = ARGC_CAP; |
| 12788 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12789 | if (!args[cur_arg]) { |
| 12790 | memprintf(err, "missing length value"); |
| 12791 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12792 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12793 | } |
| 12794 | /* we copy the table name for now, it will be resolved later */ |
| 12795 | len = atoi(args[cur_arg]); |
| 12796 | if (len <= 0) { |
| 12797 | memprintf(err, "length must be > 0"); |
| 12798 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12799 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12800 | } |
| 12801 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12802 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12803 | if (!len) { |
| 12804 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12805 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12806 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12807 | } |
| 12808 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12809 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12810 | hdr->next = px->req_cap; |
| 12811 | hdr->name = NULL; /* not a header capture */ |
| 12812 | hdr->namelen = 0; |
| 12813 | hdr->len = len; |
| 12814 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12815 | hdr->index = px->nb_req_cap++; |
| 12816 | |
| 12817 | px->req_cap = hdr; |
| 12818 | px->to_log |= LW_REQHDR; |
| 12819 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12820 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12821 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12822 | rule->arg.cap.expr = expr; |
| 12823 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12824 | } |
| 12825 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12826 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12827 | int id; |
| 12828 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12829 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12830 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12831 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12832 | if (!args[cur_arg]) { |
| 12833 | memprintf(err, "missing id value"); |
| 12834 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12835 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12836 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12837 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12838 | id = strtol(args[cur_arg], &error, 10); |
| 12839 | if (*error != '\0') { |
| 12840 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12841 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12842 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12843 | } |
| 12844 | cur_arg++; |
| 12845 | |
| 12846 | proxy->conf.args.ctx = ARGC_CAP; |
| 12847 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12848 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12849 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12850 | rule->arg.capid.expr = expr; |
| 12851 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12852 | } |
| 12853 | |
| 12854 | else { |
| 12855 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12856 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12857 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12858 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12859 | |
| 12860 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12861 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12862 | } |
| 12863 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12864 | /* This function executes the "capture" action and store the result in a |
| 12865 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12866 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12867 | * error occurs the action is cancelled, but the rule processing continues. |
| 12868 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12869 | 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] | 12870 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12871 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12872 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12873 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12874 | char **cap = s->res_cap; |
| 12875 | struct proxy *fe = strm_fe(s); |
| 12876 | int len; |
| 12877 | int i; |
| 12878 | |
| 12879 | /* Look for the original configuration. */ |
| 12880 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12881 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12882 | i--, h = h->next); |
| 12883 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12884 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12885 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12886 | 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] | 12887 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12888 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12889 | |
| 12890 | if (cap[h->index] == NULL) |
| 12891 | cap[h->index] = pool_alloc2(h->pool); |
| 12892 | |
| 12893 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12894 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12895 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12896 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12897 | if (len > h->len) |
| 12898 | len = h->len; |
| 12899 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12900 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12901 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12902 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12903 | } |
| 12904 | |
| 12905 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12906 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12907 | * 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] | 12908 | * 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] | 12909 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12910 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12911 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12912 | { |
| 12913 | struct sample_expr *expr; |
| 12914 | int cur_arg; |
| 12915 | int id; |
| 12916 | char *error; |
| 12917 | |
| 12918 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12919 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12920 | strcmp(args[cur_arg], "unless") == 0) |
| 12921 | break; |
| 12922 | |
| 12923 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12924 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12925 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12926 | } |
| 12927 | |
| 12928 | cur_arg = *orig_arg; |
| 12929 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12930 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12931 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12932 | |
| 12933 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12934 | memprintf(err, |
| 12935 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12936 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12937 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12938 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12939 | } |
| 12940 | |
| 12941 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12942 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12943 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12944 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12945 | } |
| 12946 | |
| 12947 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12948 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12949 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12950 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12951 | } |
| 12952 | |
| 12953 | cur_arg++; |
| 12954 | |
| 12955 | if (!args[cur_arg]) { |
| 12956 | memprintf(err, "missing id value"); |
| 12957 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12958 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12959 | } |
| 12960 | |
| 12961 | id = strtol(args[cur_arg], &error, 10); |
| 12962 | if (*error != '\0') { |
| 12963 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12964 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12965 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12966 | } |
| 12967 | cur_arg++; |
| 12968 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12969 | proxy->conf.args.ctx = ARGC_CAP; |
| 12970 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12971 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12972 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12973 | rule->arg.capid.expr = expr; |
| 12974 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12975 | |
| 12976 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12977 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12978 | } |
| 12979 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12980 | /* |
| 12981 | * Return the struct http_req_action_kw associated to a keyword. |
| 12982 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12983 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12984 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12985 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12986 | } |
| 12987 | |
| 12988 | /* |
| 12989 | * Return the struct http_res_action_kw associated to a keyword. |
| 12990 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12991 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12992 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12993 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12994 | } |
| 12995 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12996 | |
| 12997 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12998 | * now. |
| 12999 | */ |
| 13000 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 13001 | { |
| 13002 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 13003 | return 1; |
| 13004 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 13005 | if (*args[2]) { |
| 13006 | struct proxy *px; |
| 13007 | |
| 13008 | px = proxy_find_by_name(args[2], 0, 0); |
| 13009 | if (px) |
| 13010 | appctx->ctx.errors.iid = px->uuid; |
| 13011 | else |
| 13012 | appctx->ctx.errors.iid = atoi(args[2]); |
| 13013 | |
| 13014 | if (!appctx->ctx.errors.iid) { |
| 13015 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 13016 | appctx->st0 = CLI_ST_PRINT; |
| 13017 | return 1; |
| 13018 | } |
| 13019 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13020 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 13021 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 13022 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13023 | appctx->ctx.errors.flag = 0; |
| 13024 | if (strcmp(args[3], "request") == 0) |
| 13025 | appctx->ctx.errors.flag |= 4; // ignore response |
| 13026 | else if (strcmp(args[3], "response") == 0) |
| 13027 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13028 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13029 | return 0; |
| 13030 | } |
| 13031 | |
| 13032 | /* This function dumps all captured errors onto the stream interface's |
| 13033 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 13034 | * to be called again, otherwise non-zero. |
| 13035 | */ |
| 13036 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 13037 | { |
| 13038 | struct stream_interface *si = appctx->owner; |
| 13039 | extern const char *monthname[12]; |
| 13040 | |
| 13041 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 13042 | return 1; |
| 13043 | |
| 13044 | chunk_reset(&trash); |
| 13045 | |
| 13046 | if (!appctx->ctx.errors.px) { |
| 13047 | /* the function had not been called yet, let's prepare the |
| 13048 | * buffer for a response. |
| 13049 | */ |
| 13050 | struct tm tm; |
| 13051 | |
| 13052 | get_localtime(date.tv_sec, &tm); |
| 13053 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 13054 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13055 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 13056 | error_snapshot_id); |
| 13057 | |
| 13058 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13059 | /* Socket buffer full. Let's try again later from the same point */ |
| 13060 | si_applet_cant_put(si); |
| 13061 | return 0; |
| 13062 | } |
| 13063 | |
| 13064 | appctx->ctx.errors.px = proxy; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13065 | appctx->ctx.errors.bol = 0; |
| 13066 | appctx->ctx.errors.ptr = -1; |
| 13067 | } |
| 13068 | |
| 13069 | /* we have two inner loops here, one for the proxy, the other one for |
| 13070 | * the buffer. |
| 13071 | */ |
| 13072 | while (appctx->ctx.errors.px) { |
| 13073 | struct error_snapshot *es; |
| 13074 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13075 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13076 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13077 | if (appctx->ctx.errors.flag & 2) // skip req |
| 13078 | goto next; |
| 13079 | } |
| 13080 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13081 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13082 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 13083 | goto next; |
| 13084 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13085 | |
| 13086 | if (!es->when.tv_sec) |
| 13087 | goto next; |
| 13088 | |
| 13089 | if (appctx->ctx.errors.iid >= 0 && |
| 13090 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 13091 | es->oe->uuid != appctx->ctx.errors.iid) |
| 13092 | goto next; |
| 13093 | |
| 13094 | if (appctx->ctx.errors.ptr < 0) { |
| 13095 | /* just print headers now */ |
| 13096 | |
| 13097 | char pn[INET6_ADDRSTRLEN]; |
| 13098 | struct tm tm; |
| 13099 | int port; |
| 13100 | |
| 13101 | get_localtime(es->when.tv_sec, &tm); |
| 13102 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 13103 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 13104 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 13105 | |
| 13106 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 13107 | case AF_INET: |
| 13108 | case AF_INET6: |
| 13109 | port = get_host_port(&es->src); |
| 13110 | break; |
| 13111 | default: |
| 13112 | port = 0; |
| 13113 | } |
| 13114 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13115 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13116 | case 0: |
| 13117 | chunk_appendf(&trash, |
| 13118 | " frontend %s (#%d): invalid request\n" |
| 13119 | " backend %s (#%d)", |
| 13120 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13121 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 13122 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 13123 | break; |
| 13124 | case 1: |
| 13125 | chunk_appendf(&trash, |
| 13126 | " backend %s (#%d): invalid response\n" |
| 13127 | " frontend %s (#%d)", |
| 13128 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 13129 | es->oe->id, es->oe->uuid); |
| 13130 | break; |
| 13131 | } |
| 13132 | |
| 13133 | chunk_appendf(&trash, |
| 13134 | ", server %s (#%d), event #%u\n" |
| 13135 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 13136 | " 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] | 13137 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 13138 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 13139 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 13140 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 13141 | es->ev_id, |
| 13142 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 13143 | 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] | 13144 | es->m_clen, es->m_blen, |
| 13145 | es->b_flags, es->b_out, es->b_tot, |
| 13146 | es->len, es->b_wrap, es->pos); |
| 13147 | |
| 13148 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13149 | /* Socket buffer full. Let's try again later from the same point */ |
| 13150 | si_applet_cant_put(si); |
| 13151 | return 0; |
| 13152 | } |
| 13153 | appctx->ctx.errors.ptr = 0; |
| 13154 | appctx->ctx.errors.sid = es->sid; |
| 13155 | } |
| 13156 | |
| 13157 | if (appctx->ctx.errors.sid != es->sid) { |
| 13158 | /* the snapshot changed while we were dumping it */ |
| 13159 | chunk_appendf(&trash, |
| 13160 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
| 13161 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13162 | si_applet_cant_put(si); |
| 13163 | return 0; |
| 13164 | } |
| 13165 | goto next; |
| 13166 | } |
| 13167 | |
| 13168 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 13169 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 13170 | int newptr; |
| 13171 | int newline; |
| 13172 | |
| 13173 | newline = appctx->ctx.errors.bol; |
| 13174 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 13175 | if (newptr == appctx->ctx.errors.ptr) |
| 13176 | return 0; |
| 13177 | |
| 13178 | if (bi_putchk(si_ic(si), &trash) == -1) { |
| 13179 | /* Socket buffer full. Let's try again later from the same point */ |
| 13180 | si_applet_cant_put(si); |
| 13181 | return 0; |
| 13182 | } |
| 13183 | appctx->ctx.errors.ptr = newptr; |
| 13184 | appctx->ctx.errors.bol = newline; |
| 13185 | }; |
| 13186 | next: |
| 13187 | appctx->ctx.errors.bol = 0; |
| 13188 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 13189 | appctx->ctx.errors.flag ^= 1; |
| 13190 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13191 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13192 | } |
| 13193 | |
| 13194 | /* dump complete */ |
| 13195 | return 1; |
| 13196 | } |
| 13197 | |
| 13198 | /* register cli keywords */ |
| 13199 | static struct cli_kw_list cli_kws = {{ },{ |
| 13200 | { { "show", "errors", NULL }, |
| 13201 | "show errors : report last request and response errors for each proxy", |
| 13202 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 13203 | }, |
| 13204 | {{},} |
| 13205 | }}; |
| 13206 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13207 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13208 | /* All supported ACL keywords must be declared here. */ |
| 13209 | /************************************************************************/ |
| 13210 | |
| 13211 | /* Note: must not be declared <const> as its list will be overwritten. |
| 13212 | * Please take care of keeping this list alphabetically sorted. |
| 13213 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13214 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13215 | { "base", "base", PAT_MATCH_STR }, |
| 13216 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 13217 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 13218 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 13219 | { "base_end", "base", PAT_MATCH_END }, |
| 13220 | { "base_len", "base", PAT_MATCH_LEN }, |
| 13221 | { "base_reg", "base", PAT_MATCH_REG }, |
| 13222 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 13223 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13224 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 13225 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 13226 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 13227 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 13228 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 13229 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 13230 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 13231 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13232 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13233 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 13234 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 13235 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 13236 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 13237 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 13238 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 13239 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 13240 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13241 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13242 | /* these two declarations uses strings with list storage (in place |
| 13243 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 13244 | * and delete functions are relative to the list management. The parse |
| 13245 | * and match method are related to the corresponding fetch methods. This |
| 13246 | * is very particular ACL declaration mode. |
| 13247 | */ |
| 13248 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 13249 | { "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] | 13250 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13251 | { "path", "path", PAT_MATCH_STR }, |
| 13252 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 13253 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 13254 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 13255 | { "path_end", "path", PAT_MATCH_END }, |
| 13256 | { "path_len", "path", PAT_MATCH_LEN }, |
| 13257 | { "path_reg", "path", PAT_MATCH_REG }, |
| 13258 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13259 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13260 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 13261 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13262 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13263 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 13264 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 13265 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 13266 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 13267 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 13268 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 13269 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 13270 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13271 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13272 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 13273 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 13274 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 13275 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 13276 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 13277 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 13278 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 13279 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13280 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13281 | { "url", "url", PAT_MATCH_STR }, |
| 13282 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 13283 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 13284 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 13285 | { "url_end", "url", PAT_MATCH_END }, |
| 13286 | { "url_len", "url", PAT_MATCH_LEN }, |
| 13287 | { "url_reg", "url", PAT_MATCH_REG }, |
| 13288 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13289 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 13290 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 13291 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 13292 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 13293 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 13294 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 13295 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 13296 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 13297 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13298 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 13299 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 13300 | }}; |
| 13301 | |
| 13302 | /************************************************************************/ |
| 13303 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13304 | /************************************************************************/ |
| 13305 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 13306 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13307 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13308 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13309 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 13310 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 13311 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13312 | { "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] | 13313 | |
| 13314 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13315 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13316 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 13317 | { "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] | 13318 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 13319 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 13320 | { "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] | 13321 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13322 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 13323 | * are only here to match the ACL's name, are request-only and are used |
| 13324 | * for ACL compatibility only. |
| 13325 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13326 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13327 | { "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] | 13328 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13329 | { "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] | 13330 | |
| 13331 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 13332 | * only here to match the ACL's name, are request-only and are used for |
| 13333 | * ACL compatibility only. |
| 13334 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13335 | { "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] | 13336 | { "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] | 13337 | { "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] | 13338 | { "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] | 13339 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 13340 | { "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] | 13341 | { "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] | 13342 | { "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] | 13343 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13344 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 13345 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13346 | |
| 13347 | /* HTTP protocol on the request path */ |
| 13348 | { "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] | 13349 | { "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] | 13350 | |
| 13351 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13352 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13353 | { "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] | 13354 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 13355 | { "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] | 13356 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13357 | { "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] | 13358 | { "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] | 13359 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13360 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13361 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 13362 | { "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] | 13363 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13364 | /* 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] | 13365 | { "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] | 13366 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13367 | { "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] | 13368 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13369 | { "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] | 13370 | { "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] | 13371 | { "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] | 13372 | { "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] | 13373 | { "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] | 13374 | { "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] | 13375 | { "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] | 13376 | |
| 13377 | /* 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] | 13378 | { "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] | 13379 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13380 | { "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] | 13381 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13382 | { "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] | 13383 | { "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] | 13384 | { "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] | 13385 | { "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] | 13386 | { "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] | 13387 | { "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] | 13388 | { "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] | 13389 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13390 | /* 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] | 13391 | { "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] | 13392 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13393 | { "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] | 13394 | { "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] | 13395 | |
| 13396 | /* 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] | 13397 | { "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] | 13398 | { "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] | 13399 | { "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] | 13400 | { "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] | 13401 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13402 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 13403 | { "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] | 13404 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13405 | { "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] | 13406 | { "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] | 13407 | { "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] | 13408 | { "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] | 13409 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13410 | { "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] | 13411 | { "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] | 13412 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13413 | }}; |
| 13414 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13415 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13416 | /************************************************************************/ |
| 13417 | /* All supported converter keywords must be declared here. */ |
| 13418 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13419 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13420 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13421 | { "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] | 13422 | { "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] | 13423 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13424 | { "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] | 13425 | { "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] | 13426 | { NULL, NULL, 0, 0, 0 }, |
| 13427 | }}; |
| 13428 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13429 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13430 | /************************************************************************/ |
| 13431 | /* All supported http-request action keywords must be declared here. */ |
| 13432 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13433 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13434 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13435 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13436 | { "set-method", parse_set_req_line }, |
| 13437 | { "set-path", parse_set_req_line }, |
| 13438 | { "set-query", parse_set_req_line }, |
| 13439 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13440 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13441 | } |
| 13442 | }; |
| 13443 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13444 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13445 | .kw = { |
| 13446 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 13447 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13448 | { NULL, NULL } |
| 13449 | } |
| 13450 | }; |
| 13451 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13452 | __attribute__((constructor)) |
| 13453 | static void __http_protocol_init(void) |
| 13454 | { |
| 13455 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13456 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13457 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13458 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13459 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13460 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13461 | } |
| 13462 | |
| 13463 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13464 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13465 | * Local variables: |
| 13466 | * c-indent-level: 8 |
| 13467 | * c-basic-offset: 8 |
| 13468 | * End: |
| 13469 | */ |