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 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 45 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 46 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 47 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 50 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 51 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 52 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 54 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 56 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 58 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 62 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 63 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 64 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 65 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 67 | #include <proto/pattern.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 69 | const char HTTP_100[] = |
| 70 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 71 | |
| 72 | const struct chunk http_100_chunk = { |
| 73 | .str = (char *)&HTTP_100, |
| 74 | .len = sizeof(HTTP_100)-1 |
| 75 | }; |
| 76 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 77 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 80 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 81 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 85 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
| 89 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 90 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 95 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the same method */ |
| 98 | const char *HTTP_307 = |
| 99 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 100 | "Cache-Control: no-cache\r\n" |
| 101 | "Content-length: 0\r\n" |
| 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
| 104 | /* same as 301 except that the browser MUST retry with the same method */ |
| 105 | const char *HTTP_308 = |
| 106 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 107 | "Content-length: 0\r\n" |
| 108 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 109 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 111 | const char *HTTP_401_fmt = |
| 112 | "HTTP/1.0 401 Unauthorized\r\n" |
| 113 | "Cache-Control: no-cache\r\n" |
| 114 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 115 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 117 | "\r\n" |
| 118 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 119 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 120 | const char *HTTP_407_fmt = |
| 121 | "HTTP/1.0 407 Unauthorized\r\n" |
| 122 | "Cache-Control: no-cache\r\n" |
| 123 | "Connection: close\r\n" |
| 124 | "Content-Type: text/html\r\n" |
| 125 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 126 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 127 | "<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] | 128 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | |
| 130 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 131 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 132 | [HTTP_ERR_400] = 400, |
| 133 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 134 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 136 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 137 | [HTTP_ERR_500] = 500, |
| 138 | [HTTP_ERR_502] = 502, |
| 139 | [HTTP_ERR_503] = 503, |
| 140 | [HTTP_ERR_504] = 504, |
| 141 | }; |
| 142 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 143 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 144 | [HTTP_ERR_200] = |
| 145 | "HTTP/1.0 200 OK\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 151 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 152 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 153 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_403] = |
| 161 | "HTTP/1.0 403 Forbidden\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 167 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 168 | [HTTP_ERR_405] = |
| 169 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<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", |
| 175 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 176 | [HTTP_ERR_408] = |
| 177 | "HTTP/1.0 408 Request Time-out\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 183 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 184 | [HTTP_ERR_429] = |
| 185 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 191 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 192 | [HTTP_ERR_500] = |
| 193 | "HTTP/1.0 500 Server Error\r\n" |
| 194 | "Cache-Control: no-cache\r\n" |
| 195 | "Connection: close\r\n" |
| 196 | "Content-Type: text/html\r\n" |
| 197 | "\r\n" |
| 198 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 199 | |
| 200 | [HTTP_ERR_502] = |
| 201 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 202 | "Cache-Control: no-cache\r\n" |
| 203 | "Connection: close\r\n" |
| 204 | "Content-Type: text/html\r\n" |
| 205 | "\r\n" |
| 206 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 207 | |
| 208 | [HTTP_ERR_503] = |
| 209 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 210 | "Cache-Control: no-cache\r\n" |
| 211 | "Connection: close\r\n" |
| 212 | "Content-Type: text/html\r\n" |
| 213 | "\r\n" |
| 214 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 215 | |
| 216 | [HTTP_ERR_504] = |
| 217 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 218 | "Cache-Control: no-cache\r\n" |
| 219 | "Connection: close\r\n" |
| 220 | "Content-Type: text/html\r\n" |
| 221 | "\r\n" |
| 222 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 223 | |
| 224 | }; |
| 225 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 226 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 227 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 228 | [STAT_STATUS_DENY] = "DENY", |
| 229 | [STAT_STATUS_DONE] = "DONE", |
| 230 | [STAT_STATUS_ERRP] = "ERRP", |
| 231 | [STAT_STATUS_EXCD] = "EXCD", |
| 232 | [STAT_STATUS_NONE] = "NONE", |
| 233 | [STAT_STATUS_PART] = "PART", |
| 234 | [STAT_STATUS_UNKN] = "UNKN", |
| 235 | }; |
| 236 | |
| 237 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 238 | /* List head of all known action keywords for "http-request" */ |
| 239 | struct http_req_action_kw_list http_req_keywords = { |
| 240 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 241 | }; |
| 242 | |
| 243 | /* List head of all known action keywords for "http-response" */ |
| 244 | struct http_res_action_kw_list http_res_keywords = { |
| 245 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 246 | }; |
| 247 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 248 | /* We must put the messages here since GCC cannot initialize consts depending |
| 249 | * on strlen(). |
| 250 | */ |
| 251 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 252 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 253 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 254 | static struct hdr_ctx static_hdr_ctx; |
| 255 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 256 | #define FD_SETS_ARE_BITFIELDS |
| 257 | #ifdef FD_SETS_ARE_BITFIELDS |
| 258 | /* |
| 259 | * This map is used with all the FD_* macros to check whether a particular bit |
| 260 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 261 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 262 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 263 | * exclusively to the macros. |
| 264 | */ |
| 265 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 266 | 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] | 267 | 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] | 268 | |
| 269 | #else |
| 270 | #error "Check if your OS uses bitfields for fd_sets" |
| 271 | #endif |
| 272 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 273 | 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] | 274 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 275 | void init_proto_http() |
| 276 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 277 | int i; |
| 278 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 279 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 280 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 281 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 282 | if (!http_err_msgs[msg]) { |
| 283 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 284 | abort(); |
| 285 | } |
| 286 | |
| 287 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 288 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 289 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 290 | |
| 291 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 292 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 293 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 294 | * printable characters above. |
| 295 | */ |
| 296 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 297 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 298 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 299 | for (i = 0; i < 32; i++) { |
| 300 | FD_SET(i, hdr_encode_map); |
| 301 | FD_SET(i, url_encode_map); |
| 302 | } |
| 303 | for (i = 127; i < 256; i++) { |
| 304 | FD_SET(i, hdr_encode_map); |
| 305 | FD_SET(i, url_encode_map); |
| 306 | } |
| 307 | |
| 308 | tmp = "\"#{|}"; |
| 309 | while (*tmp) { |
| 310 | FD_SET(*tmp, hdr_encode_map); |
| 311 | tmp++; |
| 312 | } |
| 313 | |
| 314 | tmp = "\"#"; |
| 315 | while (*tmp) { |
| 316 | FD_SET(*tmp, url_encode_map); |
| 317 | tmp++; |
| 318 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 319 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 320 | /* initialize the http header encoding map. The draft httpbis define the |
| 321 | * header content as: |
| 322 | * |
| 323 | * HTTP-message = start-line |
| 324 | * *( header-field CRLF ) |
| 325 | * CRLF |
| 326 | * [ message-body ] |
| 327 | * header-field = field-name ":" OWS field-value OWS |
| 328 | * field-value = *( field-content / obs-fold ) |
| 329 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 330 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 331 | * field-vchar = VCHAR / obs-text |
| 332 | * VCHAR = %x21-7E |
| 333 | * obs-text = %x80-FF |
| 334 | * |
| 335 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 336 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 337 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 338 | */ |
| 339 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 340 | for (i = 0x00; i <= 0x08; i++) |
| 341 | FD_SET(i, http_encode_map); |
| 342 | for (i = 0x0a; i <= 0x1f; i++) |
| 343 | FD_SET(i, http_encode_map); |
| 344 | FD_SET(0x7f, http_encode_map); |
| 345 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 346 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 347 | 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] | 348 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 349 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 350 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 351 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 352 | /* |
| 353 | * We have 26 list of methods (1 per first letter), each of which can have |
| 354 | * up to 3 entries (2 valid, 1 null). |
| 355 | */ |
| 356 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 357 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 358 | int len; |
| 359 | const char text[8]; |
| 360 | }; |
| 361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 362 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 363 | ['C' - 'A'] = { |
| 364 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 365 | }, |
| 366 | ['D' - 'A'] = { |
| 367 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 368 | }, |
| 369 | ['G' - 'A'] = { |
| 370 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 371 | }, |
| 372 | ['H' - 'A'] = { |
| 373 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 374 | }, |
| 375 | ['P' - 'A'] = { |
| 376 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 377 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 378 | }, |
| 379 | ['T' - 'A'] = { |
| 380 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 381 | }, |
| 382 | /* rest is empty like this : |
| 383 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 384 | */ |
| 385 | }; |
| 386 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 387 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
| 388 | [HTTP_METH_NONE] = { "", 0 }, |
| 389 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 390 | [HTTP_METH_GET] = { "GET", 3 }, |
| 391 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 392 | [HTTP_METH_POST] = { "POST", 4 }, |
| 393 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 394 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 395 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 396 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 397 | }; |
| 398 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 399 | /* 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] | 400 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 401 | * RFC2616 for those chars. |
| 402 | */ |
| 403 | |
| 404 | const char http_is_spht[256] = { |
| 405 | [' '] = 1, ['\t'] = 1, |
| 406 | }; |
| 407 | |
| 408 | const char http_is_crlf[256] = { |
| 409 | ['\r'] = 1, ['\n'] = 1, |
| 410 | }; |
| 411 | |
| 412 | const char http_is_lws[256] = { |
| 413 | [' '] = 1, ['\t'] = 1, |
| 414 | ['\r'] = 1, ['\n'] = 1, |
| 415 | }; |
| 416 | |
| 417 | const char http_is_sep[256] = { |
| 418 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 419 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 420 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 421 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 422 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 423 | }; |
| 424 | |
| 425 | const char http_is_ctl[256] = { |
| 426 | [0 ... 31] = 1, |
| 427 | [127] = 1, |
| 428 | }; |
| 429 | |
| 430 | /* |
| 431 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 432 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 433 | * them correctly. Instead, define every non-CTL char's status. |
| 434 | */ |
| 435 | const char http_is_token[256] = { |
| 436 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 437 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 438 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 439 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 440 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 441 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 442 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 443 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 444 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 445 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 446 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 447 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 448 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 449 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 450 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 451 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 452 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 453 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 454 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 455 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 456 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 457 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 458 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 459 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 460 | }; |
| 461 | |
| 462 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 463 | /* |
| 464 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 465 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 466 | */ |
| 467 | const char http_is_ver_token[256] = { |
| 468 | ['.'] = 1, ['/'] = 1, |
| 469 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 470 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 471 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 475 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 476 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 477 | * 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] | 478 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 479 | * of headers is automatically adjusted. The number of bytes added is returned |
| 480 | * on success, otherwise <0 is returned indicating an error. |
| 481 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 482 | 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] | 483 | { |
| 484 | int bytes, len; |
| 485 | |
| 486 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 487 | 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] | 488 | if (!bytes) |
| 489 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 490 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 491 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 492 | } |
| 493 | |
| 494 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 495 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 496 | * 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] | 497 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 498 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 499 | * of headers is automatically adjusted. The number of bytes added is returned |
| 500 | * on success, otherwise <0 is returned indicating an error. |
| 501 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 502 | int http_header_add_tail2(struct http_msg *msg, |
| 503 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 504 | { |
| 505 | int bytes; |
| 506 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 507 | 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] | 508 | if (!bytes) |
| 509 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 510 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 511 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 512 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 513 | |
| 514 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 515 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 516 | * If so, returns the position of the first non-space character relative to |
| 517 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 518 | * to return a pointer to the place after the first space. Returns 0 if the |
| 519 | * header name does not match. Checks are case-insensitive. |
| 520 | */ |
| 521 | int http_header_match2(const char *hdr, const char *end, |
| 522 | const char *name, int len) |
| 523 | { |
| 524 | const char *val; |
| 525 | |
| 526 | if (hdr + len >= end) |
| 527 | return 0; |
| 528 | if (hdr[len] != ':') |
| 529 | return 0; |
| 530 | if (strncasecmp(hdr, name, len) != 0) |
| 531 | return 0; |
| 532 | val = hdr + len + 1; |
| 533 | while (val < end && HTTP_IS_SPHT(*val)) |
| 534 | val++; |
| 535 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 536 | return len + 2; /* we may replace starting from second space */ |
| 537 | return val - hdr; |
| 538 | } |
| 539 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 540 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 541 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 542 | * structure holds everything necessary to use the header and find next |
| 543 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 544 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 545 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 546 | * http_find_header2() except that it is designed to work with full-line headers |
| 547 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 548 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 549 | * header is rescanned. This allows rescanning after a header deletion. |
| 550 | */ |
| 551 | int http_find_full_header2(const char *name, int len, |
| 552 | char *sol, struct hdr_idx *idx, |
| 553 | struct hdr_ctx *ctx) |
| 554 | { |
| 555 | char *eol, *sov; |
| 556 | int cur_idx, old_idx; |
| 557 | |
| 558 | cur_idx = ctx->idx; |
| 559 | if (cur_idx) { |
| 560 | /* We have previously returned a header, let's search another one */ |
| 561 | sol = ctx->line; |
| 562 | eol = sol + idx->v[cur_idx].len; |
| 563 | goto next_hdr; |
| 564 | } |
| 565 | |
| 566 | /* first request for this header */ |
| 567 | sol += hdr_idx_first_pos(idx); |
| 568 | old_idx = 0; |
| 569 | cur_idx = hdr_idx_first_idx(idx); |
| 570 | while (cur_idx) { |
| 571 | eol = sol + idx->v[cur_idx].len; |
| 572 | |
| 573 | if (len == 0) { |
| 574 | /* No argument was passed, we want any header. |
| 575 | * To achieve this, we simply build a fake request. */ |
| 576 | while (sol + len < eol && sol[len] != ':') |
| 577 | len++; |
| 578 | name = sol; |
| 579 | } |
| 580 | |
| 581 | if ((len < eol - sol) && |
| 582 | (sol[len] == ':') && |
| 583 | (strncasecmp(sol, name, len) == 0)) { |
| 584 | ctx->del = len; |
| 585 | sov = sol + len + 1; |
| 586 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 587 | sov++; |
| 588 | |
| 589 | ctx->line = sol; |
| 590 | ctx->prev = old_idx; |
| 591 | ctx->idx = cur_idx; |
| 592 | ctx->val = sov - sol; |
| 593 | ctx->tws = 0; |
| 594 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 595 | eol--; |
| 596 | ctx->tws++; |
| 597 | } |
| 598 | ctx->vlen = eol - sov; |
| 599 | return 1; |
| 600 | } |
| 601 | next_hdr: |
| 602 | sol = eol + idx->v[cur_idx].cr + 1; |
| 603 | old_idx = cur_idx; |
| 604 | cur_idx = idx->v[cur_idx].next; |
| 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 609 | /* Find the first or next header field in message buffer <sol> using headers |
| 610 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 611 | * everything necessary to use the header and find next occurrence. If its |
| 612 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 613 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 614 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 615 | * no header name. |
| 616 | */ |
| 617 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 618 | { |
| 619 | char *eol, *sov; |
| 620 | int cur_idx, old_idx; |
| 621 | int len; |
| 622 | |
| 623 | cur_idx = ctx->idx; |
| 624 | if (cur_idx) { |
| 625 | /* We have previously returned a header, let's search another one */ |
| 626 | sol = ctx->line; |
| 627 | eol = sol + idx->v[cur_idx].len; |
| 628 | goto next_hdr; |
| 629 | } |
| 630 | |
| 631 | /* first request for this header */ |
| 632 | sol += hdr_idx_first_pos(idx); |
| 633 | old_idx = 0; |
| 634 | cur_idx = hdr_idx_first_idx(idx); |
| 635 | while (cur_idx) { |
| 636 | eol = sol + idx->v[cur_idx].len; |
| 637 | |
| 638 | len = 0; |
| 639 | while (1) { |
| 640 | if (len >= eol - sol) |
| 641 | goto next_hdr; |
| 642 | if (sol[len] == ':') |
| 643 | break; |
| 644 | len++; |
| 645 | } |
| 646 | |
| 647 | ctx->del = len; |
| 648 | sov = sol + len + 1; |
| 649 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 650 | sov++; |
| 651 | |
| 652 | ctx->line = sol; |
| 653 | ctx->prev = old_idx; |
| 654 | ctx->idx = cur_idx; |
| 655 | ctx->val = sov - sol; |
| 656 | ctx->tws = 0; |
| 657 | |
| 658 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 659 | eol--; |
| 660 | ctx->tws++; |
| 661 | } |
| 662 | ctx->vlen = eol - sov; |
| 663 | return 1; |
| 664 | |
| 665 | next_hdr: |
| 666 | sol = eol + idx->v[cur_idx].cr + 1; |
| 667 | old_idx = cur_idx; |
| 668 | cur_idx = idx->v[cur_idx].next; |
| 669 | } |
| 670 | return 0; |
| 671 | } |
| 672 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 673 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 674 | * par 2.2 for more information. Note that it requires a valid header to return |
| 675 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 676 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 677 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 678 | { |
| 679 | int quoted, qdpair; |
| 680 | |
| 681 | quoted = qdpair = 0; |
| 682 | for (; s < e; s++) { |
| 683 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 684 | else if (quoted) { |
| 685 | if (*s == '\\') qdpair = 1; |
| 686 | else if (*s == '"') quoted = 0; |
| 687 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 688 | else if (*s == '"') quoted = 1; |
| 689 | else if (*s == ',') return s; |
| 690 | } |
| 691 | return s; |
| 692 | } |
| 693 | |
| 694 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 695 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 696 | * structure holds everything necessary to use the header and find next |
| 697 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 698 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 699 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 700 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 701 | * is NULL when searching for a new values of a header, the current header is |
| 702 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 703 | */ |
| 704 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 705 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 706 | struct hdr_ctx *ctx) |
| 707 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 708 | char *eol, *sov; |
| 709 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 710 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 711 | cur_idx = ctx->idx; |
| 712 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 713 | /* We have previously returned a value, let's search |
| 714 | * another one on the same line. |
| 715 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 716 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 717 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 718 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 719 | eol = sol + idx->v[cur_idx].len; |
| 720 | |
| 721 | if (sov >= eol) |
| 722 | /* no more values in this header */ |
| 723 | goto next_hdr; |
| 724 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 725 | /* values remaining for this header, skip the comma but save it |
| 726 | * for later use (eg: for header deletion). |
| 727 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 728 | sov++; |
| 729 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 730 | sov++; |
| 731 | |
| 732 | goto return_hdr; |
| 733 | } |
| 734 | |
| 735 | /* first request for this header */ |
| 736 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 737 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 738 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 739 | while (cur_idx) { |
| 740 | eol = sol + idx->v[cur_idx].len; |
| 741 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 742 | if (len == 0) { |
| 743 | /* No argument was passed, we want any header. |
| 744 | * To achieve this, we simply build a fake request. */ |
| 745 | while (sol + len < eol && sol[len] != ':') |
| 746 | len++; |
| 747 | name = sol; |
| 748 | } |
| 749 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 750 | if ((len < eol - sol) && |
| 751 | (sol[len] == ':') && |
| 752 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 753 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 754 | sov = sol + len + 1; |
| 755 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 756 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 757 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 758 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 759 | ctx->prev = old_idx; |
| 760 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 761 | ctx->idx = cur_idx; |
| 762 | ctx->val = sov - sol; |
| 763 | |
| 764 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 765 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 766 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 767 | eol--; |
| 768 | ctx->tws++; |
| 769 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 770 | ctx->vlen = eol - sov; |
| 771 | return 1; |
| 772 | } |
| 773 | next_hdr: |
| 774 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 775 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 776 | cur_idx = idx->v[cur_idx].next; |
| 777 | } |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 782 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 783 | struct hdr_ctx *ctx) |
| 784 | { |
| 785 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 786 | } |
| 787 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 788 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 789 | * the http_find_header functions. The value is removed, as well as surrounding |
| 790 | * 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] | 791 | * 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] | 792 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 793 | * no more header, so any processing may stop. The ctx is always left in a form |
| 794 | * that can be handled by http_find_header2() to find next occurrence. |
| 795 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 796 | 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] | 797 | { |
| 798 | int cur_idx = ctx->idx; |
| 799 | char *sol = ctx->line; |
| 800 | struct hdr_idx_elem *hdr; |
| 801 | int delta, skip_comma; |
| 802 | |
| 803 | if (!cur_idx) |
| 804 | return 0; |
| 805 | |
| 806 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 807 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 808 | /* 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] | 809 | 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] | 810 | http_msg_move_end(msg, delta); |
| 811 | idx->used--; |
| 812 | hdr->len = 0; /* unused entry */ |
| 813 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 814 | if (idx->tail == ctx->idx) |
| 815 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 816 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 817 | 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] | 818 | 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] | 819 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 820 | return ctx->idx; |
| 821 | } |
| 822 | |
| 823 | /* 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] | 824 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 825 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 826 | */ |
| 827 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 828 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 829 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 830 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 831 | NULL, 0); |
| 832 | hdr->len += delta; |
| 833 | http_msg_move_end(msg, delta); |
| 834 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 835 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 836 | return ctx->idx; |
| 837 | } |
| 838 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 839 | /* This function handles a server error at the stream interface level. The |
| 840 | * stream interface is assumed to be already in a closed state. An optional |
| 841 | * message is copied into the input buffer, and an HTTP status code stored. |
| 842 | * 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] | 843 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 844 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 845 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 847 | { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 848 | channel_auto_read(si_oc(si)); |
| 849 | channel_abort(si_oc(si)); |
| 850 | channel_auto_close(si_oc(si)); |
| 851 | channel_erase(si_oc(si)); |
| 852 | channel_auto_close(si_ic(si)); |
| 853 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 854 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 855 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 856 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 857 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 858 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 859 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 860 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 861 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 864 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 865 | * and message. |
| 866 | */ |
| 867 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 868 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 869 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 870 | if (s->be->errmsg[msgnum].str) |
| 871 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 872 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 873 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 874 | else |
| 875 | return &http_err_chunks[msgnum]; |
| 876 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 877 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 878 | /* |
| 879 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 880 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 881 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 882 | 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] | 883 | { |
| 884 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 885 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 886 | |
| 887 | m = ((unsigned)*str - 'A'); |
| 888 | |
| 889 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 890 | for (h = http_methods[m]; h->len > 0; h++) { |
| 891 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 892 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 893 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 894 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 895 | }; |
| 896 | return HTTP_METH_OTHER; |
| 897 | } |
| 898 | return HTTP_METH_NONE; |
| 899 | |
| 900 | } |
| 901 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 902 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 903 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 904 | * It is returned otherwise. |
| 905 | */ |
| 906 | static char * |
| 907 | http_get_path(struct http_txn *txn) |
| 908 | { |
| 909 | char *ptr, *end; |
| 910 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 911 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 912 | end = ptr + txn->req.sl.rq.u_l; |
| 913 | |
| 914 | if (ptr >= end) |
| 915 | return NULL; |
| 916 | |
| 917 | /* RFC2616, par. 5.1.2 : |
| 918 | * Request-URI = "*" | absuri | abspath | authority |
| 919 | */ |
| 920 | |
| 921 | if (*ptr == '*') |
| 922 | return NULL; |
| 923 | |
| 924 | if (isalpha((unsigned char)*ptr)) { |
| 925 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 926 | ptr++; |
| 927 | while (ptr < end && |
| 928 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 929 | ptr++; |
| 930 | /* skip '://' */ |
| 931 | if (ptr == end || *ptr++ != ':') |
| 932 | return NULL; |
| 933 | if (ptr == end || *ptr++ != '/') |
| 934 | return NULL; |
| 935 | if (ptr == end || *ptr++ != '/') |
| 936 | return NULL; |
| 937 | } |
| 938 | /* skip [user[:passwd]@]host[:[port]] */ |
| 939 | |
| 940 | while (ptr < end && *ptr != '/') |
| 941 | ptr++; |
| 942 | |
| 943 | if (ptr == end) |
| 944 | return NULL; |
| 945 | |
| 946 | /* OK, we got the '/' ! */ |
| 947 | return ptr; |
| 948 | } |
| 949 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 950 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 951 | * If not found, return NULL. It is returned otherwise. |
| 952 | */ |
| 953 | static char * |
| 954 | http_get_path_from_string(char *str) |
| 955 | { |
| 956 | char *ptr = str; |
| 957 | |
| 958 | /* RFC2616, par. 5.1.2 : |
| 959 | * Request-URI = "*" | absuri | abspath | authority |
| 960 | */ |
| 961 | |
| 962 | if (*ptr == '*') |
| 963 | return NULL; |
| 964 | |
| 965 | if (isalpha((unsigned char)*ptr)) { |
| 966 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 967 | ptr++; |
| 968 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 969 | ptr++; |
| 970 | /* skip '://' */ |
| 971 | if (*ptr == '\0' || *ptr++ != ':') |
| 972 | return NULL; |
| 973 | if (*ptr == '\0' || *ptr++ != '/') |
| 974 | return NULL; |
| 975 | if (*ptr == '\0' || *ptr++ != '/') |
| 976 | return NULL; |
| 977 | } |
| 978 | /* skip [user[:passwd]@]host[:[port]] */ |
| 979 | |
| 980 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 981 | ptr++; |
| 982 | |
| 983 | if (*ptr == '\0' || *ptr == ' ') |
| 984 | return NULL; |
| 985 | |
| 986 | /* OK, we got the '/' ! */ |
| 987 | return ptr; |
| 988 | } |
| 989 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 990 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 991 | * in redirect mode. This may only be called just after the stream interface |
| 992 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 993 | * follow normal proxy processing. NOTE: this function is designed to support |
| 994 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 995 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 996 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 997 | { |
| 998 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 999 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1000 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1001 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1002 | |
| 1003 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1004 | trash.len = strlen(HTTP_302); |
| 1005 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1006 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1007 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1008 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1009 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1010 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1011 | return; |
| 1012 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1013 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1014 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1015 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1016 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1017 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1018 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1019 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1020 | * to temporarily rewind the buffer. |
| 1021 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1022 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1023 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1024 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1025 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1026 | 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] | 1027 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1028 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1029 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1030 | if (!path) |
| 1031 | return; |
| 1032 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1033 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1034 | return; |
| 1035 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1036 | memcpy(trash.str + trash.len, path, len); |
| 1037 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1038 | |
| 1039 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1040 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1041 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1042 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1043 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1044 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1045 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1046 | |
| 1047 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1048 | si_shutr(si); |
| 1049 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1050 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1051 | si->state = SI_ST_CLO; |
| 1052 | |
| 1053 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1054 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1055 | |
| 1056 | /* 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] | 1057 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1058 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1061 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1062 | * that the server side is closed. Note that err_type is actually a |
| 1063 | * bitmask, where almost only aborts may be cumulated with other |
| 1064 | * values. We consider that aborted operations are more important |
| 1065 | * than timeouts or errors due to the fact that nobody else in the |
| 1066 | * logs might explain incomplete retries. All others should avoid |
| 1067 | * being cumulated. It should normally not be possible to have multiple |
| 1068 | * 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] | 1069 | * Note that connection errors appearing on the second request of a keep-alive |
| 1070 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1071 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1072 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1073 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1074 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1077 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1078 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1079 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1080 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1081 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1082 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1083 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1084 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1085 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1086 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1087 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1088 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1089 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1090 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1091 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1092 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1093 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1094 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1095 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1096 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1097 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1098 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1099 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1100 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1101 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1102 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1103 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1106 | extern const char sess_term_cond[8]; |
| 1107 | extern const char sess_fin_state[8]; |
| 1108 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1109 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1110 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1111 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1112 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1114 | /* |
| 1115 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1116 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1117 | */ |
| 1118 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1119 | char **cap, struct cap_hdr *cap_hdr) |
| 1120 | { |
| 1121 | char *eol, *sol, *col, *sov; |
| 1122 | int cur_idx; |
| 1123 | struct cap_hdr *h; |
| 1124 | int len; |
| 1125 | |
| 1126 | sol = som + hdr_idx_first_pos(idx); |
| 1127 | cur_idx = hdr_idx_first_idx(idx); |
| 1128 | |
| 1129 | while (cur_idx) { |
| 1130 | eol = sol + idx->v[cur_idx].len; |
| 1131 | |
| 1132 | col = sol; |
| 1133 | while (col < eol && *col != ':') |
| 1134 | col++; |
| 1135 | |
| 1136 | sov = col + 1; |
| 1137 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1138 | sov++; |
| 1139 | |
| 1140 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1141 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1142 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1143 | if (cap[h->index] == NULL) |
| 1144 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1145 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1146 | |
| 1147 | if (cap[h->index] == NULL) { |
| 1148 | Alert("HTTP capture : out of memory.\n"); |
| 1149 | continue; |
| 1150 | } |
| 1151 | |
| 1152 | len = eol - sov; |
| 1153 | if (len > h->len) |
| 1154 | len = h->len; |
| 1155 | |
| 1156 | memcpy(cap[h->index], sov, len); |
| 1157 | cap[h->index][len]=0; |
| 1158 | } |
| 1159 | } |
| 1160 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1161 | cur_idx = idx->v[cur_idx].next; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1166 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1167 | */ |
| 1168 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1169 | |
| 1170 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1171 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1172 | */ |
| 1173 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1174 | ptr++; \ |
| 1175 | if (likely(ptr < end)) \ |
| 1176 | goto good; \ |
| 1177 | else { \ |
| 1178 | state = (st); \ |
| 1179 | goto http_msg_ood; \ |
| 1180 | } \ |
| 1181 | } while (0) |
| 1182 | |
| 1183 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1184 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1185 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1186 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1187 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1188 | * will give undefined results. |
| 1189 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1190 | * and that msg->sol points to the beginning of the response. |
| 1191 | * If a complete line is found (which implies that at least one CR or LF is |
| 1192 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1193 | * returned indicating an incomplete line (which does not mean that parts have |
| 1194 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1195 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1196 | * upon next call. |
| 1197 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1198 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1199 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1200 | * 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] | 1201 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1202 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1203 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1204 | enum ht_state state, const char *ptr, const char *end, |
| 1205 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1207 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1208 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1210 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1211 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1212 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1213 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1214 | |
| 1215 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1216 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1217 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1218 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1219 | state = HTTP_MSG_ERROR; |
| 1220 | break; |
| 1221 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1222 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1223 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1224 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1225 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1226 | goto http_msg_rpcode; |
| 1227 | } |
| 1228 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1229 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1230 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1231 | state = HTTP_MSG_ERROR; |
| 1232 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1233 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1234 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1235 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1236 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1237 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1238 | |
| 1239 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1240 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1241 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1242 | } |
| 1243 | |
| 1244 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1245 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1246 | http_msg_rsp_reason: |
| 1247 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1248 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1249 | msg->sl.st.r_l = 0; |
| 1250 | goto http_msg_rpline_eol; |
| 1251 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1252 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1253 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1254 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1255 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1256 | goto http_msg_rpreason; |
| 1257 | } |
| 1258 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1259 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1260 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1261 | goto http_msg_rsp_reason; |
| 1262 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1263 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1264 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1265 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1266 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1267 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | http_msg_rpline_eol: |
| 1269 | /* We have seen the end of line. Note that we do not |
| 1270 | * necessarily have the \n yet, but at least we know that we |
| 1271 | * have EITHER \r OR \n, otherwise the response would not be |
| 1272 | * complete. We can then record the response length and return |
| 1273 | * to the caller which will be able to register it. |
| 1274 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1275 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1276 | return ptr; |
| 1277 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1278 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1279 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1280 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1281 | exit(1); |
| 1282 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1283 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1287 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | if (ret_state) |
| 1289 | *ret_state = state; |
| 1290 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1291 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | } |
| 1294 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1296 | * This function parses a request line between <ptr> and <end>, starting with |
| 1297 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1298 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1299 | * will give undefined results. |
| 1300 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1301 | * and that msg->sol points to the beginning of the request. |
| 1302 | * If a complete line is found (which implies that at least one CR or LF is |
| 1303 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1304 | * returned indicating an incomplete line (which does not mean that parts have |
| 1305 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1306 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1307 | * upon next call. |
| 1308 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1309 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1311 | * 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] | 1312 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1313 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1314 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1315 | enum ht_state state, const char *ptr, const char *end, |
| 1316 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1317 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1318 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1319 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1322 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1323 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1325 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1326 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1327 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1329 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1330 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1332 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1333 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1334 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1335 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1336 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1337 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1339 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | msg->sl.rq.v_l = 0; |
| 1341 | goto http_msg_rqline_eol; |
| 1342 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1343 | state = HTTP_MSG_ERROR; |
| 1344 | break; |
| 1345 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1346 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1347 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1349 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | goto http_msg_rquri; |
| 1351 | } |
| 1352 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1353 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1354 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1355 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1356 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1357 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1358 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1359 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1360 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1361 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1363 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1365 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1366 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1367 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1368 | /* non-ASCII chars are forbidden unless option |
| 1369 | * accept-invalid-http-request is enabled in the frontend. |
| 1370 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1371 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1372 | if (msg->err_pos < -1) |
| 1373 | goto invalid_char; |
| 1374 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1375 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1376 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1377 | } |
| 1378 | |
| 1379 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1380 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1381 | goto http_msg_req09_uri_e; |
| 1382 | } |
| 1383 | |
| 1384 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1385 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1386 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1387 | state = HTTP_MSG_ERROR; |
| 1388 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1391 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1393 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | goto http_msg_rqver; |
| 1395 | } |
| 1396 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1397 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1398 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1399 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1400 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1402 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1403 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1405 | |
| 1406 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1407 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1408 | http_msg_rqline_eol: |
| 1409 | /* We have seen the end of line. Note that we do not |
| 1410 | * necessarily have the \n yet, but at least we know that we |
| 1411 | * have EITHER \r OR \n, otherwise the request would not be |
| 1412 | * complete. We can then record the request length and return |
| 1413 | * to the caller which will be able to register it. |
| 1414 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1415 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1416 | return ptr; |
| 1417 | } |
| 1418 | |
| 1419 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1420 | state = HTTP_MSG_ERROR; |
| 1421 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1424 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1426 | exit(1); |
| 1427 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1428 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1432 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | if (ret_state) |
| 1434 | *ret_state = state; |
| 1435 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1436 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1440 | /* |
| 1441 | * Returns the data from Authorization header. Function may be called more |
| 1442 | * than once so data is stored in txn->auth_data. When no header is found |
| 1443 | * 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] | 1444 | * searching again for something we are unable to find anyway. However, if |
| 1445 | * 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] | 1446 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1447 | */ |
| 1448 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1449 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1450 | * set according to global.tune.bufsize. |
| 1451 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1452 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1453 | |
| 1454 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1455 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1456 | { |
| 1457 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1458 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1459 | struct chunk auth_method; |
| 1460 | struct hdr_ctx ctx; |
| 1461 | char *h, *p; |
| 1462 | int len; |
| 1463 | |
| 1464 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1465 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1466 | #endif |
| 1467 | |
| 1468 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1469 | return 0; |
| 1470 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1471 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1472 | |
| 1473 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1474 | |
| 1475 | if (txn->flags & TX_USE_PX_CONN) { |
| 1476 | h = "Proxy-Authorization"; |
| 1477 | len = strlen(h); |
| 1478 | } else { |
| 1479 | h = "Authorization"; |
| 1480 | len = strlen(h); |
| 1481 | } |
| 1482 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1483 | 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] | 1484 | return 0; |
| 1485 | |
| 1486 | h = ctx.line + ctx.val; |
| 1487 | |
| 1488 | p = memchr(h, ' ', ctx.vlen); |
| 1489 | if (!p || p == h) |
| 1490 | return 0; |
| 1491 | |
| 1492 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1493 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1494 | |
| 1495 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1496 | |
| 1497 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1498 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1499 | |
| 1500 | if (len < 0) |
| 1501 | return 0; |
| 1502 | |
| 1503 | |
| 1504 | get_http_auth_buff[len] = '\0'; |
| 1505 | |
| 1506 | p = strchr(get_http_auth_buff, ':'); |
| 1507 | |
| 1508 | if (!p) |
| 1509 | return 0; |
| 1510 | |
| 1511 | txn->auth.user = get_http_auth_buff; |
| 1512 | *p = '\0'; |
| 1513 | txn->auth.pass = p+1; |
| 1514 | |
| 1515 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1516 | return 1; |
| 1517 | } |
| 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1522 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1523 | /* |
| 1524 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1525 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1526 | * ensuring that the message does not wrap. The function can be preempted |
| 1527 | * everywhere when data are missing and recalled at the exact same location |
| 1528 | * with no information loss. The message may even be realigned between two |
| 1529 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1530 | * 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] | 1531 | * fields. Note that msg->sol will be initialized after completing the first |
| 1532 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1533 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1534 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1535 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1537 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1539 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1540 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1541 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1542 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1543 | ptr = buf->p + msg->next; |
| 1544 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (unlikely(ptr >= end)) |
| 1547 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1548 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1550 | /* |
| 1551 | * First, states that are specific to the response only. |
| 1552 | * We check them first so that request and headers are |
| 1553 | * closer to each other (accessed more often). |
| 1554 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1555 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1556 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1557 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1558 | /* we have a start of message, but we have to check |
| 1559 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1560 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1561 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1562 | if (unlikely(ptr != buf->p)) { |
| 1563 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1564 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1565 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1566 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1567 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1568 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1569 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1570 | hdr_idx_init(idx); |
| 1571 | state = HTTP_MSG_RPVER; |
| 1572 | goto http_msg_rpver; |
| 1573 | } |
| 1574 | |
| 1575 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1576 | goto http_msg_invalid; |
| 1577 | |
| 1578 | if (unlikely(*ptr == '\n')) |
| 1579 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1580 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1581 | /* stop here */ |
| 1582 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1583 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1584 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1585 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1586 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1587 | /* stop here */ |
| 1588 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1589 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1590 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1591 | case HTTP_MSG_RPVER_SP: |
| 1592 | case HTTP_MSG_RPCODE: |
| 1593 | case HTTP_MSG_RPCODE_SP: |
| 1594 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1595 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1596 | state, ptr, end, |
| 1597 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1598 | if (unlikely(!ptr)) |
| 1599 | return; |
| 1600 | |
| 1601 | /* we have a full response and we know that we have either a CR |
| 1602 | * or an LF at <ptr>. |
| 1603 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1604 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1605 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1606 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1607 | if (likely(*ptr == '\r')) |
| 1608 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1609 | goto http_msg_rpline_end; |
| 1610 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1611 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1612 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1613 | /* msg->sol must point to the first of CR or LF. */ |
| 1614 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1615 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1616 | /* stop here */ |
| 1617 | |
| 1618 | /* |
| 1619 | * Second, states that are specific to the request only |
| 1620 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1622 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1624 | /* we have a start of message, but we have to check |
| 1625 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1626 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1627 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1628 | if (likely(ptr != buf->p)) { |
| 1629 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1630 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1631 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1632 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1633 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1634 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1635 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1636 | state = HTTP_MSG_RQMETH; |
| 1637 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1639 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1641 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1643 | if (unlikely(*ptr == '\n')) |
| 1644 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1645 | 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] | 1646 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1647 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1648 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1649 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1650 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1651 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1653 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1654 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1655 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | case HTTP_MSG_RQMETH_SP: |
| 1657 | case HTTP_MSG_RQURI: |
| 1658 | case HTTP_MSG_RQURI_SP: |
| 1659 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1660 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1661 | state, ptr, end, |
| 1662 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1663 | if (unlikely(!ptr)) |
| 1664 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1665 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | /* we have a full request and we know that we have either a CR |
| 1667 | * or an LF at <ptr>. |
| 1668 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1670 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1671 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1672 | if (likely(*ptr == '\r')) |
| 1673 | 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] | 1674 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1677 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1678 | /* check for HTTP/0.9 request : no version information available. |
| 1679 | * msg->sol must point to the first of CR or LF. |
| 1680 | */ |
| 1681 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1682 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1685 | 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] | 1686 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1688 | /* |
| 1689 | * Common states below |
| 1690 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1691 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1692 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1693 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1695 | goto http_msg_hdr_name; |
| 1696 | } |
| 1697 | |
| 1698 | if (likely(*ptr == '\r')) |
| 1699 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1700 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1703 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | /* assumes msg->sol points to the first char */ |
| 1705 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1706 | 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] | 1707 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1708 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | 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] | 1710 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1711 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1712 | goto http_msg_invalid; |
| 1713 | |
| 1714 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1715 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1716 | |
| 1717 | /* and we still accept this non-token character */ |
| 1718 | 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] | 1719 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1720 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1721 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1722 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1724 | 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] | 1725 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1727 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1728 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1730 | goto http_msg_hdr_val; |
| 1731 | } |
| 1732 | |
| 1733 | if (likely(*ptr == '\r')) |
| 1734 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1735 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1736 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1737 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1738 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1740 | 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] | 1741 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1743 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1744 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1745 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1746 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1747 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | goto http_msg_hdr_l1_sp; |
| 1749 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1750 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1751 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | goto http_msg_complete_header; |
| 1753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1755 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1756 | /* assumes msg->sol points to the first char, and msg->sov |
| 1757 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | */ |
| 1759 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1760 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1762 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1763 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1764 | * real header end in case it ends with lots of LWS, but is this |
| 1765 | * really needed ? |
| 1766 | */ |
| 1767 | if (likely(*ptr == '\r')) |
| 1768 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1769 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1772 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1773 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1774 | 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] | 1775 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1777 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1779 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1780 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1781 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1782 | goto http_msg_hdr_val; |
| 1783 | } |
| 1784 | http_msg_complete_header: |
| 1785 | /* |
| 1786 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1787 | * Assumes msg->sol points to the first char, msg->sov points |
| 1788 | * to the first character of the value and msg->eol to the |
| 1789 | * first CR or LF so we know how the line ends. We insert last |
| 1790 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1792 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | idx, idx->tail) < 0)) |
| 1794 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1796 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1798 | goto http_msg_hdr_name; |
| 1799 | } |
| 1800 | |
| 1801 | if (likely(*ptr == '\r')) |
| 1802 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1803 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1804 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1806 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1807 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1808 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1809 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1810 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1811 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1812 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1813 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1814 | msg->eoh = msg->sol; |
| 1815 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1816 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1817 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1818 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1819 | |
| 1820 | case HTTP_MSG_ERROR: |
| 1821 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1822 | break; |
| 1823 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1824 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1825 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1826 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1827 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1828 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1829 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | } |
| 1831 | http_msg_ood: |
| 1832 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1833 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1834 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1835 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1836 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1837 | http_msg_invalid: |
| 1838 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1839 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1840 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1841 | return; |
| 1842 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1843 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1844 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1845 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1846 | * nothing is done and 1 is returned. |
| 1847 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1848 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1849 | { |
| 1850 | int delta; |
| 1851 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1852 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1853 | |
| 1854 | if (msg->sl.rq.v_l != 0) |
| 1855 | return 1; |
| 1856 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1857 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1858 | if (txn->meth != HTTP_METH_GET) |
| 1859 | return 0; |
| 1860 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1861 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1862 | delta = 0; |
| 1863 | |
| 1864 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1865 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1866 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1867 | } |
| 1868 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1869 | 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] | 1870 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1871 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1872 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1873 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1874 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1875 | NULL, NULL); |
| 1876 | if (unlikely(!cur_end)) |
| 1877 | return 0; |
| 1878 | |
| 1879 | /* we have a full HTTP/1.0 request now and we know that |
| 1880 | * we have either a CR or an LF at <ptr>. |
| 1881 | */ |
| 1882 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1883 | return 1; |
| 1884 | } |
| 1885 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1886 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1887 | * and "keep-alive" values. If we already know that some headers may safely |
| 1888 | * 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] | 1889 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1890 | * - 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] | 1891 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1892 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1893 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1894 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1895 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1896 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1897 | 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] | 1898 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1899 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1900 | const char *hdr_val = "Connection"; |
| 1901 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1902 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1903 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1904 | return; |
| 1905 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1906 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1907 | hdr_val = "Proxy-Connection"; |
| 1908 | hdr_len = 16; |
| 1909 | } |
| 1910 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1911 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1912 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1913 | 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] | 1914 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1915 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1916 | if (to_del & 2) |
| 1917 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1918 | else |
| 1919 | txn->flags |= TX_CON_KAL_SET; |
| 1920 | } |
| 1921 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1922 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1923 | if (to_del & 1) |
| 1924 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1925 | else |
| 1926 | txn->flags |= TX_CON_CLO_SET; |
| 1927 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1928 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1929 | txn->flags |= TX_HDR_CONN_UPG; |
| 1930 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1931 | } |
| 1932 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1933 | txn->flags |= TX_HDR_CONN_PRS; |
| 1934 | return; |
| 1935 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1936 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1937 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1938 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1939 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1940 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1941 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1942 | 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] | 1943 | { |
| 1944 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1945 | const char *hdr_val = "Connection"; |
| 1946 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1947 | |
| 1948 | ctx.idx = 0; |
| 1949 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1950 | |
| 1951 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1952 | hdr_val = "Proxy-Connection"; |
| 1953 | hdr_len = 16; |
| 1954 | } |
| 1955 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1956 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1957 | 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] | 1958 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1959 | if (wanted & TX_CON_KAL_SET) |
| 1960 | txn->flags |= TX_CON_KAL_SET; |
| 1961 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1962 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1963 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1964 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1965 | if (wanted & TX_CON_CLO_SET) |
| 1966 | txn->flags |= TX_CON_CLO_SET; |
| 1967 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1968 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1969 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1970 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1971 | |
| 1972 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1973 | return; |
| 1974 | |
| 1975 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1976 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1977 | hdr_val = "Connection: close"; |
| 1978 | hdr_len = 17; |
| 1979 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1980 | hdr_val = "Proxy-Connection: close"; |
| 1981 | hdr_len = 23; |
| 1982 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1983 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1987 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1988 | hdr_val = "Connection: keep-alive"; |
| 1989 | hdr_len = 22; |
| 1990 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1991 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1992 | hdr_len = 28; |
| 1993 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1994 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1995 | } |
| 1996 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1999 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 2000 | * the first byte of data after the chunk size, so that we know we can forward |
| 2001 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 2002 | * the chunk size. That way it is always possible to differentiate between the |
| 2003 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2004 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2005 | * Note: 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] | 2006 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2007 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2008 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2009 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2010 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2011 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2012 | const char *end = buf->data + buf->size; |
| 2013 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2014 | unsigned int chunk = 0; |
| 2015 | |
| 2016 | /* The chunk size is in the following form, though we are only |
| 2017 | * interested in the size and CRLF : |
| 2018 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2019 | */ |
| 2020 | while (1) { |
| 2021 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2022 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2023 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2024 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2025 | if (c < 0) /* not a hex digit anymore */ |
| 2026 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2027 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2028 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2029 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2030 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2031 | chunk = (chunk << 4) + c; |
| 2032 | } |
| 2033 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2034 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2035 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2036 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | |
| 2038 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2039 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2040 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2041 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2042 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2045 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2046 | * for the end of chunk size. |
| 2047 | */ |
| 2048 | while (1) { |
| 2049 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2050 | /* we now have a CR or an LF at ptr */ |
| 2051 | if (likely(*ptr == '\r')) { |
| 2052 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2053 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2054 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2055 | return 0; |
| 2056 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2057 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2058 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2059 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2060 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2061 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2062 | /* done */ |
| 2063 | break; |
| 2064 | } |
| 2065 | else if (*ptr == ';') { |
| 2066 | /* chunk extension, ends at next CRLF */ |
| 2067 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2068 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2069 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2070 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2071 | |
| 2072 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2073 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2074 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2075 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2076 | return 0; |
| 2077 | } |
| 2078 | /* we have a CRLF now, loop above */ |
| 2079 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2080 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2081 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2082 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2085 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2086 | * which may or may not be present. We save that into ->next, |
| 2087 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2088 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2089 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2090 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2091 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2092 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2093 | msg->chunk_len = chunk; |
| 2094 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2095 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2096 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2097 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2098 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2099 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2102 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2103 | * message <msg>. The first visited position is msg->next. If the end of |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2104 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2105 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2106 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2107 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2108 | * If a parse error is encountered, the function returns < 0 and does not |
| 2109 | * change anything except maybe msg->next. Note that the message must |
| 2110 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2111 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2112 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2113 | * already parsed and not forwarded. It is also important to note that this |
| 2114 | * function is designed to be able to parse wrapped headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2115 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2116 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2117 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2118 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2119 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2120 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2121 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2122 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2123 | const char *ptr = b_ptr(buf, msg->next); |
| 2124 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2125 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2126 | |
| 2127 | /* scan current line and stop at LF or CRLF */ |
| 2128 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2129 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2130 | return 0; |
| 2131 | |
| 2132 | if (*ptr == '\n') { |
| 2133 | if (!p1) |
| 2134 | p1 = ptr; |
| 2135 | p2 = ptr; |
| 2136 | break; |
| 2137 | } |
| 2138 | |
| 2139 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2140 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2141 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2142 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2143 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2144 | p1 = ptr; |
| 2145 | } |
| 2146 | |
| 2147 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2148 | if (ptr >= buf->data + buf->size) |
| 2149 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* after LF; point to beginning of next line */ |
| 2153 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2154 | if (p2 >= buf->data + buf->size) |
| 2155 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2156 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2157 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2158 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2159 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2160 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2161 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2162 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2163 | * Everything was scheduled for forwarding, there's nothing |
| 2164 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2165 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2166 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2167 | msg->msg_state = HTTP_MSG_DONE; |
| 2168 | return 1; |
| 2169 | } |
| 2170 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2171 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2175 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2176 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2177 | * msg->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2178 | * Note that the caller must ensure that ->p points to the first byte to parse. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2179 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2180 | * not enough data are available, the function does not change anything and |
| 2181 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2182 | * does not change anything. Note: this function is designed to parse wrapped |
| 2183 | * CRLF at the end of the buffer. |
| 2184 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2185 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2186 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2187 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2188 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2189 | int bytes; |
| 2190 | |
| 2191 | /* NB: we'll check data availabilty at the end. It's not a |
| 2192 | * problem because whatever we match first will be checked |
| 2193 | * against the correct length. |
| 2194 | */ |
| 2195 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2196 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2197 | if (*ptr == '\r') { |
| 2198 | bytes++; |
| 2199 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2200 | if (ptr >= buf->data + buf->size) |
| 2201 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2204 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2205 | return 0; |
| 2206 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2207 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2208 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2209 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2210 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2211 | |
| 2212 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2213 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2214 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2215 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2216 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2217 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2218 | return 1; |
| 2219 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2220 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2221 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2222 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2223 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2224 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2225 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2226 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2227 | { |
| 2228 | int q = 1000; |
| 2229 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2230 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2231 | goto out; |
| 2232 | q = (*qvalue++ - '0') * 1000; |
| 2233 | |
| 2234 | if (*qvalue++ != '.') |
| 2235 | goto out; |
| 2236 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2237 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2238 | goto out; |
| 2239 | q += (*qvalue++ - '0') * 100; |
| 2240 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2241 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2242 | goto out; |
| 2243 | q += (*qvalue++ - '0') * 10; |
| 2244 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2245 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2246 | goto out; |
| 2247 | q += (*qvalue++ - '0') * 1; |
| 2248 | out: |
| 2249 | if (q > 1000) |
| 2250 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2251 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2252 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2253 | return q; |
| 2254 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2255 | |
| 2256 | /* |
| 2257 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2258 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2259 | int select_compression_request_header(struct stream *s, struct buffer *req) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2260 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2261 | struct http_txn *txn = s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2262 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2263 | struct hdr_ctx ctx; |
| 2264 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2265 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2266 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2267 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2268 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2269 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2270 | */ |
| 2271 | ctx.idx = 0; |
| 2272 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2273 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2274 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2275 | (ctx.vlen < 31 || |
| 2276 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2277 | ctx.line[ctx.val + 30] < '6' || |
| 2278 | (ctx.line[ctx.val + 30] == '6' && |
| 2279 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2280 | s->comp_algo = NULL; |
| 2281 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2284 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2285 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2286 | int best_q = 0; |
| 2287 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2288 | ctx.idx = 0; |
| 2289 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2290 | const char *qval; |
| 2291 | int q; |
| 2292 | int toklen; |
| 2293 | |
| 2294 | /* try to isolate the token from the optional q-value */ |
| 2295 | toklen = 0; |
| 2296 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2297 | toklen++; |
| 2298 | |
| 2299 | qval = ctx.line + ctx.val + toklen; |
| 2300 | while (1) { |
| 2301 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2302 | qval++; |
| 2303 | |
| 2304 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2305 | qval = NULL; |
| 2306 | break; |
| 2307 | } |
| 2308 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2309 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2310 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2311 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2312 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2313 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2314 | qval = NULL; |
| 2315 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2316 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2317 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2318 | break; |
| 2319 | |
| 2320 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2321 | qval++; |
| 2322 | } |
| 2323 | |
| 2324 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2325 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2326 | |
| 2327 | if (q <= best_q) |
| 2328 | continue; |
| 2329 | |
| 2330 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2331 | if (*(ctx.line + ctx.val) == '*' || |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2332 | word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2333 | s->comp_algo = comp_algo; |
| 2334 | best_q = q; |
| 2335 | break; |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2342 | if (s->comp_algo) { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2343 | if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2344 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2345 | ctx.idx = 0; |
| 2346 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2347 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2348 | } |
| 2349 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2350 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | /* identity is implicit does not require headers */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2354 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2355 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2356 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2357 | s->comp_algo = comp_algo; |
| 2358 | return 1; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2364 | return 0; |
| 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Selects a comression algorithm depending of the server response. |
| 2369 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2370 | int select_compression_response_header(struct stream *s, struct buffer *res) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2371 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2372 | struct http_txn *txn = s->txn; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2373 | struct http_msg *msg = &txn->rsp; |
| 2374 | struct hdr_ctx ctx; |
| 2375 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2376 | |
| 2377 | /* no common compression algorithm was found in request header */ |
| 2378 | if (s->comp_algo == NULL) |
| 2379 | goto fail; |
| 2380 | |
| 2381 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2382 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2383 | goto fail; |
| 2384 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2385 | /* compress 200,201,202,203 responses only */ |
| 2386 | if ((txn->status != 200) && |
| 2387 | (txn->status != 201) && |
| 2388 | (txn->status != 202) && |
| 2389 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2390 | goto fail; |
| 2391 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2392 | /* Content-Length is null */ |
| 2393 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2394 | goto fail; |
| 2395 | |
| 2396 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2397 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2398 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2399 | goto fail; |
| 2400 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2401 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2402 | ctx.idx = 0; |
| 2403 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2404 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2405 | goto fail; |
| 2406 | } |
| 2407 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2408 | comp_type = NULL; |
| 2409 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2410 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2411 | * not listed in the "compression type" directive if any. If no content-type was |
| 2412 | * found but configuration requires one, we don't compress either. Backend has |
| 2413 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2414 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2415 | ctx.idx = 0; |
| 2416 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2417 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2418 | goto fail; |
| 2419 | |
| 2420 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2421 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2422 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2423 | if (ctx.vlen >= comp_type->name_len && |
| 2424 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2425 | /* this Content-Type should be compressed */ |
| 2426 | break; |
| 2427 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2428 | /* this Content-Type should not be compressed */ |
| 2429 | if (comp_type == NULL) |
| 2430 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2431 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2432 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2433 | else { /* no content-type header */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2434 | if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2435 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2438 | /* limit compression rate */ |
| 2439 | if (global.comp_rate_lim > 0) |
| 2440 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2441 | goto fail; |
| 2442 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2443 | /* limit cpu usage */ |
| 2444 | if (idle_pct < compress_min_idle) |
| 2445 | goto fail; |
| 2446 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2447 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2448 | if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2449 | goto fail; |
| 2450 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2451 | s->flags |= SF_COMP_READY; |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2452 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2453 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2454 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2455 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2456 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2457 | |
| 2458 | /* add Transfer-Encoding header */ |
| 2459 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2460 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2461 | |
| 2462 | /* |
| 2463 | * Add Content-Encoding header when it's not identity encoding. |
| 2464 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2465 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2466 | * header. |
| 2467 | */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2468 | if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2469 | trash.len = 18; |
| 2470 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2471 | memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len); |
| 2472 | trash.len += s->comp_algo->ua_name_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2473 | trash.str[trash.len] = '\0'; |
| 2474 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2475 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2476 | return 1; |
| 2477 | |
| 2478 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2479 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2480 | return 0; |
| 2481 | } |
| 2482 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2483 | 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] | 2484 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2485 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2486 | int tmp = TX_CON_WANT_KAL; |
| 2487 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2488 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2489 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2490 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2491 | tmp = TX_CON_WANT_TUN; |
| 2492 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2493 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2494 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2495 | tmp = TX_CON_WANT_TUN; |
| 2496 | } |
| 2497 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2498 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2499 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2500 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2501 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2502 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2503 | tmp = TX_CON_WANT_CLO; |
| 2504 | else |
| 2505 | tmp = TX_CON_WANT_SCL; |
| 2506 | } |
| 2507 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2508 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2509 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2510 | tmp = TX_CON_WANT_CLO; |
| 2511 | |
| 2512 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2513 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2514 | |
| 2515 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2516 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2517 | /* parse the Connection header and possibly clean it */ |
| 2518 | int to_del = 0; |
| 2519 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2520 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2521 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2522 | to_del |= 2; /* remove "keep-alive" */ |
| 2523 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2524 | to_del |= 1; /* remove "close" */ |
| 2525 | http_parse_connection_header(txn, msg, to_del); |
| 2526 | } |
| 2527 | |
| 2528 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2529 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2530 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2531 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2532 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2533 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2534 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2535 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2536 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2537 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2538 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2539 | * processing can continue on next analysers, or zero if it either needs more |
| 2540 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2541 | * 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] | 2542 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2543 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2544 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2545 | 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] | 2546 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2547 | /* |
| 2548 | * We will parse the partial (or complete) lines. |
| 2549 | * We will check the request syntax, and also join multi-line |
| 2550 | * headers. An index of all the lines will be elaborated while |
| 2551 | * parsing. |
| 2552 | * |
| 2553 | * For the parsing, we use a 28 states FSM. |
| 2554 | * |
| 2555 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2556 | * req->buf->p = beginning of request |
| 2557 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2558 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2559 | * msg->eol = end of current header or line (LF or CRLF) |
| 2560 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2561 | * |
| 2562 | * 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] | 2563 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2564 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2565 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2566 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2567 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2568 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2569 | int use_close_only; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2570 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2571 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2573 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2574 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2575 | 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] | 2576 | now_ms, __FUNCTION__, |
| 2577 | s, |
| 2578 | req, |
| 2579 | req->rex, req->wex, |
| 2580 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2581 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2582 | req->analysers); |
| 2583 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2584 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2585 | s->srv_error = http_return_srv_error; |
| 2586 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2587 | /* There's a protected area at the end of the buffer for rewriting |
| 2588 | * purposes. We don't want to start to parse the request if the |
| 2589 | * protected area is affected, because we may have to move processed |
| 2590 | * data later, which is much more complicated. |
| 2591 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2592 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2593 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2594 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2595 | 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] | 2596 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2597 | /* 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] | 2598 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2599 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2600 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2601 | return 0; |
| 2602 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2603 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2604 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2605 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2606 | } |
| 2607 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2608 | /* Note that we have the same problem with the response ; we |
| 2609 | * may want to send a redirect, error or anything which requires |
| 2610 | * some spare space. So we'll ensure that we have at least |
| 2611 | * maxrewrite bytes available in the response buffer before |
| 2612 | * processing that one. This will only affect pipelined |
| 2613 | * keep-alive requests. |
| 2614 | */ |
| 2615 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2616 | unlikely(!channel_is_rewritable(&s->res) || |
| 2617 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2618 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2619 | if (s->res.buf->o) { |
| 2620 | 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] | 2621 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2622 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2623 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2624 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2625 | s->res.flags |= CF_WAKE_WRITE; |
| 2626 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2627 | return 0; |
| 2628 | } |
| 2629 | } |
| 2630 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2631 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2632 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2635 | /* 1: we might have to print this header in debug mode */ |
| 2636 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2637 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2638 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2639 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2640 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2641 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2642 | /* this is a bit complex : in case of error on the request line, |
| 2643 | * we know that rq.l is still zero, so we display only the part |
| 2644 | * up to the end of the line (truncated by debug_hdr). |
| 2645 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2646 | 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] | 2647 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2648 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2649 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2650 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | while (cur_idx) { |
| 2653 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2654 | debug_hdr("clihdr", s, sol, eol); |
| 2655 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2656 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2657 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2658 | } |
| 2659 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2660 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2661 | /* |
| 2662 | * Now we quickly check if we have found a full valid request. |
| 2663 | * If not so, we check the FD and buffer states before leaving. |
| 2664 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2665 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2666 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2667 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2668 | * 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] | 2669 | * 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] | 2670 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2671 | * Last, we may increase some tracked counters' http request errors on |
| 2672 | * the cases that are deliberately the client's fault. For instance, |
| 2673 | * a timeout or connection reset is not counted as an error. However |
| 2674 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2676 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2677 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2678 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2679 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2680 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2681 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2682 | stream_inc_http_req_ctr(s); |
| 2683 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2684 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2685 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2686 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2688 | /* 1: Since we are in header mode, if there's no space |
| 2689 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2690 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2691 | * must terminate it now. |
| 2692 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2693 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | /* FIXME: check if URI is set and return Status |
| 2695 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2696 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2697 | stream_inc_http_req_ctr(s); |
| 2698 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2699 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2700 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2701 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | goto return_bad_req; |
| 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 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2706 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2707 | if (!(s->flags & SF_ERR_MASK)) |
| 2708 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2709 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2710 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2711 | goto failed_keep_alive; |
| 2712 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2713 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2714 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2715 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2716 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2717 | } |
| 2718 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2719 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2720 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2721 | msg->msg_state = HTTP_MSG_ERROR; |
| 2722 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2723 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2724 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2725 | proxy_inc_fe_req_ctr(sess->fe); |
| 2726 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2727 | if (sess->listener->counters) |
| 2728 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2729 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2730 | if (!(s->flags & SF_FINST_MASK)) |
| 2731 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | return 0; |
| 2733 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2734 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2735 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2736 | 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] | 2737 | if (!(s->flags & SF_ERR_MASK)) |
| 2738 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2739 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2740 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2741 | goto failed_keep_alive; |
| 2742 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2743 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2744 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2745 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2746 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2747 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2748 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2749 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2750 | msg->msg_state = HTTP_MSG_ERROR; |
| 2751 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2752 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2753 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2754 | proxy_inc_fe_req_ctr(sess->fe); |
| 2755 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2756 | if (sess->listener->counters) |
| 2757 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2758 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2759 | if (!(s->flags & SF_FINST_MASK)) |
| 2760 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2761 | return 0; |
| 2762 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2763 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2764 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2765 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2766 | if (!(s->flags & SF_ERR_MASK)) |
| 2767 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2768 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2769 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2770 | goto failed_keep_alive; |
| 2771 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2772 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2773 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2774 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2775 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2776 | msg->msg_state = HTTP_MSG_ERROR; |
| 2777 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2778 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2779 | stream_inc_http_err_ctr(s); |
| 2780 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2781 | proxy_inc_fe_req_ctr(sess->fe); |
| 2782 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2783 | if (sess->listener->counters) |
| 2784 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2786 | if (!(s->flags & SF_FINST_MASK)) |
| 2787 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2788 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2791 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2792 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2793 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2794 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2795 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2796 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2797 | /* We need more data, we have to re-enable quick-ack in case we |
| 2798 | * previously disabled it, otherwise we might cause the client |
| 2799 | * to delay next data. |
| 2800 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2801 | 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] | 2802 | } |
| 2803 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2804 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2805 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2806 | /* If the client starts to talk, let's fall back to |
| 2807 | * request timeout processing. |
| 2808 | */ |
| 2809 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2810 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2811 | } |
| 2812 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2813 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2814 | if (!tick_isset(req->analyse_exp)) { |
| 2815 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2816 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2817 | tick_isset(s->be->timeout.httpka)) |
| 2818 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2819 | else |
| 2820 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2821 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2822 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2823 | /* we're not ready yet */ |
| 2824 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2825 | |
| 2826 | failed_keep_alive: |
| 2827 | /* Here we process low-level errors for keep-alive requests. In |
| 2828 | * short, if the request is not the first one and it experiences |
| 2829 | * a timeout, read error or shutdown, we just silently close so |
| 2830 | * that the client can try again. |
| 2831 | */ |
| 2832 | txn->status = 0; |
| 2833 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2834 | req->analysers = 0; |
| 2835 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2836 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2837 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2838 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2839 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2840 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2841 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2842 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2843 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2844 | * 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] | 2845 | * 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] | 2846 | * 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] | 2847 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2848 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2849 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2850 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2851 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2852 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2853 | 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] | 2854 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2855 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2856 | /* kill the pending keep-alive timeout */ |
| 2857 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2858 | req->analyse_exp = TICK_ETERNITY; |
| 2859 | } |
| 2860 | |
| 2861 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2862 | /* Maybe we found in invalid header name while we were configured not |
| 2863 | * to block on that, so we have to capture it now. |
| 2864 | */ |
| 2865 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2866 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2867 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2868 | /* |
| 2869 | * 1: identify the method |
| 2870 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2871 | 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] | 2872 | |
| 2873 | /* we can make use of server redirect on GET and HEAD */ |
| 2874 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2875 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2876 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2877 | /* |
| 2878 | * 2: check if the URI matches the monitor_uri. |
| 2879 | * We have to do this for every request which gets in, because |
| 2880 | * the monitor-uri is defined by the frontend. |
| 2881 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2882 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2883 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2884 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2885 | sess->fe->monitor_uri, |
| 2886 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2887 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2888 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2889 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2890 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2891 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2892 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2893 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2894 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2895 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2896 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2897 | 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] | 2898 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2899 | ret = acl_pass(ret); |
| 2900 | if (cond->pol == ACL_COND_UNLESS) |
| 2901 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2902 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2903 | if (ret) { |
| 2904 | /* we fail this request, let's return 503 service unavail */ |
| 2905 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2906 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2907 | if (!(s->flags & SF_ERR_MASK)) |
| 2908 | 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] | 2909 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2910 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2911 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2912 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2913 | /* nothing to fail, let's reply normaly */ |
| 2914 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2915 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2916 | if (!(s->flags & SF_ERR_MASK)) |
| 2917 | 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] | 2918 | goto return_prx_cond; |
| 2919 | } |
| 2920 | |
| 2921 | /* |
| 2922 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2923 | * Note: we cannot log anymore if the request has been |
| 2924 | * classified as invalid. |
| 2925 | */ |
| 2926 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2927 | /* we have a complete HTTP request that we must log */ |
| 2928 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2929 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2930 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2931 | if (urilen >= REQURI_LEN) |
| 2932 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2933 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2934 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2935 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2936 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2937 | s->do_log(s); |
| 2938 | } else { |
| 2939 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2940 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2941 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2942 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2943 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2944 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2945 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2946 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2947 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2948 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2949 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2950 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2951 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2952 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2953 | |
| 2954 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2955 | 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] | 2956 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2957 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2958 | * we have what looks like a proxied connection instead of a connection, |
| 2959 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2960 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2961 | * happen to do that despite being non-standard :-( |
| 2962 | * We consider that a request not beginning with either '/' or '*' is |
| 2963 | * a proxied connection, which covers both "scheme://location" and |
| 2964 | * CONNECT ip:port. |
| 2965 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2966 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2967 | 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] | 2968 | txn->flags |= TX_USE_PX_CONN; |
| 2969 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2970 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2971 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2972 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2973 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2974 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2975 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2976 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2977 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2978 | /* 6: determine the transfer-length. |
| 2979 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2980 | * the presence of a message-body in a REQUEST and its transfer length |
| 2981 | * must be determined that way (in order of precedence) : |
| 2982 | * 1. The presence of a message-body in a request is signaled by the |
| 2983 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2984 | * in the request's header fields. When a request message contains |
| 2985 | * both a message-body of non-zero length and a method that does |
| 2986 | * not define any semantics for that request message-body, then an |
| 2987 | * origin server SHOULD either ignore the message-body or respond |
| 2988 | * with an appropriate error message (e.g., 413). A proxy or |
| 2989 | * gateway, when presented the same request, SHOULD either forward |
| 2990 | * the request inbound with the message- body or ignore the |
| 2991 | * message-body when determining a response. |
| 2992 | * |
| 2993 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2994 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2995 | * transfer-length is defined by the use of this transfer-coding. |
| 2996 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2997 | * transfer-coding is not present, the transfer-length is defined |
| 2998 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2999 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3000 | * 3. If a Content-Length header field is present, its decimal value in |
| 3001 | * OCTETs represents both the entity-length and the transfer-length. |
| 3002 | * If a message is received with both a Transfer-Encoding header |
| 3003 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3004 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3005 | * 4. By the server closing the connection. (Closing the connection |
| 3006 | * cannot be used to indicate the end of a request body, since that |
| 3007 | * would leave no possibility for the server to send back a response.) |
| 3008 | * |
| 3009 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 3010 | * transfer-codings MUST include "chunked", unless the message indicates |
| 3011 | * it is terminated by closing the connection. When the "chunked" |
| 3012 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 3013 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3014 | */ |
| 3015 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3016 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3017 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3018 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3019 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3020 | 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] | 3021 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3022 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3023 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3024 | /* bad transfer-encoding (chunked followed by something else) */ |
| 3025 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3026 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3027 | break; |
| 3028 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3029 | } |
| 3030 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame^] | 3031 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3032 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame^] | 3033 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3034 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3035 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3036 | } |
| 3037 | else while (!use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3038 | 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] | 3039 | signed long long cl; |
| 3040 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3041 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3042 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3043 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3044 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3045 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3046 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3047 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3048 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3049 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3050 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3051 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3052 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3053 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3054 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3055 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3056 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3057 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3058 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3059 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3061 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3062 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3063 | } |
| 3064 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3065 | /* bodyless requests have a known length */ |
| 3066 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3067 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3068 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3069 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3070 | * only change if both the request and the config reference something else. |
| 3071 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3072 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3073 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3074 | * if FE and BE have the same settings (common). The method consists in |
| 3075 | * checking if options changed between the two calls (implying that either |
| 3076 | * one is non-null, or one of them is non-null and we are there for the first |
| 3077 | * time. |
| 3078 | */ |
| 3079 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3080 | ((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] | 3081 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3082 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3083 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3084 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3085 | req->analyse_exp = TICK_ETERNITY; |
| 3086 | return 1; |
| 3087 | |
| 3088 | return_bad_req: |
| 3089 | /* We centralize bad requests processing here */ |
| 3090 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3091 | /* we detected a parsing error. We want to archive this request |
| 3092 | * in the dedicated proxy area for later troubleshooting. |
| 3093 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3094 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3098 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3099 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3100 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3101 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3102 | if (sess->listener->counters) |
| 3103 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3104 | |
| 3105 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3106 | if (!(s->flags & SF_ERR_MASK)) |
| 3107 | s->flags |= SF_ERR_PRXCOND; |
| 3108 | if (!(s->flags & SF_FINST_MASK)) |
| 3109 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3110 | |
| 3111 | req->analysers = 0; |
| 3112 | req->analyse_exp = TICK_ETERNITY; |
| 3113 | return 0; |
| 3114 | } |
| 3115 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3116 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3117 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3118 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3119 | * and program a response message if something was unexpected. It cannot fail |
| 3120 | * 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] | 3121 | * 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] | 3122 | * 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] | 3123 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3124 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3125 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3126 | { |
| 3127 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3128 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3129 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3130 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3131 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3132 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3133 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3134 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3135 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3136 | appctx = si_appctx(si); |
| 3137 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3138 | appctx->st1 = appctx->st2 = 0; |
| 3139 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3140 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3141 | 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] | 3142 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3143 | |
| 3144 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3145 | lookup = uri + uri_auth->uri_len; |
| 3146 | |
| 3147 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3148 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3149 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3150 | break; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | if (uri_auth->refresh) { |
| 3155 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3156 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3157 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3158 | break; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3164 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3165 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3166 | break; |
| 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3171 | if (memcmp(h, ";st=", 4) == 0) { |
| 3172 | int i; |
| 3173 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3174 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3175 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3176 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3177 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3178 | break; |
| 3179 | } |
| 3180 | } |
| 3181 | break; |
| 3182 | } |
| 3183 | } |
| 3184 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3185 | appctx->ctx.stats.scope_str = 0; |
| 3186 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3187 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3188 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3189 | int itx = 0; |
| 3190 | const char *h2; |
| 3191 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3192 | const char *err; |
| 3193 | |
| 3194 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3195 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3196 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3197 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3198 | itx++; |
| 3199 | h++; |
| 3200 | } |
| 3201 | |
| 3202 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3203 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3204 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3205 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3206 | /* 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] | 3207 | memcpy(scope_txt, h2, itx); |
| 3208 | scope_txt[itx] = '\0'; |
| 3209 | err = invalid_char(scope_txt); |
| 3210 | if (err) { |
| 3211 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3212 | appctx->ctx.stats.scope_str = 0; |
| 3213 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3214 | } |
| 3215 | break; |
| 3216 | } |
| 3217 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3218 | |
| 3219 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3220 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3221 | int ret = 1; |
| 3222 | |
| 3223 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3224 | 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] | 3225 | ret = acl_pass(ret); |
| 3226 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3227 | ret = !ret; |
| 3228 | } |
| 3229 | |
| 3230 | if (ret) { |
| 3231 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3232 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3233 | break; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3238 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3239 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3240 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3241 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3242 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3243 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3244 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3245 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3246 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3247 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3248 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3249 | else { |
| 3250 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3251 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3252 | } |
| 3253 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3254 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3255 | return 1; |
| 3256 | } |
| 3257 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3258 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3259 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3260 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3261 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3262 | { |
| 3263 | #ifdef IP_TOS |
| 3264 | if (from.ss_family == AF_INET) |
| 3265 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3266 | #endif |
| 3267 | #ifdef IPV6_TCLASS |
| 3268 | if (from.ss_family == AF_INET6) { |
| 3269 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3270 | /* v4-mapped addresses need IP_TOS */ |
| 3271 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3272 | else |
| 3273 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3274 | } |
| 3275 | #endif |
| 3276 | } |
| 3277 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3278 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3279 | const char* name, unsigned int name_len, |
| 3280 | const char *str, struct my_regex *re, |
| 3281 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3282 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3283 | struct hdr_ctx ctx; |
| 3284 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3285 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3286 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3287 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3288 | struct chunk *output = get_trash_chunk(); |
| 3289 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3290 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3291 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3292 | /* Choose the header browsing function. */ |
| 3293 | switch (action) { |
| 3294 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3295 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3296 | http_find_hdr_func = http_find_header2; |
| 3297 | break; |
| 3298 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3299 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3300 | http_find_hdr_func = http_find_full_header2; |
| 3301 | break; |
| 3302 | default: /* impossible */ |
| 3303 | return -1; |
| 3304 | } |
| 3305 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3306 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3307 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3308 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3309 | char *val = ctx.line + ctx.val; |
| 3310 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3311 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3312 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3313 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3314 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3315 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3316 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3317 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3318 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3319 | 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] | 3320 | |
| 3321 | hdr->len += delta; |
| 3322 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3323 | |
| 3324 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3325 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3331 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3332 | const char* name, unsigned int name_len, |
| 3333 | struct list *fmt, struct my_regex *re, |
| 3334 | int action) |
| 3335 | { |
| 3336 | struct chunk *replace = get_trash_chunk(); |
| 3337 | |
| 3338 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3339 | if (replace->len >= replace->size - 1) |
| 3340 | return -1; |
| 3341 | |
| 3342 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3343 | } |
| 3344 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3345 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3346 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3347 | * further processing of the request (auth, deny, ...), and defaults to |
| 3348 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3349 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3350 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3351 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3352 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3353 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3354 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3355 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3356 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3357 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3358 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3359 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3360 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3361 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3362 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3363 | * resume condition. If a resume is needed it is always in the action |
| 3364 | * and never in the ACL or converters. In this case, we initialise the |
| 3365 | * current rule, and go to the action execution point. |
| 3366 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3367 | if (s->current_rule) { |
| 3368 | rule = s->current_rule; |
| 3369 | s->current_rule = NULL; |
| 3370 | if (s->current_rule_list == rules) |
| 3371 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3372 | } |
| 3373 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3374 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3375 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3376 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3377 | continue; |
| 3378 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3379 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3380 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3381 | int ret; |
| 3382 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3383 | 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] | 3384 | ret = acl_pass(ret); |
| 3385 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3386 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3387 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3388 | |
| 3389 | if (!ret) /* condition not matched */ |
| 3390 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3391 | } |
| 3392 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3393 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3394 | switch (rule->action) { |
| 3395 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3396 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3397 | |
| 3398 | case HTTP_REQ_ACT_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3399 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3400 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3401 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3402 | case HTTP_REQ_ACT_TARPIT: |
| 3403 | txn->flags |= TX_CLTARPIT; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3404 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3405 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3406 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3407 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3408 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3409 | auth_realm = rule->arg.auth.realm; |
| 3410 | if (!auth_realm) { |
| 3411 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3412 | auth_realm = STATS_DEFAULT_REALM; |
| 3413 | else |
| 3414 | auth_realm = px->id; |
| 3415 | } |
| 3416 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3417 | * count one error, because normal browsing won't significantly |
| 3418 | * increase the counter but brute force attempts will. |
| 3419 | */ |
| 3420 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3421 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3422 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3423 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3424 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3425 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3426 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3427 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3428 | return HTTP_RULE_RES_BADREQ; |
| 3429 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3430 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3431 | case HTTP_REQ_ACT_SET_NICE: |
| 3432 | s->task->nice = rule->arg.nice; |
| 3433 | break; |
| 3434 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3435 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3436 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3437 | 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] | 3438 | break; |
| 3439 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3440 | case HTTP_REQ_ACT_SET_MARK: |
| 3441 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3442 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3443 | 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] | 3444 | #endif |
| 3445 | break; |
| 3446 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3447 | case HTTP_REQ_ACT_SET_LOGL: |
| 3448 | s->logs.level = rule->arg.loglevel; |
| 3449 | break; |
| 3450 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3451 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3452 | case HTTP_REQ_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3453 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3454 | rule->arg.hdr_add.name_len, |
| 3455 | &rule->arg.hdr_add.fmt, |
| 3456 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3457 | return HTTP_RULE_RES_BADREQ; |
| 3458 | break; |
| 3459 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3460 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3461 | ctx.idx = 0; |
| 3462 | /* remove all occurrences of the header */ |
| 3463 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3464 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3465 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3466 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3467 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3468 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3469 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3470 | case HTTP_REQ_ACT_ADD_HDR: |
| 3471 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3472 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3473 | trash.len = rule->arg.hdr_add.name_len; |
| 3474 | trash.str[trash.len++] = ':'; |
| 3475 | trash.str[trash.len++] = ' '; |
| 3476 | 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] | 3477 | |
| 3478 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3479 | /* remove all occurrences of the header */ |
| 3480 | ctx.idx = 0; |
| 3481 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3482 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3483 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3484 | } |
| 3485 | } |
| 3486 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3487 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3488 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3489 | |
| 3490 | case HTTP_REQ_ACT_DEL_ACL: |
| 3491 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3492 | struct pat_ref *ref; |
| 3493 | char *key; |
| 3494 | int len; |
| 3495 | |
| 3496 | /* collect reference */ |
| 3497 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3498 | if (!ref) |
| 3499 | continue; |
| 3500 | |
| 3501 | /* collect key */ |
| 3502 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3503 | key = trash.str; |
| 3504 | key[len] = '\0'; |
| 3505 | |
| 3506 | /* perform update */ |
| 3507 | /* returned code: 1=ok, 0=ko */ |
| 3508 | pat_ref_delete(ref, key); |
| 3509 | |
| 3510 | break; |
| 3511 | } |
| 3512 | |
| 3513 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3514 | struct pat_ref *ref; |
| 3515 | char *key; |
| 3516 | struct chunk *trash_key; |
| 3517 | int len; |
| 3518 | |
| 3519 | trash_key = get_trash_chunk(); |
| 3520 | |
| 3521 | /* collect reference */ |
| 3522 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3523 | if (!ref) |
| 3524 | continue; |
| 3525 | |
| 3526 | /* collect key */ |
| 3527 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3528 | key = trash_key->str; |
| 3529 | key[len] = '\0'; |
| 3530 | |
| 3531 | /* perform update */ |
| 3532 | /* add entry only if it does not already exist */ |
| 3533 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3534 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3535 | |
| 3536 | break; |
| 3537 | } |
| 3538 | |
| 3539 | case HTTP_REQ_ACT_SET_MAP: { |
| 3540 | struct pat_ref *ref; |
| 3541 | char *key, *value; |
| 3542 | struct chunk *trash_key, *trash_value; |
| 3543 | int len; |
| 3544 | |
| 3545 | trash_key = get_trash_chunk(); |
| 3546 | trash_value = get_trash_chunk(); |
| 3547 | |
| 3548 | /* collect reference */ |
| 3549 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3550 | if (!ref) |
| 3551 | continue; |
| 3552 | |
| 3553 | /* collect key */ |
| 3554 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3555 | key = trash_key->str; |
| 3556 | key[len] = '\0'; |
| 3557 | |
| 3558 | /* collect value */ |
| 3559 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3560 | value = trash_value->str; |
| 3561 | value[len] = '\0'; |
| 3562 | |
| 3563 | /* perform update */ |
| 3564 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3565 | /* update entry if it exists */ |
| 3566 | pat_ref_set(ref, key, value, NULL); |
| 3567 | else |
| 3568 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3569 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3570 | |
| 3571 | break; |
| 3572 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3573 | |
| 3574 | case HTTP_REQ_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3575 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3576 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3577 | return HTTP_RULE_RES_YIELD; |
| 3578 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3579 | break; |
| 3580 | |
| 3581 | case HTTP_REQ_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3582 | rule->action_ptr(rule, px, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3583 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3584 | |
| 3585 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3586 | /* Note: only the first valid tracking parameter of each |
| 3587 | * applies. |
| 3588 | */ |
| 3589 | |
| 3590 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3591 | struct stktable *t; |
| 3592 | struct stksess *ts; |
| 3593 | struct stktable_key *key; |
| 3594 | void *ptr; |
| 3595 | |
| 3596 | t = rule->act_prm.trk_ctr.table.t; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3597 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3598 | |
| 3599 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3600 | stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3601 | |
| 3602 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3603 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3604 | if (ptr) |
| 3605 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3606 | |
| 3607 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3608 | if (ptr) |
| 3609 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3610 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3611 | |
| 3612 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3613 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3614 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3615 | } |
| 3616 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3617 | } |
| 3618 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3619 | |
| 3620 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3621 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3625 | /* Executes the http-response rules <rules> for stream <s>, proxy <px> and |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3626 | * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD |
| 3627 | * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the |
| 3628 | * evaluation of next rule list. If *STOP is returned, the process must stop |
| 3629 | * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3630 | * rule. If *YIELD is returned, the czller must call again the function with |
| 3631 | * the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3632 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3633 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3634 | 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] | 3635 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3636 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3637 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3638 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3639 | struct http_res_rule *rule; |
| 3640 | struct hdr_ctx ctx; |
| 3641 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3642 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3643 | * resume condition. If a resume is needed it is always in the action |
| 3644 | * and never in the ACL or converters. In this case, we initialise the |
| 3645 | * current rule, and go to the action execution point. |
| 3646 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3647 | if (s->current_rule) { |
| 3648 | rule = s->current_rule; |
| 3649 | s->current_rule = NULL; |
| 3650 | if (s->current_rule_list == rules) |
| 3651 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3652 | } |
| 3653 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3654 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3655 | list_for_each_entry(rule, rules, list) { |
| 3656 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3657 | continue; |
| 3658 | |
| 3659 | /* check optional condition */ |
| 3660 | if (rule->cond) { |
| 3661 | int ret; |
| 3662 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3663 | 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] | 3664 | ret = acl_pass(ret); |
| 3665 | |
| 3666 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3667 | ret = !ret; |
| 3668 | |
| 3669 | if (!ret) /* condition not matched */ |
| 3670 | continue; |
| 3671 | } |
| 3672 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3673 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3674 | switch (rule->action) { |
| 3675 | case HTTP_RES_ACT_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3676 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3677 | |
| 3678 | case HTTP_RES_ACT_DENY: |
| 3679 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3680 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3681 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3682 | case HTTP_RES_ACT_SET_NICE: |
| 3683 | s->task->nice = rule->arg.nice; |
| 3684 | break; |
| 3685 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3686 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3687 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3688 | 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] | 3689 | break; |
| 3690 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3691 | case HTTP_RES_ACT_SET_MARK: |
| 3692 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3693 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3694 | 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] | 3695 | #endif |
| 3696 | break; |
| 3697 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3698 | case HTTP_RES_ACT_SET_LOGL: |
| 3699 | s->logs.level = rule->arg.loglevel; |
| 3700 | break; |
| 3701 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3702 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3703 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3704 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3705 | rule->arg.hdr_add.name_len, |
| 3706 | &rule->arg.hdr_add.fmt, |
| 3707 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3708 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3709 | break; |
| 3710 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3711 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3712 | ctx.idx = 0; |
| 3713 | /* remove all occurrences of the header */ |
| 3714 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3715 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3716 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3717 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3718 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3719 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3720 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3721 | case HTTP_RES_ACT_ADD_HDR: |
| 3722 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3723 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3724 | trash.len = rule->arg.hdr_add.name_len; |
| 3725 | trash.str[trash.len++] = ':'; |
| 3726 | trash.str[trash.len++] = ' '; |
| 3727 | 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] | 3728 | |
| 3729 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3730 | /* remove all occurrences of the header */ |
| 3731 | ctx.idx = 0; |
| 3732 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3733 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3734 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3735 | } |
| 3736 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3737 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3738 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3739 | |
| 3740 | case HTTP_RES_ACT_DEL_ACL: |
| 3741 | case HTTP_RES_ACT_DEL_MAP: { |
| 3742 | struct pat_ref *ref; |
| 3743 | char *key; |
| 3744 | int len; |
| 3745 | |
| 3746 | /* collect reference */ |
| 3747 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3748 | if (!ref) |
| 3749 | continue; |
| 3750 | |
| 3751 | /* collect key */ |
| 3752 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3753 | key = trash.str; |
| 3754 | key[len] = '\0'; |
| 3755 | |
| 3756 | /* perform update */ |
| 3757 | /* returned code: 1=ok, 0=ko */ |
| 3758 | pat_ref_delete(ref, key); |
| 3759 | |
| 3760 | break; |
| 3761 | } |
| 3762 | |
| 3763 | case HTTP_RES_ACT_ADD_ACL: { |
| 3764 | struct pat_ref *ref; |
| 3765 | char *key; |
| 3766 | struct chunk *trash_key; |
| 3767 | int len; |
| 3768 | |
| 3769 | trash_key = get_trash_chunk(); |
| 3770 | |
| 3771 | /* collect reference */ |
| 3772 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3773 | if (!ref) |
| 3774 | continue; |
| 3775 | |
| 3776 | /* collect key */ |
| 3777 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3778 | key = trash_key->str; |
| 3779 | key[len] = '\0'; |
| 3780 | |
| 3781 | /* perform update */ |
| 3782 | /* check if the entry already exists */ |
| 3783 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3784 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3785 | |
| 3786 | break; |
| 3787 | } |
| 3788 | |
| 3789 | case HTTP_RES_ACT_SET_MAP: { |
| 3790 | struct pat_ref *ref; |
| 3791 | char *key, *value; |
| 3792 | struct chunk *trash_key, *trash_value; |
| 3793 | int len; |
| 3794 | |
| 3795 | trash_key = get_trash_chunk(); |
| 3796 | trash_value = get_trash_chunk(); |
| 3797 | |
| 3798 | /* collect reference */ |
| 3799 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3800 | if (!ref) |
| 3801 | continue; |
| 3802 | |
| 3803 | /* collect key */ |
| 3804 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3805 | key = trash_key->str; |
| 3806 | key[len] = '\0'; |
| 3807 | |
| 3808 | /* collect value */ |
| 3809 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3810 | value = trash_value->str; |
| 3811 | value[len] = '\0'; |
| 3812 | |
| 3813 | /* perform update */ |
| 3814 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3815 | /* update entry if it exists */ |
| 3816 | pat_ref_set(ref, key, value, NULL); |
| 3817 | else |
| 3818 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3819 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3820 | |
| 3821 | break; |
| 3822 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3823 | |
| 3824 | case HTTP_RES_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3825 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3826 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3827 | return HTTP_RULE_RES_YIELD; |
| 3828 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3829 | break; |
| 3830 | |
| 3831 | case HTTP_RES_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3832 | rule->action_ptr(rule, px, s); |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3833 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3838 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3842 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3843 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3844 | * as too large a request to build a valid response. |
| 3845 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3846 | 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] | 3847 | { |
| 3848 | struct http_msg *msg = &txn->req; |
| 3849 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3850 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3851 | |
| 3852 | /* build redirect message */ |
| 3853 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3854 | case 308: |
| 3855 | msg_fmt = HTTP_308; |
| 3856 | break; |
| 3857 | case 307: |
| 3858 | msg_fmt = HTTP_307; |
| 3859 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3860 | case 303: |
| 3861 | msg_fmt = HTTP_303; |
| 3862 | break; |
| 3863 | case 301: |
| 3864 | msg_fmt = HTTP_301; |
| 3865 | break; |
| 3866 | case 302: |
| 3867 | default: |
| 3868 | msg_fmt = HTTP_302; |
| 3869 | break; |
| 3870 | } |
| 3871 | |
| 3872 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3873 | return 0; |
| 3874 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3875 | location = trash.str + trash.len; |
| 3876 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3877 | switch(rule->type) { |
| 3878 | case REDIRECT_TYPE_SCHEME: { |
| 3879 | const char *path; |
| 3880 | const char *host; |
| 3881 | struct hdr_ctx ctx; |
| 3882 | int pathlen; |
| 3883 | int hostlen; |
| 3884 | |
| 3885 | host = ""; |
| 3886 | hostlen = 0; |
| 3887 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3888 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3889 | host = ctx.line + ctx.val; |
| 3890 | hostlen = ctx.vlen; |
| 3891 | } |
| 3892 | |
| 3893 | path = http_get_path(txn); |
| 3894 | /* build message using path */ |
| 3895 | if (path) { |
| 3896 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3897 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3898 | int qs = 0; |
| 3899 | while (qs < pathlen) { |
| 3900 | if (path[qs] == '?') { |
| 3901 | pathlen = qs; |
| 3902 | break; |
| 3903 | } |
| 3904 | qs++; |
| 3905 | } |
| 3906 | } |
| 3907 | } else { |
| 3908 | path = "/"; |
| 3909 | pathlen = 1; |
| 3910 | } |
| 3911 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3912 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3913 | /* check if we can add scheme + "://" + host + path */ |
| 3914 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3915 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3916 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3917 | /* add scheme */ |
| 3918 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3919 | trash.len += rule->rdr_len; |
| 3920 | } |
| 3921 | else { |
| 3922 | /* add scheme with executing log format */ |
| 3923 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3924 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3925 | /* check if we can add scheme + "://" + host + path */ |
| 3926 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3927 | return 0; |
| 3928 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3929 | /* add "://" */ |
| 3930 | memcpy(trash.str + trash.len, "://", 3); |
| 3931 | trash.len += 3; |
| 3932 | |
| 3933 | /* add host */ |
| 3934 | memcpy(trash.str + trash.len, host, hostlen); |
| 3935 | trash.len += hostlen; |
| 3936 | |
| 3937 | /* add path */ |
| 3938 | memcpy(trash.str + trash.len, path, pathlen); |
| 3939 | trash.len += pathlen; |
| 3940 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3941 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3942 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3943 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3944 | if (trash.len > trash.size - 5) |
| 3945 | return 0; |
| 3946 | trash.str[trash.len] = '/'; |
| 3947 | trash.len++; |
| 3948 | } |
| 3949 | |
| 3950 | break; |
| 3951 | } |
| 3952 | case REDIRECT_TYPE_PREFIX: { |
| 3953 | const char *path; |
| 3954 | int pathlen; |
| 3955 | |
| 3956 | path = http_get_path(txn); |
| 3957 | /* build message using path */ |
| 3958 | if (path) { |
| 3959 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3960 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3961 | int qs = 0; |
| 3962 | while (qs < pathlen) { |
| 3963 | if (path[qs] == '?') { |
| 3964 | pathlen = qs; |
| 3965 | break; |
| 3966 | } |
| 3967 | qs++; |
| 3968 | } |
| 3969 | } |
| 3970 | } else { |
| 3971 | path = "/"; |
| 3972 | pathlen = 1; |
| 3973 | } |
| 3974 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3975 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3976 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3977 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3978 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3979 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3980 | * add anything, otherwise it makes it hard for the user to |
| 3981 | * configure a self-redirection. |
| 3982 | */ |
| 3983 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3984 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3985 | trash.len += rule->rdr_len; |
| 3986 | } |
| 3987 | } |
| 3988 | else { |
| 3989 | /* add prefix with executing log format */ |
| 3990 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3991 | |
| 3992 | /* Check length */ |
| 3993 | if (trash.len + pathlen > trash.size - 4) |
| 3994 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | /* add path */ |
| 3998 | memcpy(trash.str + trash.len, path, pathlen); |
| 3999 | trash.len += pathlen; |
| 4000 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4001 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4002 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4003 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4004 | if (trash.len > trash.size - 5) |
| 4005 | return 0; |
| 4006 | trash.str[trash.len] = '/'; |
| 4007 | trash.len++; |
| 4008 | } |
| 4009 | |
| 4010 | break; |
| 4011 | } |
| 4012 | case REDIRECT_TYPE_LOCATION: |
| 4013 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4014 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4015 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4016 | return 0; |
| 4017 | |
| 4018 | /* add location */ |
| 4019 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4020 | trash.len += rule->rdr_len; |
| 4021 | } |
| 4022 | else { |
| 4023 | /* add location with executing log format */ |
| 4024 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4025 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4026 | /* Check left length */ |
| 4027 | if (trash.len > trash.size - 4) |
| 4028 | return 0; |
| 4029 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4030 | break; |
| 4031 | } |
| 4032 | |
| 4033 | if (rule->cookie_len) { |
| 4034 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4035 | trash.len += 14; |
| 4036 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4037 | trash.len += rule->cookie_len; |
| 4038 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4039 | trash.len += 2; |
| 4040 | } |
| 4041 | |
| 4042 | /* add end of headers and the keep-alive/close status. |
| 4043 | * We may choose to set keep-alive if the Location begins |
| 4044 | * with a slash, because the client will come back to the |
| 4045 | * same server. |
| 4046 | */ |
| 4047 | txn->status = rule->code; |
| 4048 | /* let's log the request time */ |
| 4049 | s->logs.tv_request = now; |
| 4050 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4051 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4052 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 4053 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 4054 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4055 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4056 | /* keep-alive possible */ |
| 4057 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 4058 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4059 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4060 | trash.len += 30; |
| 4061 | } else { |
| 4062 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4063 | trash.len += 24; |
| 4064 | } |
| 4065 | } |
| 4066 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4067 | trash.len += 4; |
| 4068 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 4069 | /* "eat" the request */ |
| 4070 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 4071 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4072 | msg->sov = 0; |
| 4073 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4074 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4075 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4076 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4077 | } else { |
| 4078 | /* keep-alive not possible */ |
| 4079 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4080 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4081 | trash.len += 29; |
| 4082 | } else { |
| 4083 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4084 | trash.len += 23; |
| 4085 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4086 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4087 | txn->req.chn->analysers = 0; |
| 4088 | } |
| 4089 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4090 | if (!(s->flags & SF_ERR_MASK)) |
| 4091 | s->flags |= SF_ERR_LOCAL; |
| 4092 | if (!(s->flags & SF_FINST_MASK)) |
| 4093 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4094 | |
| 4095 | return 1; |
| 4096 | } |
| 4097 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4098 | /* This stream analyser runs all HTTP request processing which is common to |
| 4099 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4100 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4101 | * 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] | 4102 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4103 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4104 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4105 | 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] | 4106 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4107 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4108 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4109 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4110 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4111 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4112 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4113 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4114 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4115 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4116 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4117 | } |
| 4118 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4119 | 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] | 4120 | now_ms, __FUNCTION__, |
| 4121 | s, |
| 4122 | req, |
| 4123 | req->rex, req->wex, |
| 4124 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4125 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4126 | req->analysers); |
| 4127 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4128 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4129 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4130 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4131 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4132 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4133 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4134 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4135 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4136 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4137 | goto return_prx_yield; |
| 4138 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4139 | case HTTP_RULE_RES_CONT: |
| 4140 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4141 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4142 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4143 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4144 | if (txn->flags & TX_CLTARPIT) |
| 4145 | goto tarpit; |
| 4146 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4147 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4148 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4149 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4150 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4151 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4152 | goto done; |
| 4153 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4154 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4155 | goto return_bad_req; |
| 4156 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4157 | } |
| 4158 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4159 | /* OK at this stage, we know that the request was accepted according to |
| 4160 | * the http-request rules, we can check for the stats. Note that the |
| 4161 | * URI is detected *before* the req* rules in order not to be affected |
| 4162 | * by a possible reqrep, while they are processed *after* so that a |
| 4163 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4164 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4165 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4166 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4167 | 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] | 4168 | txn->status = 500; |
| 4169 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4170 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4171 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4172 | if (!(s->flags & SF_ERR_MASK)) |
| 4173 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4174 | goto return_prx_cond; |
| 4175 | } |
| 4176 | |
| 4177 | /* parse the whole stats request and extract the relevant information */ |
| 4178 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4179 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4180 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4181 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4182 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4183 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4184 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4185 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4186 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4187 | } |
| 4188 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4189 | /* evaluate the req* rules except reqadd */ |
| 4190 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4191 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4192 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4193 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4194 | if (txn->flags & TX_CLDENY) |
| 4195 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4196 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4197 | if (txn->flags & TX_CLTARPIT) |
| 4198 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4199 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4200 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4201 | /* add request headers from the rule sets in the same order */ |
| 4202 | list_for_each_entry(wl, &px->req_add, list) { |
| 4203 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4204 | 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] | 4205 | ret = acl_pass(ret); |
| 4206 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4207 | ret = !ret; |
| 4208 | if (!ret) |
| 4209 | continue; |
| 4210 | } |
| 4211 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4212 | 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] | 4213 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4214 | } |
| 4215 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4216 | |
| 4217 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4218 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4219 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4220 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4221 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4222 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4223 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4224 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4225 | if (!(s->flags & SF_FINST_MASK)) |
| 4226 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4227 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4228 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4229 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4230 | select_compression_request_header(s, req->buf); |
| 4231 | |
| 4232 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Willy Tarreau | 5506e3f | 2014-11-20 22:23:10 +0100 | [diff] [blame] | 4233 | req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4234 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4235 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4236 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4237 | /* check whether we have some ACLs set to redirect this request */ |
| 4238 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4239 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4240 | int ret; |
| 4241 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4242 | 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] | 4243 | ret = acl_pass(ret); |
| 4244 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4245 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4246 | if (!ret) |
| 4247 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4248 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4249 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4250 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4251 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4252 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4253 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4254 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4255 | * If this happens, then the data will not come immediately, so we must |
| 4256 | * send all what we have without waiting. Note that due to the small gain |
| 4257 | * 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] | 4258 | * 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] | 4259 | * itself once used. |
| 4260 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4261 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4262 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4263 | done: /* done with this analyser, continue with next ones that the calling |
| 4264 | * points will have set, if any. |
| 4265 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4266 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4267 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4268 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4269 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4270 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4271 | tarpit: |
| 4272 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4273 | * which may be the same as the connect timeout if unspecified. |
| 4274 | * If unset, then set it to zero because we really want it to |
| 4275 | * eventually expire. We build the tarpit as an analyser. |
| 4276 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4277 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4278 | |
| 4279 | /* wipe the request out so that we can drop the connection early |
| 4280 | * if the client closes first. |
| 4281 | */ |
| 4282 | channel_dont_connect(req); |
| 4283 | req->analysers = 0; /* remove switching rules etc... */ |
| 4284 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4285 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4286 | if (!req->analyse_exp) |
| 4287 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4288 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4289 | sess->fe->fe_counters.denied_req++; |
| 4290 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4291 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4292 | if (sess->listener->counters) |
| 4293 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4294 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4295 | |
| 4296 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4297 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4298 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4299 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4300 | stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4301 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4302 | sess->fe->fe_counters.denied_req++; |
| 4303 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4304 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4305 | if (sess->listener->counters) |
| 4306 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4307 | goto return_prx_cond; |
| 4308 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4309 | return_bad_req: |
| 4310 | /* We centralize bad requests processing here */ |
| 4311 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4312 | /* we detected a parsing error. We want to archive this request |
| 4313 | * in the dedicated proxy area for later troubleshooting. |
| 4314 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4315 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4316 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4317 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4318 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4319 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4320 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4321 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4322 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4323 | if (sess->listener->counters) |
| 4324 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4325 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4326 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4327 | if (!(s->flags & SF_ERR_MASK)) |
| 4328 | s->flags |= SF_ERR_PRXCOND; |
| 4329 | if (!(s->flags & SF_FINST_MASK)) |
| 4330 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4331 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4332 | req->analysers = 0; |
| 4333 | req->analyse_exp = TICK_ETERNITY; |
| 4334 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4335 | |
| 4336 | return_prx_yield: |
| 4337 | channel_dont_connect(req); |
| 4338 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4339 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4340 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4341 | /* This function performs all the processing enabled for the current request. |
| 4342 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4343 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4344 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4345 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4346 | 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] | 4347 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4348 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4349 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4350 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4351 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4352 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4353 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4354 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4355 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4356 | return 0; |
| 4357 | } |
| 4358 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4359 | 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] | 4360 | now_ms, __FUNCTION__, |
| 4361 | s, |
| 4362 | req, |
| 4363 | req->rex, req->wex, |
| 4364 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4365 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4366 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4367 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4368 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4369 | select_compression_request_header(s, req->buf); |
| 4370 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4371 | /* |
| 4372 | * Right now, we know that we have processed the entire headers |
| 4373 | * and that unwanted requests have been filtered out. We can do |
| 4374 | * whatever we want with the remaining request. Also, now we |
| 4375 | * may have separate values for ->fe, ->be. |
| 4376 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4377 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4378 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4379 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4380 | * incoming request. Note that this requires that a connection is |
| 4381 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4382 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4383 | 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] | 4384 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4385 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4386 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4387 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4388 | if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4389 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4390 | txn->status = 500; |
| 4391 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4392 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4393 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4394 | if (!(s->flags & SF_ERR_MASK)) |
| 4395 | s->flags |= SF_ERR_RESOURCE; |
| 4396 | if (!(s->flags & SF_FINST_MASK)) |
| 4397 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4398 | |
| 4399 | return 0; |
| 4400 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4401 | |
| 4402 | path = http_get_path(txn); |
| 4403 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4404 | 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] | 4405 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4406 | /* if the path was found, we have to remove everything between |
| 4407 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4408 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4409 | * u_l characters by a single "/". |
| 4410 | */ |
| 4411 | if (path) { |
| 4412 | char *cur_ptr = req->buf->p; |
| 4413 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4414 | int delta; |
| 4415 | |
| 4416 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4417 | http_msg_move_end(&txn->req, delta); |
| 4418 | cur_end += delta; |
| 4419 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4420 | goto return_bad_req; |
| 4421 | } |
| 4422 | else { |
| 4423 | char *cur_ptr = req->buf->p; |
| 4424 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4425 | int delta; |
| 4426 | |
| 4427 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4428 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4429 | http_msg_move_end(&txn->req, delta); |
| 4430 | cur_end += delta; |
| 4431 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4432 | goto return_bad_req; |
| 4433 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4434 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4435 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4436 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4437 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4438 | * Note that doing so might move headers in the request, but |
| 4439 | * the fields will stay coherent and the URI will not move. |
| 4440 | * This should only be performed in the backend. |
| 4441 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4442 | if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4443 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4444 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4445 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4446 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4447 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4448 | * so let's do the same now. |
| 4449 | */ |
| 4450 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4451 | /* It needs to look into the URI unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4452 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4453 | get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4454 | } |
| 4455 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4456 | /* add unique-id if "header-unique-id" is specified */ |
| 4457 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4458 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4459 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4460 | goto return_bad_req; |
| 4461 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4462 | 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] | 4463 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4464 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4465 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4466 | 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] | 4467 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4468 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4469 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4470 | goto return_bad_req; |
| 4471 | } |
| 4472 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4473 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4474 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4475 | * asks for it. |
| 4476 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4477 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4478 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4479 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4480 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4481 | 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] | 4482 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4483 | /* The header is set to be added only if none is present |
| 4484 | * and we found it, so don't do anything. |
| 4485 | */ |
| 4486 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4487 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4488 | /* Add an X-Forwarded-For header unless the source IP is |
| 4489 | * in the 'except' network range. |
| 4490 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4491 | if ((!sess->fe->except_mask.s_addr || |
| 4492 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4493 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4494 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4495 | (((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] | 4496 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4497 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4498 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4499 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4500 | |
| 4501 | /* Note: we rely on the backend to get the header name to be used for |
| 4502 | * x-forwarded-for, because the header is really meant for the backends. |
| 4503 | * However, if the backend did not specify any option, we have to rely |
| 4504 | * on the frontend's header name. |
| 4505 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4506 | if (s->be->fwdfor_hdr_len) { |
| 4507 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4508 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4509 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4510 | len = sess->fe->fwdfor_hdr_len; |
| 4511 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4512 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4513 | 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] | 4514 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4515 | 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] | 4516 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4517 | } |
| 4518 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4519 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4520 | /* FIXME: for the sake of completeness, we should also support |
| 4521 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4522 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4523 | int len; |
| 4524 | char pn[INET6_ADDRSTRLEN]; |
| 4525 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4526 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4527 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4528 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4529 | /* Note: we rely on the backend to get the header name to be used for |
| 4530 | * x-forwarded-for, because the header is really meant for the backends. |
| 4531 | * However, if the backend did not specify any option, we have to rely |
| 4532 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4533 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4534 | if (s->be->fwdfor_hdr_len) { |
| 4535 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4536 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4537 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4538 | len = sess->fe->fwdfor_hdr_len; |
| 4539 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4540 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4541 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4542 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4543 | 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] | 4544 | goto return_bad_req; |
| 4545 | } |
| 4546 | } |
| 4547 | |
| 4548 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4549 | * 10: add X-Original-To if either the frontend or the backend |
| 4550 | * asks for it. |
| 4551 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4552 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4553 | |
| 4554 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4555 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4556 | /* Add an X-Original-To header unless the destination IP is |
| 4557 | * in the 'except' network range. |
| 4558 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4559 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4560 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4561 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4562 | ((!sess->fe->except_mask_to.s_addr || |
| 4563 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4564 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4565 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4566 | (((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] | 4567 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4568 | int len; |
| 4569 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4570 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4571 | |
| 4572 | /* Note: we rely on the backend to get the header name to be used for |
| 4573 | * x-original-to, because the header is really meant for the backends. |
| 4574 | * However, if the backend did not specify any option, we have to rely |
| 4575 | * on the frontend's header name. |
| 4576 | */ |
| 4577 | if (s->be->orgto_hdr_len) { |
| 4578 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4579 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4580 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4581 | len = sess->fe->orgto_hdr_len; |
| 4582 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4583 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4584 | 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] | 4585 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4586 | 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] | 4587 | goto return_bad_req; |
| 4588 | } |
| 4589 | } |
| 4590 | } |
| 4591 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4592 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4593 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4594 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4595 | * "Upgrade" is present in the Connection header. |
| 4596 | */ |
| 4597 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4598 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4599 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4600 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4601 | unsigned int want_flags = 0; |
| 4602 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4603 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4604 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4605 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4606 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4607 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4608 | want_flags |= TX_CON_CLO_SET; |
| 4609 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4610 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4611 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4612 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4613 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4614 | want_flags |= TX_CON_KAL_SET; |
| 4615 | } |
| 4616 | |
| 4617 | 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] | 4618 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4619 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4620 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4621 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4622 | /* If we have no server assigned yet and we're balancing on url_param |
| 4623 | * with a POST request, we may be interested in checking the body for |
| 4624 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4625 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4626 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4627 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4628 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4629 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4630 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4631 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4632 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4633 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4634 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4635 | #ifdef TCP_QUICKACK |
| 4636 | /* We expect some data from the client. Unless we know for sure |
| 4637 | * we already have a full request, we have to re-enable quick-ack |
| 4638 | * in case we previously disabled it, otherwise we might cause |
| 4639 | * the client to delay further data. |
| 4640 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4641 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4642 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4643 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4644 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4645 | 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] | 4646 | #endif |
| 4647 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4648 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4649 | /************************************************************* |
| 4650 | * OK, that's finished for the headers. We have done what we * |
| 4651 | * could. Let's switch to the DATA state. * |
| 4652 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4653 | req->analyse_exp = TICK_ETERNITY; |
| 4654 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4655 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4656 | /* if the server closes the connection, we want to immediately react |
| 4657 | * and close the socket to save packets and syscalls. |
| 4658 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4659 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4660 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4661 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4662 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4663 | /* OK let's go on with the BODY now */ |
| 4664 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4665 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4666 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4667 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4668 | /* we detected a parsing error. We want to archive this request |
| 4669 | * in the dedicated proxy area for later troubleshooting. |
| 4670 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4671 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4672 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4673 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4674 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4675 | txn->status = 400; |
| 4676 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4677 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4678 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4679 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4680 | if (sess->listener->counters) |
| 4681 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4682 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4683 | if (!(s->flags & SF_ERR_MASK)) |
| 4684 | s->flags |= SF_ERR_PRXCOND; |
| 4685 | if (!(s->flags & SF_FINST_MASK)) |
| 4686 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4687 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4688 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4689 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4690 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4691 | * returns zero, at the beginning because it prevents any other processing |
| 4692 | * from occurring, and at the end because it terminates the request. |
| 4693 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4694 | 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] | 4695 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4696 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4697 | |
| 4698 | /* This connection is being tarpitted. The CLIENT side has |
| 4699 | * already set the connect expiration date to the right |
| 4700 | * timeout. We just have to check that the client is still |
| 4701 | * there and that the timeout has not expired. |
| 4702 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4703 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4704 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4705 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4706 | return 0; |
| 4707 | |
| 4708 | /* We will set the queue timer to the time spent, just for |
| 4709 | * logging purposes. We fake a 500 server error, so that the |
| 4710 | * attacker will not suspect his connection has been tarpitted. |
| 4711 | * It will not cause trouble to the logs because we can exclude |
| 4712 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4713 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4714 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4715 | |
| 4716 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4717 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4718 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4719 | |
| 4720 | req->analysers = 0; |
| 4721 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4722 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4723 | if (!(s->flags & SF_ERR_MASK)) |
| 4724 | s->flags |= SF_ERR_PRXCOND; |
| 4725 | if (!(s->flags & SF_FINST_MASK)) |
| 4726 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4727 | return 0; |
| 4728 | } |
| 4729 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4730 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4731 | * for either the buffer to be full, or the full advertised contents to have |
| 4732 | * reached the buffer. It must only be called after the standard HTTP request |
| 4733 | * processing has occurred, because it expects the request to be parsed and will |
| 4734 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4735 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4736 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4737 | * 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] | 4738 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4739 | 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] | 4740 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4741 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4742 | struct http_txn *txn = s->txn; |
| 4743 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4744 | |
| 4745 | /* We have to parse the HTTP request body to find any required data. |
| 4746 | * "balance url_param check_post" should have been the only way to get |
| 4747 | * into this. We were brought here after HTTP header analysis, so all |
| 4748 | * related structures are ready. |
| 4749 | */ |
| 4750 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4751 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4752 | /* This is the first call */ |
| 4753 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4754 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4755 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4756 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4757 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4758 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4759 | */ |
| 4760 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4761 | struct hdr_ctx ctx; |
| 4762 | ctx.idx = 0; |
| 4763 | /* Expect is allowed in 1.1, look for it */ |
| 4764 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4765 | 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] | 4766 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4767 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4768 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4769 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4770 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4771 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4772 | /* 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] | 4773 | * req->buf->p still points to the beginning of the message. We |
| 4774 | * must save the body in msg->next because it survives buffer |
| 4775 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4776 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4777 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4778 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4779 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4780 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4781 | else |
| 4782 | msg->msg_state = HTTP_MSG_DATA; |
| 4783 | } |
| 4784 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4785 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4786 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4787 | if (req->buf->i - msg->sov < msg->body_len) |
| 4788 | goto missing_data; |
| 4789 | |
| 4790 | /* OK we have everything we need now */ |
| 4791 | goto http_end; |
| 4792 | } |
| 4793 | |
| 4794 | /* OK here we're parsing a chunked-encoded message */ |
| 4795 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4796 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4797 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4798 | * 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] | 4799 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4800 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4801 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4802 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4803 | if (!ret) |
| 4804 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4805 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4806 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4807 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4808 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4811 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4812 | * We have the first data byte is in msg->sov. We're waiting for at |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4813 | * least a whole chunk or the whole content length bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4814 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4815 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4816 | goto http_end; |
| 4817 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4818 | if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4819 | goto http_end; |
| 4820 | |
| 4821 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4822 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4823 | * we have the maximum we can expect. |
| 4824 | */ |
| 4825 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4826 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4827 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4828 | 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] | 4829 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4830 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4831 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4832 | if (!(s->flags & SF_ERR_MASK)) |
| 4833 | s->flags |= SF_ERR_CLITO; |
| 4834 | if (!(s->flags & SF_FINST_MASK)) |
| 4835 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4836 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4837 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4838 | |
| 4839 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4840 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4841 | /* Not enough data. We'll re-use the http-request |
| 4842 | * timeout here. Ideally, we should set the timeout |
| 4843 | * relative to the accept() date. We just set the |
| 4844 | * request timeout once at the beginning of the |
| 4845 | * request. |
| 4846 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4847 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4848 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4849 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4850 | return 0; |
| 4851 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4852 | |
| 4853 | http_end: |
| 4854 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4855 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4856 | req->analysers &= ~an_bit; |
| 4857 | req->analyse_exp = TICK_ETERNITY; |
| 4858 | return 1; |
| 4859 | |
| 4860 | return_bad_req: /* let's centralize all bad requests */ |
| 4861 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4862 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4863 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4864 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4865 | if (!(s->flags & SF_ERR_MASK)) |
| 4866 | s->flags |= SF_ERR_PRXCOND; |
| 4867 | if (!(s->flags & SF_FINST_MASK)) |
| 4868 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4869 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4870 | return_err_msg: |
| 4871 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4872 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4873 | if (sess->listener->counters) |
| 4874 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4875 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4876 | } |
| 4877 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4878 | /* send a server's name with an outgoing request over an established connection. |
| 4879 | * Note: this function is designed to be called once the request has been scheduled |
| 4880 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4881 | * proceeding. |
| 4882 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4883 | 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] | 4884 | |
| 4885 | struct hdr_ctx ctx; |
| 4886 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4887 | char *hdr_name = be->server_id_hdr_name; |
| 4888 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4889 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4890 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4891 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4892 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4893 | ctx.idx = 0; |
| 4894 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4895 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4896 | if (old_o) { |
| 4897 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4898 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4899 | txn->req.next += old_o; |
| 4900 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4901 | } |
| 4902 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4903 | old_i = chn->buf->i; |
| 4904 | 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] | 4905 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4906 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4910 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4911 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4912 | hdr_val += hdr_name_len; |
| 4913 | *hdr_val++ = ':'; |
| 4914 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4915 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4916 | 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] | 4917 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4918 | if (old_o) { |
| 4919 | /* If this was a forwarded request, we must readjust the amount of |
| 4920 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4921 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4922 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4923 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4924 | old_o += chn->buf->i - old_i; |
| 4925 | b_adv(chn->buf, old_o); |
| 4926 | txn->req.next -= old_o; |
| 4927 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4928 | } |
| 4929 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4930 | return 0; |
| 4931 | } |
| 4932 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4933 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4934 | * right now but it works. |
| 4935 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4936 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4937 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4938 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4939 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4940 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4941 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4942 | * server's connections. We need a safer way to reinitialize buffer |
| 4943 | * flags. We also need a more accurate method for computing per-request |
| 4944 | * data. |
| 4945 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4946 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4947 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4948 | * to the server. |
| 4949 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4950 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4951 | !si_conn_ready(&s->si[1])) { |
| 4952 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4953 | si_shutr(&s->si[1]); |
| 4954 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4955 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4956 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4957 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4958 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4959 | if (unlikely(s->srv_conn)) |
| 4960 | sess_change_server(s, NULL); |
| 4961 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4962 | |
| 4963 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4964 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4965 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4966 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4967 | int n; |
| 4968 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4969 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4970 | if (n < 1 || n > 5) |
| 4971 | n = 0; |
| 4972 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4973 | if (fe->mode == PR_MODE_HTTP) { |
| 4974 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4975 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4976 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4977 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4978 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4979 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4980 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4981 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4982 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4983 | s->be->be_counters.p.http.comp_rsp++; |
| 4984 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4988 | s->logs.bytes_in -= s->req.buf->i; |
| 4989 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4990 | |
| 4991 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4992 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4993 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4994 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4995 | s->do_log(s); |
| 4996 | } |
| 4997 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4998 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4999 | stream_stop_content_counters(s); |
| 5000 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5001 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5002 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5003 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 5004 | tv_zero(&s->logs.tv_request); |
| 5005 | s->logs.t_queue = -1; |
| 5006 | s->logs.t_connect = -1; |
| 5007 | s->logs.t_data = -1; |
| 5008 | s->logs.t_close = 0; |
| 5009 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5010 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5011 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5012 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5013 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5014 | |
| 5015 | if (s->pend_pos) |
| 5016 | pendconn_free(s->pend_pos); |
| 5017 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5018 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5019 | if (s->flags & SF_CURR_SESS) { |
| 5020 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5021 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5022 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5023 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5024 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5025 | } |
| 5026 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5027 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5028 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5029 | /* only release our endpoint if we don't intend to reuse the |
| 5030 | * connection. |
| 5031 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5032 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5033 | !si_conn_ready(&s->si[1])) { |
| 5034 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5035 | } |
| 5036 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5037 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5038 | s->si[1].err_type = SI_ET_NONE; |
| 5039 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5040 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5041 | 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] | 5042 | 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); |
| 5043 | 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] | 5044 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5045 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5046 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5047 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5048 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5049 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5050 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5051 | |
| 5052 | if (prev_status == 401 || prev_status == 407) { |
| 5053 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5054 | * a chance of being able to send the visitor again to the same |
| 5055 | * server over the same connection. This is required by some |
| 5056 | * broken protocols such as NTLM, and anyway whenever there is |
| 5057 | * an opportunity for sending the challenge to the proper place, |
| 5058 | * it's better to do it (at least it helps with debugging). |
| 5059 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5060 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5061 | } |
| 5062 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5063 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5064 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5065 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5066 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5067 | s->req.flags |= CF_NEVER_WAIT; |
| 5068 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5069 | } |
| 5070 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5071 | /* if the request buffer is not empty, it means we're |
| 5072 | * about to process another request, so send pending |
| 5073 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5074 | * Just don't do this if the buffer is close to be full, |
| 5075 | * because the request will wait for it to flush a little |
| 5076 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5077 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5078 | if (s->req.buf->i) { |
| 5079 | if (s->res.buf->o && |
| 5080 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5081 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5082 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5083 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5084 | |
| 5085 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5086 | channel_auto_read(&s->req); |
| 5087 | channel_auto_close(&s->req); |
| 5088 | channel_auto_read(&s->res); |
| 5089 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5090 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5091 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5092 | si_idle_conn(&s->si[1]); |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5093 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5094 | s->req.analysers = strm_li(s)->analysers; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5095 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | |
| 5099 | /* This function updates the request state machine according to the response |
| 5100 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5101 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5102 | * it is only used to find when a request/response couple is complete. Both |
| 5103 | * this function and its equivalent should loop until both return zero. It |
| 5104 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5105 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5106 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5107 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5108 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5109 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5110 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5111 | unsigned int old_state = txn->req.msg_state; |
| 5112 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5113 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5114 | return 0; |
| 5115 | |
| 5116 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5117 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5118 | * We can shut the read side unless we want to abort_on_close, |
| 5119 | * or we have a POST request. The issue with POST requests is |
| 5120 | * that some browsers still send a CRLF after the request, and |
| 5121 | * this CRLF must be read so that it does not remain in the kernel |
| 5122 | * buffers, otherwise a close could cause an RST on some systems |
| 5123 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5124 | * Note that if we're using keep-alive on the client side, we'd |
| 5125 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5126 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5127 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5128 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5129 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5130 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5131 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5132 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5133 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5134 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5135 | /* if the server closes the connection, we want to immediately react |
| 5136 | * and close the socket to save packets and syscalls. |
| 5137 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5138 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5139 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5140 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5141 | goto wait_other_side; |
| 5142 | |
| 5143 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5144 | /* The server has not finished to respond, so we |
| 5145 | * don't want to move in order not to upset it. |
| 5146 | */ |
| 5147 | goto wait_other_side; |
| 5148 | } |
| 5149 | |
| 5150 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5151 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5152 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5153 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5154 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5155 | goto wait_other_side; |
| 5156 | } |
| 5157 | |
| 5158 | /* When we get here, it means that both the request and the |
| 5159 | * response have finished receiving. Depending on the connection |
| 5160 | * mode, we'll have to wait for the last bytes to leave in either |
| 5161 | * direction, and sometimes for a close to be effective. |
| 5162 | */ |
| 5163 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5164 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5165 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5166 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5167 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5168 | } |
| 5169 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5170 | /* Option forceclose is set, or either side wants to close, |
| 5171 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5172 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5173 | * once both states are CLOSED. |
| 5174 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5175 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5176 | channel_shutr_now(chn); |
| 5177 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5178 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5179 | } |
| 5180 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5181 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5182 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5183 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5184 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5185 | channel_auto_read(chn); |
| 5186 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5187 | chn->flags |= CF_NEVER_WAIT; |
| 5188 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5189 | } |
| 5190 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5191 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5192 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5193 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5194 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5195 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5196 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5197 | goto http_msg_closing; |
| 5198 | } |
| 5199 | else { |
| 5200 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5201 | goto http_msg_closed; |
| 5202 | } |
| 5203 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5204 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5208 | http_msg_closing: |
| 5209 | /* nothing else to forward, just waiting for the output buffer |
| 5210 | * to be empty and for the shutw_now to take effect. |
| 5211 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5212 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5213 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5214 | goto http_msg_closed; |
| 5215 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5216 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5217 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5218 | goto wait_other_side; |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5223 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5224 | /* see above in MSG_DONE why we only do this in these states */ |
| 5225 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5226 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5227 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5228 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5229 | goto wait_other_side; |
| 5230 | } |
| 5231 | |
| 5232 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5233 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | |
| 5237 | /* This function updates the response state machine according to the request |
| 5238 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5239 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5240 | * it is only used to find when a request/response couple is complete. Both |
| 5241 | * this function and its equivalent should loop until both return zero. It |
| 5242 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5243 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5244 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5245 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5246 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5247 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5248 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5249 | unsigned int old_state = txn->rsp.msg_state; |
| 5250 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5251 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5252 | return 0; |
| 5253 | |
| 5254 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5255 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5256 | * still monitor the server connection for a possible close |
| 5257 | * while the request is being uploaded, so we don't disable |
| 5258 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5259 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5260 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5261 | |
| 5262 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5263 | goto wait_other_side; |
| 5264 | |
| 5265 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5266 | /* The client seems to still be sending data, probably |
| 5267 | * because we got an error response during an upload. |
| 5268 | * We have the choice of either breaking the connection |
| 5269 | * or letting it pass through. Let's do the later. |
| 5270 | */ |
| 5271 | goto wait_other_side; |
| 5272 | } |
| 5273 | |
| 5274 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5275 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5276 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5277 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5278 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5279 | goto wait_other_side; |
| 5280 | } |
| 5281 | |
| 5282 | /* When we get here, it means that both the request and the |
| 5283 | * response have finished receiving. Depending on the connection |
| 5284 | * mode, we'll have to wait for the last bytes to leave in either |
| 5285 | * direction, and sometimes for a close to be effective. |
| 5286 | */ |
| 5287 | |
| 5288 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5289 | /* Server-close mode : shut read and wait for the request |
| 5290 | * side to close its output buffer. The caller will detect |
| 5291 | * when we're in DONE and the other is in CLOSED and will |
| 5292 | * catch that for the final cleanup. |
| 5293 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5294 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5295 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5296 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5297 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5298 | /* Option forceclose is set, or either side wants to close, |
| 5299 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5300 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5301 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5302 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5303 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5304 | channel_shutr_now(chn); |
| 5305 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5306 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5307 | } |
| 5308 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5309 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5310 | * need to forward remaining data. Keep-alive will need to monitor |
| 5311 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5312 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5313 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5314 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5315 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5316 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | } |
| 5318 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5319 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5320 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5321 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5322 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5323 | goto http_msg_closing; |
| 5324 | } |
| 5325 | else { |
| 5326 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5327 | goto http_msg_closed; |
| 5328 | } |
| 5329 | } |
| 5330 | goto wait_other_side; |
| 5331 | } |
| 5332 | |
| 5333 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5334 | http_msg_closing: |
| 5335 | /* nothing else to forward, just waiting for the output buffer |
| 5336 | * to be empty and for the shutw_now to take effect. |
| 5337 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5338 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5339 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5340 | goto http_msg_closed; |
| 5341 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5342 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5343 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5344 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5345 | if (objt_server(s->target)) |
| 5346 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5347 | goto wait_other_side; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5352 | http_msg_closed: |
| 5353 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5354 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5355 | channel_auto_close(chn); |
| 5356 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5357 | goto wait_other_side; |
| 5358 | } |
| 5359 | |
| 5360 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5361 | /* We force the response to leave immediately if we're waiting for the |
| 5362 | * other side, since there is no pending shutdown to push it out. |
| 5363 | */ |
| 5364 | if (!channel_is_empty(chn)) |
| 5365 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5366 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | |
| 5370 | /* Resync the request and response state machines. Return 1 if either state |
| 5371 | * changes. |
| 5372 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5373 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5374 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5375 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5376 | int old_req_state = txn->req.msg_state; |
| 5377 | int old_res_state = txn->rsp.msg_state; |
| 5378 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5379 | http_sync_req_state(s); |
| 5380 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5381 | if (!http_sync_res_state(s)) |
| 5382 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | if (!http_sync_req_state(s)) |
| 5384 | break; |
| 5385 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5386 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5387 | /* OK, both state machines agree on a compatible state. |
| 5388 | * There are a few cases we're interested in : |
| 5389 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5390 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5391 | * directions, so let's simply disable both analysers. |
| 5392 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5393 | * request. |
| 5394 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5395 | * with server-close mode means we've completed one request and we |
| 5396 | * must re-initialize the server connection. |
| 5397 | */ |
| 5398 | |
| 5399 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5400 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5401 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5402 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5403 | s->req.analysers = 0; |
| 5404 | channel_auto_close(&s->req); |
| 5405 | channel_auto_read(&s->req); |
| 5406 | s->res.analysers = 0; |
| 5407 | channel_auto_close(&s->res); |
| 5408 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5409 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5410 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5411 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5412 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5413 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5414 | s->res.analysers = 0; |
| 5415 | channel_auto_close(&s->res); |
| 5416 | channel_auto_read(&s->res); |
| 5417 | s->req.analysers = 0; |
| 5418 | channel_abort(&s->req); |
| 5419 | channel_auto_close(&s->req); |
| 5420 | channel_auto_read(&s->req); |
| 5421 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5422 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5423 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5424 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5425 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5426 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5427 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5428 | /* server-close/keep-alive: terminate this transaction, |
| 5429 | * possibly killing the server connection and reinitialize |
| 5430 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5431 | */ |
| 5432 | http_end_txn_clean_session(s); |
| 5433 | } |
| 5434 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5435 | return txn->req.msg_state != old_req_state || |
| 5436 | txn->rsp.msg_state != old_res_state; |
| 5437 | } |
| 5438 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5439 | /* This function is an analyser which forwards request body (including chunk |
| 5440 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5441 | * zero byte. The only situation where it must not be called is when we're in |
| 5442 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5443 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5444 | * 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] | 5445 | * 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] | 5446 | * 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] | 5447 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5448 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5449 | 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] | 5450 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5451 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5452 | struct http_txn *txn = s->txn; |
| 5453 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5454 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5455 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5456 | return 0; |
| 5457 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5458 | 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] | 5459 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5460 | /* Output closed while we were sending data. We must abort and |
| 5461 | * wake the other side up. |
| 5462 | */ |
| 5463 | msg->msg_state = HTTP_MSG_ERROR; |
| 5464 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5465 | return 1; |
| 5466 | } |
| 5467 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5468 | /* Note that we don't have to send 100-continue back because we don't |
| 5469 | * need the data to complete our job, and it's up to the server to |
| 5470 | * decide whether to return 100, 417 or anything else in return of |
| 5471 | * an "Expect: 100-continue" header. |
| 5472 | */ |
| 5473 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5474 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5475 | /* we have msg->sov which points to the first byte of message |
| 5476 | * body, and req->buf.p still points to the beginning of the |
| 5477 | * message. We forward the headers now, as we don't need them |
| 5478 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5479 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5480 | b_adv(req->buf, msg->sov); |
| 5481 | msg->next -= msg->sov; |
| 5482 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5483 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5484 | /* The previous analysers guarantee that the state is somewhere |
| 5485 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5486 | * msg->next are always correct. |
| 5487 | */ |
| 5488 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5489 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5490 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5491 | else |
| 5492 | msg->msg_state = HTTP_MSG_DATA; |
| 5493 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5494 | } |
| 5495 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5496 | /* Some post-connect processing might want us to refrain from starting to |
| 5497 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5498 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5499 | */ |
| 5500 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5501 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5502 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5503 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5504 | goto missing_data; |
| 5505 | } |
| 5506 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5507 | } |
| 5508 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5509 | /* in most states, we should abort in case of early close */ |
| 5510 | channel_auto_close(req); |
| 5511 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5512 | if (req->to_forward) { |
| 5513 | /* We can't process the buffer's contents yet */ |
| 5514 | req->flags |= CF_WAKE_WRITE; |
| 5515 | goto missing_data; |
| 5516 | } |
| 5517 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5518 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5519 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5520 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5521 | /* we may have some pending data starting at req->buf->p */ |
| 5522 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5523 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5524 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5525 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5526 | msg->next += msg->chunk_len; |
| 5527 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5528 | |
| 5529 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5530 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5531 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5532 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5533 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5534 | } |
| 5535 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5536 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5537 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5538 | * TRAILERS state. |
| 5539 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5540 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5541 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5542 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5543 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5544 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5545 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5546 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5547 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5548 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5549 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5550 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5551 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5552 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5553 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5554 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5555 | |
| 5556 | if (ret == 0) |
| 5557 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5558 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5559 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5560 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5561 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5562 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5563 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5564 | /* we're in MSG_CHUNK_SIZE now */ |
| 5565 | } |
| 5566 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5567 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5568 | |
| 5569 | if (ret == 0) |
| 5570 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5571 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5572 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5573 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5574 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5575 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5576 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5577 | /* we're in HTTP_MSG_DONE now */ |
| 5578 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5579 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5580 | int old_state = msg->msg_state; |
| 5581 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5582 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5583 | |
| 5584 | /* we may have some pending data starting at req->buf->p |
| 5585 | * such as last chunk of data or trailers. |
| 5586 | */ |
| 5587 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5588 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5589 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5590 | msg->next = 0; |
| 5591 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5592 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5593 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5594 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5595 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5596 | if (http_resync_states(s)) { |
| 5597 | /* some state changes occurred, maybe the analyser |
| 5598 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5599 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5600 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5601 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5602 | /* request errors are most likely due to |
| 5603 | * the server aborting the transfer. |
| 5604 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5605 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5606 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5607 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5608 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5609 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5610 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5611 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5612 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5613 | |
| 5614 | /* If "option abortonclose" is set on the backend, we |
| 5615 | * want to monitor the client's connection and forward |
| 5616 | * any shutdown notification to the server, which will |
| 5617 | * decide whether to close or to go on processing the |
| 5618 | * request. |
| 5619 | */ |
| 5620 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5621 | channel_auto_read(req); |
| 5622 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5623 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5624 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5625 | /* POST requests may require to read extra CRLF |
| 5626 | * sent by broken browsers and which could cause |
| 5627 | * an RST to be sent upon close on some systems |
| 5628 | * (eg: Linux). |
| 5629 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5630 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5631 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5632 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5633 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5634 | } |
| 5635 | } |
| 5636 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5637 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5638 | /* we may have some pending data starting at req->buf->p */ |
| 5639 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5640 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5641 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5642 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5643 | msg->next = 0; |
| 5644 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5645 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5646 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5647 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5648 | if (!(s->flags & SF_ERR_MASK)) |
| 5649 | s->flags |= SF_ERR_CLICL; |
| 5650 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5651 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5652 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5653 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5654 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5655 | } |
| 5656 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5657 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5658 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5659 | if (objt_server(s->target)) |
| 5660 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5661 | |
| 5662 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5663 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5664 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5665 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5666 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5667 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5668 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5669 | /* 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] | 5670 | * 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] | 5671 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5672 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5673 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5674 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5675 | /* 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] | 5676 | * 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] | 5677 | * 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] | 5678 | * modes are already handled by the stream sock layer. We must not do |
| 5679 | * this in content-length mode because it could present the MSG_MORE |
| 5680 | * flag with the last block of forwarded data, which would cause an |
| 5681 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5682 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5683 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5684 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5685 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5686 | return 0; |
| 5687 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5688 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5689 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5690 | if (sess->listener->counters) |
| 5691 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5692 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5693 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5694 | /* we may have some pending data starting at req->buf->p */ |
| 5695 | b_adv(req->buf, msg->next); |
| 5696 | msg->next = 0; |
| 5697 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5698 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5699 | if (txn->status) { |
| 5700 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5701 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5702 | } else { |
| 5703 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5704 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5705 | } |
| 5706 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5707 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5708 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5709 | if (!(s->flags & SF_ERR_MASK)) |
| 5710 | s->flags |= SF_ERR_PRXCOND; |
| 5711 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5712 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5713 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5714 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5715 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5716 | } |
| 5717 | return 0; |
| 5718 | |
| 5719 | aborted_xfer: |
| 5720 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5721 | if (txn->status) { |
| 5722 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5723 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5724 | } else { |
| 5725 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5726 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5727 | } |
| 5728 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5729 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5730 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5731 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5732 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5733 | if (objt_server(s->target)) |
| 5734 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5735 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5736 | if (!(s->flags & SF_ERR_MASK)) |
| 5737 | s->flags |= SF_ERR_SRVCL; |
| 5738 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5739 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5740 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5741 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5742 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5743 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5744 | return 0; |
| 5745 | } |
| 5746 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5747 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5748 | * processing can continue on next analysers, or zero if it either needs more |
| 5749 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5750 | * 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] | 5751 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5752 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5753 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5754 | 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] | 5755 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5756 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5757 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5758 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5759 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5760 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5761 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5762 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5763 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5764 | 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] | 5765 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5766 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5767 | rep, |
| 5768 | rep->rex, rep->wex, |
| 5769 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5770 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5771 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5772 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5773 | /* |
| 5774 | * Now parse the partial (or complete) lines. |
| 5775 | * We will check the response syntax, and also join multi-line |
| 5776 | * headers. An index of all the lines will be elaborated while |
| 5777 | * parsing. |
| 5778 | * |
| 5779 | * For the parsing, we use a 28 states FSM. |
| 5780 | * |
| 5781 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5782 | * rep->buf->p = beginning of response |
| 5783 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5784 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5785 | * msg->eol = end of current header or line (LF or CRLF) |
| 5786 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5787 | */ |
| 5788 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5789 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5790 | /* There's a protected area at the end of the buffer for rewriting |
| 5791 | * purposes. We don't want to start to parse the request if the |
| 5792 | * protected area is affected, because we may have to move processed |
| 5793 | * data later, which is much more complicated. |
| 5794 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5795 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5796 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5797 | /* some data has still not left the buffer, wake us once that's done */ |
| 5798 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5799 | goto abort_response; |
| 5800 | channel_dont_close(rep); |
| 5801 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5802 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5803 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5804 | } |
| 5805 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5806 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5807 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5808 | buffer_slow_realign(rep->buf); |
| 5809 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5810 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5811 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5812 | } |
| 5813 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5814 | /* 1: we might have to print this header in debug mode */ |
| 5815 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5816 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5817 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5818 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5819 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5820 | sol = rep->buf->p; |
| 5821 | 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] | 5822 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5823 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5824 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5825 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5826 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5827 | while (cur_idx) { |
| 5828 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5829 | debug_hdr("srvhdr", s, sol, eol); |
| 5830 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5831 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5832 | } |
| 5833 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5834 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5835 | /* |
| 5836 | * Now we quickly check if we have found a full valid response. |
| 5837 | * If not so, we check the FD and buffer states before leaving. |
| 5838 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5839 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5840 | * responses are checked first. |
| 5841 | * |
| 5842 | * Depending on whether the client is still there or not, we |
| 5843 | * may send an error response back or not. Note that normally |
| 5844 | * we should only check for HTTP status there, and check I/O |
| 5845 | * errors somewhere else. |
| 5846 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5847 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5848 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5849 | /* Invalid response */ |
| 5850 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5851 | /* we detected a parsing error. We want to archive this response |
| 5852 | * in the dedicated proxy area for later troubleshooting. |
| 5853 | */ |
| 5854 | hdr_response_bad: |
| 5855 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5856 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5857 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5858 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5859 | if (objt_server(s->target)) { |
| 5860 | objt_server(s->target)->counters.failed_resp++; |
| 5861 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5862 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5863 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5864 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5865 | rep->analysers = 0; |
| 5866 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5867 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5868 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5869 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5870 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5871 | if (!(s->flags & SF_ERR_MASK)) |
| 5872 | s->flags |= SF_ERR_PRXCOND; |
| 5873 | if (!(s->flags & SF_FINST_MASK)) |
| 5874 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5875 | |
| 5876 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5877 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5878 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5879 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5880 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5881 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5882 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5883 | goto hdr_response_bad; |
| 5884 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5886 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5887 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5888 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5889 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5890 | else if (txn->flags & TX_NOT_FIRST) |
| 5891 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5892 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5893 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5894 | if (objt_server(s->target)) { |
| 5895 | objt_server(s->target)->counters.failed_resp++; |
| 5896 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5897 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5898 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5899 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5900 | rep->analysers = 0; |
| 5901 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5902 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5903 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5904 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5905 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5906 | if (!(s->flags & SF_ERR_MASK)) |
| 5907 | s->flags |= SF_ERR_SRVCL; |
| 5908 | if (!(s->flags & SF_FINST_MASK)) |
| 5909 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5910 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5911 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5912 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5913 | /* read timeout : return a 504 to the client. */ |
| 5914 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5915 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5916 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5917 | else if (txn->flags & TX_NOT_FIRST) |
| 5918 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5919 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5920 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5921 | if (objt_server(s->target)) { |
| 5922 | objt_server(s->target)->counters.failed_resp++; |
| 5923 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5924 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5925 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5926 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5927 | rep->analysers = 0; |
| 5928 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5929 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5930 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5931 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5932 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5933 | if (!(s->flags & SF_ERR_MASK)) |
| 5934 | s->flags |= SF_ERR_SRVTO; |
| 5935 | if (!(s->flags & SF_FINST_MASK)) |
| 5936 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5937 | return 0; |
| 5938 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5939 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5940 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5941 | 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] | 5942 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5943 | s->be->be_counters.cli_aborts++; |
| 5944 | if (objt_server(s->target)) |
| 5945 | objt_server(s->target)->counters.cli_aborts++; |
| 5946 | |
| 5947 | rep->analysers = 0; |
| 5948 | channel_auto_close(rep); |
| 5949 | |
| 5950 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5951 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5952 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5953 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5954 | if (!(s->flags & SF_ERR_MASK)) |
| 5955 | s->flags |= SF_ERR_CLICL; |
| 5956 | if (!(s->flags & SF_FINST_MASK)) |
| 5957 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5958 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5959 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5960 | return 0; |
| 5961 | } |
| 5962 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5963 | /* 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] | 5964 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5965 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5966 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5967 | else if (txn->flags & TX_NOT_FIRST) |
| 5968 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5969 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5970 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5971 | if (objt_server(s->target)) { |
| 5972 | objt_server(s->target)->counters.failed_resp++; |
| 5973 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5974 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5975 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5976 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5977 | rep->analysers = 0; |
| 5978 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5979 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5980 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5981 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5982 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5983 | if (!(s->flags & SF_ERR_MASK)) |
| 5984 | s->flags |= SF_ERR_SRVCL; |
| 5985 | if (!(s->flags & SF_FINST_MASK)) |
| 5986 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5987 | return 0; |
| 5988 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5989 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5990 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5991 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5992 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5993 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5994 | else if (txn->flags & TX_NOT_FIRST) |
| 5995 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5996 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5997 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5998 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5999 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6000 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6001 | if (!(s->flags & SF_ERR_MASK)) |
| 6002 | s->flags |= SF_ERR_CLICL; |
| 6003 | if (!(s->flags & SF_FINST_MASK)) |
| 6004 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6005 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6006 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6007 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6008 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6009 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6010 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6011 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6012 | return 0; |
| 6013 | } |
| 6014 | |
| 6015 | /* More interesting part now : we know that we have a complete |
| 6016 | * response which at least looks like HTTP. We have an indicator |
| 6017 | * of each header's length, so we can parse them quickly. |
| 6018 | */ |
| 6019 | |
| 6020 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6021 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6022 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6023 | /* |
| 6024 | * 1: get the status code |
| 6025 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6026 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6027 | if (n < 1 || n > 5) |
| 6028 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6029 | /* when the client triggers a 4xx from the server, it's most often due |
| 6030 | * to a missing object or permission. These events should be tracked |
| 6031 | * because if they happen often, it may indicate a brute force or a |
| 6032 | * vulnerability scan. |
| 6033 | */ |
| 6034 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6035 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6036 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6037 | if (objt_server(s->target)) |
| 6038 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6039 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6040 | /* check if the response is HTTP/1.1 or above */ |
| 6041 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6042 | ((rep->buf->p[5] > '1') || |
| 6043 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6044 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6045 | |
| 6046 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6047 | 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] | 6048 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6049 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6050 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6051 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6052 | 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] | 6053 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6054 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6055 | * and 505 are triggered on demand by client request, so we must not |
| 6056 | * count them as server failures. |
| 6057 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6058 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6059 | 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] | 6060 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6061 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6062 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6063 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6064 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6065 | /* |
| 6066 | * 2: check for cacheability. |
| 6067 | */ |
| 6068 | |
| 6069 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6070 | case 100: |
| 6071 | /* |
| 6072 | * We may be facing a 100-continue response, in which case this |
| 6073 | * is not the right response, and we're waiting for the next one. |
| 6074 | * Let's allow this response to go to the client and wait for the |
| 6075 | * next one. |
| 6076 | */ |
| 6077 | hdr_idx_init(&txn->hdr_idx); |
| 6078 | msg->next -= channel_forward(rep, msg->next); |
| 6079 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6080 | txn->status = 0; |
| 6081 | s->logs.t_data = -1; /* was not a response yet */ |
| 6082 | goto next_one; |
| 6083 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6084 | case 200: |
| 6085 | case 203: |
| 6086 | case 206: |
| 6087 | case 300: |
| 6088 | case 301: |
| 6089 | case 410: |
| 6090 | /* RFC2616 @13.4: |
| 6091 | * "A response received with a status code of |
| 6092 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6093 | * by a cache (...) unless a cache-control |
| 6094 | * directive prohibits caching." |
| 6095 | * |
| 6096 | * RFC2616 @9.5: POST method : |
| 6097 | * "Responses to this method are not cacheable, |
| 6098 | * unless the response includes appropriate |
| 6099 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6100 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6101 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6102 | ((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] | 6103 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6104 | break; |
| 6105 | default: |
| 6106 | break; |
| 6107 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6108 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6109 | /* |
| 6110 | * 3: we may need to capture headers |
| 6111 | */ |
| 6112 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6113 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6114 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6115 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6116 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6117 | /* 4: determine the transfer-length. |
| 6118 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 6119 | * the presence of a message-body in a RESPONSE and its transfer length |
| 6120 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6121 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6122 | * All responses to the HEAD request method MUST NOT include a |
| 6123 | * message-body, even though the presence of entity-header fields |
| 6124 | * might lead one to believe they do. All 1xx (informational), 204 |
| 6125 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 6126 | * message-body. All other responses do include a message-body, |
| 6127 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6128 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6129 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 6130 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 6131 | * always terminated by the first empty line after the header fields, |
| 6132 | * regardless of the entity-header fields present in the message. |
| 6133 | * |
| 6134 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 6135 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 6136 | * transfer-length is defined by the use of this transfer-coding. |
| 6137 | * If a Transfer-Encoding header field is present and the "chunked" |
| 6138 | * transfer-coding is not present, the transfer-length is defined by |
| 6139 | * the sender closing the connection. |
| 6140 | * |
| 6141 | * 3. If a Content-Length header field is present, its decimal value in |
| 6142 | * OCTETs represents both the entity-length and the transfer-length. |
| 6143 | * If a message is received with both a Transfer-Encoding header |
| 6144 | * field and a Content-Length header field, the latter MUST be ignored. |
| 6145 | * |
| 6146 | * 4. If the message uses the media type "multipart/byteranges", and |
| 6147 | * the transfer-length is not otherwise specified, then this self- |
| 6148 | * delimiting media type defines the transfer-length. This media |
| 6149 | * type MUST NOT be used unless the sender knows that the recipient |
| 6150 | * can parse it; the presence in a request of a Range header with |
| 6151 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 6152 | * client can parse multipart/byteranges responses. |
| 6153 | * |
| 6154 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6155 | */ |
| 6156 | |
| 6157 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6158 | * 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] | 6159 | * 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] | 6160 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6161 | */ |
| 6162 | if (txn->meth == HTTP_METH_HEAD || |
| 6163 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6164 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6165 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6166 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6167 | goto skip_content_length; |
| 6168 | } |
| 6169 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6170 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6171 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6172 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6173 | 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] | 6174 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6175 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6176 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6177 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6178 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6179 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6180 | break; |
| 6181 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6182 | } |
| 6183 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame^] | 6184 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6185 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame^] | 6186 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 6187 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6188 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6189 | } |
| 6190 | else while (!use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6191 | 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] | 6192 | signed long long cl; |
| 6193 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6194 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6195 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6196 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6197 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6198 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6199 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6200 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6201 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6202 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6203 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6204 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6205 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6206 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6207 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6208 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6209 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6210 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6211 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6212 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6213 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6214 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6215 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6216 | } |
| 6217 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6218 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6219 | select_compression_response_header(s, rep->buf); |
| 6220 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6221 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6222 | /* Now we have to check if we need to modify the Connection header. |
| 6223 | * This is more difficult on the response than it is on the request, |
| 6224 | * because we can have two different HTTP versions and we don't know |
| 6225 | * how the client will interprete a response. For instance, let's say |
| 6226 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6227 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6228 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6229 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6230 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6231 | * indicating how a request MAY be understood by the client. In case |
| 6232 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6233 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6234 | * will fall back to explicit close. Note that we won't take risks with |
| 6235 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6236 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6237 | */ |
| 6238 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6239 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6240 | txn->status == 101)) { |
| 6241 | /* Either we've established an explicit tunnel, or we're |
| 6242 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6243 | * to understand the next protocols. We have to switch to tunnel |
| 6244 | * mode, so that we transfer the request and responses then let |
| 6245 | * this protocol pass unmodified. When we later implement specific |
| 6246 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6247 | * header which contains information about that protocol for |
| 6248 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6249 | */ |
| 6250 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6251 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6252 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6253 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6254 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6255 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6256 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6258 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6259 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6260 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6261 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6262 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6263 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6264 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6265 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6266 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6267 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6268 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6269 | /* now adjust header transformations depending on current state */ |
| 6270 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6271 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6272 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6273 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6274 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6275 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6276 | else { /* SCL / KAL */ |
| 6277 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6278 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6279 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6280 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6281 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6282 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6283 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6284 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6285 | /* Some keep-alive responses are converted to Server-close if |
| 6286 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6287 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6288 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6289 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6290 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6291 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6292 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6293 | } |
| 6294 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6295 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6296 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6297 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6298 | /* end of job, return OK */ |
| 6299 | rep->analysers &= ~an_bit; |
| 6300 | rep->analyse_exp = TICK_ETERNITY; |
| 6301 | channel_auto_close(rep); |
| 6302 | return 1; |
| 6303 | |
| 6304 | abort_keep_alive: |
| 6305 | /* A keep-alive request to the server failed on a network error. |
| 6306 | * The client is required to retry. We need to close without returning |
| 6307 | * any other information so that the client retries. |
| 6308 | */ |
| 6309 | txn->status = 0; |
| 6310 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6311 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6312 | channel_auto_close(rep); |
| 6313 | s->logs.logwait = 0; |
| 6314 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6315 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6316 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6317 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6318 | return 0; |
| 6319 | } |
| 6320 | |
| 6321 | /* This function performs all the processing enabled for the current response. |
| 6322 | * 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] | 6323 | * 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] | 6324 | * other functions. It works like process_request (see indications above). |
| 6325 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6326 | 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] | 6327 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6328 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6329 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6330 | struct http_msg *msg = &txn->rsp; |
| 6331 | struct proxy *cur_proxy; |
| 6332 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6333 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6334 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6335 | 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] | 6336 | now_ms, __FUNCTION__, |
| 6337 | s, |
| 6338 | rep, |
| 6339 | rep->rex, rep->wex, |
| 6340 | rep->flags, |
| 6341 | rep->buf->i, |
| 6342 | rep->analysers); |
| 6343 | |
| 6344 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6345 | return 0; |
| 6346 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6347 | /* The stats applet needs to adjust the Connection header but we don't |
| 6348 | * apply any filter there. |
| 6349 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6350 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6351 | rep->analysers &= ~an_bit; |
| 6352 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6353 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6354 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6355 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6356 | /* |
| 6357 | * We will have to evaluate the filters. |
| 6358 | * As opposed to version 1.2, now they will be evaluated in the |
| 6359 | * filters order and not in the header order. This means that |
| 6360 | * each filter has to be validated among all headers. |
| 6361 | * |
| 6362 | * Filters are tried with ->be first, then with ->fe if it is |
| 6363 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6364 | * |
| 6365 | * Maybe we are in resume condiion. In this case I choose the |
| 6366 | * "struct proxy" which contains the rule list matching the resume |
| 6367 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6368 | * the process with the first one. |
| 6369 | * |
| 6370 | * In fact, I check only correspondance betwwen the current list |
| 6371 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6372 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6373 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6374 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6375 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6376 | else |
| 6377 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6378 | while (1) { |
| 6379 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6380 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6381 | /* evaluate http-response rules */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6382 | if (ret == HTTP_RULE_RES_CONT) |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6383 | 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] | 6384 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6385 | /* we need to be called again. */ |
| 6386 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6387 | channel_dont_close(rep); |
| 6388 | return 0; |
| 6389 | } |
| 6390 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6391 | /* try headers filters */ |
| 6392 | if (rule_set->rsp_exp != NULL) { |
| 6393 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6394 | return_bad_resp: |
| 6395 | if (objt_server(s->target)) { |
| 6396 | objt_server(s->target)->counters.failed_resp++; |
| 6397 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6398 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6399 | s->be->be_counters.failed_resp++; |
| 6400 | return_srv_prx_502: |
| 6401 | rep->analysers = 0; |
| 6402 | txn->status = 502; |
| 6403 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6404 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6405 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6406 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6407 | if (!(s->flags & SF_ERR_MASK)) |
| 6408 | s->flags |= SF_ERR_PRXCOND; |
| 6409 | if (!(s->flags & SF_FINST_MASK)) |
| 6410 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6411 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6412 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6413 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6414 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6415 | /* has the response been denied ? */ |
| 6416 | if (txn->flags & TX_SVDENY) { |
| 6417 | if (objt_server(s->target)) |
| 6418 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6419 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6420 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6421 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6422 | if (sess->listener->counters) |
| 6423 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6424 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6425 | goto return_srv_prx_502; |
| 6426 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6427 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6428 | /* add response headers from the rule sets in the same order */ |
| 6429 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6430 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6431 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6432 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6433 | 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] | 6434 | ret = acl_pass(ret); |
| 6435 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6436 | ret = !ret; |
| 6437 | if (!ret) |
| 6438 | continue; |
| 6439 | } |
| 6440 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6441 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6442 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6443 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6444 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6445 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6446 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6447 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6448 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6449 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6450 | /* After this point, this anayzer can't return yield, so we can |
| 6451 | * remove the bit corresponding to this analyzer from the list. |
| 6452 | * |
| 6453 | * Note that the intermediate returns and goto found previously |
| 6454 | * reset the analyzers. |
| 6455 | */ |
| 6456 | rep->analysers &= ~an_bit; |
| 6457 | rep->analyse_exp = TICK_ETERNITY; |
| 6458 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6459 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6460 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6461 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6462 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6463 | /* |
| 6464 | * Now check for a server cookie. |
| 6465 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6466 | if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6467 | (s->be->options & PR_O_CHK_CACHE)) |
| 6468 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6469 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6470 | /* |
| 6471 | * Check for cache-control or pragma headers if required. |
| 6472 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6473 | 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] | 6474 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6475 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6476 | /* |
| 6477 | * Add server cookie in the response if needed |
| 6478 | */ |
| 6479 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6480 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6481 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6482 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6483 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6484 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6485 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6486 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6487 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6488 | /* the server is known, it's not the one the client requested, or the |
| 6489 | * cookie's last seen date needs to be refreshed. We have to |
| 6490 | * insert a set-cookie here, except if we want to insert only on POST |
| 6491 | * requests and this one isn't. Note that servers which don't have cookies |
| 6492 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6493 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6494 | if (!objt_server(s->target)->cookie) { |
| 6495 | chunk_printf(&trash, |
| 6496 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6497 | s->be->cookie_name); |
| 6498 | } |
| 6499 | else { |
| 6500 | 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] | 6501 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6502 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6503 | /* emit last_date, which is mandatory */ |
| 6504 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6505 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6506 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6507 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6508 | if (s->be->cookie_maxlife) { |
| 6509 | /* emit first_date, which is either the original one or |
| 6510 | * the current date. |
| 6511 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6512 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6513 | s30tob64(txn->cookie_first_date ? |
| 6514 | txn->cookie_first_date >> 2 : |
| 6515 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6516 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6517 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6518 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6519 | chunk_appendf(&trash, "; path=/"); |
| 6520 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6521 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6522 | if (s->be->cookie_domain) |
| 6523 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6524 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6525 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6526 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6527 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6528 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6529 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6530 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6531 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6532 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6533 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6534 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6535 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6536 | /* the server did not change, only the date was updated */ |
| 6537 | txn->flags |= TX_SCK_UPDATED; |
| 6538 | else |
| 6539 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6540 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6541 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6542 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6543 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6544 | * 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] | 6545 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6546 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6547 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6548 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6549 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6550 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6551 | "Cache-control: private", 22) < 0)) |
| 6552 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6553 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6554 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6555 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6556 | /* |
| 6557 | * Check if result will be cacheable with a cookie. |
| 6558 | * We'll block the response if security checks have caught |
| 6559 | * nasty things such as a cacheable cookie. |
| 6560 | */ |
| 6561 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6562 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6563 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6564 | /* we're in presence of a cacheable response containing |
| 6565 | * a set-cookie header. We'll block it as requested by |
| 6566 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6567 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6568 | if (objt_server(s->target)) |
| 6569 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6570 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6571 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6572 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6573 | if (sess->listener->counters) |
| 6574 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6575 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6576 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6577 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6578 | send_log(s->be, LOG_ALERT, |
| 6579 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6580 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6581 | goto return_srv_prx_502; |
| 6582 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6583 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6584 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6585 | /* |
| 6586 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6587 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6588 | * 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] | 6589 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6590 | * want to touch any 101 response either since it's switching to another |
| 6591 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6592 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6593 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6594 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6595 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6596 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6597 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6598 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6599 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6600 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6601 | /* we want a keep-alive response here. Keep-alive header |
| 6602 | * required if either side is not 1.1. |
| 6603 | */ |
| 6604 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6605 | want_flags |= TX_CON_KAL_SET; |
| 6606 | } |
| 6607 | else { |
| 6608 | /* we want a close response here. Close header required if |
| 6609 | * the server is 1.1, regardless of the client. |
| 6610 | */ |
| 6611 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6612 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6613 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6614 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6615 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6616 | http_change_connection_header(txn, msg, want_flags); |
| 6617 | } |
| 6618 | |
| 6619 | skip_header_mangling: |
| 6620 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6621 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6622 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6623 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6624 | /* if the user wants to log as soon as possible, without counting |
| 6625 | * bytes from the server, then this is the right moment. We have |
| 6626 | * to temporarily assign bytes_out to log what we currently have. |
| 6627 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6628 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6629 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6630 | s->logs.bytes_out = txn->rsp.eoh; |
| 6631 | s->do_log(s); |
| 6632 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6633 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6634 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6635 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6636 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6637 | /* This function is an analyser which forwards response body (including chunk |
| 6638 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6639 | * zero byte. The only situation where it must not be called is when we're in |
| 6640 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6641 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6642 | * 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] | 6643 | * 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] | 6644 | * |
| 6645 | * It is capable of compressing response data both in content-length mode and |
| 6646 | * in chunked mode. The state machines follows different flows depending on |
| 6647 | * whether content-length and chunked modes are used, since there are no |
| 6648 | * trailers in content-length : |
| 6649 | * |
| 6650 | * chk-mode cl-mode |
| 6651 | * ,----- BODY -----. |
| 6652 | * / \ |
| 6653 | * V size > 0 V chk-mode |
| 6654 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6655 | * | | size == 0 | last byte | |
| 6656 | * | v final crlf v inspected | |
| 6657 | * | TRAILERS -----------> DONE | |
| 6658 | * | | |
| 6659 | * `----------------------------------------------' |
| 6660 | * |
| 6661 | * Compression only happens in the DATA state, and must be flushed in final |
| 6662 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6663 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6664 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6665 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6666 | 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] | 6667 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6668 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6669 | struct http_txn *txn = s->txn; |
| 6670 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6671 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6672 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6673 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6674 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6675 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6676 | return 0; |
| 6677 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6678 | 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] | 6679 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6680 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6681 | /* Output closed while we were sending data. We must abort and |
| 6682 | * wake the other side up. |
| 6683 | */ |
| 6684 | msg->msg_state = HTTP_MSG_ERROR; |
| 6685 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6686 | return 1; |
| 6687 | } |
| 6688 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6689 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6690 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6691 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6692 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6693 | /* we have msg->sov which points to the first byte of message |
| 6694 | * body, and res->buf.p still points to the beginning of the |
| 6695 | * message. We forward the headers now, as we don't need them |
| 6696 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6697 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6698 | b_adv(res->buf, msg->sov); |
| 6699 | msg->next -= msg->sov; |
| 6700 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6701 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6702 | /* The previous analysers guarantee that the state is somewhere |
| 6703 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6704 | * msg->next are always correct. |
| 6705 | */ |
| 6706 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6707 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6708 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6709 | else |
| 6710 | msg->msg_state = HTTP_MSG_DATA; |
| 6711 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6712 | } |
| 6713 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6714 | if (res->to_forward) { |
| 6715 | /* We can't process the buffer's contents yet */ |
| 6716 | res->flags |= CF_WAKE_WRITE; |
| 6717 | goto missing_data; |
| 6718 | } |
| 6719 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6720 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6721 | /* We need a compression buffer in the DATA state to put the |
| 6722 | * output of compressed data, and in CRLF state to let the |
| 6723 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6724 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6725 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6726 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6727 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6728 | goto aborted_xfer; /* no memory */ |
| 6729 | } |
| 6730 | |
| 6731 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6732 | if (ret < 0) { |
| 6733 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6734 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6735 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6736 | compressing = 1; |
| 6737 | } |
| 6738 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6739 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6740 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6741 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6742 | /* we may have some pending data starting at res->buf->p */ |
| 6743 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6744 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6745 | if (ret < 0) |
| 6746 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6747 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6748 | if (msg->chunk_len) { |
| 6749 | /* input empty or output full */ |
| 6750 | if (res->buf->i > msg->next) |
| 6751 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6752 | goto missing_data; |
| 6753 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6754 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6755 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6756 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6757 | /* output full */ |
| 6758 | res->flags |= CF_WAKE_WRITE; |
| 6759 | goto missing_data; |
| 6760 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6761 | msg->next += msg->chunk_len; |
| 6762 | msg->chunk_len = 0; |
| 6763 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6764 | |
| 6765 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6766 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6767 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6768 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6769 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6770 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6771 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6772 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6773 | |
| 6774 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6775 | /* we want the CRLF after the data */ |
| 6776 | |
| 6777 | ret = http_skip_chunk_crlf(msg); |
| 6778 | if (ret == 0) |
| 6779 | goto missing_data; |
| 6780 | else if (ret < 0) { |
| 6781 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6782 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe); |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6783 | goto return_bad_res; |
| 6784 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6785 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6786 | |
| 6787 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6788 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6789 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6790 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6791 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6792 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6793 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6794 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6795 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6796 | else if (ret < 0) { |
| 6797 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6798 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6799 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6800 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6801 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6802 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6803 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6804 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6805 | if (unlikely(compressing)) { |
| 6806 | /* we need to flush output contents before syncing FSMs */ |
| 6807 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6808 | compressing = 0; |
| 6809 | } |
| 6810 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6811 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6812 | if (ret == 0) |
| 6813 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6814 | else if (ret < 0) { |
| 6815 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6816 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6817 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6818 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6819 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6820 | |
| 6821 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6822 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6823 | if (unlikely(compressing)) { |
| 6824 | /* we need to flush output contents before syncing FSMs */ |
| 6825 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6826 | compressing = 0; |
| 6827 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6828 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6829 | /* we may have some pending data starting at res->buf->p |
| 6830 | * such as a last chunk of data or trailers. |
| 6831 | */ |
| 6832 | b_adv(res->buf, msg->next); |
| 6833 | msg->next = 0; |
| 6834 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6835 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6836 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6837 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6838 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6839 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6840 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6841 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6842 | /* some state changes occurred, maybe the analyser |
| 6843 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6844 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6845 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6846 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6847 | /* response errors are most likely due to |
| 6848 | * the client aborting the transfer. |
| 6849 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6850 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6851 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6852 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6853 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6854 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6855 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6856 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6857 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6858 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6859 | } |
| 6860 | } |
| 6861 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6862 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6863 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6864 | if (unlikely(compressing)) { |
| 6865 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6866 | compressing = 0; |
| 6867 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6868 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6869 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6870 | b_adv(res->buf, msg->next); |
| 6871 | msg->next = 0; |
| 6872 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6873 | } |
| 6874 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6875 | if (res->flags & CF_SHUTW) |
| 6876 | goto aborted_xfer; |
| 6877 | |
| 6878 | /* stop waiting for data if the input is closed before the end. If the |
| 6879 | * client side was already closed, it means that the client has aborted, |
| 6880 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6881 | * server abort. |
| 6882 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6883 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6884 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6885 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6886 | if (!(s->flags & SF_ERR_MASK)) |
| 6887 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6888 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6889 | if (objt_server(s->target)) |
| 6890 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6891 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6892 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6893 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6894 | /* 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] | 6895 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6896 | goto return_bad_res; |
| 6897 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6898 | /* 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] | 6899 | * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6900 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6901 | * close. |
| 6902 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6903 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6904 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6905 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6906 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6907 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6908 | /* 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] | 6909 | * 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] | 6910 | * 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] | 6911 | * modes are already handled by the stream sock layer. We must not do |
| 6912 | * this in content-length mode because it could present the MSG_MORE |
| 6913 | * flag with the last block of forwarded data, which would cause an |
| 6914 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6915 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6916 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6917 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6918 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6919 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6920 | return 0; |
| 6921 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6922 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6923 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6924 | if (objt_server(s->target)) |
| 6925 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6926 | |
| 6927 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6928 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6929 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6930 | compressing = 0; |
| 6931 | } |
| 6932 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6933 | /* we may have some pending data starting at res->buf->p */ |
| 6934 | if (s->comp_algo == NULL) { |
| 6935 | b_adv(res->buf, msg->next); |
| 6936 | msg->next = 0; |
| 6937 | } |
| 6938 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6939 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6940 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6941 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6942 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6943 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6944 | if (objt_server(s->target)) |
| 6945 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6946 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6947 | if (!(s->flags & SF_ERR_MASK)) |
| 6948 | s->flags |= SF_ERR_PRXCOND; |
| 6949 | if (!(s->flags & SF_FINST_MASK)) |
| 6950 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6951 | return 0; |
| 6952 | |
| 6953 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6954 | if (unlikely(compressing)) { |
| 6955 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6956 | compressing = 0; |
| 6957 | } |
| 6958 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6959 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6960 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6961 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6962 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6963 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6964 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6965 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6966 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6967 | if (objt_server(s->target)) |
| 6968 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6969 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6970 | if (!(s->flags & SF_ERR_MASK)) |
| 6971 | s->flags |= SF_ERR_CLICL; |
| 6972 | if (!(s->flags & SF_FINST_MASK)) |
| 6973 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6974 | return 0; |
| 6975 | } |
| 6976 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6977 | /* Iterate the same filter through all request headers. |
| 6978 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6979 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6980 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6981 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6982 | 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] | 6983 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6984 | char *cur_ptr, *cur_end, *cur_next; |
| 6985 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6986 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6987 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6988 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6989 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6990 | last_hdr = 0; |
| 6991 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6992 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6993 | old_idx = 0; |
| 6994 | |
| 6995 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6996 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6997 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6998 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6999 | (exp->action == ACT_ALLOW || |
| 7000 | exp->action == ACT_DENY || |
| 7001 | exp->action == ACT_TARPIT)) |
| 7002 | return 0; |
| 7003 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7004 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7005 | if (!cur_idx) |
| 7006 | break; |
| 7007 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7008 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7009 | cur_ptr = cur_next; |
| 7010 | cur_end = cur_ptr + cur_hdr->len; |
| 7011 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7012 | |
| 7013 | /* Now we have one header between cur_ptr and cur_end, |
| 7014 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7015 | */ |
| 7016 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7017 | 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] | 7018 | switch (exp->action) { |
| 7019 | case ACT_SETBE: |
| 7020 | /* It is not possible to jump a second time. |
| 7021 | * FIXME: should we return an HTTP/500 here so that |
| 7022 | * the admin knows there's a problem ? |
| 7023 | */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7024 | if (s->be != strm_fe(s)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7025 | break; |
| 7026 | |
| 7027 | /* Swithing Proxy */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7028 | stream_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7029 | last_hdr = 1; |
| 7030 | break; |
| 7031 | |
| 7032 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7033 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7034 | last_hdr = 1; |
| 7035 | break; |
| 7036 | |
| 7037 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7038 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7039 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7040 | break; |
| 7041 | |
| 7042 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7043 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7044 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7045 | break; |
| 7046 | |
| 7047 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7048 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7049 | if (trash.len < 0) |
| 7050 | return -1; |
| 7051 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7052 | 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] | 7053 | /* FIXME: if the user adds a newline in the replacement, the |
| 7054 | * index will not be recalculated for now, and the new line |
| 7055 | * will not be counted as a new header. |
| 7056 | */ |
| 7057 | |
| 7058 | cur_end += delta; |
| 7059 | cur_next += delta; |
| 7060 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7061 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7062 | break; |
| 7063 | |
| 7064 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7065 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7066 | cur_next += delta; |
| 7067 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7068 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7069 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7070 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7071 | cur_hdr->len = 0; |
| 7072 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7073 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7074 | break; |
| 7075 | |
| 7076 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7077 | } |
| 7078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7079 | /* keep the link from this header to next one in case of later |
| 7080 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7081 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7082 | old_idx = cur_idx; |
| 7083 | } |
| 7084 | return 0; |
| 7085 | } |
| 7086 | |
| 7087 | |
| 7088 | /* Apply the filter to the request line. |
| 7089 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7090 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7091 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7092 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7093 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7094 | 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] | 7095 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7096 | char *cur_ptr, *cur_end; |
| 7097 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7098 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7099 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7100 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7101 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7102 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7103 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7104 | (exp->action == ACT_ALLOW || |
| 7105 | exp->action == ACT_DENY || |
| 7106 | exp->action == ACT_TARPIT)) |
| 7107 | return 0; |
| 7108 | else if (exp->action == ACT_REMOVE) |
| 7109 | return 0; |
| 7110 | |
| 7111 | done = 0; |
| 7112 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7113 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7114 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7115 | |
| 7116 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7117 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7118 | 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] | 7119 | switch (exp->action) { |
| 7120 | case ACT_SETBE: |
| 7121 | /* It is not possible to jump a second time. |
| 7122 | * FIXME: should we return an HTTP/500 here so that |
| 7123 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7124 | */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7125 | if (s->be != strm_fe(s)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7126 | break; |
| 7127 | |
| 7128 | /* Swithing Proxy */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7129 | stream_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7130 | done = 1; |
| 7131 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7132 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7133 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7134 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7135 | done = 1; |
| 7136 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7137 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7138 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7139 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7140 | done = 1; |
| 7141 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7142 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7143 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7144 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7145 | done = 1; |
| 7146 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7147 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7148 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7149 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7150 | if (trash.len < 0) |
| 7151 | return -1; |
| 7152 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7153 | 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] | 7154 | /* FIXME: if the user adds a newline in the replacement, the |
| 7155 | * index will not be recalculated for now, and the new line |
| 7156 | * will not be counted as a new header. |
| 7157 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7158 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7159 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7160 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7161 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7162 | HTTP_MSG_RQMETH, |
| 7163 | cur_ptr, cur_end + 1, |
| 7164 | NULL, NULL); |
| 7165 | if (unlikely(!cur_end)) |
| 7166 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7168 | /* we have a full request and we know that we have either a CR |
| 7169 | * or an LF at <ptr>. |
| 7170 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7171 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7172 | 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] | 7173 | /* there is no point trying this regex on headers */ |
| 7174 | return 1; |
| 7175 | } |
| 7176 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7177 | return done; |
| 7178 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7179 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7180 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7181 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7182 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7183 | * 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] | 7184 | * 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] | 7185 | * unparsable request. Since it can manage the switch to another backend, it |
| 7186 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7187 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7188 | 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] | 7189 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7190 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7191 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7192 | struct hdr_exp *exp; |
| 7193 | |
| 7194 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7195 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7196 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7197 | /* |
| 7198 | * The interleaving of transformations and verdicts |
| 7199 | * makes it difficult to decide to continue or stop |
| 7200 | * the evaluation. |
| 7201 | */ |
| 7202 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7203 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7204 | break; |
| 7205 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7206 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7207 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7208 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7209 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7210 | |
| 7211 | /* if this filter had a condition, evaluate it now and skip to |
| 7212 | * next filter if the condition does not match. |
| 7213 | */ |
| 7214 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7215 | 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] | 7216 | ret = acl_pass(ret); |
| 7217 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7218 | ret = !ret; |
| 7219 | |
| 7220 | if (!ret) |
| 7221 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7222 | } |
| 7223 | |
| 7224 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7225 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7226 | if (unlikely(ret < 0)) |
| 7227 | return -1; |
| 7228 | |
| 7229 | if (likely(ret == 0)) { |
| 7230 | /* The filter did not match the request, it can be |
| 7231 | * iterated through all headers. |
| 7232 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7233 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7234 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7235 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7236 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7237 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
| 7240 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7241 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7242 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7243 | * Try to retrieve the server associated to the appsession. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7244 | * If the server is found, it's assigned to the stream. |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7245 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7246 | void manage_client_side_appsession(struct stream *s, const char *buf, int len) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7247 | struct http_txn *txn = s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7248 | appsess *asession = NULL; |
| 7249 | char *sessid_temp = NULL; |
| 7250 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7251 | if (len > s->be->appsession_len) { |
| 7252 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7253 | } |
| 7254 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7255 | if (s->be->options2 & PR_O2_AS_REQL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7256 | /* request-learn option is enabled : store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7257 | if (txn->sessid != NULL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7258 | /* free previously allocated memory as we don't need the stream id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7259 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7260 | } |
| 7261 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7262 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7263 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7264 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7265 | return; |
| 7266 | } |
| 7267 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7268 | memcpy(txn->sessid, buf, len); |
| 7269 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7270 | } |
| 7271 | |
| 7272 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7273 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7274 | send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7275 | return; |
| 7276 | } |
| 7277 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7278 | memcpy(sessid_temp, buf, len); |
| 7279 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7280 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7281 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7282 | /* free previously allocated memory */ |
| 7283 | pool_free2(apools.sessid, sessid_temp); |
| 7284 | |
| 7285 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7286 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7287 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7288 | asession->request_count++; |
| 7289 | |
| 7290 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7291 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7292 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7293 | while (srv) { |
| 7294 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7295 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7296 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7297 | (s->flags & SF_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7298 | /* we found the server and it's usable */ |
| 7299 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7300 | 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] | 7301 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7302 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7303 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7304 | break; |
| 7305 | } else { |
| 7306 | txn->flags &= ~TX_CK_MASK; |
| 7307 | txn->flags |= TX_CK_DOWN; |
| 7308 | } |
| 7309 | } |
| 7310 | srv = srv->next; |
| 7311 | } |
| 7312 | } |
| 7313 | } |
| 7314 | } |
| 7315 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7316 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7317 | * same way as with headers above except that the semi-colon also ends a token. |
| 7318 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7319 | * return a valid result. |
| 7320 | */ |
| 7321 | char *find_cookie_value_end(char *s, const char *e) |
| 7322 | { |
| 7323 | int quoted, qdpair; |
| 7324 | |
| 7325 | quoted = qdpair = 0; |
| 7326 | for (; s < e; s++) { |
| 7327 | if (qdpair) qdpair = 0; |
| 7328 | else if (quoted) { |
| 7329 | if (*s == '\\') qdpair = 1; |
| 7330 | else if (*s == '"') quoted = 0; |
| 7331 | } |
| 7332 | else if (*s == '"') quoted = 1; |
| 7333 | else if (*s == ',' || *s == ';') return s; |
| 7334 | } |
| 7335 | return s; |
| 7336 | } |
| 7337 | |
| 7338 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7339 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7340 | * the first delimiter is updated if required. The function tries as much as |
| 7341 | * possible to respect the following principles : |
| 7342 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7343 | * colon, in which case <next> is simply removed |
| 7344 | * - set exactly one space character after the new first delimiter, unless |
| 7345 | * there are not enough characters in the block being moved to do so. |
| 7346 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7347 | * one. |
| 7348 | * |
| 7349 | * It is the caller's responsibility to ensure that : |
| 7350 | * - <from> points to a valid delimiter or the colon ; |
| 7351 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7352 | * - there are non-space chars before <from> ; |
| 7353 | * - there is a CR/LF at or after <next>. |
| 7354 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7355 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7356 | { |
| 7357 | char *prev = *from; |
| 7358 | |
| 7359 | if (*prev == ':') { |
| 7360 | /* We're removing the first value, preserve the colon and add a |
| 7361 | * space if possible. |
| 7362 | */ |
| 7363 | if (!http_is_crlf[(unsigned char)*next]) |
| 7364 | next++; |
| 7365 | prev++; |
| 7366 | if (prev < next) |
| 7367 | *prev++ = ' '; |
| 7368 | |
| 7369 | while (http_is_spht[(unsigned char)*next]) |
| 7370 | next++; |
| 7371 | } else { |
| 7372 | /* Remove useless spaces before the old delimiter. */ |
| 7373 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7374 | prev--; |
| 7375 | *from = prev; |
| 7376 | |
| 7377 | /* copy the delimiter and if possible a space if we're |
| 7378 | * not at the end of the line. |
| 7379 | */ |
| 7380 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7381 | *prev++ = *next++; |
| 7382 | if (prev + 1 < next) |
| 7383 | *prev++ = ' '; |
| 7384 | while (http_is_spht[(unsigned char)*next]) |
| 7385 | next++; |
| 7386 | } |
| 7387 | } |
| 7388 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7389 | } |
| 7390 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7391 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7392 | * 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] | 7393 | * desirable to call it only when needed. This code is quite complex because |
| 7394 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7395 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7396 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7397 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7398 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7399 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7400 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7401 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7402 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7403 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7404 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7405 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7406 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7407 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7408 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7409 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7410 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7411 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7412 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7413 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7414 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7415 | hdr_beg = hdr_next; |
| 7416 | hdr_end = hdr_beg + cur_hdr->len; |
| 7417 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7418 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7419 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7420 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7421 | * "Cookie:" headers. |
| 7422 | */ |
| 7423 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7424 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7425 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7426 | old_idx = cur_idx; |
| 7427 | continue; |
| 7428 | } |
| 7429 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7430 | del_from = NULL; /* nothing to be deleted */ |
| 7431 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7432 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7433 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7434 | * attributes whose name begin with a '$', and associate them with |
| 7435 | * the right cookie, if we want to delete this cookie. |
| 7436 | * So there are 3 cases for each cookie read : |
| 7437 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7438 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7439 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7440 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7441 | * "special" cookie. |
| 7442 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7443 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7444 | * *MUST* delete it. |
| 7445 | * |
| 7446 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7447 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7448 | * the RFC explicitly allows spaces before it, and not within the |
| 7449 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7450 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7451 | * after the equal sign too, resulting in some (rare) buggy |
| 7452 | * implementations trying to do that. So let's do what servers do. |
| 7453 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7454 | * allowed quoted strings in values, with any possible character |
| 7455 | * after a backslash, including control chars and delimitors, which |
| 7456 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7457 | * within values even without quotes. |
| 7458 | * |
| 7459 | * We have to keep multiple pointers in order to support cookie |
| 7460 | * removal at the beginning, middle or end of header without |
| 7461 | * corrupting the header. All of these headers are valid : |
| 7462 | * |
| 7463 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7464 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7465 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7466 | * | | | | | | | | | |
| 7467 | * | | | | | | | | hdr_end <--+ |
| 7468 | * | | | | | | | +--> next |
| 7469 | * | | | | | | +----> val_end |
| 7470 | * | | | | | +-----------> val_beg |
| 7471 | * | | | | +--------------> equal |
| 7472 | * | | | +----------------> att_end |
| 7473 | * | | +---------------------> att_beg |
| 7474 | * | +--------------------------> prev |
| 7475 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7476 | */ |
| 7477 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7478 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7479 | /* Iterate through all cookies on this line */ |
| 7480 | |
| 7481 | /* find att_beg */ |
| 7482 | att_beg = prev + 1; |
| 7483 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7484 | att_beg++; |
| 7485 | |
| 7486 | /* find att_end : this is the first character after the last non |
| 7487 | * space before the equal. It may be equal to hdr_end. |
| 7488 | */ |
| 7489 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7490 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7491 | while (equal < hdr_end) { |
| 7492 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7493 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7494 | if (http_is_spht[(unsigned char)*equal++]) |
| 7495 | continue; |
| 7496 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7497 | } |
| 7498 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7499 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7500 | * is between <att_beg> and <equal>, both may be identical. |
| 7501 | */ |
| 7502 | |
| 7503 | /* look for end of cookie if there is an equal sign */ |
| 7504 | if (equal < hdr_end && *equal == '=') { |
| 7505 | /* look for the beginning of the value */ |
| 7506 | val_beg = equal + 1; |
| 7507 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7508 | val_beg++; |
| 7509 | |
| 7510 | /* find the end of the value, respecting quotes */ |
| 7511 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7512 | |
| 7513 | /* make val_end point to the first white space or delimitor after the value */ |
| 7514 | val_end = next; |
| 7515 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7516 | val_end--; |
| 7517 | } else { |
| 7518 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7519 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7520 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7521 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7522 | * they will automatically be removed if a header before them is removed, |
| 7523 | * since they're supposed to be linked together. |
| 7524 | */ |
| 7525 | if (*att_beg == '$') |
| 7526 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7527 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7528 | /* Ignore cookies with no equal sign */ |
| 7529 | if (equal == next) { |
| 7530 | /* This is not our cookie, so we must preserve it. But if we already |
| 7531 | * scheduled another cookie for removal, we cannot remove the |
| 7532 | * complete header, but we can remove the previous block itself. |
| 7533 | */ |
| 7534 | preserve_hdr = 1; |
| 7535 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7536 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7537 | val_end += delta; |
| 7538 | next += delta; |
| 7539 | hdr_end += delta; |
| 7540 | hdr_next += delta; |
| 7541 | cur_hdr->len += delta; |
| 7542 | http_msg_move_end(&txn->req, delta); |
| 7543 | prev = del_from; |
| 7544 | del_from = NULL; |
| 7545 | } |
| 7546 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7547 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7548 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7549 | /* if there are spaces around the equal sign, we need to |
| 7550 | * strip them otherwise we'll get trouble for cookie captures, |
| 7551 | * or even for rewrites. Since this happens extremely rarely, |
| 7552 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7553 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7554 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7555 | int stripped_before = 0; |
| 7556 | int stripped_after = 0; |
| 7557 | |
| 7558 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7559 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7560 | equal += stripped_before; |
| 7561 | val_beg += stripped_before; |
| 7562 | } |
| 7563 | |
| 7564 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7565 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7566 | val_beg += stripped_after; |
| 7567 | stripped_before += stripped_after; |
| 7568 | } |
| 7569 | |
| 7570 | val_end += stripped_before; |
| 7571 | next += stripped_before; |
| 7572 | hdr_end += stripped_before; |
| 7573 | hdr_next += stripped_before; |
| 7574 | cur_hdr->len += stripped_before; |
| 7575 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7576 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7577 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7578 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7579 | /* First, let's see if we want to capture this cookie. We check |
| 7580 | * that we don't already have a client side cookie, because we |
| 7581 | * can only capture one. Also as an optimisation, we ignore |
| 7582 | * cookies shorter than the declared name. |
| 7583 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7584 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7585 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7586 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7587 | int log_len = val_end - att_beg; |
| 7588 | |
| 7589 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7590 | Alert("HTTP logging : out of memory.\n"); |
| 7591 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7592 | if (log_len > sess->fe->capture_len) |
| 7593 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7594 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7595 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7596 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7597 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7598 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7599 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7600 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7601 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7602 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7603 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7604 | * For cookies in prefix mode, the form is : |
| 7605 | * |
| 7606 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7607 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7608 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7609 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7610 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7611 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7612 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7613 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7614 | * have the server ID between val_beg and delim, and the original cookie between |
| 7615 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7616 | * |
| 7617 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7618 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7619 | * | || || | |+-> next |
| 7620 | * | || || | +--> val_end |
| 7621 | * | || || +---------> delim |
| 7622 | * | || |+------------> val_beg |
| 7623 | * | || +-------------> att_end = equal |
| 7624 | * | |+-----------------> att_beg |
| 7625 | * | +------------------> prev |
| 7626 | * +-------------------------> hdr_beg |
| 7627 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7628 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7629 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7630 | for (delim = val_beg; delim < val_end; delim++) |
| 7631 | if (*delim == COOKIE_DELIM) |
| 7632 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7633 | } else { |
| 7634 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7635 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7636 | /* Now check if the cookie contains a date field, which would |
| 7637 | * appear after a vertical bar ('|') just after the server name |
| 7638 | * and before the delimiter. |
| 7639 | */ |
| 7640 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7641 | if (vbar1) { |
| 7642 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7643 | * right is the last seen date. It is a base64 encoded |
| 7644 | * 30-bit value representing the UNIX date since the |
| 7645 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7646 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7647 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7648 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7649 | if (val_end - vbar1 >= 5) { |
| 7650 | val = b64tos30(vbar1); |
| 7651 | if (val > 0) |
| 7652 | txn->cookie_last_date = val << 2; |
| 7653 | } |
| 7654 | /* look for a second vertical bar */ |
| 7655 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7656 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7657 | val = b64tos30(vbar1 + 1); |
| 7658 | if (val > 0) |
| 7659 | txn->cookie_first_date = val << 2; |
| 7660 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7661 | } |
| 7662 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7663 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7664 | /* if the cookie has an expiration date and the proxy wants to check |
| 7665 | * it, then we do that now. We first check if the cookie is too old, |
| 7666 | * then only if it has expired. We detect strict overflow because the |
| 7667 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7668 | * in the future are ignored if their offset is beyond one day. This |
| 7669 | * allows an admin to fix timezone issues without expiring everyone |
| 7670 | * and at the same time avoids keeping unwanted side effects for too |
| 7671 | * long. |
| 7672 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7673 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7674 | (((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] | 7675 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7676 | txn->flags &= ~TX_CK_MASK; |
| 7677 | txn->flags |= TX_CK_OLD; |
| 7678 | delim = val_beg; // let's pretend we have not found the cookie |
| 7679 | txn->cookie_first_date = 0; |
| 7680 | txn->cookie_last_date = 0; |
| 7681 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7682 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7683 | (((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] | 7684 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7685 | txn->flags &= ~TX_CK_MASK; |
| 7686 | txn->flags |= TX_CK_EXPIRED; |
| 7687 | delim = val_beg; // let's pretend we have not found the cookie |
| 7688 | txn->cookie_first_date = 0; |
| 7689 | txn->cookie_last_date = 0; |
| 7690 | } |
| 7691 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7692 | /* Here, we'll look for the first running server which supports the cookie. |
| 7693 | * This allows to share a same cookie between several servers, for example |
| 7694 | * to dedicate backup servers to specific servers only. |
| 7695 | * However, to prevent clients from sticking to cookie-less backup server |
| 7696 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7697 | * empty cookies and mark them as invalid. |
| 7698 | * The same behaviour is applied when persistence must be ignored. |
| 7699 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7700 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7701 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7702 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7703 | while (srv) { |
| 7704 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7705 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7706 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7707 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7708 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7709 | /* we found the server and we can use it */ |
| 7710 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7711 | 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] | 7712 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7713 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7714 | break; |
| 7715 | } else { |
| 7716 | /* we found a server, but it's down, |
| 7717 | * mark it as such and go on in case |
| 7718 | * another one is available. |
| 7719 | */ |
| 7720 | txn->flags &= ~TX_CK_MASK; |
| 7721 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7722 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7723 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7724 | srv = srv->next; |
| 7725 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7726 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7727 | 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] | 7728 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7729 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7730 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7731 | txn->flags |= TX_CK_UNUSED; |
| 7732 | else |
| 7733 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7734 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7735 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7736 | /* depending on the cookie mode, we may have to either : |
| 7737 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7738 | * the server never sees it ; |
| 7739 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7740 | * application cookie so that it does not get accidentely removed later, |
| 7741 | * if we're in cookie prefix mode |
| 7742 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7743 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7744 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7745 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7746 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7747 | val_end += delta; |
| 7748 | next += delta; |
| 7749 | hdr_end += delta; |
| 7750 | hdr_next += delta; |
| 7751 | cur_hdr->len += delta; |
| 7752 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7753 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7754 | del_from = NULL; |
| 7755 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7756 | } |
| 7757 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7758 | (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] | 7759 | del_from = prev; |
| 7760 | } |
| 7761 | } else { |
| 7762 | /* This is not our cookie, so we must preserve it. But if we already |
| 7763 | * scheduled another cookie for removal, we cannot remove the |
| 7764 | * complete header, but we can remove the previous block itself. |
| 7765 | */ |
| 7766 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7767 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7768 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7769 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7770 | if (att_beg >= del_from) |
| 7771 | att_beg += delta; |
| 7772 | if (att_end >= del_from) |
| 7773 | att_end += delta; |
| 7774 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7775 | val_end += delta; |
| 7776 | next += delta; |
| 7777 | hdr_end += delta; |
| 7778 | hdr_next += delta; |
| 7779 | cur_hdr->len += delta; |
| 7780 | http_msg_move_end(&txn->req, delta); |
| 7781 | prev = del_from; |
| 7782 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7783 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7784 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7785 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7786 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7787 | if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7788 | int cmp_len, value_len; |
| 7789 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7790 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7791 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7792 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7793 | value_begin = att_beg + s->be->appsession_name_len; |
| 7794 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7795 | } else { |
| 7796 | cmp_len = att_end - att_beg; |
| 7797 | value_begin = val_beg; |
| 7798 | value_len = val_end - val_beg; |
| 7799 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7800 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7801 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7802 | if (cmp_len == s->be->appsession_name_len && |
| 7803 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7804 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7805 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7806 | } |
| 7807 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7808 | /* continue with next cookie on this header line */ |
| 7809 | att_beg = next; |
| 7810 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7811 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7812 | /* There are no more cookies on this line. |
| 7813 | * We may still have one (or several) marked for deletion at the |
| 7814 | * end of the line. We must do this now in two ways : |
| 7815 | * - if some cookies must be preserved, we only delete from the |
| 7816 | * mark to the end of line ; |
| 7817 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7818 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7819 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7820 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7821 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7822 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7823 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7824 | cur_hdr->len += delta; |
| 7825 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7826 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7827 | |
| 7828 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7829 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7830 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7831 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7832 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7833 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7834 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7835 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7836 | } |
| 7837 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7838 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7839 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7840 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7841 | } |
| 7842 | |
| 7843 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7844 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7845 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7846 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7847 | 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] | 7848 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7849 | char *cur_ptr, *cur_end, *cur_next; |
| 7850 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7851 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7852 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7853 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7854 | |
| 7855 | last_hdr = 0; |
| 7856 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7857 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7858 | old_idx = 0; |
| 7859 | |
| 7860 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7861 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7862 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7863 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7864 | (exp->action == ACT_ALLOW || |
| 7865 | exp->action == ACT_DENY)) |
| 7866 | return 0; |
| 7867 | |
| 7868 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7869 | if (!cur_idx) |
| 7870 | break; |
| 7871 | |
| 7872 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7873 | cur_ptr = cur_next; |
| 7874 | cur_end = cur_ptr + cur_hdr->len; |
| 7875 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7876 | |
| 7877 | /* Now we have one header between cur_ptr and cur_end, |
| 7878 | * and the next header starts at cur_next. |
| 7879 | */ |
| 7880 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7881 | 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] | 7882 | switch (exp->action) { |
| 7883 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7884 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7885 | last_hdr = 1; |
| 7886 | break; |
| 7887 | |
| 7888 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7889 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7890 | last_hdr = 1; |
| 7891 | break; |
| 7892 | |
| 7893 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7894 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7895 | if (trash.len < 0) |
| 7896 | return -1; |
| 7897 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7898 | 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] | 7899 | /* FIXME: if the user adds a newline in the replacement, the |
| 7900 | * index will not be recalculated for now, and the new line |
| 7901 | * will not be counted as a new header. |
| 7902 | */ |
| 7903 | |
| 7904 | cur_end += delta; |
| 7905 | cur_next += delta; |
| 7906 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7907 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7908 | break; |
| 7909 | |
| 7910 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7911 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7912 | cur_next += delta; |
| 7913 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7914 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7915 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7916 | txn->hdr_idx.used--; |
| 7917 | cur_hdr->len = 0; |
| 7918 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7919 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7920 | break; |
| 7921 | |
| 7922 | } |
| 7923 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7924 | |
| 7925 | /* keep the link from this header to next one in case of later |
| 7926 | * removal of next header. |
| 7927 | */ |
| 7928 | old_idx = cur_idx; |
| 7929 | } |
| 7930 | return 0; |
| 7931 | } |
| 7932 | |
| 7933 | |
| 7934 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7935 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7936 | * or -1 if a replacement resulted in an invalid status line. |
| 7937 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7938 | 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] | 7939 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7940 | char *cur_ptr, *cur_end; |
| 7941 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7942 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7943 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7944 | |
| 7945 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7946 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7947 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7948 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7949 | (exp->action == ACT_ALLOW || |
| 7950 | exp->action == ACT_DENY)) |
| 7951 | return 0; |
| 7952 | else if (exp->action == ACT_REMOVE) |
| 7953 | return 0; |
| 7954 | |
| 7955 | done = 0; |
| 7956 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7957 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7958 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7959 | |
| 7960 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7961 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7962 | 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] | 7963 | switch (exp->action) { |
| 7964 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7965 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7966 | done = 1; |
| 7967 | break; |
| 7968 | |
| 7969 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7970 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7971 | done = 1; |
| 7972 | break; |
| 7973 | |
| 7974 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7975 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7976 | if (trash.len < 0) |
| 7977 | return -1; |
| 7978 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7979 | 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] | 7980 | /* FIXME: if the user adds a newline in the replacement, the |
| 7981 | * index will not be recalculated for now, and the new line |
| 7982 | * will not be counted as a new header. |
| 7983 | */ |
| 7984 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7985 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7986 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7987 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7988 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7989 | cur_ptr, cur_end + 1, |
| 7990 | NULL, NULL); |
| 7991 | if (unlikely(!cur_end)) |
| 7992 | return -1; |
| 7993 | |
| 7994 | /* we have a full respnse and we know that we have either a CR |
| 7995 | * or an LF at <ptr>. |
| 7996 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7997 | 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] | 7998 | 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] | 7999 | /* there is no point trying this regex on headers */ |
| 8000 | return 1; |
| 8001 | } |
| 8002 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8003 | return done; |
| 8004 | } |
| 8005 | |
| 8006 | |
| 8007 | |
| 8008 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8009 | * 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] | 8010 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8011 | * unparsable response. |
| 8012 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8013 | 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] | 8014 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8015 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8016 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8017 | struct hdr_exp *exp; |
| 8018 | |
| 8019 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8020 | int ret; |
| 8021 | |
| 8022 | /* |
| 8023 | * The interleaving of transformations and verdicts |
| 8024 | * makes it difficult to decide to continue or stop |
| 8025 | * the evaluation. |
| 8026 | */ |
| 8027 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8028 | if (txn->flags & TX_SVDENY) |
| 8029 | break; |
| 8030 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8031 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8032 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8033 | exp->action == ACT_PASS)) { |
| 8034 | exp = exp->next; |
| 8035 | continue; |
| 8036 | } |
| 8037 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8038 | /* if this filter had a condition, evaluate it now and skip to |
| 8039 | * next filter if the condition does not match. |
| 8040 | */ |
| 8041 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8042 | 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] | 8043 | ret = acl_pass(ret); |
| 8044 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8045 | ret = !ret; |
| 8046 | if (!ret) |
| 8047 | continue; |
| 8048 | } |
| 8049 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8050 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8051 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8052 | if (unlikely(ret < 0)) |
| 8053 | return -1; |
| 8054 | |
| 8055 | if (likely(ret == 0)) { |
| 8056 | /* The filter did not match the response, it can be |
| 8057 | * iterated through all headers. |
| 8058 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8059 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8060 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8061 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8062 | } |
| 8063 | return 0; |
| 8064 | } |
| 8065 | |
| 8066 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8067 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8068 | * 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] | 8069 | * desirable to call it only when needed. This function is also used when we |
| 8070 | * 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] | 8071 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8072 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8073 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8074 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8075 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8076 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8077 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8078 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8079 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8080 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8081 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8082 | /* Iterate through the headers. |
| 8083 | * we start with the start line. |
| 8084 | */ |
| 8085 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8086 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8087 | |
| 8088 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8089 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8090 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8091 | |
| 8092 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8093 | hdr_beg = hdr_next; |
| 8094 | hdr_end = hdr_beg + cur_hdr->len; |
| 8095 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8096 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8097 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8098 | * next header starts at hdr_next. We're only interested in |
| 8099 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8100 | */ |
| 8101 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8102 | is_cookie2 = 0; |
| 8103 | prev = hdr_beg + 10; |
| 8104 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8105 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8106 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8107 | if (!val) { |
| 8108 | old_idx = cur_idx; |
| 8109 | continue; |
| 8110 | } |
| 8111 | is_cookie2 = 1; |
| 8112 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8113 | } |
| 8114 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8115 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8116 | * <prev> points to the colon. |
| 8117 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8118 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8119 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8120 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8121 | * check-cache is enabled) and we are not interested in checking |
| 8122 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8123 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8124 | if (s->be->cookie_name == NULL && |
| 8125 | s->be->appsession_name == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8126 | sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8127 | return; |
| 8128 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8129 | /* OK so now we know we have to process this response cookie. |
| 8130 | * The format of the Set-Cookie header is slightly different |
| 8131 | * from the format of the Cookie header in that it does not |
| 8132 | * support the comma as a cookie delimiter (thus the header |
| 8133 | * cannot be folded) because the Expires attribute described in |
| 8134 | * the original Netscape's spec may contain an unquoted date |
| 8135 | * with a comma inside. We have to live with this because |
| 8136 | * many browsers don't support Max-Age and some browsers don't |
| 8137 | * support quoted strings. However the Set-Cookie2 header is |
| 8138 | * clean. |
| 8139 | * |
| 8140 | * We have to keep multiple pointers in order to support cookie |
| 8141 | * removal at the beginning, middle or end of header without |
| 8142 | * corrupting the header (in case of set-cookie2). A special |
| 8143 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8144 | * fields after the first semi-colon. The <next> pointer points |
| 8145 | * either to the end of line (set-cookie) or next unquoted comma |
| 8146 | * (set-cookie2). All of these headers are valid : |
| 8147 | * |
| 8148 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8149 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8150 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8151 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8152 | * | | | | | | | | | | |
| 8153 | * | | | | | | | | +-> next hdr_end <--+ |
| 8154 | * | | | | | | | +------------> scav |
| 8155 | * | | | | | | +--------------> val_end |
| 8156 | * | | | | | +--------------------> val_beg |
| 8157 | * | | | | +----------------------> equal |
| 8158 | * | | | +------------------------> att_end |
| 8159 | * | | +----------------------------> att_beg |
| 8160 | * | +------------------------------> prev |
| 8161 | * +-----------------------------------------> hdr_beg |
| 8162 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8163 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8164 | for (; prev < hdr_end; prev = next) { |
| 8165 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8166 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8167 | /* find att_beg */ |
| 8168 | att_beg = prev + 1; |
| 8169 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8170 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8171 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8172 | /* find att_end : this is the first character after the last non |
| 8173 | * space before the equal. It may be equal to hdr_end. |
| 8174 | */ |
| 8175 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8176 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8177 | while (equal < hdr_end) { |
| 8178 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8179 | break; |
| 8180 | if (http_is_spht[(unsigned char)*equal++]) |
| 8181 | continue; |
| 8182 | att_end = equal; |
| 8183 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8184 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8185 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8186 | * is between <att_beg> and <equal>, both may be identical. |
| 8187 | */ |
| 8188 | |
| 8189 | /* look for end of cookie if there is an equal sign */ |
| 8190 | if (equal < hdr_end && *equal == '=') { |
| 8191 | /* look for the beginning of the value */ |
| 8192 | val_beg = equal + 1; |
| 8193 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8194 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8195 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8196 | /* find the end of the value, respecting quotes */ |
| 8197 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8198 | |
| 8199 | /* make val_end point to the first white space or delimitor after the value */ |
| 8200 | val_end = next; |
| 8201 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8202 | val_end--; |
| 8203 | } else { |
| 8204 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8205 | val_beg = val_end = next = equal; |
| 8206 | } |
| 8207 | |
| 8208 | if (next < hdr_end) { |
| 8209 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8210 | * a colon or semi-colon before the end. So skip all attr-value |
| 8211 | * pairs and look for the next comma. For Set-Cookie, since |
| 8212 | * commas are permitted in values, skip to the end. |
| 8213 | */ |
| 8214 | if (is_cookie2) |
| 8215 | next = find_hdr_value_end(next, hdr_end); |
| 8216 | else |
| 8217 | next = hdr_end; |
| 8218 | } |
| 8219 | |
| 8220 | /* Now everything is as on the diagram above */ |
| 8221 | |
| 8222 | /* Ignore cookies with no equal sign */ |
| 8223 | if (equal == val_end) |
| 8224 | continue; |
| 8225 | |
| 8226 | /* If there are spaces around the equal sign, we need to |
| 8227 | * strip them otherwise we'll get trouble for cookie captures, |
| 8228 | * or even for rewrites. Since this happens extremely rarely, |
| 8229 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8230 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8231 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8232 | int stripped_before = 0; |
| 8233 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8234 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8235 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8236 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8237 | equal += stripped_before; |
| 8238 | val_beg += stripped_before; |
| 8239 | } |
| 8240 | |
| 8241 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8242 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8243 | val_beg += stripped_after; |
| 8244 | stripped_before += stripped_after; |
| 8245 | } |
| 8246 | |
| 8247 | val_end += stripped_before; |
| 8248 | next += stripped_before; |
| 8249 | hdr_end += stripped_before; |
| 8250 | hdr_next += stripped_before; |
| 8251 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8252 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8253 | } |
| 8254 | |
| 8255 | /* First, let's see if we want to capture this cookie. We check |
| 8256 | * that we don't already have a server side cookie, because we |
| 8257 | * can only capture one. Also as an optimisation, we ignore |
| 8258 | * cookies shorter than the declared name. |
| 8259 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8260 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8261 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8262 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8263 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8264 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8265 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8266 | Alert("HTTP logging : out of memory.\n"); |
| 8267 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8268 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8269 | if (log_len > sess->fe->capture_len) |
| 8270 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8271 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8272 | txn->srv_cookie[log_len] = 0; |
| 8273 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8274 | } |
| 8275 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8276 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8277 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8278 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8279 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8280 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8281 | /* assume passive cookie by default */ |
| 8282 | txn->flags &= ~TX_SCK_MASK; |
| 8283 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8284 | |
| 8285 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8286 | * this occurrence because we'll insert another one later. |
| 8287 | * 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] | 8288 | * a direct access. |
| 8289 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8290 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8291 | /* The "preserve" flag was set, we don't want to touch the |
| 8292 | * server's cookie. |
| 8293 | */ |
| 8294 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8295 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8296 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8297 | /* this cookie must be deleted */ |
| 8298 | if (*prev == ':' && next == hdr_end) { |
| 8299 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8300 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8301 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8302 | txn->hdr_idx.used--; |
| 8303 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8304 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8305 | hdr_next += delta; |
| 8306 | http_msg_move_end(&txn->rsp, delta); |
| 8307 | /* note: while both invalid now, <next> and <hdr_end> |
| 8308 | * are still equal, so the for() will stop as expected. |
| 8309 | */ |
| 8310 | } else { |
| 8311 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8312 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8313 | next = prev; |
| 8314 | hdr_end += delta; |
| 8315 | hdr_next += delta; |
| 8316 | cur_hdr->len += delta; |
| 8317 | http_msg_move_end(&txn->rsp, delta); |
| 8318 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8319 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8320 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8321 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8322 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8323 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8324 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8325 | * with this server since we know it. |
| 8326 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8327 | 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] | 8328 | next += delta; |
| 8329 | hdr_end += delta; |
| 8330 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8331 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8332 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8333 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8334 | txn->flags &= ~TX_SCK_MASK; |
| 8335 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8336 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8337 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8338 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8339 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8340 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8341 | 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] | 8342 | next += delta; |
| 8343 | hdr_end += delta; |
| 8344 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8345 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8346 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8347 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8348 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8349 | txn->flags &= ~TX_SCK_MASK; |
| 8350 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8351 | } |
| 8352 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8353 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8354 | else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8355 | int cmp_len, value_len; |
| 8356 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8357 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8358 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8359 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8360 | value_begin = att_beg + s->be->appsession_name_len; |
| 8361 | value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8362 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8363 | cmp_len = att_end - att_beg; |
| 8364 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8365 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8366 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8367 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8368 | if ((cmp_len == s->be->appsession_name_len) && |
| 8369 | (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8370 | /* free a possibly previously allocated memory */ |
| 8371 | pool_free2(apools.sessid, txn->sessid); |
| 8372 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8373 | /* Store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8374 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8375 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8376 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8377 | return; |
| 8378 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8379 | memcpy(txn->sessid, value_begin, value_len); |
| 8380 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8381 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8382 | } |
| 8383 | /* that's done for this cookie, check the next one on the same |
| 8384 | * line when next != hdr_end (only if is_cookie2). |
| 8385 | */ |
| 8386 | } |
| 8387 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8388 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8389 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8390 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8391 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8392 | appsess *asession = NULL; |
| 8393 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8394 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8395 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8396 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8397 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8398 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8399 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8400 | return; |
| 8401 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8402 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8403 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8404 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8405 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8406 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8407 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8408 | return; |
| 8409 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8410 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8411 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8412 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8413 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8414 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8415 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8416 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8417 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8418 | return; |
| 8419 | } |
| 8420 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8421 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8422 | |
| 8423 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8424 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8425 | } |
| 8426 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8427 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8428 | asession->request_count++; |
| 8429 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8430 | } |
| 8431 | |
| 8432 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8433 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8434 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8435 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8436 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8437 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8438 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8439 | char *p1, *p2; |
| 8440 | |
| 8441 | char *cur_ptr, *cur_end, *cur_next; |
| 8442 | int cur_idx; |
| 8443 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8444 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8445 | return; |
| 8446 | |
| 8447 | /* Iterate through the headers. |
| 8448 | * we start with the start line. |
| 8449 | */ |
| 8450 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8451 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8452 | |
| 8453 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8454 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8455 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8456 | |
| 8457 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8458 | cur_ptr = cur_next; |
| 8459 | cur_end = cur_ptr + cur_hdr->len; |
| 8460 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8461 | |
| 8462 | /* We have one full header between cur_ptr and cur_end, and the |
| 8463 | * next header starts at cur_next. We're only interested in |
| 8464 | * "Cookie:" headers. |
| 8465 | */ |
| 8466 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8467 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8468 | if (val) { |
| 8469 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8470 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8471 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8472 | return; |
| 8473 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8474 | } |
| 8475 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8476 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8477 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8478 | continue; |
| 8479 | |
| 8480 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8481 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8482 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8483 | |
| 8484 | if (p1 >= cur_end) /* no more info */ |
| 8485 | continue; |
| 8486 | |
| 8487 | /* p1 is at the beginning of the value */ |
| 8488 | p2 = p1; |
| 8489 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8490 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8491 | p2++; |
| 8492 | |
| 8493 | /* we have a complete value between p1 and p2 */ |
| 8494 | if (p2 < cur_end && *p2 == '=') { |
| 8495 | /* we have something of the form no-cache="set-cookie" */ |
| 8496 | if ((cur_end - p1 >= 21) && |
| 8497 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8498 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8499 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8500 | continue; |
| 8501 | } |
| 8502 | |
| 8503 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8504 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8505 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8506 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8507 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8508 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8509 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8510 | return; |
| 8511 | } |
| 8512 | |
| 8513 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8514 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8515 | continue; |
| 8516 | } |
| 8517 | } |
| 8518 | } |
| 8519 | |
| 8520 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8521 | /* |
| 8522 | * Try to retrieve a known appsession in the URI, then the associated server. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8523 | * If the server is found, it's assigned to the stream. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8524 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8525 | void get_srv_from_appsession(struct stream *s, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8526 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8527 | char *end_params, *first_param, *cur_param, *next_param; |
| 8528 | char separator; |
| 8529 | int value_len; |
| 8530 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8531 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8532 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8533 | if (s->be->appsession_name == NULL || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8534 | (s->txn->meth != HTTP_METH_GET && s->txn->meth != HTTP_METH_POST && s->txn->meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8535 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8536 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8537 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8538 | first_param = NULL; |
| 8539 | switch (mode) { |
| 8540 | case PR_O2_AS_M_PP: |
| 8541 | first_param = memchr(begin, ';', len); |
| 8542 | break; |
| 8543 | case PR_O2_AS_M_QS: |
| 8544 | first_param = memchr(begin, '?', len); |
| 8545 | break; |
| 8546 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8547 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8548 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8549 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8550 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8551 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8552 | switch (mode) { |
| 8553 | case PR_O2_AS_M_PP: |
| 8554 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8555 | end_params = (char *) begin + len; |
| 8556 | } |
| 8557 | separator = ';'; |
| 8558 | break; |
| 8559 | case PR_O2_AS_M_QS: |
| 8560 | end_params = (char *) begin + len; |
| 8561 | separator = '&'; |
| 8562 | break; |
| 8563 | default: |
| 8564 | /* unknown mode, shouldn't happen */ |
| 8565 | return; |
| 8566 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8567 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8568 | cur_param = next_param = end_params; |
| 8569 | while (cur_param > first_param) { |
| 8570 | cur_param--; |
| 8571 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8572 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8573 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8574 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8575 | (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8576 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8577 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8578 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8579 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8580 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8581 | } |
| 8582 | break; |
| 8583 | } |
| 8584 | next_param = cur_param; |
| 8585 | } |
| 8586 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8587 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8588 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8589 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8590 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8591 | } |
| 8592 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8593 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8594 | * 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] | 8595 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8596 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8597 | * 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] | 8598 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8599 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8600 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8601 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8602 | 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] | 8603 | { |
| 8604 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8605 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8606 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8607 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8608 | if (!uri_auth) |
| 8609 | return 0; |
| 8610 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8611 | 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] | 8612 | return 0; |
| 8613 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8614 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8615 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8616 | return 0; |
| 8617 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8618 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8619 | return 0; |
| 8620 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8621 | return 1; |
| 8622 | } |
| 8623 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8624 | /* |
| 8625 | * 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] | 8626 | * By default it tries to report the error position as msg->err_pos. However if |
| 8627 | * this one is not set, it will then report msg->next, which is the last known |
| 8628 | * 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] | 8629 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8630 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8631 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8632 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8633 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8634 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8635 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8636 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8637 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8638 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8639 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8640 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8641 | len1 = MIN(len1, es->len); |
| 8642 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8643 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8644 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8645 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8646 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8647 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8648 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8649 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8650 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8651 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8652 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8653 | es->when = date; // user-visible date |
| 8654 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8655 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8656 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8657 | if (objt_conn(sess->origin)) |
| 8658 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8659 | else |
| 8660 | memset(&es->src, 0, sizeof(es->src)); |
| 8661 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8662 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8663 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8664 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8665 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8666 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8667 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8668 | es->b_out = chn->buf->o; |
| 8669 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8670 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8671 | es->m_clen = msg->chunk_len; |
| 8672 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8673 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8674 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8675 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8676 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8677 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8678 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8679 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8680 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8681 | * 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] | 8682 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8683 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8684 | * 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] | 8685 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8686 | 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] | 8687 | struct hdr_idx *idx, int occ, |
| 8688 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8689 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8690 | struct hdr_ctx local_ctx; |
| 8691 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8692 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8693 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8694 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8695 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8696 | if (!ctx) { |
| 8697 | local_ctx.idx = 0; |
| 8698 | ctx = &local_ctx; |
| 8699 | } |
| 8700 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8701 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8702 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8703 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8704 | occ--; |
| 8705 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8706 | *vptr = ctx->line + ctx->val; |
| 8707 | *vlen = ctx->vlen; |
| 8708 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8709 | } |
| 8710 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8711 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8712 | } |
| 8713 | |
| 8714 | /* negative occurrence, we scan all the list then walk back */ |
| 8715 | if (-occ > MAX_HDR_HISTORY) |
| 8716 | return 0; |
| 8717 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8718 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8719 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8720 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8721 | len_hist[hist_ptr] = ctx->vlen; |
| 8722 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8723 | hist_ptr = 0; |
| 8724 | found++; |
| 8725 | } |
| 8726 | if (-occ > found) |
| 8727 | return 0; |
| 8728 | /* 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] | 8729 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8730 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8731 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8732 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8733 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8734 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8735 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8736 | *vptr = ptr_hist[hist_ptr]; |
| 8737 | *vlen = len_hist[hist_ptr]; |
| 8738 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8739 | } |
| 8740 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8741 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8742 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8743 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8744 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8745 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8746 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8747 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8748 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8749 | * line header values and does not stop at commas. |
| 8750 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8751 | */ |
| 8752 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8753 | struct hdr_idx *idx, int occ, |
| 8754 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8755 | { |
| 8756 | struct hdr_ctx local_ctx; |
| 8757 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8758 | int len_hist[MAX_HDR_HISTORY]; |
| 8759 | unsigned int hist_ptr; |
| 8760 | int found; |
| 8761 | |
| 8762 | if (!ctx) { |
| 8763 | local_ctx.idx = 0; |
| 8764 | ctx = &local_ctx; |
| 8765 | } |
| 8766 | |
| 8767 | if (occ >= 0) { |
| 8768 | /* search from the beginning */ |
| 8769 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8770 | occ--; |
| 8771 | if (occ <= 0) { |
| 8772 | *vptr = ctx->line + ctx->val; |
| 8773 | *vlen = ctx->vlen; |
| 8774 | return 1; |
| 8775 | } |
| 8776 | } |
| 8777 | return 0; |
| 8778 | } |
| 8779 | |
| 8780 | /* negative occurrence, we scan all the list then walk back */ |
| 8781 | if (-occ > MAX_HDR_HISTORY) |
| 8782 | return 0; |
| 8783 | |
| 8784 | found = hist_ptr = 0; |
| 8785 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8786 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8787 | len_hist[hist_ptr] = ctx->vlen; |
| 8788 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8789 | hist_ptr = 0; |
| 8790 | found++; |
| 8791 | } |
| 8792 | if (-occ > found) |
| 8793 | return 0; |
| 8794 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8795 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8796 | */ |
| 8797 | hist_ptr += occ; |
| 8798 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8799 | hist_ptr -= MAX_HDR_HISTORY; |
| 8800 | *vptr = ptr_hist[hist_ptr]; |
| 8801 | *vlen = len_hist[hist_ptr]; |
| 8802 | return 1; |
| 8803 | } |
| 8804 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8805 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8806 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8807 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8808 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8809 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8810 | 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] | 8811 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8812 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8813 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8814 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8815 | 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] | 8816 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8817 | 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] | 8818 | 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] | 8819 | |
| 8820 | for (max = 0; start + max < end; max++) |
| 8821 | if (start[max] == '\r' || start[max] == '\n') |
| 8822 | break; |
| 8823 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8824 | UBOUND(max, trash.size - trash.len - 3); |
| 8825 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8826 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8827 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8828 | } |
| 8829 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8830 | |
| 8831 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8832 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8833 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8834 | * assigned to the stream and returned. |
| 8835 | */ |
| 8836 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8837 | { |
| 8838 | struct http_txn *txn = s->txn; |
| 8839 | |
| 8840 | if (txn) |
| 8841 | return txn; |
| 8842 | |
| 8843 | txn = pool_alloc2(pool2_http_txn); |
| 8844 | if (!txn) |
| 8845 | return txn; |
| 8846 | |
| 8847 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8848 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8849 | if (!txn->hdr_idx.v) { |
| 8850 | pool_free2(pool2_http_txn, txn); |
| 8851 | return NULL; |
| 8852 | } |
| 8853 | |
| 8854 | s->txn = txn; |
| 8855 | return txn; |
| 8856 | } |
| 8857 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8858 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8859 | * 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] | 8860 | * the required fields are properly allocated and that we only need to (re)init |
| 8861 | * them. This should be used before processing any new request. |
| 8862 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8863 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8864 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8865 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8866 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8867 | |
| 8868 | txn->flags = 0; |
| 8869 | txn->status = -1; |
| 8870 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8871 | txn->cookie_first_date = 0; |
| 8872 | txn->cookie_last_date = 0; |
| 8873 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8874 | txn->sessid = NULL; |
| 8875 | txn->srv_cookie = NULL; |
| 8876 | txn->cli_cookie = NULL; |
| 8877 | txn->uri = NULL; |
| 8878 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8879 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8880 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8881 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8882 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8883 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8884 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8885 | txn->req.chunk_len = 0LL; |
| 8886 | txn->req.body_len = 0LL; |
| 8887 | txn->rsp.chunk_len = 0LL; |
| 8888 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8889 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8890 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8891 | txn->req.chn = &s->req; |
| 8892 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8893 | |
| 8894 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8895 | |
| 8896 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8897 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8898 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8899 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8900 | if (txn->hdr_idx.v) |
| 8901 | hdr_idx_init(&txn->hdr_idx); |
| 8902 | } |
| 8903 | |
| 8904 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8905 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8906 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8907 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8908 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8909 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8910 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8911 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8912 | s->comp_algo->end(&s->comp_ctx); |
| 8913 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8914 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8915 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8916 | /* these ones will have been dynamically allocated */ |
| 8917 | pool_free2(pool2_requri, txn->uri); |
| 8918 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8919 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8920 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8921 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8922 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8923 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8924 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8925 | txn->uri = NULL; |
| 8926 | txn->srv_cookie = NULL; |
| 8927 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8928 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8929 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8930 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8931 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8932 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8933 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8934 | } |
| 8935 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8936 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8937 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8938 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8939 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8940 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8941 | } |
| 8942 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8943 | } |
| 8944 | |
| 8945 | /* 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] | 8946 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8947 | { |
| 8948 | http_end_txn(s); |
| 8949 | http_init_txn(s); |
| 8950 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8951 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8952 | * any rulelist match the NULL pointer. |
| 8953 | */ |
| 8954 | s->current_rule_list = NULL; |
| 8955 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8956 | s->be = strm_fe(s); |
| 8957 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8958 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8959 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8960 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8961 | /* re-init store persistence */ |
| 8962 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8963 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8964 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8965 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8966 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8967 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8968 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8969 | /* We must trim any excess data from the response buffer, because we |
| 8970 | * may have blocked an invalid response from a server that we don't |
| 8971 | * want to accidentely forward once we disable the analysers, nor do |
| 8972 | * we want those data to come along with next response. A typical |
| 8973 | * example of such data would be from a buggy server responding to |
| 8974 | * a HEAD with some data, or sending more than the advertised |
| 8975 | * content-length. |
| 8976 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8977 | if (unlikely(s->res.buf->i)) |
| 8978 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8979 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8980 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8981 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8982 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8983 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8984 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8985 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8986 | s->req.rex = TICK_ETERNITY; |
| 8987 | s->req.wex = TICK_ETERNITY; |
| 8988 | s->req.analyse_exp = TICK_ETERNITY; |
| 8989 | s->res.rex = TICK_ETERNITY; |
| 8990 | s->res.wex = TICK_ETERNITY; |
| 8991 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8992 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8993 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8994 | void free_http_res_rules(struct list *r) |
| 8995 | { |
| 8996 | struct http_res_rule *tr, *pr; |
| 8997 | |
| 8998 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8999 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9000 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9001 | free(pr); |
| 9002 | } |
| 9003 | } |
| 9004 | |
| 9005 | void free_http_req_rules(struct list *r) |
| 9006 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9007 | struct http_req_rule *tr, *pr; |
| 9008 | |
| 9009 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9010 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9011 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9012 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9013 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9014 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9015 | free(pr); |
| 9016 | } |
| 9017 | } |
| 9018 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9019 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9020 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9021 | { |
| 9022 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9023 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9024 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9025 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9026 | |
| 9027 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 9028 | if (!rule) { |
| 9029 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9030 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9031 | } |
| 9032 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9033 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9034 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9035 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 9036 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9037 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9038 | int code; |
| 9039 | int hc; |
| 9040 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9041 | rule->action = HTTP_REQ_ACT_DENY; |
| 9042 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9043 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9044 | cur_arg++; |
| 9045 | if (!args[cur_arg]) { |
| 9046 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9047 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9048 | goto out_err; |
| 9049 | } |
| 9050 | |
| 9051 | code = atol(args[cur_arg]); |
| 9052 | cur_arg++; |
| 9053 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9054 | if (http_err_codes[hc] == code) { |
| 9055 | rule->deny_status = hc; |
| 9056 | break; |
| 9057 | } |
| 9058 | } |
| 9059 | |
| 9060 | if (hc >= HTTP_ERR_SIZE) { |
| 9061 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9062 | file, linenum, code); |
| 9063 | } |
| 9064 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9065 | } else if (!strcmp(args[0], "tarpit")) { |
| 9066 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 9067 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9068 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9069 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9070 | cur_arg = 1; |
| 9071 | |
| 9072 | while(*args[cur_arg]) { |
| 9073 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9074 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9075 | cur_arg+=2; |
| 9076 | continue; |
| 9077 | } else |
| 9078 | break; |
| 9079 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9080 | } else if (!strcmp(args[0], "set-nice")) { |
| 9081 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 9082 | cur_arg = 1; |
| 9083 | |
| 9084 | if (!*args[cur_arg] || |
| 9085 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9086 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9087 | file, linenum, args[0]); |
| 9088 | goto out_err; |
| 9089 | } |
| 9090 | rule->arg.nice = atoi(args[cur_arg]); |
| 9091 | if (rule->arg.nice < -1024) |
| 9092 | rule->arg.nice = -1024; |
| 9093 | else if (rule->arg.nice > 1024) |
| 9094 | rule->arg.nice = 1024; |
| 9095 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9096 | } else if (!strcmp(args[0], "set-tos")) { |
| 9097 | #ifdef IP_TOS |
| 9098 | char *err; |
| 9099 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 9100 | cur_arg = 1; |
| 9101 | |
| 9102 | if (!*args[cur_arg] || |
| 9103 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9104 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9105 | file, linenum, args[0]); |
| 9106 | goto out_err; |
| 9107 | } |
| 9108 | |
| 9109 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9110 | if (err && *err != '\0') { |
| 9111 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9112 | file, linenum, err, args[0]); |
| 9113 | goto out_err; |
| 9114 | } |
| 9115 | cur_arg++; |
| 9116 | #else |
| 9117 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9118 | goto out_err; |
| 9119 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9120 | } else if (!strcmp(args[0], "set-mark")) { |
| 9121 | #ifdef SO_MARK |
| 9122 | char *err; |
| 9123 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 9124 | cur_arg = 1; |
| 9125 | |
| 9126 | if (!*args[cur_arg] || |
| 9127 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9128 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9129 | file, linenum, args[0]); |
| 9130 | goto out_err; |
| 9131 | } |
| 9132 | |
| 9133 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9134 | if (err && *err != '\0') { |
| 9135 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9136 | file, linenum, err, args[0]); |
| 9137 | goto out_err; |
| 9138 | } |
| 9139 | cur_arg++; |
| 9140 | global.last_checks |= LSTCHK_NETADM; |
| 9141 | #else |
| 9142 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9143 | goto out_err; |
| 9144 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9145 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9146 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 9147 | cur_arg = 1; |
| 9148 | |
| 9149 | if (!*args[cur_arg] || |
| 9150 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9151 | bad_log_level: |
| 9152 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9153 | file, linenum, args[0]); |
| 9154 | goto out_err; |
| 9155 | } |
| 9156 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9157 | rule->arg.loglevel = -1; |
| 9158 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9159 | goto bad_log_level; |
| 9160 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9161 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9162 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9163 | cur_arg = 1; |
| 9164 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9165 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9166 | (*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] | 9167 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9168 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9169 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9170 | } |
| 9171 | |
| 9172 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9173 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9174 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9175 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9176 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9177 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9178 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9179 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9180 | free(proxy->conf.lfs_file); |
| 9181 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9182 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9183 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9184 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9185 | rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9186 | cur_arg = 1; |
| 9187 | |
| 9188 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9189 | (*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] | 9190 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9191 | file, linenum, args[0]); |
| 9192 | goto out_err; |
| 9193 | } |
| 9194 | |
| 9195 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9196 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9197 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9198 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9199 | error = NULL; |
| 9200 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9201 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9202 | args[cur_arg + 1], error); |
| 9203 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9204 | goto out_err; |
| 9205 | } |
| 9206 | |
| 9207 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9208 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9209 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9210 | file, linenum); |
| 9211 | |
| 9212 | free(proxy->conf.lfs_file); |
| 9213 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9214 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9215 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9216 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9217 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9218 | cur_arg = 1; |
| 9219 | |
| 9220 | if (!*args[cur_arg] || |
| 9221 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9222 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9223 | file, linenum, args[0]); |
| 9224 | goto out_err; |
| 9225 | } |
| 9226 | |
| 9227 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9228 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9229 | |
| 9230 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9231 | free(proxy->conf.lfs_file); |
| 9232 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9233 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9234 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9235 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9236 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9237 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9238 | struct sample_expr *expr; |
| 9239 | unsigned int where; |
| 9240 | char *err = NULL; |
| 9241 | |
| 9242 | cur_arg = 1; |
| 9243 | proxy->conf.args.ctx = ARGC_TRK; |
| 9244 | |
| 9245 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9246 | if (!expr) { |
| 9247 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9248 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9249 | free(err); |
| 9250 | goto out_err; |
| 9251 | } |
| 9252 | |
| 9253 | where = 0; |
| 9254 | if (proxy->cap & PR_CAP_FE) |
| 9255 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9256 | if (proxy->cap & PR_CAP_BE) |
| 9257 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9258 | |
| 9259 | if (!(expr->fetch->val & where)) { |
| 9260 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9261 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9262 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9263 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9264 | free(expr); |
| 9265 | goto out_err; |
| 9266 | } |
| 9267 | |
| 9268 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9269 | cur_arg++; |
| 9270 | if (!args[cur_arg]) { |
| 9271 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9272 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9273 | free(expr); |
| 9274 | goto out_err; |
| 9275 | } |
| 9276 | /* we copy the table name for now, it will be resolved later */ |
| 9277 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9278 | cur_arg++; |
| 9279 | } |
| 9280 | rule->act_prm.trk_ctr.expr = expr; |
| 9281 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9282 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9283 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9284 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9285 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9286 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) { |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9287 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9288 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9289 | goto out_err; |
| 9290 | } |
| 9291 | |
| 9292 | /* this redirect rule might already contain a parsed condition which |
| 9293 | * we'll pass to the http-request rule. |
| 9294 | */ |
| 9295 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9296 | rule->arg.redir = redir; |
| 9297 | rule->cond = redir->cond; |
| 9298 | redir->cond = NULL; |
| 9299 | cur_arg = 2; |
| 9300 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9301 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9302 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9303 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9304 | /* |
| 9305 | * '+ 8' for 'add-acl(' |
| 9306 | * '- 9' for 'add-acl(' + trailing ')' |
| 9307 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9308 | 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] | 9309 | |
| 9310 | cur_arg = 1; |
| 9311 | |
| 9312 | if (!*args[cur_arg] || |
| 9313 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9314 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9315 | file, linenum, args[0]); |
| 9316 | goto out_err; |
| 9317 | } |
| 9318 | |
| 9319 | LIST_INIT(&rule->arg.map.key); |
| 9320 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9321 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9322 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9323 | file, linenum); |
| 9324 | free(proxy->conf.lfs_file); |
| 9325 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9326 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9327 | cur_arg += 1; |
| 9328 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9329 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9330 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9331 | /* |
| 9332 | * '+ 8' for 'del-acl(' |
| 9333 | * '- 9' for 'del-acl(' + trailing ')' |
| 9334 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9335 | 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] | 9336 | |
| 9337 | cur_arg = 1; |
| 9338 | |
| 9339 | if (!*args[cur_arg] || |
| 9340 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9341 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9342 | file, linenum, args[0]); |
| 9343 | goto out_err; |
| 9344 | } |
| 9345 | |
| 9346 | LIST_INIT(&rule->arg.map.key); |
| 9347 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9348 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9349 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9350 | file, linenum); |
| 9351 | free(proxy->conf.lfs_file); |
| 9352 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9353 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9354 | cur_arg += 1; |
| 9355 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9356 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9357 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9358 | /* |
| 9359 | * '+ 8' for 'del-map(' |
| 9360 | * '- 9' for 'del-map(' + trailing ')' |
| 9361 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9362 | 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] | 9363 | |
| 9364 | cur_arg = 1; |
| 9365 | |
| 9366 | if (!*args[cur_arg] || |
| 9367 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9368 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9369 | file, linenum, args[0]); |
| 9370 | goto out_err; |
| 9371 | } |
| 9372 | |
| 9373 | LIST_INIT(&rule->arg.map.key); |
| 9374 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9375 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9376 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9377 | file, linenum); |
| 9378 | free(proxy->conf.lfs_file); |
| 9379 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9380 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9381 | cur_arg += 1; |
| 9382 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9383 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9384 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9385 | /* |
| 9386 | * '+ 8' for 'set-map(' |
| 9387 | * '- 9' for 'set-map(' + trailing ')' |
| 9388 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9389 | 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] | 9390 | |
| 9391 | cur_arg = 1; |
| 9392 | |
| 9393 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9394 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9395 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9396 | file, linenum, args[0]); |
| 9397 | goto out_err; |
| 9398 | } |
| 9399 | |
| 9400 | LIST_INIT(&rule->arg.map.key); |
| 9401 | LIST_INIT(&rule->arg.map.value); |
| 9402 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9403 | |
| 9404 | /* key pattern */ |
| 9405 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9406 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9407 | file, linenum); |
| 9408 | |
| 9409 | /* value pattern */ |
| 9410 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9411 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9412 | file, linenum); |
| 9413 | free(proxy->conf.lfs_file); |
| 9414 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9415 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9416 | |
| 9417 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9418 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9419 | char *errmsg = NULL; |
| 9420 | cur_arg = 1; |
| 9421 | /* try in the module list */ |
| 9422 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9423 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9424 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9425 | free(errmsg); |
| 9426 | goto out_err; |
| 9427 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9428 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9429 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9430 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9431 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9432 | } |
| 9433 | |
| 9434 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9435 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9436 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9437 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9438 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9439 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9440 | file, linenum, args[0], errmsg); |
| 9441 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9442 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9443 | } |
| 9444 | rule->cond = cond; |
| 9445 | } |
| 9446 | else if (*args[cur_arg]) { |
| 9447 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9448 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9449 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9450 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9451 | } |
| 9452 | |
| 9453 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9454 | out_err: |
| 9455 | free(rule); |
| 9456 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9457 | } |
| 9458 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9459 | /* parse an "http-respose" rule */ |
| 9460 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9461 | { |
| 9462 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9463 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9464 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9465 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9466 | |
| 9467 | rule = calloc(1, sizeof(*rule)); |
| 9468 | if (!rule) { |
| 9469 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9470 | goto out_err; |
| 9471 | } |
| 9472 | |
| 9473 | if (!strcmp(args[0], "allow")) { |
| 9474 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9475 | cur_arg = 1; |
| 9476 | } else if (!strcmp(args[0], "deny")) { |
| 9477 | rule->action = HTTP_RES_ACT_DENY; |
| 9478 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9479 | } else if (!strcmp(args[0], "set-nice")) { |
| 9480 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9481 | cur_arg = 1; |
| 9482 | |
| 9483 | if (!*args[cur_arg] || |
| 9484 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9485 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9486 | file, linenum, args[0]); |
| 9487 | goto out_err; |
| 9488 | } |
| 9489 | rule->arg.nice = atoi(args[cur_arg]); |
| 9490 | if (rule->arg.nice < -1024) |
| 9491 | rule->arg.nice = -1024; |
| 9492 | else if (rule->arg.nice > 1024) |
| 9493 | rule->arg.nice = 1024; |
| 9494 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9495 | } else if (!strcmp(args[0], "set-tos")) { |
| 9496 | #ifdef IP_TOS |
| 9497 | char *err; |
| 9498 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9499 | cur_arg = 1; |
| 9500 | |
| 9501 | if (!*args[cur_arg] || |
| 9502 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9503 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9504 | file, linenum, args[0]); |
| 9505 | goto out_err; |
| 9506 | } |
| 9507 | |
| 9508 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9509 | if (err && *err != '\0') { |
| 9510 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9511 | file, linenum, err, args[0]); |
| 9512 | goto out_err; |
| 9513 | } |
| 9514 | cur_arg++; |
| 9515 | #else |
| 9516 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9517 | goto out_err; |
| 9518 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9519 | } else if (!strcmp(args[0], "set-mark")) { |
| 9520 | #ifdef SO_MARK |
| 9521 | char *err; |
| 9522 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9523 | cur_arg = 1; |
| 9524 | |
| 9525 | if (!*args[cur_arg] || |
| 9526 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9527 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9528 | file, linenum, args[0]); |
| 9529 | goto out_err; |
| 9530 | } |
| 9531 | |
| 9532 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9533 | if (err && *err != '\0') { |
| 9534 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9535 | file, linenum, err, args[0]); |
| 9536 | goto out_err; |
| 9537 | } |
| 9538 | cur_arg++; |
| 9539 | global.last_checks |= LSTCHK_NETADM; |
| 9540 | #else |
| 9541 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9542 | goto out_err; |
| 9543 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9544 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9545 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9546 | cur_arg = 1; |
| 9547 | |
| 9548 | if (!*args[cur_arg] || |
| 9549 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9550 | bad_log_level: |
| 9551 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9552 | file, linenum, args[0]); |
| 9553 | goto out_err; |
| 9554 | } |
| 9555 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9556 | rule->arg.loglevel = -1; |
| 9557 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9558 | goto bad_log_level; |
| 9559 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9560 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9561 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9562 | cur_arg = 1; |
| 9563 | |
| 9564 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9565 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9566 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9567 | file, linenum, args[0]); |
| 9568 | goto out_err; |
| 9569 | } |
| 9570 | |
| 9571 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9572 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9573 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9574 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9575 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9576 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9577 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9578 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9579 | free(proxy->conf.lfs_file); |
| 9580 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9581 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9582 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9583 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9584 | rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9585 | cur_arg = 1; |
| 9586 | |
| 9587 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9588 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9589 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9590 | file, linenum, args[0]); |
| 9591 | goto out_err; |
| 9592 | } |
| 9593 | |
| 9594 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9595 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9596 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9597 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9598 | error = NULL; |
| 9599 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9600 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9601 | args[cur_arg + 1], error); |
| 9602 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9603 | goto out_err; |
| 9604 | } |
| 9605 | |
| 9606 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9607 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9608 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9609 | file, linenum); |
| 9610 | |
| 9611 | free(proxy->conf.lfs_file); |
| 9612 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9613 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9614 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9615 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9616 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9617 | cur_arg = 1; |
| 9618 | |
| 9619 | if (!*args[cur_arg] || |
| 9620 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9621 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9622 | file, linenum, args[0]); |
| 9623 | goto out_err; |
| 9624 | } |
| 9625 | |
| 9626 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9627 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9628 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9629 | proxy->conf.args.ctx = ARGC_HRS; |
| 9630 | free(proxy->conf.lfs_file); |
| 9631 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9632 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9633 | cur_arg += 1; |
| 9634 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9635 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9636 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9637 | /* |
| 9638 | * '+ 8' for 'add-acl(' |
| 9639 | * '- 9' for 'add-acl(' + trailing ')' |
| 9640 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9641 | 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] | 9642 | |
| 9643 | cur_arg = 1; |
| 9644 | |
| 9645 | if (!*args[cur_arg] || |
| 9646 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9647 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9648 | file, linenum, args[0]); |
| 9649 | goto out_err; |
| 9650 | } |
| 9651 | |
| 9652 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9653 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9654 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9655 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9656 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9657 | free(proxy->conf.lfs_file); |
| 9658 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9659 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9660 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9661 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9662 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9663 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9664 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9665 | /* |
| 9666 | * '+ 8' for 'del-acl(' |
| 9667 | * '- 9' for 'del-acl(' + trailing ')' |
| 9668 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9669 | 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] | 9670 | |
| 9671 | cur_arg = 1; |
| 9672 | |
| 9673 | if (!*args[cur_arg] || |
| 9674 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9675 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9676 | file, linenum, args[0]); |
| 9677 | goto out_err; |
| 9678 | } |
| 9679 | |
| 9680 | LIST_INIT(&rule->arg.map.key); |
| 9681 | proxy->conf.args.ctx = ARGC_HRS; |
| 9682 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9683 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9684 | file, linenum); |
| 9685 | free(proxy->conf.lfs_file); |
| 9686 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9687 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9688 | cur_arg += 1; |
| 9689 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9690 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9691 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9692 | /* |
| 9693 | * '+ 8' for 'del-map(' |
| 9694 | * '- 9' for 'del-map(' + trailing ')' |
| 9695 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9696 | 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] | 9697 | |
| 9698 | cur_arg = 1; |
| 9699 | |
| 9700 | if (!*args[cur_arg] || |
| 9701 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9702 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9703 | file, linenum, args[0]); |
| 9704 | goto out_err; |
| 9705 | } |
| 9706 | |
| 9707 | LIST_INIT(&rule->arg.map.key); |
| 9708 | proxy->conf.args.ctx = ARGC_HRS; |
| 9709 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9710 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9711 | file, linenum); |
| 9712 | free(proxy->conf.lfs_file); |
| 9713 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9714 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9715 | cur_arg += 1; |
| 9716 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9717 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9718 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9719 | /* |
| 9720 | * '+ 8' for 'set-map(' |
| 9721 | * '- 9' for 'set-map(' + trailing ')' |
| 9722 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9723 | 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] | 9724 | |
| 9725 | cur_arg = 1; |
| 9726 | |
| 9727 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9728 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9729 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9730 | file, linenum, args[0]); |
| 9731 | goto out_err; |
| 9732 | } |
| 9733 | |
| 9734 | LIST_INIT(&rule->arg.map.key); |
| 9735 | LIST_INIT(&rule->arg.map.value); |
| 9736 | |
| 9737 | proxy->conf.args.ctx = ARGC_HRS; |
| 9738 | |
| 9739 | /* key pattern */ |
| 9740 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9741 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9742 | file, linenum); |
| 9743 | |
| 9744 | /* value pattern */ |
| 9745 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9746 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9747 | file, linenum); |
| 9748 | |
| 9749 | free(proxy->conf.lfs_file); |
| 9750 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9751 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9752 | |
| 9753 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9754 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9755 | char *errmsg = NULL; |
| 9756 | cur_arg = 1; |
| 9757 | /* try in the module list */ |
| 9758 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9759 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9760 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9761 | free(errmsg); |
| 9762 | goto out_err; |
| 9763 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9764 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9765 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9766 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9767 | goto out_err; |
| 9768 | } |
| 9769 | |
| 9770 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9771 | struct acl_cond *cond; |
| 9772 | char *errmsg = NULL; |
| 9773 | |
| 9774 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9775 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9776 | file, linenum, args[0], errmsg); |
| 9777 | free(errmsg); |
| 9778 | goto out_err; |
| 9779 | } |
| 9780 | rule->cond = cond; |
| 9781 | } |
| 9782 | else if (*args[cur_arg]) { |
| 9783 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9784 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9785 | file, linenum, args[0], args[cur_arg]); |
| 9786 | goto out_err; |
| 9787 | } |
| 9788 | |
| 9789 | return rule; |
| 9790 | out_err: |
| 9791 | free(rule); |
| 9792 | return NULL; |
| 9793 | } |
| 9794 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9795 | /* 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] | 9796 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9797 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9798 | */ |
| 9799 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9800 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9801 | { |
| 9802 | struct redirect_rule *rule; |
| 9803 | int cur_arg; |
| 9804 | int type = REDIRECT_TYPE_NONE; |
| 9805 | int code = 302; |
| 9806 | const char *destination = NULL; |
| 9807 | const char *cookie = NULL; |
| 9808 | int cookie_set = 0; |
| 9809 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9810 | struct acl_cond *cond = NULL; |
| 9811 | |
| 9812 | cur_arg = 0; |
| 9813 | while (*(args[cur_arg])) { |
| 9814 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9815 | if (!*args[cur_arg + 1]) |
| 9816 | goto missing_arg; |
| 9817 | |
| 9818 | type = REDIRECT_TYPE_LOCATION; |
| 9819 | cur_arg++; |
| 9820 | destination = args[cur_arg]; |
| 9821 | } |
| 9822 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9823 | if (!*args[cur_arg + 1]) |
| 9824 | goto missing_arg; |
| 9825 | |
| 9826 | type = REDIRECT_TYPE_PREFIX; |
| 9827 | cur_arg++; |
| 9828 | destination = args[cur_arg]; |
| 9829 | } |
| 9830 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9831 | if (!*args[cur_arg + 1]) |
| 9832 | goto missing_arg; |
| 9833 | |
| 9834 | type = REDIRECT_TYPE_SCHEME; |
| 9835 | cur_arg++; |
| 9836 | destination = args[cur_arg]; |
| 9837 | } |
| 9838 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9839 | if (!*args[cur_arg + 1]) |
| 9840 | goto missing_arg; |
| 9841 | |
| 9842 | cur_arg++; |
| 9843 | cookie = args[cur_arg]; |
| 9844 | cookie_set = 1; |
| 9845 | } |
| 9846 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9847 | if (!*args[cur_arg + 1]) |
| 9848 | goto missing_arg; |
| 9849 | |
| 9850 | cur_arg++; |
| 9851 | cookie = args[cur_arg]; |
| 9852 | cookie_set = 0; |
| 9853 | } |
| 9854 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9855 | if (!*args[cur_arg + 1]) |
| 9856 | goto missing_arg; |
| 9857 | |
| 9858 | cur_arg++; |
| 9859 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9860 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9861 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9862 | "'%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] | 9863 | args[cur_arg - 1], args[cur_arg]); |
| 9864 | return NULL; |
| 9865 | } |
| 9866 | } |
| 9867 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9868 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9869 | } |
| 9870 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9871 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9872 | } |
| 9873 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9874 | strcmp(args[cur_arg], "unless") == 0) { |
| 9875 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9876 | if (!cond) { |
| 9877 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9878 | return NULL; |
| 9879 | } |
| 9880 | break; |
| 9881 | } |
| 9882 | else { |
| 9883 | memprintf(errmsg, |
| 9884 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9885 | args[cur_arg]); |
| 9886 | return NULL; |
| 9887 | } |
| 9888 | cur_arg++; |
| 9889 | } |
| 9890 | |
| 9891 | if (type == REDIRECT_TYPE_NONE) { |
| 9892 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9893 | return NULL; |
| 9894 | } |
| 9895 | |
| 9896 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9897 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9898 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9899 | |
| 9900 | if (!use_fmt) { |
| 9901 | /* old-style static redirect rule */ |
| 9902 | rule->rdr_str = strdup(destination); |
| 9903 | rule->rdr_len = strlen(destination); |
| 9904 | } |
| 9905 | else { |
| 9906 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9907 | |
| 9908 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9909 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9910 | * makes it hard for the user to configure a self-redirection. |
| 9911 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9912 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9913 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9914 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9915 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9916 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9917 | free(curproxy->conf.lfs_file); |
| 9918 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9919 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9920 | } |
| 9921 | } |
| 9922 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9923 | if (cookie) { |
| 9924 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9925 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9926 | */ |
| 9927 | rule->cookie_len = strlen(cookie); |
| 9928 | if (cookie_set) { |
| 9929 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9930 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9931 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9932 | rule->cookie_len += 9; |
| 9933 | } else { |
| 9934 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9935 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9936 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9937 | rule->cookie_len += 20; |
| 9938 | } |
| 9939 | } |
| 9940 | rule->type = type; |
| 9941 | rule->code = code; |
| 9942 | rule->flags = flags; |
| 9943 | LIST_INIT(&rule->list); |
| 9944 | return rule; |
| 9945 | |
| 9946 | missing_arg: |
| 9947 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9948 | return NULL; |
| 9949 | } |
| 9950 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9951 | /************************************************************************/ |
| 9952 | /* The code below is dedicated to ACL parsing and matching */ |
| 9953 | /************************************************************************/ |
| 9954 | |
| 9955 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9956 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9957 | * which means that a request or response is ready. If some data is missing, |
| 9958 | * 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] | 9959 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9960 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9961 | * |
| 9962 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9963 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9964 | * decide whether or not an HTTP message is present ; |
| 9965 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9966 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9967 | * 1 if an HTTP message is ready |
| 9968 | */ |
| 9969 | static int |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9970 | smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9971 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9972 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9973 | struct http_txn *txn; |
| 9974 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9975 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 9976 | /* Note: this function may only be used from places where |
| 9977 | * http_init_txn() has already been done, and implies that <s>, |
| 9978 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 9979 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9980 | */ |
| 9981 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9982 | if (!s) |
| 9983 | return 0; |
| 9984 | txn = s->txn; |
| 9985 | |
| 9986 | if (!txn) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9987 | return 0; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9988 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9989 | |
| 9990 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9991 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9992 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9993 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9994 | /* If the buffer does not leave enough free space at the end, |
| 9995 | * we must first realign it. |
| 9996 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9997 | if (s->req.buf->p > s->req.buf->data && |
| 9998 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9999 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10000 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10001 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10002 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10003 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10004 | |
| 10005 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10006 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10007 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10008 | |
| 10009 | /* Still no valid request ? */ |
| 10010 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10011 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10012 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10013 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10014 | } |
| 10015 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10016 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10017 | return 0; |
| 10018 | } |
| 10019 | |
| 10020 | /* OK we just got a valid HTTP request. We have some minor |
| 10021 | * preparation to perform so that further checks can rely |
| 10022 | * on HTTP tests. |
| 10023 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10024 | |
| 10025 | /* If the request was parsed but was too large, we must absolutely |
| 10026 | * return an error so that it is not processed. At the moment this |
| 10027 | * cannot happen, but if the parsers are to change in the future, |
| 10028 | * we want this check to be maintained. |
| 10029 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10030 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10031 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10032 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10033 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10034 | return 1; |
| 10035 | } |
| 10036 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10037 | 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] | 10038 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10039 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10040 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10041 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10042 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10043 | } |
| 10044 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10045 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10046 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10047 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10048 | |
| 10049 | /* otherwise everything's ready for the request */ |
| 10050 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10051 | else { |
| 10052 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10053 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10054 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10055 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10056 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10057 | } |
| 10058 | |
| 10059 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10060 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10061 | return 1; |
| 10062 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10063 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 10064 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 10065 | * least the type and uint value are modified. |
| 10066 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10067 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10068 | do { int r = smp_prefetch_http(px, strm, opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10069 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10070 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10071 | do { int r = smp_prefetch_http(px, strm, opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10072 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10073 | |
| 10074 | /* 1. Check on METHOD |
| 10075 | * We use the pre-parsed method if it is known, and store its number as an |
| 10076 | * integer. If it is unknown, we use the pointer and the length. |
| 10077 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10078 | 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] | 10079 | { |
| 10080 | int len, meth; |
| 10081 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10082 | len = strlen(text); |
| 10083 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10084 | |
| 10085 | pattern->val.i = meth; |
| 10086 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10087 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10088 | pattern->len = len; |
| 10089 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10090 | else { |
| 10091 | pattern->ptr.str = NULL; |
| 10092 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10093 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10094 | return 1; |
| 10095 | } |
| 10096 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10097 | /* This function fetches the method of current HTTP request and stores |
| 10098 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10099 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10100 | * in <len> and <ptr> is NULL ; |
| 10101 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10102 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10103 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10104 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10105 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10106 | smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10107 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10108 | { |
| 10109 | int meth; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10110 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10111 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10112 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10113 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10114 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10115 | smp->type = SMP_T_METH; |
| 10116 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10117 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10118 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10119 | /* ensure the indexes are not affected */ |
| 10120 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10121 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10122 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 10123 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10124 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10125 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10126 | return 1; |
| 10127 | } |
| 10128 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10129 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10130 | 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] | 10131 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10132 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10133 | struct pattern_list *lst; |
| 10134 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10135 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10136 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10137 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10138 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10139 | /* well-known method */ |
| 10140 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 10141 | if (smp->data.meth.meth == pattern->val.i) |
| 10142 | return pattern; |
| 10143 | else |
| 10144 | continue; |
| 10145 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10146 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10147 | /* Other method, we must compare the strings */ |
| 10148 | if (pattern->len != smp->data.meth.str.len) |
| 10149 | continue; |
| 10150 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10151 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 10152 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 10153 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10154 | return pattern; |
| 10155 | } |
| 10156 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10157 | } |
| 10158 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10159 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10160 | smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10161 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10162 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10163 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10164 | char *ptr; |
| 10165 | int len; |
| 10166 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10167 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10168 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10169 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10170 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10171 | |
| 10172 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10173 | if (len <= 0) |
| 10174 | return 0; |
| 10175 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10176 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10177 | smp->data.str.str = ptr; |
| 10178 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10179 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10180 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10181 | return 1; |
| 10182 | } |
| 10183 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10184 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10185 | smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10186 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10187 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10188 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10189 | char *ptr; |
| 10190 | int len; |
| 10191 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10192 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10193 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10194 | txn = strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10195 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10196 | return 0; |
| 10197 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10198 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10199 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10200 | |
| 10201 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10202 | if (len <= 0) |
| 10203 | return 0; |
| 10204 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10205 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10206 | smp->data.str.str = ptr; |
| 10207 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10208 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10209 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10210 | return 1; |
| 10211 | } |
| 10212 | |
| 10213 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10214 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10215 | smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10216 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10217 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10218 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10219 | char *ptr; |
| 10220 | int len; |
| 10221 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10222 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10223 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10224 | txn = strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10225 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10226 | return 0; |
| 10227 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10228 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10229 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10230 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10231 | smp->type = SMP_T_UINT; |
| 10232 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10233 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10234 | return 1; |
| 10235 | } |
| 10236 | |
| 10237 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10238 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10239 | smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10240 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10241 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10242 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10243 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10244 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10245 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10246 | txn = strm->txn; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10247 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10248 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10249 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10250 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10251 | return 1; |
| 10252 | } |
| 10253 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10254 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10255 | smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10256 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10257 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10258 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10259 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10260 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10261 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10262 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10263 | txn = strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10264 | 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] | 10265 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10266 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10267 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10268 | smp->type = SMP_T_IPV4; |
| 10269 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10270 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10271 | return 1; |
| 10272 | } |
| 10273 | |
| 10274 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10275 | smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10276 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10277 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10278 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10279 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10280 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10281 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10282 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10283 | txn = strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10284 | 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] | 10285 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10286 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10287 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10288 | smp->type = SMP_T_UINT; |
| 10289 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10290 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10291 | return 1; |
| 10292 | } |
| 10293 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10294 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10295 | * Accepts an optional argument of type string containing the header field name, |
| 10296 | * and an optional argument of type signed or unsigned integer to request an |
| 10297 | * 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] | 10298 | * headers are considered from the first one. It does not stop on commas and |
| 10299 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10300 | */ |
| 10301 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10302 | smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10303 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10304 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10305 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10306 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10307 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10308 | int occ = 0; |
| 10309 | const char *name_str = NULL; |
| 10310 | int name_len = 0; |
| 10311 | |
| 10312 | if (!ctx) { |
| 10313 | /* first call */ |
| 10314 | ctx = &static_hdr_ctx; |
| 10315 | ctx->idx = 0; |
| 10316 | smp->ctx.a[0] = ctx; |
| 10317 | } |
| 10318 | |
| 10319 | if (args) { |
| 10320 | if (args[0].type != ARGT_STR) |
| 10321 | return 0; |
| 10322 | name_str = args[0].data.str.str; |
| 10323 | name_len = args[0].data.str.len; |
| 10324 | |
| 10325 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10326 | occ = args[1].data.uint; |
| 10327 | } |
| 10328 | |
| 10329 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10330 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10331 | idx = &strm->txn->hdr_idx; |
| 10332 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10333 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10334 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10335 | /* search for header from the beginning */ |
| 10336 | ctx->idx = 0; |
| 10337 | |
| 10338 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10339 | /* no explicit occurrence and single fetch => last header by default */ |
| 10340 | occ = -1; |
| 10341 | |
| 10342 | if (!occ) |
| 10343 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10344 | smp->flags |= SMP_F_NOT_LAST; |
| 10345 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10346 | smp->type = SMP_T_STR; |
| 10347 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10348 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10349 | return 1; |
| 10350 | |
| 10351 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10352 | return 0; |
| 10353 | } |
| 10354 | |
| 10355 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10356 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10357 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10358 | */ |
| 10359 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10360 | smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10361 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10362 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10363 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10364 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10365 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10366 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10367 | const char *name = NULL; |
| 10368 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10369 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10370 | if (args && args->type == ARGT_STR) { |
| 10371 | name = args->data.str.str; |
| 10372 | len = args->data.str.len; |
| 10373 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10374 | |
| 10375 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10376 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10377 | idx = &strm->txn->hdr_idx; |
| 10378 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10379 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10380 | ctx.idx = 0; |
| 10381 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10382 | 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] | 10383 | cnt++; |
| 10384 | |
| 10385 | smp->type = SMP_T_UINT; |
| 10386 | smp->data.uint = cnt; |
| 10387 | smp->flags = SMP_F_VOL_HDR; |
| 10388 | return 1; |
| 10389 | } |
| 10390 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10391 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10392 | smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10393 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10394 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10395 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10396 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10397 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10398 | struct chunk *temp; |
| 10399 | char del = ','; |
| 10400 | |
| 10401 | if (args && args->type == ARGT_STR) |
| 10402 | del = *args[0].data.str.str; |
| 10403 | |
| 10404 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10405 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10406 | idx = &strm->txn->hdr_idx; |
| 10407 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10408 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10409 | temp = get_trash_chunk(); |
| 10410 | |
| 10411 | ctx.idx = 0; |
| 10412 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10413 | if (temp->len) |
| 10414 | temp->str[temp->len++] = del; |
| 10415 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10416 | temp->len += ctx.del; |
| 10417 | } |
| 10418 | |
| 10419 | smp->type = SMP_T_STR; |
| 10420 | smp->data.str.str = temp->str; |
| 10421 | smp->data.str.len = temp->len; |
| 10422 | smp->flags = SMP_F_VOL_HDR; |
| 10423 | return 1; |
| 10424 | } |
| 10425 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10426 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10427 | * Accepts an optional argument of type string containing the header field name, |
| 10428 | * and an optional argument of type signed or unsigned integer to request an |
| 10429 | * 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] | 10430 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10431 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10432 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10433 | smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10434 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10435 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10436 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10437 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10438 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10439 | int occ = 0; |
| 10440 | const char *name_str = NULL; |
| 10441 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10442 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10443 | if (!ctx) { |
| 10444 | /* first call */ |
| 10445 | ctx = &static_hdr_ctx; |
| 10446 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10447 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10448 | } |
| 10449 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10450 | if (args) { |
| 10451 | if (args[0].type != ARGT_STR) |
| 10452 | return 0; |
| 10453 | name_str = args[0].data.str.str; |
| 10454 | name_len = args[0].data.str.len; |
| 10455 | |
| 10456 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10457 | occ = args[1].data.uint; |
| 10458 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10459 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10460 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10461 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10462 | idx = &strm->txn->hdr_idx; |
| 10463 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10464 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10465 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10466 | /* search for header from the beginning */ |
| 10467 | ctx->idx = 0; |
| 10468 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10469 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10470 | /* no explicit occurrence and single fetch => last header by default */ |
| 10471 | occ = -1; |
| 10472 | |
| 10473 | if (!occ) |
| 10474 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10475 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10476 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10477 | smp->type = SMP_T_STR; |
| 10478 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10479 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10480 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10481 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10482 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10483 | return 0; |
| 10484 | } |
| 10485 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10486 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10487 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10488 | */ |
| 10489 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10490 | smp_fetch_hdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10491 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10492 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10493 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10494 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10495 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10496 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10497 | const char *name = NULL; |
| 10498 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10499 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10500 | if (args && args->type == ARGT_STR) { |
| 10501 | name = args->data.str.str; |
| 10502 | len = args->data.str.len; |
| 10503 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10504 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10505 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10506 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10507 | idx = &strm->txn->hdr_idx; |
| 10508 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10509 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10510 | ctx.idx = 0; |
| 10511 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10512 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10513 | cnt++; |
| 10514 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10515 | smp->type = SMP_T_UINT; |
| 10516 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10517 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10518 | return 1; |
| 10519 | } |
| 10520 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10521 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10522 | * takes a mandatory argument of type string and an optional one of type int |
| 10523 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10524 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10525 | */ |
| 10526 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10527 | smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10528 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10529 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10530 | int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10531 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10532 | if (ret > 0) { |
| 10533 | smp->type = SMP_T_UINT; |
| 10534 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10535 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10536 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10537 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10538 | } |
| 10539 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10540 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10541 | * and an optional one of type int to designate a specific occurrence. |
| 10542 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10543 | */ |
| 10544 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10545 | smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10546 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10547 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10548 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10549 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10550 | while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10551 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10552 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10553 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10554 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10555 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10556 | if (smp->data.str.len < temp->size - 1) { |
| 10557 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10558 | temp->str[smp->data.str.len] = '\0'; |
| 10559 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10560 | smp->type = SMP_T_IPV6; |
| 10561 | break; |
| 10562 | } |
| 10563 | } |
| 10564 | } |
| 10565 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10566 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10567 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10568 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10569 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10570 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10571 | } |
| 10572 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10573 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10574 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10575 | */ |
| 10576 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10577 | smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10578 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10579 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10580 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10581 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10582 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10583 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10584 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10585 | txn = strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10586 | 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] | 10587 | ptr = http_get_path(txn); |
| 10588 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10589 | return 0; |
| 10590 | |
| 10591 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10592 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10593 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10594 | |
| 10595 | while (ptr < end && *ptr != '?') |
| 10596 | ptr++; |
| 10597 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10598 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10599 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10600 | return 1; |
| 10601 | } |
| 10602 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10603 | /* This produces a concatenation of the first occurrence of the Host header |
| 10604 | * followed by the path component if it begins with a slash ('/'). This means |
| 10605 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10606 | * If no Host header is found, then the path is returned as-is. The returned |
| 10607 | * 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] | 10608 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10609 | */ |
| 10610 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10611 | smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10612 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10613 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10614 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10615 | char *ptr, *end, *beg; |
| 10616 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10617 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10618 | |
| 10619 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10620 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10621 | txn = strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10622 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10623 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10624 | return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10625 | |
| 10626 | /* 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] | 10627 | temp = get_trash_chunk(); |
| 10628 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10629 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10630 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10631 | smp->data.str.len = ctx.vlen; |
| 10632 | |
| 10633 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10634 | 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] | 10635 | beg = http_get_path(txn); |
| 10636 | if (!beg) |
| 10637 | beg = end; |
| 10638 | |
| 10639 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10640 | |
| 10641 | if (beg < ptr && *beg == '/') { |
| 10642 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10643 | smp->data.str.len += ptr - beg; |
| 10644 | } |
| 10645 | |
| 10646 | smp->flags = SMP_F_VOL_1ST; |
| 10647 | return 1; |
| 10648 | } |
| 10649 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10650 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10651 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10652 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10653 | * 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] | 10654 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10655 | * 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] | 10656 | * high-traffic sites without having to store whole paths. |
| 10657 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10658 | int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10659 | smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10660 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10661 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10662 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10663 | struct hdr_ctx ctx; |
| 10664 | unsigned int hash = 0; |
| 10665 | char *ptr, *beg, *end; |
| 10666 | int len; |
| 10667 | |
| 10668 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10669 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10670 | txn = strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10671 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10672 | 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] | 10673 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10674 | ptr = ctx.line + ctx.val; |
| 10675 | len = ctx.vlen; |
| 10676 | while (len--) |
| 10677 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10678 | } |
| 10679 | |
| 10680 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10681 | 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] | 10682 | beg = http_get_path(txn); |
| 10683 | if (!beg) |
| 10684 | beg = end; |
| 10685 | |
| 10686 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10687 | |
| 10688 | if (beg < ptr && *beg == '/') { |
| 10689 | while (beg < ptr) |
| 10690 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10691 | } |
| 10692 | hash = full_hash(hash); |
| 10693 | |
| 10694 | smp->type = SMP_T_UINT; |
| 10695 | smp->data.uint = hash; |
| 10696 | smp->flags = SMP_F_VOL_1ST; |
| 10697 | return 1; |
| 10698 | } |
| 10699 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10700 | /* 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] | 10701 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10702 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10703 | * 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] | 10704 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10705 | * 8 bytes would still work. |
| 10706 | */ |
| 10707 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10708 | smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10709 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10710 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10711 | struct chunk *temp; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 10712 | struct connection *cli_conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10713 | |
| 10714 | if (!cli_conn) |
| 10715 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10716 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10717 | if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10718 | return 0; |
| 10719 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10720 | temp = get_trash_chunk(); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10721 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10722 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10723 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10724 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10725 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10726 | 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] | 10727 | temp->len += 4; |
| 10728 | break; |
| 10729 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10730 | 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] | 10731 | temp->len += 16; |
| 10732 | break; |
| 10733 | default: |
| 10734 | return 0; |
| 10735 | } |
| 10736 | |
| 10737 | smp->data.str = *temp; |
| 10738 | smp->type = SMP_T_BIN; |
| 10739 | return 1; |
| 10740 | } |
| 10741 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10742 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10743 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10744 | * of type string carrying the whole query string. |
| 10745 | */ |
| 10746 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10747 | smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10748 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10749 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10750 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10751 | char *ptr, *end; |
| 10752 | |
| 10753 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10754 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10755 | txn = strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10756 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10757 | end = ptr + txn->req.sl.rq.u_l; |
| 10758 | |
| 10759 | /* look up the '?' */ |
| 10760 | do { |
| 10761 | if (ptr == end) |
| 10762 | return 0; |
| 10763 | } while (*ptr++ != '?'); |
| 10764 | |
| 10765 | smp->type = SMP_T_STR; |
| 10766 | smp->data.str.str = ptr; |
| 10767 | smp->data.str.len = end - ptr; |
| 10768 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10769 | return 1; |
| 10770 | } |
| 10771 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10772 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10773 | smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10774 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10775 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10776 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10777 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10778 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10779 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10780 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10781 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10782 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10783 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10784 | return 1; |
| 10785 | } |
| 10786 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10787 | /* return a valid test if the current request is the first one on the connection */ |
| 10788 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10789 | smp_fetch_http_first_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10790 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10791 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10792 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10793 | smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10794 | return 1; |
| 10795 | } |
| 10796 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10797 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10798 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10799 | smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10800 | 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] | 10801 | { |
| 10802 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10803 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10804 | return 0; |
| 10805 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10806 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10807 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10808 | if (!get_http_auth(strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10809 | return 0; |
| 10810 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10811 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10812 | smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10813 | return 1; |
| 10814 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10815 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10816 | /* Accepts exactly 1 argument of type userlist */ |
| 10817 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10818 | smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10819 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10820 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10821 | if (!args || args->type != ARGT_USR) |
| 10822 | return 0; |
| 10823 | |
| 10824 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10825 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10826 | if (!get_http_auth(strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10827 | return 0; |
| 10828 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10829 | /* if the user does not belong to the userlist or has a wrong password, |
| 10830 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10831 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10832 | */ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10833 | if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10834 | return 0; |
| 10835 | |
| 10836 | /* pat_match_auth() will need the user list */ |
| 10837 | smp->ctx.a[0] = args->data.usr; |
| 10838 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10839 | smp->type = SMP_T_STR; |
| 10840 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10841 | smp->data.str.str = strm->txn->auth.user; |
| 10842 | smp->data.str.len = strlen(strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10843 | |
| 10844 | return 1; |
| 10845 | } |
| 10846 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10847 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10848 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10849 | * the cookie value is returned into *value and *value_l, and the function |
| 10850 | * returns a pointer to the next pointer to search from if the value was found. |
| 10851 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10852 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10853 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10854 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10855 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10856 | * list is expected. |
| 10857 | */ |
| 10858 | static char * |
| 10859 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10860 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10861 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10862 | { |
| 10863 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10864 | char *next; |
| 10865 | |
| 10866 | /* we search at least a cookie name followed by an equal, and more |
| 10867 | * generally something like this : |
| 10868 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10869 | */ |
| 10870 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10871 | /* Iterate through all cookies on this line */ |
| 10872 | |
| 10873 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10874 | att_beg++; |
| 10875 | |
| 10876 | /* find att_end : this is the first character after the last non |
| 10877 | * space before the equal. It may be equal to hdr_end. |
| 10878 | */ |
| 10879 | equal = att_end = att_beg; |
| 10880 | |
| 10881 | while (equal < hdr_end) { |
| 10882 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10883 | break; |
| 10884 | if (http_is_spht[(unsigned char)*equal++]) |
| 10885 | continue; |
| 10886 | att_end = equal; |
| 10887 | } |
| 10888 | |
| 10889 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10890 | * is between <att_beg> and <equal>, both may be identical. |
| 10891 | */ |
| 10892 | |
| 10893 | /* look for end of cookie if there is an equal sign */ |
| 10894 | if (equal < hdr_end && *equal == '=') { |
| 10895 | /* look for the beginning of the value */ |
| 10896 | val_beg = equal + 1; |
| 10897 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10898 | val_beg++; |
| 10899 | |
| 10900 | /* find the end of the value, respecting quotes */ |
| 10901 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10902 | |
| 10903 | /* make val_end point to the first white space or delimitor after the value */ |
| 10904 | val_end = next; |
| 10905 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10906 | val_end--; |
| 10907 | } else { |
| 10908 | val_beg = val_end = next = equal; |
| 10909 | } |
| 10910 | |
| 10911 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10912 | * they will automatically be removed if a header before them is removed, |
| 10913 | * since they're supposed to be linked together. |
| 10914 | */ |
| 10915 | if (*att_beg == '$') |
| 10916 | continue; |
| 10917 | |
| 10918 | /* Ignore cookies with no equal sign */ |
| 10919 | if (equal == next) |
| 10920 | continue; |
| 10921 | |
| 10922 | /* Now we have the cookie name between att_beg and att_end, and |
| 10923 | * its value between val_beg and val_end. |
| 10924 | */ |
| 10925 | |
| 10926 | if (att_end - att_beg == cookie_name_l && |
| 10927 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10928 | /* let's return this value and indicate where to go on from */ |
| 10929 | *value = val_beg; |
| 10930 | *value_l = val_end - val_beg; |
| 10931 | return next + 1; |
| 10932 | } |
| 10933 | |
| 10934 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10935 | if (!list) |
| 10936 | break; |
| 10937 | } |
| 10938 | |
| 10939 | return NULL; |
| 10940 | } |
| 10941 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10942 | /* Fetch a captured HTTP request header. The index is the position of |
| 10943 | * the "capture" option in the configuration file |
| 10944 | */ |
| 10945 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10946 | smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10947 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10948 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 10949 | struct proxy *fe = strm_fe(strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10950 | int idx; |
| 10951 | |
| 10952 | if (!args || args->type != ARGT_UINT) |
| 10953 | return 0; |
| 10954 | |
| 10955 | idx = args->data.uint; |
| 10956 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10957 | if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10958 | return 0; |
| 10959 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10960 | smp->type = SMP_T_STR; |
| 10961 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10962 | smp->data.str.str = strm->req_cap[idx]; |
| 10963 | smp->data.str.len = strlen(strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10964 | |
| 10965 | return 1; |
| 10966 | } |
| 10967 | |
| 10968 | /* Fetch a captured HTTP response header. The index is the position of |
| 10969 | * the "capture" option in the configuration file |
| 10970 | */ |
| 10971 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10972 | smp_fetch_capture_header_res(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10973 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10974 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 10975 | struct proxy *fe = strm_fe(strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10976 | int idx; |
| 10977 | |
| 10978 | if (!args || args->type != ARGT_UINT) |
| 10979 | return 0; |
| 10980 | |
| 10981 | idx = args->data.uint; |
| 10982 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10983 | if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10984 | return 0; |
| 10985 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10986 | smp->type = SMP_T_STR; |
| 10987 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10988 | smp->data.str.str = strm->res_cap[idx]; |
| 10989 | smp->data.str.len = strlen(strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10990 | |
| 10991 | return 1; |
| 10992 | } |
| 10993 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10994 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10995 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10996 | smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 10997 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10998 | { |
| 10999 | struct chunk *temp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11000 | struct http_txn *txn = strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11001 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11002 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11003 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11004 | return 0; |
| 11005 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11006 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11007 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11008 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11009 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11010 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11011 | temp = get_trash_chunk(); |
| 11012 | temp->str = txn->uri; |
| 11013 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11014 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11015 | smp->type = SMP_T_STR; |
| 11016 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11017 | |
| 11018 | return 1; |
| 11019 | |
| 11020 | } |
| 11021 | |
| 11022 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11023 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11024 | smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11025 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11026 | { |
| 11027 | struct chunk *temp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11028 | struct http_txn *txn = strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11029 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11030 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11031 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11032 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11033 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11034 | ptr = txn->uri; |
| 11035 | |
| 11036 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11037 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11038 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11039 | if (!*ptr) |
| 11040 | return 0; |
| 11041 | |
| 11042 | ptr++; /* skip the space */ |
| 11043 | |
| 11044 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11045 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11046 | if (!ptr) |
| 11047 | return 0; |
| 11048 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11049 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11050 | |
| 11051 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11052 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11053 | smp->type = SMP_T_STR; |
| 11054 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11055 | |
| 11056 | return 1; |
| 11057 | } |
| 11058 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11059 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11060 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11061 | */ |
| 11062 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11063 | smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11064 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11065 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11066 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11067 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11068 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11069 | return 0; |
| 11070 | |
| 11071 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 11072 | smp->data.str.str = "HTTP/1.1"; |
| 11073 | else |
| 11074 | smp->data.str.str = "HTTP/1.0"; |
| 11075 | |
| 11076 | smp->data.str.len = 8; |
| 11077 | smp->type = SMP_T_STR; |
| 11078 | smp->flags = SMP_F_CONST; |
| 11079 | return 1; |
| 11080 | |
| 11081 | } |
| 11082 | |
| 11083 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11084 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11085 | */ |
| 11086 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11087 | smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11088 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11089 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11090 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11091 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11092 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11093 | return 0; |
| 11094 | |
| 11095 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 11096 | smp->data.str.str = "HTTP/1.1"; |
| 11097 | else |
| 11098 | smp->data.str.str = "HTTP/1.0"; |
| 11099 | |
| 11100 | smp->data.str.len = 8; |
| 11101 | smp->type = SMP_T_STR; |
| 11102 | smp->flags = SMP_F_CONST; |
| 11103 | return 1; |
| 11104 | |
| 11105 | } |
| 11106 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11107 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11108 | /* 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] | 11109 | * 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] | 11110 | * 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] | 11111 | * 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] | 11112 | * 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] | 11113 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11114 | * 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] | 11115 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11116 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11117 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11118 | int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11119 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11120 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11121 | struct http_txn *txn; |
| 11122 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11123 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11124 | const struct http_msg *msg; |
| 11125 | const char *hdr_name; |
| 11126 | int hdr_name_len; |
| 11127 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11128 | int occ = 0; |
| 11129 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11130 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11131 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11132 | return 0; |
| 11133 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11134 | if (!ctx) { |
| 11135 | /* first call */ |
| 11136 | ctx = &static_hdr_ctx; |
| 11137 | ctx->idx = 0; |
| 11138 | smp->ctx.a[2] = ctx; |
| 11139 | } |
| 11140 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11141 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11142 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11143 | txn = strm->txn; |
| 11144 | idx = &strm->txn->hdr_idx; |
| 11145 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11146 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11147 | msg = &txn->req; |
| 11148 | hdr_name = "Cookie"; |
| 11149 | hdr_name_len = 6; |
| 11150 | } else { |
| 11151 | msg = &txn->rsp; |
| 11152 | hdr_name = "Set-Cookie"; |
| 11153 | hdr_name_len = 10; |
| 11154 | } |
| 11155 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11156 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 11157 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11158 | occ = -1; |
| 11159 | |
| 11160 | /* OK so basically here, either we want only one value and it's the |
| 11161 | * last one, or we want to iterate over all of them and we fetch the |
| 11162 | * next one. |
| 11163 | */ |
| 11164 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11165 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11166 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11167 | /* search for the header from the beginning, we must first initialize |
| 11168 | * the search parameters. |
| 11169 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11170 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11171 | ctx->idx = 0; |
| 11172 | } |
| 11173 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11174 | smp->flags |= SMP_F_VOL_HDR; |
| 11175 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11176 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11177 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11178 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11179 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11180 | goto out; |
| 11181 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11182 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11183 | continue; |
| 11184 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11185 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11186 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11187 | } |
| 11188 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11189 | smp->type = SMP_T_STR; |
| 11190 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11191 | 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] | 11192 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11193 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11194 | &smp->data.str.str, |
| 11195 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11196 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11197 | found = 1; |
| 11198 | if (occ >= 0) { |
| 11199 | /* one value was returned into smp->data.str.{str,len} */ |
| 11200 | smp->flags |= SMP_F_NOT_LAST; |
| 11201 | return 1; |
| 11202 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11203 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11204 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11205 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11206 | /* all cookie headers and values were scanned. If we're looking for the |
| 11207 | * last occurrence, we may return it now. |
| 11208 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11209 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11210 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11211 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11212 | } |
| 11213 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11214 | /* 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] | 11215 | * 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] | 11216 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11217 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11218 | */ |
| 11219 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11220 | smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11221 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11222 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11223 | struct http_txn *txn; |
| 11224 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11225 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11226 | const struct http_msg *msg; |
| 11227 | const char *hdr_name; |
| 11228 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11229 | int cnt; |
| 11230 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11231 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11232 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11233 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11234 | return 0; |
| 11235 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11236 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11237 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11238 | txn = strm->txn; |
| 11239 | idx = &strm->txn->hdr_idx; |
| 11240 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11241 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11242 | msg = &txn->req; |
| 11243 | hdr_name = "Cookie"; |
| 11244 | hdr_name_len = 6; |
| 11245 | } else { |
| 11246 | msg = &txn->rsp; |
| 11247 | hdr_name = "Set-Cookie"; |
| 11248 | hdr_name_len = 10; |
| 11249 | } |
| 11250 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11251 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11252 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11253 | ctx.idx = 0; |
| 11254 | cnt = 0; |
| 11255 | |
| 11256 | while (1) { |
| 11257 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11258 | if (!val_beg) { |
| 11259 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11260 | break; |
| 11261 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11262 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11263 | continue; |
| 11264 | |
| 11265 | val_beg = ctx.line + ctx.val; |
| 11266 | val_end = val_beg + ctx.vlen; |
| 11267 | } |
| 11268 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11269 | smp->type = SMP_T_STR; |
| 11270 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11271 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11272 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11273 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11274 | &smp->data.str.str, |
| 11275 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11276 | cnt++; |
| 11277 | } |
| 11278 | } |
| 11279 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11280 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11281 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11282 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11283 | return 1; |
| 11284 | } |
| 11285 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11286 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11287 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11288 | */ |
| 11289 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11290 | smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11291 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11292 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11293 | int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11294 | |
| 11295 | if (ret > 0) { |
| 11296 | smp->type = SMP_T_UINT; |
| 11297 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11298 | } |
| 11299 | |
| 11300 | return ret; |
| 11301 | } |
| 11302 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11303 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11304 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11305 | /************************************************************************/ |
| 11306 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11307 | /* |
| 11308 | * Given a path string and its length, find the position of beginning of the |
| 11309 | * query string. Returns NULL if no query string is found in the path. |
| 11310 | * |
| 11311 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11312 | * |
| 11313 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11314 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11315 | 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] | 11316 | { |
| 11317 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11318 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11319 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11320 | return p ? p + 1 : NULL; |
| 11321 | } |
| 11322 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11323 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11324 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11325 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11326 | } |
| 11327 | |
| 11328 | /* |
| 11329 | * Given a url parameter, find the starting position of the first occurence, |
| 11330 | * or NULL if the parameter is not found. |
| 11331 | * |
| 11332 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11333 | * the function will return query_string+8. |
| 11334 | */ |
| 11335 | static char* |
| 11336 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11337 | char* url_param_name, size_t url_param_name_l, |
| 11338 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11339 | { |
| 11340 | char *pos, *last; |
| 11341 | |
| 11342 | pos = query_string; |
| 11343 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11344 | |
| 11345 | while (pos <= last) { |
| 11346 | if (pos[url_param_name_l] == '=') { |
| 11347 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11348 | return pos; |
| 11349 | pos += url_param_name_l + 1; |
| 11350 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11351 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11352 | pos++; |
| 11353 | pos++; |
| 11354 | } |
| 11355 | return NULL; |
| 11356 | } |
| 11357 | |
| 11358 | /* |
| 11359 | * Given a url parameter name, returns its value and size into *value and |
| 11360 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 11361 | * zero is returned and value/value_l are not touched. |
| 11362 | */ |
| 11363 | static int |
| 11364 | find_url_param_value(char* path, size_t path_l, |
| 11365 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11366 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11367 | { |
| 11368 | char *query_string, *qs_end; |
| 11369 | char *arg_start; |
| 11370 | char *value_start, *value_end; |
| 11371 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11372 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11373 | if (!query_string) |
| 11374 | return 0; |
| 11375 | |
| 11376 | qs_end = path + path_l; |
| 11377 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11378 | url_param_name, url_param_name_l, |
| 11379 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11380 | if (!arg_start) |
| 11381 | return 0; |
| 11382 | |
| 11383 | value_start = arg_start + url_param_name_l + 1; |
| 11384 | value_end = value_start; |
| 11385 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11386 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11387 | value_end++; |
| 11388 | |
| 11389 | *value = value_start; |
| 11390 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11391 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11392 | } |
| 11393 | |
| 11394 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11395 | smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Thierry FOURNIER | f41a809 | 2014-12-07 18:37:57 +0100 | [diff] [blame] | 11396 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11397 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11398 | char delim = '?'; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11399 | struct http_msg *msg; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11400 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11401 | if (!args || args[0].type != ARGT_STR || |
| 11402 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11403 | return 0; |
| 11404 | |
| 11405 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11406 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11407 | msg = &strm->txn->req; |
| 11408 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11409 | if (args[1].type) |
| 11410 | delim = *args[1].data.str.str; |
| 11411 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11412 | if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11413 | args->data.str.str, args->data.str.len, |
| 11414 | &smp->data.str.str, &smp->data.str.len, |
| 11415 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11416 | return 0; |
| 11417 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11418 | smp->type = SMP_T_STR; |
| 11419 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11420 | return 1; |
| 11421 | } |
| 11422 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11423 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11424 | * above). |
| 11425 | */ |
| 11426 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11427 | smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11428 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11429 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11430 | int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11431 | |
| 11432 | if (ret > 0) { |
| 11433 | smp->type = SMP_T_UINT; |
| 11434 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11435 | } |
| 11436 | |
| 11437 | return ret; |
| 11438 | } |
| 11439 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11440 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11441 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11442 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11443 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11444 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11445 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11446 | * high-traffic sites without having to store whole paths. |
| 11447 | * this differs from the base32 functions in that it includes the url parameters |
| 11448 | * as well as the path |
| 11449 | */ |
| 11450 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11451 | smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11452 | 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] | 11453 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11454 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11455 | struct hdr_ctx ctx; |
| 11456 | unsigned int hash = 0; |
| 11457 | char *ptr, *beg, *end; |
| 11458 | int len; |
| 11459 | |
| 11460 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11461 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11462 | txn = strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11463 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11464 | 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] | 11465 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11466 | ptr = ctx.line + ctx.val; |
| 11467 | len = ctx.vlen; |
| 11468 | while (len--) |
| 11469 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11470 | } |
| 11471 | |
| 11472 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11473 | 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] | 11474 | beg = http_get_path(txn); |
| 11475 | if (!beg) |
| 11476 | beg = end; |
| 11477 | |
| 11478 | for (ptr = beg; ptr < end ; ptr++); |
| 11479 | |
| 11480 | if (beg < ptr && *beg == '/') { |
| 11481 | while (beg < ptr) |
| 11482 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11483 | } |
| 11484 | hash = full_hash(hash); |
| 11485 | |
| 11486 | smp->type = SMP_T_UINT; |
| 11487 | smp->data.uint = hash; |
| 11488 | smp->flags = SMP_F_VOL_1ST; |
| 11489 | return 1; |
| 11490 | } |
| 11491 | |
| 11492 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11493 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11494 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11495 | * on the source address length. The URL hash is stored before the address so |
| 11496 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11497 | * 8 bytes would still work. |
| 11498 | */ |
| 11499 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11500 | smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11501 | 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] | 11502 | { |
| 11503 | struct chunk *temp; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 11504 | struct connection *cli_conn = objt_conn(sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11505 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11506 | if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11507 | return 0; |
| 11508 | |
| 11509 | temp = get_trash_chunk(); |
| 11510 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11511 | temp->len += sizeof(smp->data.uint); |
| 11512 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11513 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11514 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11515 | 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] | 11516 | temp->len += 4; |
| 11517 | break; |
| 11518 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11519 | 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] | 11520 | temp->len += 16; |
| 11521 | break; |
| 11522 | default: |
| 11523 | return 0; |
| 11524 | } |
| 11525 | |
| 11526 | smp->data.str = *temp; |
| 11527 | smp->type = SMP_T_BIN; |
| 11528 | return 1; |
| 11529 | } |
| 11530 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11531 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11532 | * keyword. These keywords support an optional positive or negative occurrence |
| 11533 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11534 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11535 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11536 | * error message in case of error, that the caller is responsible for freeing. |
| 11537 | * The initial location must either be freeable or NULL. |
| 11538 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11539 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11540 | { |
| 11541 | 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] | 11542 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11543 | return 0; |
| 11544 | } |
| 11545 | return 1; |
| 11546 | } |
| 11547 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11548 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11549 | * adds an optional offset found in args[0] and emits a string representing |
| 11550 | * the date in RFC-1123/5322 format. |
| 11551 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 11552 | static int sample_conv_http_date(struct stream *stream, const struct arg *args, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 11553 | struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11554 | { |
| 11555 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11556 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11557 | struct chunk *temp; |
| 11558 | struct tm *tm; |
| 11559 | time_t curr_date = smp->data.uint; |
| 11560 | |
| 11561 | /* add offset */ |
| 11562 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11563 | curr_date += args[0].data.sint; |
| 11564 | |
| 11565 | tm = gmtime(&curr_date); |
| 11566 | |
| 11567 | temp = get_trash_chunk(); |
| 11568 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11569 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11570 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11571 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11572 | |
| 11573 | smp->data.str = *temp; |
| 11574 | smp->type = SMP_T_STR; |
| 11575 | return 1; |
| 11576 | } |
| 11577 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11578 | /* Match language range with language tag. RFC2616 14.4: |
| 11579 | * |
| 11580 | * A language-range matches a language-tag if it exactly equals |
| 11581 | * the tag, or if it exactly equals a prefix of the tag such |
| 11582 | * that the first tag character following the prefix is "-". |
| 11583 | * |
| 11584 | * Return 1 if the strings match, else return 0. |
| 11585 | */ |
| 11586 | static inline int language_range_match(const char *range, int range_len, |
| 11587 | const char *tag, int tag_len) |
| 11588 | { |
| 11589 | const char *end = range + range_len; |
| 11590 | const char *tend = tag + tag_len; |
| 11591 | while (range < end) { |
| 11592 | if (*range == '-' && tag == tend) |
| 11593 | return 1; |
| 11594 | if (*range != *tag || tag == tend) |
| 11595 | return 0; |
| 11596 | range++; |
| 11597 | tag++; |
| 11598 | } |
| 11599 | /* Return true only if the last char of the tag is matched. */ |
| 11600 | return tag == tend; |
| 11601 | } |
| 11602 | |
| 11603 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 11604 | static int sample_conv_q_prefered(struct stream *stream, const struct arg *args, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 11605 | struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11606 | { |
| 11607 | const char *al = smp->data.str.str; |
| 11608 | const char *end = al + smp->data.str.len; |
| 11609 | const char *token; |
| 11610 | int toklen; |
| 11611 | int qvalue; |
| 11612 | const char *str; |
| 11613 | const char *w; |
| 11614 | int best_q = 0; |
| 11615 | |
| 11616 | /* Set the constant to the sample, because the output of the |
| 11617 | * function will be peek in the constant configuration string. |
| 11618 | */ |
| 11619 | smp->flags |= SMP_F_CONST; |
| 11620 | smp->data.str.size = 0; |
| 11621 | smp->data.str.str = ""; |
| 11622 | smp->data.str.len = 0; |
| 11623 | |
| 11624 | /* Parse the accept language */ |
| 11625 | while (1) { |
| 11626 | |
| 11627 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11628 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11629 | al++; |
| 11630 | if (al >= end) |
| 11631 | break; |
| 11632 | |
| 11633 | /* Start of the fisrt word. */ |
| 11634 | token = al; |
| 11635 | |
| 11636 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11637 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11638 | al++; |
| 11639 | if (al == token) |
| 11640 | goto expect_comma; |
| 11641 | |
| 11642 | /* Length of the token. */ |
| 11643 | toklen = al - token; |
| 11644 | qvalue = 1000; |
| 11645 | |
| 11646 | /* Check if the token exists in the list. If the token not exists, |
| 11647 | * jump to the next token. |
| 11648 | */ |
| 11649 | str = args[0].data.str.str; |
| 11650 | w = str; |
| 11651 | while (1) { |
| 11652 | if (*str == ';' || *str == '\0') { |
| 11653 | if (language_range_match(token, toklen, w, str-w)) |
| 11654 | goto look_for_q; |
| 11655 | if (*str == '\0') |
| 11656 | goto expect_comma; |
| 11657 | w = str + 1; |
| 11658 | } |
| 11659 | str++; |
| 11660 | } |
| 11661 | goto expect_comma; |
| 11662 | |
| 11663 | look_for_q: |
| 11664 | |
| 11665 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11666 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11667 | al++; |
| 11668 | if (al >= end) |
| 11669 | goto process_value; |
| 11670 | |
| 11671 | /* If ',' is found, process the result */ |
| 11672 | if (*al == ',') |
| 11673 | goto process_value; |
| 11674 | |
| 11675 | /* If the character is different from ';', look |
| 11676 | * for the end of the header part in best effort. |
| 11677 | */ |
| 11678 | if (*al != ';') |
| 11679 | goto expect_comma; |
| 11680 | |
| 11681 | /* Assumes that the char is ';', now expect "q=". */ |
| 11682 | al++; |
| 11683 | |
| 11684 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11685 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11686 | al++; |
| 11687 | if (al >= end) |
| 11688 | goto process_value; |
| 11689 | |
| 11690 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11691 | if (*al != 'q') |
| 11692 | goto process_value; |
| 11693 | al++; |
| 11694 | |
| 11695 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11696 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11697 | al++; |
| 11698 | if (al >= end) |
| 11699 | goto process_value; |
| 11700 | |
| 11701 | /* Expect '='. If no '=', continue in best effort */ |
| 11702 | if (*al != '=') |
| 11703 | goto process_value; |
| 11704 | al++; |
| 11705 | |
| 11706 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11707 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11708 | al++; |
| 11709 | if (al >= end) |
| 11710 | goto process_value; |
| 11711 | |
| 11712 | /* Parse the q value. */ |
| 11713 | qvalue = parse_qvalue(al, &al); |
| 11714 | |
| 11715 | process_value: |
| 11716 | |
| 11717 | /* If the new q value is the best q value, then store the associated |
| 11718 | * language in the response. If qvalue is the biggest value (1000), |
| 11719 | * break the process. |
| 11720 | */ |
| 11721 | if (qvalue > best_q) { |
| 11722 | smp->data.str.str = (char *)w; |
| 11723 | smp->data.str.len = str - w; |
| 11724 | if (qvalue >= 1000) |
| 11725 | break; |
| 11726 | best_q = qvalue; |
| 11727 | } |
| 11728 | |
| 11729 | expect_comma: |
| 11730 | |
| 11731 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11732 | while (al < end && *al != ',') |
| 11733 | al++; |
| 11734 | if (al >= end) |
| 11735 | break; |
| 11736 | |
| 11737 | /* Comma is found, jump it and restart the analyzer. */ |
| 11738 | al++; |
| 11739 | } |
| 11740 | |
| 11741 | /* Set default value if required. */ |
| 11742 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11743 | smp->data.str.str = args[1].data.str.str; |
| 11744 | smp->data.str.len = args[1].data.str.len; |
| 11745 | } |
| 11746 | |
| 11747 | /* Return true only if a matching language was found. */ |
| 11748 | return smp->data.str.len != 0; |
| 11749 | } |
| 11750 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11751 | /* 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] | 11752 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11753 | * the relevant part of the request line accordingly. Then it updates various |
| 11754 | * pointers to the next elements which were moved, and the total buffer length. |
| 11755 | * 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] | 11756 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11757 | * error, though this can be revisited when this code is finally exploited. |
| 11758 | * |
| 11759 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11760 | * query string and 3 to replace uri. |
| 11761 | * |
| 11762 | * In query string case, the mark question '?' must be set at the start of the |
| 11763 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11764 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11765 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11766 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11767 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11768 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11769 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11770 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11771 | int delta; |
| 11772 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11773 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11774 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11775 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11776 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11777 | |
| 11778 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11779 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11780 | txn->req.sl.rq.m_l += delta; |
| 11781 | txn->req.sl.rq.u += delta; |
| 11782 | txn->req.sl.rq.v += delta; |
| 11783 | break; |
| 11784 | |
| 11785 | case 1: // path |
| 11786 | cur_ptr = http_get_path(txn); |
| 11787 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11788 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11789 | |
| 11790 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11791 | 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] | 11792 | cur_end++; |
| 11793 | |
| 11794 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11795 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11796 | txn->req.sl.rq.u_l += delta; |
| 11797 | txn->req.sl.rq.v += delta; |
| 11798 | break; |
| 11799 | |
| 11800 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11801 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11802 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11803 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11804 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11805 | cur_ptr++; |
| 11806 | |
| 11807 | /* skip the question mark or indicate that we must insert it |
| 11808 | * (but only if the format string is not empty then). |
| 11809 | */ |
| 11810 | if (cur_ptr < cur_end) |
| 11811 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11812 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11813 | offset = 0; |
| 11814 | |
| 11815 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11816 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11817 | txn->req.sl.rq.u_l += delta; |
| 11818 | txn->req.sl.rq.v += delta; |
| 11819 | break; |
| 11820 | |
| 11821 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11822 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11823 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11824 | |
| 11825 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11826 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11827 | txn->req.sl.rq.u_l += delta; |
| 11828 | txn->req.sl.rq.v += delta; |
| 11829 | break; |
| 11830 | |
| 11831 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11832 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11833 | } |
| 11834 | |
| 11835 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11836 | 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] | 11837 | txn->req.sl.rq.l += delta; |
| 11838 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11839 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11840 | return 0; |
| 11841 | } |
| 11842 | |
| 11843 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11844 | * builds a string in the trash from the specified format string. It finds |
| 11845 | * the action to be performed in p[2], previously filled by function |
| 11846 | * parse_set_req_line(). The replacement action is excuted by the function |
| 11847 | * http_action_set_req_line_exec(). It always returns 1. If an error occurs |
| 11848 | * the action is canceled, but the rule processing continue. |
| 11849 | */ |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11850 | int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11851 | { |
| 11852 | chunk_reset(&trash); |
| 11853 | |
| 11854 | /* If we have to create a query string, prepare a '?'. */ |
| 11855 | if (*(int *)&rule->arg.act.p[2] == 2) |
| 11856 | trash.str[trash.len++] = '?'; |
| 11857 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]); |
| 11858 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11859 | http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s); |
Thierry FOURNIER | 01c3012 | 2015-03-14 14:14:47 +0100 | [diff] [blame] | 11860 | return 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11861 | } |
| 11862 | |
| 11863 | /* parse an http-request action among : |
| 11864 | * set-method |
| 11865 | * set-path |
| 11866 | * set-query |
| 11867 | * set-uri |
| 11868 | * |
| 11869 | * All of them accept a single argument of type string representing a log-format. |
| 11870 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11871 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 11872 | * It returns 0 on success, < 0 on error. |
| 11873 | */ |
| 11874 | int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 11875 | { |
| 11876 | int cur_arg = *orig_arg; |
| 11877 | |
| 11878 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 11879 | |
| 11880 | switch (args[0][4]) { |
| 11881 | case 'm' : |
| 11882 | *(int *)&rule->arg.act.p[2] = 0; |
| 11883 | rule->action_ptr = http_action_set_req_line; |
| 11884 | break; |
| 11885 | case 'p' : |
| 11886 | *(int *)&rule->arg.act.p[2] = 1; |
| 11887 | rule->action_ptr = http_action_set_req_line; |
| 11888 | break; |
| 11889 | case 'q' : |
| 11890 | *(int *)&rule->arg.act.p[2] = 2; |
| 11891 | rule->action_ptr = http_action_set_req_line; |
| 11892 | break; |
| 11893 | case 'u' : |
| 11894 | *(int *)&rule->arg.act.p[2] = 3; |
| 11895 | rule->action_ptr = http_action_set_req_line; |
| 11896 | break; |
| 11897 | default: |
| 11898 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 11899 | return -1; |
| 11900 | } |
| 11901 | |
| 11902 | if (!*args[cur_arg] || |
| 11903 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11904 | memprintf(err, "expects exactly 1 argument <format>"); |
| 11905 | return -1; |
| 11906 | } |
| 11907 | |
| 11908 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 11909 | proxy->conf.args.ctx = ARGC_HRQ; |
| 11910 | parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP, |
| 11911 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 11912 | proxy->conf.args.file, proxy->conf.args.line); |
| 11913 | |
| 11914 | (*orig_arg)++; |
| 11915 | return 0; |
| 11916 | } |
| 11917 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11918 | /* |
| 11919 | * Return the struct http_req_action_kw associated to a keyword. |
| 11920 | */ |
| 11921 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11922 | { |
| 11923 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11924 | struct http_req_action_kw_list *kw_list; |
| 11925 | int i; |
| 11926 | |
| 11927 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11928 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11929 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11930 | return &kw_list->kw[i]; |
| 11931 | } |
| 11932 | } |
| 11933 | } |
| 11934 | return NULL; |
| 11935 | } |
| 11936 | |
| 11937 | /* |
| 11938 | * Return the struct http_res_action_kw associated to a keyword. |
| 11939 | */ |
| 11940 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11941 | { |
| 11942 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11943 | struct http_res_action_kw_list *kw_list; |
| 11944 | int i; |
| 11945 | |
| 11946 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11947 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11948 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11949 | return &kw_list->kw[i]; |
| 11950 | } |
| 11951 | } |
| 11952 | } |
| 11953 | return NULL; |
| 11954 | } |
| 11955 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11956 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11957 | /* All supported ACL keywords must be declared here. */ |
| 11958 | /************************************************************************/ |
| 11959 | |
| 11960 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11961 | * Please take care of keeping this list alphabetically sorted. |
| 11962 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11963 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11964 | { "base", "base", PAT_MATCH_STR }, |
| 11965 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11966 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11967 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11968 | { "base_end", "base", PAT_MATCH_END }, |
| 11969 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11970 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11971 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11972 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11973 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11974 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11975 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11976 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11977 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11978 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11979 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11980 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11981 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11982 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11983 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11984 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11985 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11986 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11987 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11988 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11989 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11990 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11991 | /* these two declarations uses strings with list storage (in place |
| 11992 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11993 | * and delete functions are relative to the list management. The parse |
| 11994 | * and match method are related to the corresponding fetch methods. This |
| 11995 | * is very particular ACL declaration mode. |
| 11996 | */ |
| 11997 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11998 | { "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] | 11999 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12000 | { "path", "path", PAT_MATCH_STR }, |
| 12001 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12002 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12003 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12004 | { "path_end", "path", PAT_MATCH_END }, |
| 12005 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12006 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12007 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12008 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12009 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12010 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12011 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12012 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12013 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12014 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12015 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12016 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12017 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12018 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12019 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12020 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12021 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12022 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12023 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12024 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12025 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12026 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12027 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12028 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12029 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12030 | { "url", "url", PAT_MATCH_STR }, |
| 12031 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12032 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12033 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12034 | { "url_end", "url", PAT_MATCH_END }, |
| 12035 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12036 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12037 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12038 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12039 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12040 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12041 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12042 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12043 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12044 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12045 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12046 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12047 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12048 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12049 | }}; |
| 12050 | |
| 12051 | /************************************************************************/ |
| 12052 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12053 | /************************************************************************/ |
| 12054 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12055 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12056 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12057 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12058 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12059 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12060 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12061 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12062 | |
| 12063 | /* retrieve these captures from the HTTP logs */ |
| 12064 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12065 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12066 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12067 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12068 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12069 | { "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] | 12070 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12071 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12072 | * are only here to match the ACL's name, are request-only and are used |
| 12073 | * for ACL compatibility only. |
| 12074 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12075 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12076 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12077 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12078 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12079 | |
| 12080 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12081 | * only here to match the ACL's name, are request-only and are used for |
| 12082 | * ACL compatibility only. |
| 12083 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12084 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12085 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12086 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 12087 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12088 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12089 | { "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] | 12090 | { "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] | 12091 | { "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] | 12092 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12093 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12094 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12095 | |
| 12096 | /* HTTP protocol on the request path */ |
| 12097 | { "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] | 12098 | { "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] | 12099 | |
| 12100 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12101 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12102 | { "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] | 12103 | |
| 12104 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12105 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12106 | { "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] | 12107 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12108 | /* 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] | 12109 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12110 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12111 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12112 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12113 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 12114 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12115 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12116 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12117 | { "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] | 12118 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12119 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12120 | |
| 12121 | /* 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] | 12122 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12123 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12124 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12125 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12126 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 12127 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12128 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12129 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12130 | { "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] | 12131 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12132 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12133 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12134 | /* 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] | 12135 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12136 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12137 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12138 | { "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] | 12139 | |
| 12140 | /* 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] | 12141 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12142 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12143 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 12144 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12145 | |
| 12146 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12147 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12148 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12149 | { "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] | 12150 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 12151 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12152 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12153 | { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12154 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12155 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12156 | }}; |
| 12157 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12158 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12159 | /************************************************************************/ |
| 12160 | /* All supported converter keywords must be declared here. */ |
| 12161 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12162 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12163 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12164 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 12165 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12166 | { NULL, NULL, 0, 0, 0 }, |
| 12167 | }}; |
| 12168 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12169 | /************************************************************************/ |
| 12170 | /* All supported http-request action keywords must be declared here. */ |
| 12171 | /************************************************************************/ |
| 12172 | struct http_req_action_kw_list http_req_actions = { |
| 12173 | .scope = "http", |
| 12174 | .kw = { |
| 12175 | { "set-method", parse_set_req_line }, |
| 12176 | { "set-path", parse_set_req_line }, |
| 12177 | { "set-query", parse_set_req_line }, |
| 12178 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12179 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12180 | } |
| 12181 | }; |
| 12182 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12183 | __attribute__((constructor)) |
| 12184 | static void __http_protocol_init(void) |
| 12185 | { |
| 12186 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12187 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12188 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12189 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12190 | } |
| 12191 | |
| 12192 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12193 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12194 | * Local variables: |
| 12195 | * c-indent-level: 8 |
| 12196 | * c-basic-offset: 8 |
| 12197 | * End: |
| 12198 | */ |