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 | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3031 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3032 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3033 | 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] | 3034 | signed long long cl; |
| 3035 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3036 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3037 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3038 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3039 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3040 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3041 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
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; /* parse failure */ |
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 (cl < 0) { |
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; |
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 | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3051 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
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; /* already specified, was different */ |
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 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3057 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3058 | } |
| 3059 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3060 | /* bodyless requests have a known length */ |
| 3061 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3062 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3063 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3064 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3065 | * only change if both the request and the config reference something else. |
| 3066 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3067 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3068 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3069 | * if FE and BE have the same settings (common). The method consists in |
| 3070 | * checking if options changed between the two calls (implying that either |
| 3071 | * one is non-null, or one of them is non-null and we are there for the first |
| 3072 | * time. |
| 3073 | */ |
| 3074 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3075 | ((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] | 3076 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3077 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3078 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3079 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3080 | req->analyse_exp = TICK_ETERNITY; |
| 3081 | return 1; |
| 3082 | |
| 3083 | return_bad_req: |
| 3084 | /* We centralize bad requests processing here */ |
| 3085 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3086 | /* we detected a parsing error. We want to archive this request |
| 3087 | * in the dedicated proxy area for later troubleshooting. |
| 3088 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3089 | 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] | 3090 | } |
| 3091 | |
| 3092 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3093 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3094 | 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] | 3095 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3096 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3097 | if (sess->listener->counters) |
| 3098 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3099 | |
| 3100 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3101 | if (!(s->flags & SF_ERR_MASK)) |
| 3102 | s->flags |= SF_ERR_PRXCOND; |
| 3103 | if (!(s->flags & SF_FINST_MASK)) |
| 3104 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3105 | |
| 3106 | req->analysers = 0; |
| 3107 | req->analyse_exp = TICK_ETERNITY; |
| 3108 | return 0; |
| 3109 | } |
| 3110 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3111 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3112 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3113 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3114 | * and program a response message if something was unexpected. It cannot fail |
| 3115 | * 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] | 3116 | * 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] | 3117 | * 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] | 3118 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3119 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3120 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3121 | { |
| 3122 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3123 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3124 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3125 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3126 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3127 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3128 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3129 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3130 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3131 | appctx = si_appctx(si); |
| 3132 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3133 | appctx->st1 = appctx->st2 = 0; |
| 3134 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3135 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3136 | 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] | 3137 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3138 | |
| 3139 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3140 | lookup = uri + uri_auth->uri_len; |
| 3141 | |
| 3142 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3143 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3144 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3145 | break; |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | if (uri_auth->refresh) { |
| 3150 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3151 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3152 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3153 | break; |
| 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3159 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3160 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3161 | break; |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3166 | if (memcmp(h, ";st=", 4) == 0) { |
| 3167 | int i; |
| 3168 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3169 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3170 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3171 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3172 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3173 | break; |
| 3174 | } |
| 3175 | } |
| 3176 | break; |
| 3177 | } |
| 3178 | } |
| 3179 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3180 | appctx->ctx.stats.scope_str = 0; |
| 3181 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3182 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3183 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3184 | int itx = 0; |
| 3185 | const char *h2; |
| 3186 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3187 | const char *err; |
| 3188 | |
| 3189 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3190 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3191 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3192 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3193 | itx++; |
| 3194 | h++; |
| 3195 | } |
| 3196 | |
| 3197 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3198 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3199 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3200 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3201 | /* 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] | 3202 | memcpy(scope_txt, h2, itx); |
| 3203 | scope_txt[itx] = '\0'; |
| 3204 | err = invalid_char(scope_txt); |
| 3205 | if (err) { |
| 3206 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3207 | appctx->ctx.stats.scope_str = 0; |
| 3208 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3209 | } |
| 3210 | break; |
| 3211 | } |
| 3212 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3213 | |
| 3214 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3215 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3216 | int ret = 1; |
| 3217 | |
| 3218 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3219 | 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] | 3220 | ret = acl_pass(ret); |
| 3221 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3222 | ret = !ret; |
| 3223 | } |
| 3224 | |
| 3225 | if (ret) { |
| 3226 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3227 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3228 | break; |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3233 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3234 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3235 | /* we'll need the request body, possibly after sending 100-continue */ |
| 3236 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3237 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3238 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3239 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3240 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3241 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3242 | } |
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 { |
| 3245 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3246 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3247 | } |
| 3248 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3249 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3250 | return 1; |
| 3251 | } |
| 3252 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3253 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3254 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3255 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3256 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3257 | { |
| 3258 | #ifdef IP_TOS |
| 3259 | if (from.ss_family == AF_INET) |
| 3260 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3261 | #endif |
| 3262 | #ifdef IPV6_TCLASS |
| 3263 | if (from.ss_family == AF_INET6) { |
| 3264 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3265 | /* v4-mapped addresses need IP_TOS */ |
| 3266 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3267 | else |
| 3268 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3269 | } |
| 3270 | #endif |
| 3271 | } |
| 3272 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3273 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3274 | const char* name, unsigned int name_len, |
| 3275 | const char *str, struct my_regex *re, |
| 3276 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3277 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3278 | struct hdr_ctx ctx; |
| 3279 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3280 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3281 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3282 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3283 | struct chunk *output = get_trash_chunk(); |
| 3284 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3285 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3286 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3287 | /* Choose the header browsing function. */ |
| 3288 | switch (action) { |
| 3289 | case HTTP_REQ_ACT_REPLACE_VAL: |
| 3290 | case HTTP_RES_ACT_REPLACE_VAL: |
| 3291 | http_find_hdr_func = http_find_header2; |
| 3292 | break; |
| 3293 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3294 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3295 | http_find_hdr_func = http_find_full_header2; |
| 3296 | break; |
| 3297 | default: /* impossible */ |
| 3298 | return -1; |
| 3299 | } |
| 3300 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3301 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3302 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3303 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3304 | char *val = ctx.line + ctx.val; |
| 3305 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3306 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3307 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3308 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3309 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3310 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3311 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3312 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3313 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3314 | 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] | 3315 | |
| 3316 | hdr->len += delta; |
| 3317 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3318 | |
| 3319 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3320 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | return 0; |
| 3324 | } |
| 3325 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3326 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3327 | const char* name, unsigned int name_len, |
| 3328 | struct list *fmt, struct my_regex *re, |
| 3329 | int action) |
| 3330 | { |
| 3331 | struct chunk *replace = get_trash_chunk(); |
| 3332 | |
| 3333 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3334 | if (replace->len >= replace->size - 1) |
| 3335 | return -1; |
| 3336 | |
| 3337 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3338 | } |
| 3339 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3340 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3341 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3342 | * further processing of the request (auth, deny, ...), and defaults to |
| 3343 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3344 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3345 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3346 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3347 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3348 | 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] | 3349 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3350 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3351 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3352 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3353 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3354 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3355 | const char *auth_realm; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3356 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3357 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3358 | * resume condition. If a resume is needed it is always in the action |
| 3359 | * and never in the ACL or converters. In this case, we initialise the |
| 3360 | * current rule, and go to the action execution point. |
| 3361 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3362 | if (s->current_rule) { |
| 3363 | rule = s->current_rule; |
| 3364 | s->current_rule = NULL; |
| 3365 | if (s->current_rule_list == rules) |
| 3366 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3367 | } |
| 3368 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3369 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3370 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3371 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3372 | continue; |
| 3373 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3374 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3375 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3376 | int ret; |
| 3377 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3378 | 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] | 3379 | ret = acl_pass(ret); |
| 3380 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3381 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3382 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3383 | |
| 3384 | if (!ret) /* condition not matched */ |
| 3385 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3388 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3389 | switch (rule->action) { |
| 3390 | case HTTP_REQ_ACT_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3391 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3392 | |
| 3393 | case HTTP_REQ_ACT_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3394 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3395 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3396 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3397 | case HTTP_REQ_ACT_TARPIT: |
| 3398 | txn->flags |= TX_CLTARPIT; |
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 | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3401 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3402 | case HTTP_REQ_ACT_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3403 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3404 | auth_realm = rule->arg.auth.realm; |
| 3405 | if (!auth_realm) { |
| 3406 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3407 | auth_realm = STATS_DEFAULT_REALM; |
| 3408 | else |
| 3409 | auth_realm = px->id; |
| 3410 | } |
| 3411 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3412 | * count one error, because normal browsing won't significantly |
| 3413 | * increase the counter but brute force attempts will. |
| 3414 | */ |
| 3415 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3416 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3417 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3418 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3419 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3420 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3421 | case HTTP_REQ_ACT_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3422 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3423 | return HTTP_RULE_RES_BADREQ; |
| 3424 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3425 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3426 | case HTTP_REQ_ACT_SET_NICE: |
| 3427 | s->task->nice = rule->arg.nice; |
| 3428 | break; |
| 3429 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3430 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3431 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3432 | 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] | 3433 | break; |
| 3434 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3435 | case HTTP_REQ_ACT_SET_MARK: |
| 3436 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3437 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3438 | 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] | 3439 | #endif |
| 3440 | break; |
| 3441 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3442 | case HTTP_REQ_ACT_SET_LOGL: |
| 3443 | s->logs.level = rule->arg.loglevel; |
| 3444 | break; |
| 3445 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3446 | case HTTP_REQ_ACT_REPLACE_HDR: |
| 3447 | case HTTP_REQ_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3448 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3449 | rule->arg.hdr_add.name_len, |
| 3450 | &rule->arg.hdr_add.fmt, |
| 3451 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3452 | return HTTP_RULE_RES_BADREQ; |
| 3453 | break; |
| 3454 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3455 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3456 | ctx.idx = 0; |
| 3457 | /* remove all occurrences of the header */ |
| 3458 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3459 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3460 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3461 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3462 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3463 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3464 | case HTTP_REQ_ACT_SET_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3465 | case HTTP_REQ_ACT_ADD_HDR: |
| 3466 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3467 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3468 | trash.len = rule->arg.hdr_add.name_len; |
| 3469 | trash.str[trash.len++] = ':'; |
| 3470 | trash.str[trash.len++] = ' '; |
| 3471 | 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] | 3472 | |
| 3473 | if (rule->action == HTTP_REQ_ACT_SET_HDR) { |
| 3474 | /* remove all occurrences of the header */ |
| 3475 | ctx.idx = 0; |
| 3476 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3477 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3478 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3479 | } |
| 3480 | } |
| 3481 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3482 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3483 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3484 | |
| 3485 | case HTTP_REQ_ACT_DEL_ACL: |
| 3486 | case HTTP_REQ_ACT_DEL_MAP: { |
| 3487 | struct pat_ref *ref; |
| 3488 | char *key; |
| 3489 | int len; |
| 3490 | |
| 3491 | /* collect reference */ |
| 3492 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3493 | if (!ref) |
| 3494 | continue; |
| 3495 | |
| 3496 | /* collect key */ |
| 3497 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3498 | key = trash.str; |
| 3499 | key[len] = '\0'; |
| 3500 | |
| 3501 | /* perform update */ |
| 3502 | /* returned code: 1=ok, 0=ko */ |
| 3503 | pat_ref_delete(ref, key); |
| 3504 | |
| 3505 | break; |
| 3506 | } |
| 3507 | |
| 3508 | case HTTP_REQ_ACT_ADD_ACL: { |
| 3509 | struct pat_ref *ref; |
| 3510 | char *key; |
| 3511 | struct chunk *trash_key; |
| 3512 | int len; |
| 3513 | |
| 3514 | trash_key = get_trash_chunk(); |
| 3515 | |
| 3516 | /* collect reference */ |
| 3517 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3518 | if (!ref) |
| 3519 | continue; |
| 3520 | |
| 3521 | /* collect key */ |
| 3522 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3523 | key = trash_key->str; |
| 3524 | key[len] = '\0'; |
| 3525 | |
| 3526 | /* perform update */ |
| 3527 | /* add entry only if it does not already exist */ |
| 3528 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3529 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3530 | |
| 3531 | break; |
| 3532 | } |
| 3533 | |
| 3534 | case HTTP_REQ_ACT_SET_MAP: { |
| 3535 | struct pat_ref *ref; |
| 3536 | char *key, *value; |
| 3537 | struct chunk *trash_key, *trash_value; |
| 3538 | int len; |
| 3539 | |
| 3540 | trash_key = get_trash_chunk(); |
| 3541 | trash_value = get_trash_chunk(); |
| 3542 | |
| 3543 | /* collect reference */ |
| 3544 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3545 | if (!ref) |
| 3546 | continue; |
| 3547 | |
| 3548 | /* collect key */ |
| 3549 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3550 | key = trash_key->str; |
| 3551 | key[len] = '\0'; |
| 3552 | |
| 3553 | /* collect value */ |
| 3554 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3555 | value = trash_value->str; |
| 3556 | value[len] = '\0'; |
| 3557 | |
| 3558 | /* perform update */ |
| 3559 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3560 | /* update entry if it exists */ |
| 3561 | pat_ref_set(ref, key, value, NULL); |
| 3562 | else |
| 3563 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3564 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3565 | |
| 3566 | break; |
| 3567 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3568 | |
| 3569 | case HTTP_REQ_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3570 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3571 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3572 | return HTTP_RULE_RES_YIELD; |
| 3573 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3574 | break; |
| 3575 | |
| 3576 | case HTTP_REQ_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3577 | rule->action_ptr(rule, px, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3578 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3579 | |
| 3580 | case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX: |
| 3581 | /* Note: only the first valid tracking parameter of each |
| 3582 | * applies. |
| 3583 | */ |
| 3584 | |
| 3585 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3586 | struct stktable *t; |
| 3587 | struct stksess *ts; |
| 3588 | struct stktable_key *key; |
| 3589 | void *ptr; |
| 3590 | |
| 3591 | t = rule->act_prm.trk_ctr.table.t; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3592 | 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] | 3593 | |
| 3594 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3595 | 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] | 3596 | |
| 3597 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3598 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3599 | if (ptr) |
| 3600 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3601 | |
| 3602 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3603 | if (ptr) |
| 3604 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3605 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3606 | |
| 3607 | 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] | 3608 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3609 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3610 | } |
| 3611 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3612 | } |
| 3613 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3614 | |
| 3615 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3616 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3617 | } |
| 3618 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3619 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3620 | /* Executes the http-response rules <rules> for stream <s>, proxy <px> and |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3621 | * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD |
| 3622 | * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the |
| 3623 | * evaluation of next rule list. If *STOP is returned, the process must stop |
| 3624 | * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3625 | * rule. If *YIELD is returned, the czller must call again the function with |
| 3626 | * the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3627 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3628 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3629 | 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] | 3630 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3631 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3632 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3633 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3634 | struct http_res_rule *rule; |
| 3635 | struct hdr_ctx ctx; |
| 3636 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3637 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3638 | * resume condition. If a resume is needed it is always in the action |
| 3639 | * and never in the ACL or converters. In this case, we initialise the |
| 3640 | * current rule, and go to the action execution point. |
| 3641 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3642 | if (s->current_rule) { |
| 3643 | rule = s->current_rule; |
| 3644 | s->current_rule = NULL; |
| 3645 | if (s->current_rule_list == rules) |
| 3646 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3647 | } |
| 3648 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3649 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3650 | list_for_each_entry(rule, rules, list) { |
| 3651 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3652 | continue; |
| 3653 | |
| 3654 | /* check optional condition */ |
| 3655 | if (rule->cond) { |
| 3656 | int ret; |
| 3657 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3658 | 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] | 3659 | ret = acl_pass(ret); |
| 3660 | |
| 3661 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3662 | ret = !ret; |
| 3663 | |
| 3664 | if (!ret) /* condition not matched */ |
| 3665 | continue; |
| 3666 | } |
| 3667 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3668 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3669 | switch (rule->action) { |
| 3670 | case HTTP_RES_ACT_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3671 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3672 | |
| 3673 | case HTTP_RES_ACT_DENY: |
| 3674 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3675 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3676 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3677 | case HTTP_RES_ACT_SET_NICE: |
| 3678 | s->task->nice = rule->arg.nice; |
| 3679 | break; |
| 3680 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3681 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3682 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3683 | 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] | 3684 | break; |
| 3685 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3686 | case HTTP_RES_ACT_SET_MARK: |
| 3687 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3688 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3689 | 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] | 3690 | #endif |
| 3691 | break; |
| 3692 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3693 | case HTTP_RES_ACT_SET_LOGL: |
| 3694 | s->logs.level = rule->arg.loglevel; |
| 3695 | break; |
| 3696 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3697 | case HTTP_RES_ACT_REPLACE_HDR: |
| 3698 | case HTTP_RES_ACT_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3699 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3700 | rule->arg.hdr_add.name_len, |
| 3701 | &rule->arg.hdr_add.fmt, |
| 3702 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3703 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3704 | break; |
| 3705 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3706 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3707 | ctx.idx = 0; |
| 3708 | /* remove all occurrences of the header */ |
| 3709 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3710 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3711 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3712 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3713 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3714 | |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3715 | case HTTP_RES_ACT_SET_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3716 | case HTTP_RES_ACT_ADD_HDR: |
| 3717 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3718 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3719 | trash.len = rule->arg.hdr_add.name_len; |
| 3720 | trash.str[trash.len++] = ':'; |
| 3721 | trash.str[trash.len++] = ' '; |
| 3722 | 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] | 3723 | |
| 3724 | if (rule->action == HTTP_RES_ACT_SET_HDR) { |
| 3725 | /* remove all occurrences of the header */ |
| 3726 | ctx.idx = 0; |
| 3727 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3728 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3729 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3730 | } |
| 3731 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3732 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3733 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3734 | |
| 3735 | case HTTP_RES_ACT_DEL_ACL: |
| 3736 | case HTTP_RES_ACT_DEL_MAP: { |
| 3737 | struct pat_ref *ref; |
| 3738 | char *key; |
| 3739 | int len; |
| 3740 | |
| 3741 | /* collect reference */ |
| 3742 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3743 | if (!ref) |
| 3744 | continue; |
| 3745 | |
| 3746 | /* collect key */ |
| 3747 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3748 | key = trash.str; |
| 3749 | key[len] = '\0'; |
| 3750 | |
| 3751 | /* perform update */ |
| 3752 | /* returned code: 1=ok, 0=ko */ |
| 3753 | pat_ref_delete(ref, key); |
| 3754 | |
| 3755 | break; |
| 3756 | } |
| 3757 | |
| 3758 | case HTTP_RES_ACT_ADD_ACL: { |
| 3759 | struct pat_ref *ref; |
| 3760 | char *key; |
| 3761 | struct chunk *trash_key; |
| 3762 | int len; |
| 3763 | |
| 3764 | trash_key = get_trash_chunk(); |
| 3765 | |
| 3766 | /* collect reference */ |
| 3767 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3768 | if (!ref) |
| 3769 | continue; |
| 3770 | |
| 3771 | /* collect key */ |
| 3772 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3773 | key = trash_key->str; |
| 3774 | key[len] = '\0'; |
| 3775 | |
| 3776 | /* perform update */ |
| 3777 | /* check if the entry already exists */ |
| 3778 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3779 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3780 | |
| 3781 | break; |
| 3782 | } |
| 3783 | |
| 3784 | case HTTP_RES_ACT_SET_MAP: { |
| 3785 | struct pat_ref *ref; |
| 3786 | char *key, *value; |
| 3787 | struct chunk *trash_key, *trash_value; |
| 3788 | int len; |
| 3789 | |
| 3790 | trash_key = get_trash_chunk(); |
| 3791 | trash_value = get_trash_chunk(); |
| 3792 | |
| 3793 | /* collect reference */ |
| 3794 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3795 | if (!ref) |
| 3796 | continue; |
| 3797 | |
| 3798 | /* collect key */ |
| 3799 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3800 | key = trash_key->str; |
| 3801 | key[len] = '\0'; |
| 3802 | |
| 3803 | /* collect value */ |
| 3804 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3805 | value = trash_value->str; |
| 3806 | value[len] = '\0'; |
| 3807 | |
| 3808 | /* perform update */ |
| 3809 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3810 | /* update entry if it exists */ |
| 3811 | pat_ref_set(ref, key, value, NULL); |
| 3812 | else |
| 3813 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3814 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3815 | |
| 3816 | break; |
| 3817 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3818 | |
| 3819 | case HTTP_RES_ACT_CUSTOM_CONT: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3820 | if (!rule->action_ptr(rule, px, s)) { |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3821 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3822 | return HTTP_RULE_RES_YIELD; |
| 3823 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3824 | break; |
| 3825 | |
| 3826 | case HTTP_RES_ACT_CUSTOM_STOP: |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3827 | rule->action_ptr(rule, px, s); |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3828 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3833 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3837 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3838 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3839 | * as too large a request to build a valid response. |
| 3840 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3841 | 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] | 3842 | { |
| 3843 | struct http_msg *msg = &txn->req; |
| 3844 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3845 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3846 | |
| 3847 | /* build redirect message */ |
| 3848 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3849 | case 308: |
| 3850 | msg_fmt = HTTP_308; |
| 3851 | break; |
| 3852 | case 307: |
| 3853 | msg_fmt = HTTP_307; |
| 3854 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3855 | case 303: |
| 3856 | msg_fmt = HTTP_303; |
| 3857 | break; |
| 3858 | case 301: |
| 3859 | msg_fmt = HTTP_301; |
| 3860 | break; |
| 3861 | case 302: |
| 3862 | default: |
| 3863 | msg_fmt = HTTP_302; |
| 3864 | break; |
| 3865 | } |
| 3866 | |
| 3867 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3868 | return 0; |
| 3869 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3870 | location = trash.str + trash.len; |
| 3871 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3872 | switch(rule->type) { |
| 3873 | case REDIRECT_TYPE_SCHEME: { |
| 3874 | const char *path; |
| 3875 | const char *host; |
| 3876 | struct hdr_ctx ctx; |
| 3877 | int pathlen; |
| 3878 | int hostlen; |
| 3879 | |
| 3880 | host = ""; |
| 3881 | hostlen = 0; |
| 3882 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3883 | 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] | 3884 | host = ctx.line + ctx.val; |
| 3885 | hostlen = ctx.vlen; |
| 3886 | } |
| 3887 | |
| 3888 | path = http_get_path(txn); |
| 3889 | /* build message using path */ |
| 3890 | if (path) { |
| 3891 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3892 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3893 | int qs = 0; |
| 3894 | while (qs < pathlen) { |
| 3895 | if (path[qs] == '?') { |
| 3896 | pathlen = qs; |
| 3897 | break; |
| 3898 | } |
| 3899 | qs++; |
| 3900 | } |
| 3901 | } |
| 3902 | } else { |
| 3903 | path = "/"; |
| 3904 | pathlen = 1; |
| 3905 | } |
| 3906 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3907 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3908 | /* check if we can add scheme + "://" + host + path */ |
| 3909 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3910 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3911 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3912 | /* add scheme */ |
| 3913 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3914 | trash.len += rule->rdr_len; |
| 3915 | } |
| 3916 | else { |
| 3917 | /* add scheme with executing log format */ |
| 3918 | 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] | 3919 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3920 | /* check if we can add scheme + "://" + host + path */ |
| 3921 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3922 | return 0; |
| 3923 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3924 | /* add "://" */ |
| 3925 | memcpy(trash.str + trash.len, "://", 3); |
| 3926 | trash.len += 3; |
| 3927 | |
| 3928 | /* add host */ |
| 3929 | memcpy(trash.str + trash.len, host, hostlen); |
| 3930 | trash.len += hostlen; |
| 3931 | |
| 3932 | /* add path */ |
| 3933 | memcpy(trash.str + trash.len, path, pathlen); |
| 3934 | trash.len += pathlen; |
| 3935 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3936 | /* 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] | 3937 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3938 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3939 | if (trash.len > trash.size - 5) |
| 3940 | return 0; |
| 3941 | trash.str[trash.len] = '/'; |
| 3942 | trash.len++; |
| 3943 | } |
| 3944 | |
| 3945 | break; |
| 3946 | } |
| 3947 | case REDIRECT_TYPE_PREFIX: { |
| 3948 | const char *path; |
| 3949 | int pathlen; |
| 3950 | |
| 3951 | path = http_get_path(txn); |
| 3952 | /* build message using path */ |
| 3953 | if (path) { |
| 3954 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3955 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3956 | int qs = 0; |
| 3957 | while (qs < pathlen) { |
| 3958 | if (path[qs] == '?') { |
| 3959 | pathlen = qs; |
| 3960 | break; |
| 3961 | } |
| 3962 | qs++; |
| 3963 | } |
| 3964 | } |
| 3965 | } else { |
| 3966 | path = "/"; |
| 3967 | pathlen = 1; |
| 3968 | } |
| 3969 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3970 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3971 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3972 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3973 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3974 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3975 | * add anything, otherwise it makes it hard for the user to |
| 3976 | * configure a self-redirection. |
| 3977 | */ |
| 3978 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3979 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3980 | trash.len += rule->rdr_len; |
| 3981 | } |
| 3982 | } |
| 3983 | else { |
| 3984 | /* add prefix with executing log format */ |
| 3985 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3986 | |
| 3987 | /* Check length */ |
| 3988 | if (trash.len + pathlen > trash.size - 4) |
| 3989 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | /* add path */ |
| 3993 | memcpy(trash.str + trash.len, path, pathlen); |
| 3994 | trash.len += pathlen; |
| 3995 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3996 | /* 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] | 3997 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3998 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3999 | if (trash.len > trash.size - 5) |
| 4000 | return 0; |
| 4001 | trash.str[trash.len] = '/'; |
| 4002 | trash.len++; |
| 4003 | } |
| 4004 | |
| 4005 | break; |
| 4006 | } |
| 4007 | case REDIRECT_TYPE_LOCATION: |
| 4008 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4009 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4010 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4011 | return 0; |
| 4012 | |
| 4013 | /* add location */ |
| 4014 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4015 | trash.len += rule->rdr_len; |
| 4016 | } |
| 4017 | else { |
| 4018 | /* add location with executing log format */ |
| 4019 | 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] | 4020 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4021 | /* Check left length */ |
| 4022 | if (trash.len > trash.size - 4) |
| 4023 | return 0; |
| 4024 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4025 | break; |
| 4026 | } |
| 4027 | |
| 4028 | if (rule->cookie_len) { |
| 4029 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4030 | trash.len += 14; |
| 4031 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4032 | trash.len += rule->cookie_len; |
| 4033 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4034 | trash.len += 2; |
| 4035 | } |
| 4036 | |
| 4037 | /* add end of headers and the keep-alive/close status. |
| 4038 | * We may choose to set keep-alive if the Location begins |
| 4039 | * with a slash, because the client will come back to the |
| 4040 | * same server. |
| 4041 | */ |
| 4042 | txn->status = rule->code; |
| 4043 | /* let's log the request time */ |
| 4044 | s->logs.tv_request = now; |
| 4045 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4046 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4047 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 4048 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 4049 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4050 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4051 | /* keep-alive possible */ |
| 4052 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 4053 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4054 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4055 | trash.len += 30; |
| 4056 | } else { |
| 4057 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4058 | trash.len += 24; |
| 4059 | } |
| 4060 | } |
| 4061 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4062 | trash.len += 4; |
| 4063 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 4064 | /* "eat" the request */ |
| 4065 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 4066 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4067 | msg->sov = 0; |
| 4068 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4069 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4070 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4071 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4072 | } else { |
| 4073 | /* keep-alive not possible */ |
| 4074 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4075 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4076 | trash.len += 29; |
| 4077 | } else { |
| 4078 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4079 | trash.len += 23; |
| 4080 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4081 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4082 | txn->req.chn->analysers = 0; |
| 4083 | } |
| 4084 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4085 | if (!(s->flags & SF_ERR_MASK)) |
| 4086 | s->flags |= SF_ERR_LOCAL; |
| 4087 | if (!(s->flags & SF_FINST_MASK)) |
| 4088 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4089 | |
| 4090 | return 1; |
| 4091 | } |
| 4092 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4093 | /* This stream analyser runs all HTTP request processing which is common to |
| 4094 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4095 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4096 | * 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] | 4097 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4098 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4099 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4100 | 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] | 4101 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4102 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4103 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4104 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4105 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4106 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4107 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4108 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4109 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4110 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4111 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4112 | } |
| 4113 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4114 | 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] | 4115 | now_ms, __FUNCTION__, |
| 4116 | s, |
| 4117 | req, |
| 4118 | req->rex, req->wex, |
| 4119 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4120 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4121 | req->analysers); |
| 4122 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4123 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4124 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4125 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4126 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4127 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4128 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4129 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4130 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4131 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4132 | goto return_prx_yield; |
| 4133 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4134 | case HTTP_RULE_RES_CONT: |
| 4135 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4136 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4137 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4138 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4139 | if (txn->flags & TX_CLTARPIT) |
| 4140 | goto tarpit; |
| 4141 | goto deny; |
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_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4144 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4145 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4146 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4147 | goto done; |
| 4148 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4149 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4150 | goto return_bad_req; |
| 4151 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4152 | } |
| 4153 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4154 | /* OK at this stage, we know that the request was accepted according to |
| 4155 | * the http-request rules, we can check for the stats. Note that the |
| 4156 | * URI is detected *before* the req* rules in order not to be affected |
| 4157 | * by a possible reqrep, while they are processed *after* so that a |
| 4158 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4159 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4160 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4161 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4162 | 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] | 4163 | txn->status = 500; |
| 4164 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4165 | 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] | 4166 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4167 | if (!(s->flags & SF_ERR_MASK)) |
| 4168 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4169 | goto return_prx_cond; |
| 4170 | } |
| 4171 | |
| 4172 | /* parse the whole stats request and extract the relevant information */ |
| 4173 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4174 | 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] | 4175 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4176 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4177 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4178 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4179 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4180 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4181 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4182 | } |
| 4183 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4184 | /* evaluate the req* rules except reqadd */ |
| 4185 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4186 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4187 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4188 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4189 | if (txn->flags & TX_CLDENY) |
| 4190 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4191 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4192 | if (txn->flags & TX_CLTARPIT) |
| 4193 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4194 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4195 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4196 | /* add request headers from the rule sets in the same order */ |
| 4197 | list_for_each_entry(wl, &px->req_add, list) { |
| 4198 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4199 | 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] | 4200 | ret = acl_pass(ret); |
| 4201 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4202 | ret = !ret; |
| 4203 | if (!ret) |
| 4204 | continue; |
| 4205 | } |
| 4206 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4207 | 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] | 4208 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4209 | } |
| 4210 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4211 | |
| 4212 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4213 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4214 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4215 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4216 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4217 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4218 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4219 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4220 | if (!(s->flags & SF_FINST_MASK)) |
| 4221 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4222 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4223 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4224 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4225 | select_compression_request_header(s, req->buf); |
| 4226 | |
| 4227 | /* 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] | 4228 | 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] | 4229 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4230 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4231 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4232 | /* check whether we have some ACLs set to redirect this request */ |
| 4233 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4234 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4235 | int ret; |
| 4236 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4237 | 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] | 4238 | ret = acl_pass(ret); |
| 4239 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4240 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4241 | if (!ret) |
| 4242 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4243 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4244 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4245 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4246 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4247 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4248 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4249 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4250 | * If this happens, then the data will not come immediately, so we must |
| 4251 | * send all what we have without waiting. Note that due to the small gain |
| 4252 | * 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] | 4253 | * 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] | 4254 | * itself once used. |
| 4255 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4256 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4257 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4258 | done: /* done with this analyser, continue with next ones that the calling |
| 4259 | * points will have set, if any. |
| 4260 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4261 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4262 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4263 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4264 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4265 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4266 | tarpit: |
| 4267 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4268 | * which may be the same as the connect timeout if unspecified. |
| 4269 | * If unset, then set it to zero because we really want it to |
| 4270 | * eventually expire. We build the tarpit as an analyser. |
| 4271 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4272 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4273 | |
| 4274 | /* wipe the request out so that we can drop the connection early |
| 4275 | * if the client closes first. |
| 4276 | */ |
| 4277 | channel_dont_connect(req); |
| 4278 | req->analysers = 0; /* remove switching rules etc... */ |
| 4279 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4280 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4281 | if (!req->analyse_exp) |
| 4282 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4283 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4284 | sess->fe->fe_counters.denied_req++; |
| 4285 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4286 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4287 | if (sess->listener->counters) |
| 4288 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4289 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4290 | |
| 4291 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4292 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4293 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4294 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4295 | 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] | 4296 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4297 | sess->fe->fe_counters.denied_req++; |
| 4298 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4299 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4300 | if (sess->listener->counters) |
| 4301 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4302 | goto return_prx_cond; |
| 4303 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4304 | return_bad_req: |
| 4305 | /* We centralize bad requests processing here */ |
| 4306 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4307 | /* we detected a parsing error. We want to archive this request |
| 4308 | * in the dedicated proxy area for later troubleshooting. |
| 4309 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4310 | 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] | 4311 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4312 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4313 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4314 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4315 | 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] | 4316 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4317 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4318 | if (sess->listener->counters) |
| 4319 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4320 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4321 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4322 | if (!(s->flags & SF_ERR_MASK)) |
| 4323 | s->flags |= SF_ERR_PRXCOND; |
| 4324 | if (!(s->flags & SF_FINST_MASK)) |
| 4325 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4326 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4327 | req->analysers = 0; |
| 4328 | req->analyse_exp = TICK_ETERNITY; |
| 4329 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4330 | |
| 4331 | return_prx_yield: |
| 4332 | channel_dont_connect(req); |
| 4333 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4334 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4335 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4336 | /* This function performs all the processing enabled for the current request. |
| 4337 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4338 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4339 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4340 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4341 | 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] | 4342 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4343 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4344 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4345 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4346 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4347 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4348 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4349 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4350 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4351 | return 0; |
| 4352 | } |
| 4353 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4354 | 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] | 4355 | now_ms, __FUNCTION__, |
| 4356 | s, |
| 4357 | req, |
| 4358 | req->rex, req->wex, |
| 4359 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4360 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4361 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4362 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4363 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4364 | select_compression_request_header(s, req->buf); |
| 4365 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4366 | /* |
| 4367 | * Right now, we know that we have processed the entire headers |
| 4368 | * and that unwanted requests have been filtered out. We can do |
| 4369 | * whatever we want with the remaining request. Also, now we |
| 4370 | * may have separate values for ->fe, ->be. |
| 4371 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4372 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4373 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4374 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4375 | * incoming request. Note that this requires that a connection is |
| 4376 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4377 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4378 | 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] | 4379 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4380 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4381 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4382 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4383 | if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4384 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4385 | txn->status = 500; |
| 4386 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4387 | 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] | 4388 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4389 | if (!(s->flags & SF_ERR_MASK)) |
| 4390 | s->flags |= SF_ERR_RESOURCE; |
| 4391 | if (!(s->flags & SF_FINST_MASK)) |
| 4392 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4393 | |
| 4394 | return 0; |
| 4395 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4396 | |
| 4397 | path = http_get_path(txn); |
| 4398 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4399 | 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] | 4400 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4401 | /* if the path was found, we have to remove everything between |
| 4402 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4403 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4404 | * u_l characters by a single "/". |
| 4405 | */ |
| 4406 | if (path) { |
| 4407 | char *cur_ptr = req->buf->p; |
| 4408 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4409 | int delta; |
| 4410 | |
| 4411 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4412 | http_msg_move_end(&txn->req, delta); |
| 4413 | cur_end += delta; |
| 4414 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4415 | goto return_bad_req; |
| 4416 | } |
| 4417 | else { |
| 4418 | char *cur_ptr = req->buf->p; |
| 4419 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4420 | int delta; |
| 4421 | |
| 4422 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4423 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4424 | http_msg_move_end(&txn->req, delta); |
| 4425 | cur_end += delta; |
| 4426 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4427 | goto return_bad_req; |
| 4428 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4429 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4430 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4431 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4432 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4433 | * Note that doing so might move headers in the request, but |
| 4434 | * the fields will stay coherent and the URI will not move. |
| 4435 | * This should only be performed in the backend. |
| 4436 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4437 | 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] | 4438 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4439 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4440 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4441 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4442 | * 8: the appsession cookie was looked up very early in 1.2, |
| 4443 | * so let's do the same now. |
| 4444 | */ |
| 4445 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4446 | /* It needs to look into the URI unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4447 | 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] | 4448 | 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] | 4449 | } |
| 4450 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4451 | /* add unique-id if "header-unique-id" is specified */ |
| 4452 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4453 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4454 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4455 | goto return_bad_req; |
| 4456 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4457 | 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] | 4458 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4459 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4460 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4461 | 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] | 4462 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4463 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4464 | 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] | 4465 | goto return_bad_req; |
| 4466 | } |
| 4467 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4468 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4469 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4470 | * asks for it. |
| 4471 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4472 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4473 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4474 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4475 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4476 | 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] | 4477 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4478 | /* The header is set to be added only if none is present |
| 4479 | * and we found it, so don't do anything. |
| 4480 | */ |
| 4481 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4482 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4483 | /* Add an X-Forwarded-For header unless the source IP is |
| 4484 | * in the 'except' network range. |
| 4485 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4486 | if ((!sess->fe->except_mask.s_addr || |
| 4487 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4488 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4489 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4490 | (((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] | 4491 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4492 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4493 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4494 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4495 | |
| 4496 | /* Note: we rely on the backend to get the header name to be used for |
| 4497 | * x-forwarded-for, because the header is really meant for the backends. |
| 4498 | * However, if the backend did not specify any option, we have to rely |
| 4499 | * on the frontend's header name. |
| 4500 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4501 | if (s->be->fwdfor_hdr_len) { |
| 4502 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4503 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4504 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4505 | len = sess->fe->fwdfor_hdr_len; |
| 4506 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4507 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4508 | 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] | 4509 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4510 | 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] | 4511 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4512 | } |
| 4513 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4514 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4515 | /* FIXME: for the sake of completeness, we should also support |
| 4516 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4517 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4518 | int len; |
| 4519 | char pn[INET6_ADDRSTRLEN]; |
| 4520 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4521 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4522 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4523 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4524 | /* Note: we rely on the backend to get the header name to be used for |
| 4525 | * x-forwarded-for, because the header is really meant for the backends. |
| 4526 | * However, if the backend did not specify any option, we have to rely |
| 4527 | * on the frontend's header name. |
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 | if (s->be->fwdfor_hdr_len) { |
| 4530 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4531 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4532 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4533 | len = sess->fe->fwdfor_hdr_len; |
| 4534 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4535 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4536 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4537 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4538 | 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] | 4539 | goto return_bad_req; |
| 4540 | } |
| 4541 | } |
| 4542 | |
| 4543 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4544 | * 10: add X-Original-To if either the frontend or the backend |
| 4545 | * asks for it. |
| 4546 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4547 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4548 | |
| 4549 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4550 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4551 | /* Add an X-Original-To header unless the destination IP is |
| 4552 | * in the 'except' network range. |
| 4553 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4554 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4555 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4556 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4557 | ((!sess->fe->except_mask_to.s_addr || |
| 4558 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4559 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4560 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4561 | (((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] | 4562 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4563 | int len; |
| 4564 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4565 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4566 | |
| 4567 | /* Note: we rely on the backend to get the header name to be used for |
| 4568 | * x-original-to, because the header is really meant for the backends. |
| 4569 | * However, if the backend did not specify any option, we have to rely |
| 4570 | * on the frontend's header name. |
| 4571 | */ |
| 4572 | if (s->be->orgto_hdr_len) { |
| 4573 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4574 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4575 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4576 | len = sess->fe->orgto_hdr_len; |
| 4577 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4578 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4579 | 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] | 4580 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4581 | 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] | 4582 | goto return_bad_req; |
| 4583 | } |
| 4584 | } |
| 4585 | } |
| 4586 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4587 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4588 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4589 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4590 | * "Upgrade" is present in the Connection header. |
| 4591 | */ |
| 4592 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4593 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4594 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4595 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4596 | unsigned int want_flags = 0; |
| 4597 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4598 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4599 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4600 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4601 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4602 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4603 | want_flags |= TX_CON_CLO_SET; |
| 4604 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4605 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4606 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4607 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4608 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4609 | want_flags |= TX_CON_KAL_SET; |
| 4610 | } |
| 4611 | |
| 4612 | 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] | 4613 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4614 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4615 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4616 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4617 | /* If we have no server assigned yet and we're balancing on url_param |
| 4618 | * with a POST request, we may be interested in checking the body for |
| 4619 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4620 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4621 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4622 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4623 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4624 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4625 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4626 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4627 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4628 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4629 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4630 | #ifdef TCP_QUICKACK |
| 4631 | /* We expect some data from the client. Unless we know for sure |
| 4632 | * we already have a full request, we have to re-enable quick-ack |
| 4633 | * in case we previously disabled it, otherwise we might cause |
| 4634 | * the client to delay further data. |
| 4635 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4636 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4637 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4638 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4639 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4640 | 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] | 4641 | #endif |
| 4642 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4643 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4644 | /************************************************************* |
| 4645 | * OK, that's finished for the headers. We have done what we * |
| 4646 | * could. Let's switch to the DATA state. * |
| 4647 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4648 | req->analyse_exp = TICK_ETERNITY; |
| 4649 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4650 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4651 | /* if the server closes the connection, we want to immediately react |
| 4652 | * and close the socket to save packets and syscalls. |
| 4653 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4654 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4655 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4656 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4657 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4658 | /* OK let's go on with the BODY now */ |
| 4659 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4660 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4661 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4662 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4663 | /* we detected a parsing error. We want to archive this request |
| 4664 | * in the dedicated proxy area for later troubleshooting. |
| 4665 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4666 | 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] | 4667 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4668 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4669 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4670 | txn->status = 400; |
| 4671 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4672 | 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] | 4673 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4674 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4675 | if (sess->listener->counters) |
| 4676 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4677 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4678 | if (!(s->flags & SF_ERR_MASK)) |
| 4679 | s->flags |= SF_ERR_PRXCOND; |
| 4680 | if (!(s->flags & SF_FINST_MASK)) |
| 4681 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4682 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4683 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4684 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4685 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4686 | * returns zero, at the beginning because it prevents any other processing |
| 4687 | * from occurring, and at the end because it terminates the request. |
| 4688 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4689 | 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] | 4690 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4691 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4692 | |
| 4693 | /* This connection is being tarpitted. The CLIENT side has |
| 4694 | * already set the connect expiration date to the right |
| 4695 | * timeout. We just have to check that the client is still |
| 4696 | * there and that the timeout has not expired. |
| 4697 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4698 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4699 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4700 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4701 | return 0; |
| 4702 | |
| 4703 | /* We will set the queue timer to the time spent, just for |
| 4704 | * logging purposes. We fake a 500 server error, so that the |
| 4705 | * attacker will not suspect his connection has been tarpitted. |
| 4706 | * It will not cause trouble to the logs because we can exclude |
| 4707 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4708 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4709 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4710 | |
| 4711 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4712 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4713 | 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] | 4714 | |
| 4715 | req->analysers = 0; |
| 4716 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4717 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4718 | if (!(s->flags & SF_ERR_MASK)) |
| 4719 | s->flags |= SF_ERR_PRXCOND; |
| 4720 | if (!(s->flags & SF_FINST_MASK)) |
| 4721 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4722 | return 0; |
| 4723 | } |
| 4724 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4725 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4726 | * for either the buffer to be full, or the full advertised contents to have |
| 4727 | * reached the buffer. It must only be called after the standard HTTP request |
| 4728 | * processing has occurred, because it expects the request to be parsed and will |
| 4729 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4730 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4731 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4732 | * 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] | 4733 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4734 | 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] | 4735 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4736 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4737 | struct http_txn *txn = s->txn; |
| 4738 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4739 | |
| 4740 | /* We have to parse the HTTP request body to find any required data. |
| 4741 | * "balance url_param check_post" should have been the only way to get |
| 4742 | * into this. We were brought here after HTTP header analysis, so all |
| 4743 | * related structures are ready. |
| 4744 | */ |
| 4745 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4746 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4747 | /* This is the first call */ |
| 4748 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4749 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4750 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4751 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4752 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4753 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4754 | */ |
| 4755 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4756 | struct hdr_ctx ctx; |
| 4757 | ctx.idx = 0; |
| 4758 | /* Expect is allowed in 1.1, look for it */ |
| 4759 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4760 | 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] | 4761 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4762 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4763 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4764 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4765 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4766 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4767 | /* 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] | 4768 | * req->buf->p still points to the beginning of the message. We |
| 4769 | * must save the body in msg->next because it survives buffer |
| 4770 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4771 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4772 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4773 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4774 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4775 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4776 | else |
| 4777 | msg->msg_state = HTTP_MSG_DATA; |
| 4778 | } |
| 4779 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4780 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4781 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4782 | if (req->buf->i - msg->sov < msg->body_len) |
| 4783 | goto missing_data; |
| 4784 | |
| 4785 | /* OK we have everything we need now */ |
| 4786 | goto http_end; |
| 4787 | } |
| 4788 | |
| 4789 | /* OK here we're parsing a chunked-encoded message */ |
| 4790 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4791 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4792 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4793 | * 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] | 4794 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4795 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4796 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4797 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4798 | if (!ret) |
| 4799 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4800 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4801 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4802 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4803 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4806 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4807 | * 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] | 4808 | * 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] | 4809 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4810 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4811 | goto http_end; |
| 4812 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4813 | 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] | 4814 | goto http_end; |
| 4815 | |
| 4816 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4817 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4818 | * we have the maximum we can expect. |
| 4819 | */ |
| 4820 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4821 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4822 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4823 | 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] | 4824 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4825 | 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] | 4826 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4827 | if (!(s->flags & SF_ERR_MASK)) |
| 4828 | s->flags |= SF_ERR_CLITO; |
| 4829 | if (!(s->flags & SF_FINST_MASK)) |
| 4830 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4831 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4832 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4833 | |
| 4834 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4835 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4836 | /* Not enough data. We'll re-use the http-request |
| 4837 | * timeout here. Ideally, we should set the timeout |
| 4838 | * relative to the accept() date. We just set the |
| 4839 | * request timeout once at the beginning of the |
| 4840 | * request. |
| 4841 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4842 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4843 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4844 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4845 | return 0; |
| 4846 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4847 | |
| 4848 | http_end: |
| 4849 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4850 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4851 | req->analysers &= ~an_bit; |
| 4852 | req->analyse_exp = TICK_ETERNITY; |
| 4853 | return 1; |
| 4854 | |
| 4855 | return_bad_req: /* let's centralize all bad requests */ |
| 4856 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4857 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4858 | 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] | 4859 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4860 | if (!(s->flags & SF_ERR_MASK)) |
| 4861 | s->flags |= SF_ERR_PRXCOND; |
| 4862 | if (!(s->flags & SF_FINST_MASK)) |
| 4863 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4864 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4865 | return_err_msg: |
| 4866 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4867 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4868 | if (sess->listener->counters) |
| 4869 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4870 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4871 | } |
| 4872 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4873 | /* send a server's name with an outgoing request over an established connection. |
| 4874 | * Note: this function is designed to be called once the request has been scheduled |
| 4875 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4876 | * proceeding. |
| 4877 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4878 | 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] | 4879 | |
| 4880 | struct hdr_ctx ctx; |
| 4881 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4882 | char *hdr_name = be->server_id_hdr_name; |
| 4883 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4884 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4885 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4886 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4887 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4888 | ctx.idx = 0; |
| 4889 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4890 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4891 | if (old_o) { |
| 4892 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4893 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4894 | txn->req.next += old_o; |
| 4895 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4896 | } |
| 4897 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4898 | old_i = chn->buf->i; |
| 4899 | 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] | 4900 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4901 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4905 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4906 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4907 | hdr_val += hdr_name_len; |
| 4908 | *hdr_val++ = ':'; |
| 4909 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4910 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4911 | 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] | 4912 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4913 | if (old_o) { |
| 4914 | /* If this was a forwarded request, we must readjust the amount of |
| 4915 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4916 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4917 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4918 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4919 | old_o += chn->buf->i - old_i; |
| 4920 | b_adv(chn->buf, old_o); |
| 4921 | txn->req.next -= old_o; |
| 4922 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4925 | return 0; |
| 4926 | } |
| 4927 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4928 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4929 | * right now but it works. |
| 4930 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4931 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4932 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4933 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4934 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4935 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4936 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4937 | * server's connections. We need a safer way to reinitialize buffer |
| 4938 | * flags. We also need a more accurate method for computing per-request |
| 4939 | * data. |
| 4940 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4941 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4942 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4943 | * to the server. |
| 4944 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4945 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4946 | !si_conn_ready(&s->si[1])) { |
| 4947 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4948 | si_shutr(&s->si[1]); |
| 4949 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4950 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4951 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4952 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4953 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4954 | if (unlikely(s->srv_conn)) |
| 4955 | sess_change_server(s, NULL); |
| 4956 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4957 | |
| 4958 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4959 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4960 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4961 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4962 | int n; |
| 4963 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4964 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4965 | if (n < 1 || n > 5) |
| 4966 | n = 0; |
| 4967 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4968 | if (fe->mode == PR_MODE_HTTP) { |
| 4969 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4970 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4971 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4972 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4973 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4974 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4975 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4976 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4977 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4978 | s->be->be_counters.p.http.comp_rsp++; |
| 4979 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4983 | s->logs.bytes_in -= s->req.buf->i; |
| 4984 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4985 | |
| 4986 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4987 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4988 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4989 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4990 | s->do_log(s); |
| 4991 | } |
| 4992 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4993 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4994 | stream_stop_content_counters(s); |
| 4995 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4996 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4997 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4998 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4999 | tv_zero(&s->logs.tv_request); |
| 5000 | s->logs.t_queue = -1; |
| 5001 | s->logs.t_connect = -1; |
| 5002 | s->logs.t_data = -1; |
| 5003 | s->logs.t_close = 0; |
| 5004 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5005 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5006 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5007 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5008 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5009 | |
| 5010 | if (s->pend_pos) |
| 5011 | pendconn_free(s->pend_pos); |
| 5012 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5013 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5014 | if (s->flags & SF_CURR_SESS) { |
| 5015 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5016 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5017 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5018 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5019 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5020 | } |
| 5021 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5022 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5023 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5024 | /* only release our endpoint if we don't intend to reuse the |
| 5025 | * connection. |
| 5026 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5027 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5028 | !si_conn_ready(&s->si[1])) { |
| 5029 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5030 | } |
| 5031 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5032 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5033 | s->si[1].err_type = SI_ET_NONE; |
| 5034 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5035 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5036 | 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] | 5037 | 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); |
| 5038 | 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] | 5039 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5040 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5041 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5042 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5043 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5044 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5045 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5046 | |
| 5047 | if (prev_status == 401 || prev_status == 407) { |
| 5048 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5049 | * a chance of being able to send the visitor again to the same |
| 5050 | * server over the same connection. This is required by some |
| 5051 | * broken protocols such as NTLM, and anyway whenever there is |
| 5052 | * an opportunity for sending the challenge to the proper place, |
| 5053 | * it's better to do it (at least it helps with debugging). |
| 5054 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5055 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5056 | } |
| 5057 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5058 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5059 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5060 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5061 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5062 | s->req.flags |= CF_NEVER_WAIT; |
| 5063 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5064 | } |
| 5065 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5066 | /* if the request buffer is not empty, it means we're |
| 5067 | * about to process another request, so send pending |
| 5068 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5069 | * Just don't do this if the buffer is close to be full, |
| 5070 | * because the request will wait for it to flush a little |
| 5071 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5072 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5073 | if (s->req.buf->i) { |
| 5074 | if (s->res.buf->o && |
| 5075 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5076 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5077 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5078 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5079 | |
| 5080 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5081 | channel_auto_read(&s->req); |
| 5082 | channel_auto_close(&s->req); |
| 5083 | channel_auto_read(&s->res); |
| 5084 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5085 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5086 | /* we're in keep-alive with an idle connection, monitor it */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5087 | si_idle_conn(&s->si[1]); |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5088 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5089 | s->req.analysers = strm_li(s)->analysers; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5090 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5091 | } |
| 5092 | |
| 5093 | |
| 5094 | /* This function updates the request state machine according to the response |
| 5095 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5096 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5097 | * it is only used to find when a request/response couple is complete. Both |
| 5098 | * this function and its equivalent should loop until both return zero. It |
| 5099 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5100 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5101 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5102 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5103 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5104 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5105 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5106 | unsigned int old_state = txn->req.msg_state; |
| 5107 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5108 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5109 | return 0; |
| 5110 | |
| 5111 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5112 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5113 | * We can shut the read side unless we want to abort_on_close, |
| 5114 | * or we have a POST request. The issue with POST requests is |
| 5115 | * that some browsers still send a CRLF after the request, and |
| 5116 | * this CRLF must be read so that it does not remain in the kernel |
| 5117 | * buffers, otherwise a close could cause an RST on some systems |
| 5118 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5119 | * Note that if we're using keep-alive on the client side, we'd |
| 5120 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5121 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5122 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5123 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5124 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5125 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5126 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5127 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5128 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5129 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5130 | /* if the server closes the connection, we want to immediately react |
| 5131 | * and close the socket to save packets and syscalls. |
| 5132 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5133 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5134 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5135 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5136 | goto wait_other_side; |
| 5137 | |
| 5138 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5139 | /* The server has not finished to respond, so we |
| 5140 | * don't want to move in order not to upset it. |
| 5141 | */ |
| 5142 | goto wait_other_side; |
| 5143 | } |
| 5144 | |
| 5145 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5146 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5147 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5148 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5149 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5150 | goto wait_other_side; |
| 5151 | } |
| 5152 | |
| 5153 | /* When we get here, it means that both the request and the |
| 5154 | * response have finished receiving. Depending on the connection |
| 5155 | * mode, we'll have to wait for the last bytes to leave in either |
| 5156 | * direction, and sometimes for a close to be effective. |
| 5157 | */ |
| 5158 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5159 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5160 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5161 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5162 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5163 | } |
| 5164 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5165 | /* Option forceclose is set, or either side wants to close, |
| 5166 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5167 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5168 | * once both states are CLOSED. |
| 5169 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5170 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5171 | channel_shutr_now(chn); |
| 5172 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5173 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5174 | } |
| 5175 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5176 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5177 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5178 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5179 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5180 | channel_auto_read(chn); |
| 5181 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5182 | chn->flags |= CF_NEVER_WAIT; |
| 5183 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5184 | } |
| 5185 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5186 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5187 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5188 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5189 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5190 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5191 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5192 | goto http_msg_closing; |
| 5193 | } |
| 5194 | else { |
| 5195 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5196 | goto http_msg_closed; |
| 5197 | } |
| 5198 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5199 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5203 | http_msg_closing: |
| 5204 | /* nothing else to forward, just waiting for the output buffer |
| 5205 | * to be empty and for the shutw_now to take effect. |
| 5206 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5207 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5208 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5209 | goto http_msg_closed; |
| 5210 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5211 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5212 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5213 | goto wait_other_side; |
| 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5218 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5219 | /* see above in MSG_DONE why we only do this in these states */ |
| 5220 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5221 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5222 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5223 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5224 | goto wait_other_side; |
| 5225 | } |
| 5226 | |
| 5227 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5228 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | |
| 5232 | /* This function updates the response state machine according to the request |
| 5233 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5234 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5235 | * it is only used to find when a request/response couple is complete. Both |
| 5236 | * this function and its equivalent should loop until both return zero. It |
| 5237 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5238 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5239 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5240 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5241 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5242 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5243 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5244 | unsigned int old_state = txn->rsp.msg_state; |
| 5245 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5246 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5247 | return 0; |
| 5248 | |
| 5249 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5250 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5251 | * still monitor the server connection for a possible close |
| 5252 | * while the request is being uploaded, so we don't disable |
| 5253 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5254 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5255 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5256 | |
| 5257 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5258 | goto wait_other_side; |
| 5259 | |
| 5260 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5261 | /* The client seems to still be sending data, probably |
| 5262 | * because we got an error response during an upload. |
| 5263 | * We have the choice of either breaking the connection |
| 5264 | * or letting it pass through. Let's do the later. |
| 5265 | */ |
| 5266 | goto wait_other_side; |
| 5267 | } |
| 5268 | |
| 5269 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5270 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5271 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5272 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5273 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5274 | goto wait_other_side; |
| 5275 | } |
| 5276 | |
| 5277 | /* When we get here, it means that both the request and the |
| 5278 | * response have finished receiving. Depending on the connection |
| 5279 | * mode, we'll have to wait for the last bytes to leave in either |
| 5280 | * direction, and sometimes for a close to be effective. |
| 5281 | */ |
| 5282 | |
| 5283 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5284 | /* Server-close mode : shut read and wait for the request |
| 5285 | * side to close its output buffer. The caller will detect |
| 5286 | * when we're in DONE and the other is in CLOSED and will |
| 5287 | * catch that for the final cleanup. |
| 5288 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5289 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5290 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5291 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5292 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5293 | /* Option forceclose is set, or either side wants to close, |
| 5294 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5295 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5296 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5297 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5298 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5299 | channel_shutr_now(chn); |
| 5300 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5301 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5302 | } |
| 5303 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5304 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5305 | * need to forward remaining data. Keep-alive will need to monitor |
| 5306 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5307 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5308 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5309 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5310 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5311 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5314 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5315 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5316 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5318 | goto http_msg_closing; |
| 5319 | } |
| 5320 | else { |
| 5321 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5322 | goto http_msg_closed; |
| 5323 | } |
| 5324 | } |
| 5325 | goto wait_other_side; |
| 5326 | } |
| 5327 | |
| 5328 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5329 | http_msg_closing: |
| 5330 | /* nothing else to forward, just waiting for the output buffer |
| 5331 | * to be empty and for the shutw_now to take effect. |
| 5332 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5333 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5334 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5335 | goto http_msg_closed; |
| 5336 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5337 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5338 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5339 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5340 | if (objt_server(s->target)) |
| 5341 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5342 | goto wait_other_side; |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5347 | http_msg_closed: |
| 5348 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5349 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5350 | channel_auto_close(chn); |
| 5351 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5352 | goto wait_other_side; |
| 5353 | } |
| 5354 | |
| 5355 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5356 | /* We force the response to leave immediately if we're waiting for the |
| 5357 | * other side, since there is no pending shutdown to push it out. |
| 5358 | */ |
| 5359 | if (!channel_is_empty(chn)) |
| 5360 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5361 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | |
| 5365 | /* Resync the request and response state machines. Return 1 if either state |
| 5366 | * changes. |
| 5367 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5368 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5369 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5370 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5371 | int old_req_state = txn->req.msg_state; |
| 5372 | int old_res_state = txn->rsp.msg_state; |
| 5373 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5374 | http_sync_req_state(s); |
| 5375 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5376 | if (!http_sync_res_state(s)) |
| 5377 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5378 | if (!http_sync_req_state(s)) |
| 5379 | break; |
| 5380 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5381 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5382 | /* OK, both state machines agree on a compatible state. |
| 5383 | * There are a few cases we're interested in : |
| 5384 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5385 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5386 | * directions, so let's simply disable both analysers. |
| 5387 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5388 | * request. |
| 5389 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5390 | * with server-close mode means we've completed one request and we |
| 5391 | * must re-initialize the server connection. |
| 5392 | */ |
| 5393 | |
| 5394 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5395 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5396 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5397 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5398 | s->req.analysers = 0; |
| 5399 | channel_auto_close(&s->req); |
| 5400 | channel_auto_read(&s->req); |
| 5401 | s->res.analysers = 0; |
| 5402 | channel_auto_close(&s->res); |
| 5403 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5404 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5405 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5406 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5407 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5408 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5409 | s->res.analysers = 0; |
| 5410 | channel_auto_close(&s->res); |
| 5411 | channel_auto_read(&s->res); |
| 5412 | s->req.analysers = 0; |
| 5413 | channel_abort(&s->req); |
| 5414 | channel_auto_close(&s->req); |
| 5415 | channel_auto_read(&s->req); |
| 5416 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5417 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5418 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5419 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5420 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5421 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5422 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5423 | /* server-close/keep-alive: terminate this transaction, |
| 5424 | * possibly killing the server connection and reinitialize |
| 5425 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5426 | */ |
| 5427 | http_end_txn_clean_session(s); |
| 5428 | } |
| 5429 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5430 | return txn->req.msg_state != old_req_state || |
| 5431 | txn->rsp.msg_state != old_res_state; |
| 5432 | } |
| 5433 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5434 | /* This function is an analyser which forwards request body (including chunk |
| 5435 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5436 | * zero byte. The only situation where it must not be called is when we're in |
| 5437 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5438 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5439 | * 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] | 5440 | * 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] | 5441 | * 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] | 5442 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5443 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5444 | 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] | 5445 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5446 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5447 | struct http_txn *txn = s->txn; |
| 5448 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5449 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5450 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5451 | return 0; |
| 5452 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5453 | 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] | 5454 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5455 | /* Output closed while we were sending data. We must abort and |
| 5456 | * wake the other side up. |
| 5457 | */ |
| 5458 | msg->msg_state = HTTP_MSG_ERROR; |
| 5459 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5460 | return 1; |
| 5461 | } |
| 5462 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5463 | /* Note that we don't have to send 100-continue back because we don't |
| 5464 | * need the data to complete our job, and it's up to the server to |
| 5465 | * decide whether to return 100, 417 or anything else in return of |
| 5466 | * an "Expect: 100-continue" header. |
| 5467 | */ |
| 5468 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5469 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5470 | /* we have msg->sov which points to the first byte of message |
| 5471 | * body, and req->buf.p still points to the beginning of the |
| 5472 | * message. We forward the headers now, as we don't need them |
| 5473 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5474 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5475 | b_adv(req->buf, msg->sov); |
| 5476 | msg->next -= msg->sov; |
| 5477 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5478 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5479 | /* The previous analysers guarantee that the state is somewhere |
| 5480 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5481 | * msg->next are always correct. |
| 5482 | */ |
| 5483 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5484 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5485 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5486 | else |
| 5487 | msg->msg_state = HTTP_MSG_DATA; |
| 5488 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5489 | } |
| 5490 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5491 | /* Some post-connect processing might want us to refrain from starting to |
| 5492 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5493 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5494 | */ |
| 5495 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5496 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5497 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5498 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5499 | goto missing_data; |
| 5500 | } |
| 5501 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5502 | } |
| 5503 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5504 | /* in most states, we should abort in case of early close */ |
| 5505 | channel_auto_close(req); |
| 5506 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5507 | if (req->to_forward) { |
| 5508 | /* We can't process the buffer's contents yet */ |
| 5509 | req->flags |= CF_WAKE_WRITE; |
| 5510 | goto missing_data; |
| 5511 | } |
| 5512 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5513 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5514 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5515 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5516 | /* we may have some pending data starting at req->buf->p */ |
| 5517 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5518 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5519 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5520 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5521 | msg->next += msg->chunk_len; |
| 5522 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5523 | |
| 5524 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5525 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5526 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5527 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5528 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5529 | } |
| 5530 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5531 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5532 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5533 | * TRAILERS state. |
| 5534 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5535 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5536 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5537 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5538 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5539 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5540 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5541 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5542 | 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] | 5543 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5544 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5545 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5546 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5547 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5548 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5549 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5550 | |
| 5551 | if (ret == 0) |
| 5552 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5553 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5554 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5555 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5556 | 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] | 5557 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5558 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5559 | /* we're in MSG_CHUNK_SIZE now */ |
| 5560 | } |
| 5561 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5562 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5563 | |
| 5564 | if (ret == 0) |
| 5565 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5566 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5567 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5568 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5569 | 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] | 5570 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5571 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5572 | /* we're in HTTP_MSG_DONE now */ |
| 5573 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5574 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5575 | int old_state = msg->msg_state; |
| 5576 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5577 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5578 | |
| 5579 | /* we may have some pending data starting at req->buf->p |
| 5580 | * such as last chunk of data or trailers. |
| 5581 | */ |
| 5582 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5583 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5584 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5585 | msg->next = 0; |
| 5586 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5587 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5588 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5589 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5590 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5591 | if (http_resync_states(s)) { |
| 5592 | /* some state changes occurred, maybe the analyser |
| 5593 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5594 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5595 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5596 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5597 | /* request errors are most likely due to |
| 5598 | * the server aborting the transfer. |
| 5599 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5600 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5601 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5602 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5603 | 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] | 5604 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5605 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5606 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5607 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5608 | |
| 5609 | /* If "option abortonclose" is set on the backend, we |
| 5610 | * want to monitor the client's connection and forward |
| 5611 | * any shutdown notification to the server, which will |
| 5612 | * decide whether to close or to go on processing the |
| 5613 | * request. |
| 5614 | */ |
| 5615 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5616 | channel_auto_read(req); |
| 5617 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5618 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5619 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5620 | /* POST requests may require to read extra CRLF |
| 5621 | * sent by broken browsers and which could cause |
| 5622 | * an RST to be sent upon close on some systems |
| 5623 | * (eg: Linux). |
| 5624 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5625 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5626 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5627 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5628 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5632 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5633 | /* we may have some pending data starting at req->buf->p */ |
| 5634 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5635 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5636 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5637 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5638 | msg->next = 0; |
| 5639 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5640 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5641 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5642 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5643 | if (!(s->flags & SF_ERR_MASK)) |
| 5644 | s->flags |= SF_ERR_CLICL; |
| 5645 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5646 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5647 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5648 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5649 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5650 | } |
| 5651 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5652 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5653 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5654 | if (objt_server(s->target)) |
| 5655 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5656 | |
| 5657 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5658 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5659 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5660 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5661 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5662 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5663 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5664 | /* 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] | 5665 | * 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] | 5666 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5667 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5668 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5669 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5670 | /* 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] | 5671 | * 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] | 5672 | * 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] | 5673 | * modes are already handled by the stream sock layer. We must not do |
| 5674 | * this in content-length mode because it could present the MSG_MORE |
| 5675 | * flag with the last block of forwarded data, which would cause an |
| 5676 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5677 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5678 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5679 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5680 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5681 | return 0; |
| 5682 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5683 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5684 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5685 | if (sess->listener->counters) |
| 5686 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5687 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5688 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5689 | /* we may have some pending data starting at req->buf->p */ |
| 5690 | b_adv(req->buf, msg->next); |
| 5691 | msg->next = 0; |
| 5692 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5693 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5694 | if (txn->status) { |
| 5695 | /* 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] | 5696 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5697 | } else { |
| 5698 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5699 | 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] | 5700 | } |
| 5701 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5702 | 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] | 5703 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5704 | if (!(s->flags & SF_ERR_MASK)) |
| 5705 | s->flags |= SF_ERR_PRXCOND; |
| 5706 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5707 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5708 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5709 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5710 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5711 | } |
| 5712 | return 0; |
| 5713 | |
| 5714 | aborted_xfer: |
| 5715 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5716 | if (txn->status) { |
| 5717 | /* 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] | 5718 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5719 | } else { |
| 5720 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5721 | 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] | 5722 | } |
| 5723 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5724 | 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] | 5725 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5726 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5727 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5728 | if (objt_server(s->target)) |
| 5729 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5730 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5731 | if (!(s->flags & SF_ERR_MASK)) |
| 5732 | s->flags |= SF_ERR_SRVCL; |
| 5733 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5734 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5735 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5736 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5737 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5738 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5739 | return 0; |
| 5740 | } |
| 5741 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5742 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5743 | * processing can continue on next analysers, or zero if it either needs more |
| 5744 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5745 | * 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] | 5746 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5747 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5748 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5749 | 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] | 5750 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5751 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5752 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5753 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5754 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5755 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5756 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5757 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5758 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5759 | 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] | 5760 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5761 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5762 | rep, |
| 5763 | rep->rex, rep->wex, |
| 5764 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5765 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5766 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5767 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5768 | /* |
| 5769 | * Now parse the partial (or complete) lines. |
| 5770 | * We will check the response syntax, and also join multi-line |
| 5771 | * headers. An index of all the lines will be elaborated while |
| 5772 | * parsing. |
| 5773 | * |
| 5774 | * For the parsing, we use a 28 states FSM. |
| 5775 | * |
| 5776 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5777 | * rep->buf->p = beginning of response |
| 5778 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5779 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5780 | * msg->eol = end of current header or line (LF or CRLF) |
| 5781 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5782 | */ |
| 5783 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5784 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5785 | /* There's a protected area at the end of the buffer for rewriting |
| 5786 | * purposes. We don't want to start to parse the request if the |
| 5787 | * protected area is affected, because we may have to move processed |
| 5788 | * data later, which is much more complicated. |
| 5789 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5790 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5791 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5792 | /* some data has still not left the buffer, wake us once that's done */ |
| 5793 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5794 | goto abort_response; |
| 5795 | channel_dont_close(rep); |
| 5796 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5797 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5798 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5799 | } |
| 5800 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5801 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5802 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5803 | buffer_slow_realign(rep->buf); |
| 5804 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5805 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5806 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5807 | } |
| 5808 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5809 | /* 1: we might have to print this header in debug mode */ |
| 5810 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5811 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5812 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5813 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5814 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5815 | sol = rep->buf->p; |
| 5816 | 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] | 5817 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5818 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5819 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5820 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5821 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5822 | while (cur_idx) { |
| 5823 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5824 | debug_hdr("srvhdr", s, sol, eol); |
| 5825 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5826 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5827 | } |
| 5828 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5829 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5830 | /* |
| 5831 | * Now we quickly check if we have found a full valid response. |
| 5832 | * If not so, we check the FD and buffer states before leaving. |
| 5833 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5834 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5835 | * responses are checked first. |
| 5836 | * |
| 5837 | * Depending on whether the client is still there or not, we |
| 5838 | * may send an error response back or not. Note that normally |
| 5839 | * we should only check for HTTP status there, and check I/O |
| 5840 | * errors somewhere else. |
| 5841 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5842 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5843 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5844 | /* Invalid response */ |
| 5845 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5846 | /* we detected a parsing error. We want to archive this response |
| 5847 | * in the dedicated proxy area for later troubleshooting. |
| 5848 | */ |
| 5849 | hdr_response_bad: |
| 5850 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5851 | 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] | 5852 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5853 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5854 | if (objt_server(s->target)) { |
| 5855 | objt_server(s->target)->counters.failed_resp++; |
| 5856 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5857 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5858 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5859 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5860 | rep->analysers = 0; |
| 5861 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5862 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5863 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5864 | 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] | 5865 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5866 | if (!(s->flags & SF_ERR_MASK)) |
| 5867 | s->flags |= SF_ERR_PRXCOND; |
| 5868 | if (!(s->flags & SF_FINST_MASK)) |
| 5869 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5870 | |
| 5871 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5872 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5873 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5874 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5875 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5876 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5877 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5878 | goto hdr_response_bad; |
| 5879 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5880 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5881 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5882 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5883 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5884 | 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] | 5885 | else if (txn->flags & TX_NOT_FIRST) |
| 5886 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5887 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5888 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5889 | if (objt_server(s->target)) { |
| 5890 | objt_server(s->target)->counters.failed_resp++; |
| 5891 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5892 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5893 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5894 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5895 | rep->analysers = 0; |
| 5896 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5897 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5898 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5899 | 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] | 5900 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5901 | if (!(s->flags & SF_ERR_MASK)) |
| 5902 | s->flags |= SF_ERR_SRVCL; |
| 5903 | if (!(s->flags & SF_FINST_MASK)) |
| 5904 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5905 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5906 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5907 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5908 | /* read timeout : return a 504 to the client. */ |
| 5909 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5910 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5911 | 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] | 5912 | else if (txn->flags & TX_NOT_FIRST) |
| 5913 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5914 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5915 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5916 | if (objt_server(s->target)) { |
| 5917 | objt_server(s->target)->counters.failed_resp++; |
| 5918 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5919 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5920 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5921 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5922 | rep->analysers = 0; |
| 5923 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5924 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5925 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5926 | 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] | 5927 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5928 | if (!(s->flags & SF_ERR_MASK)) |
| 5929 | s->flags |= SF_ERR_SRVTO; |
| 5930 | if (!(s->flags & SF_FINST_MASK)) |
| 5931 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5932 | return 0; |
| 5933 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5934 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5935 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5936 | 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] | 5937 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5938 | s->be->be_counters.cli_aborts++; |
| 5939 | if (objt_server(s->target)) |
| 5940 | objt_server(s->target)->counters.cli_aborts++; |
| 5941 | |
| 5942 | rep->analysers = 0; |
| 5943 | channel_auto_close(rep); |
| 5944 | |
| 5945 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5946 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5947 | 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] | 5948 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5949 | if (!(s->flags & SF_ERR_MASK)) |
| 5950 | s->flags |= SF_ERR_CLICL; |
| 5951 | if (!(s->flags & SF_FINST_MASK)) |
| 5952 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5953 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5954 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5955 | return 0; |
| 5956 | } |
| 5957 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5958 | /* 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] | 5959 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5960 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5961 | 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] | 5962 | else if (txn->flags & TX_NOT_FIRST) |
| 5963 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5964 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5965 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5966 | if (objt_server(s->target)) { |
| 5967 | objt_server(s->target)->counters.failed_resp++; |
| 5968 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5969 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5970 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5971 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5972 | rep->analysers = 0; |
| 5973 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5974 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5975 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5976 | 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] | 5977 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5978 | if (!(s->flags & SF_ERR_MASK)) |
| 5979 | s->flags |= SF_ERR_SRVCL; |
| 5980 | if (!(s->flags & SF_FINST_MASK)) |
| 5981 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5982 | return 0; |
| 5983 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5984 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5985 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5986 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5987 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5988 | 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] | 5989 | else if (txn->flags & TX_NOT_FIRST) |
| 5990 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5991 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5992 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5993 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5994 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5995 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5996 | if (!(s->flags & SF_ERR_MASK)) |
| 5997 | s->flags |= SF_ERR_CLICL; |
| 5998 | if (!(s->flags & SF_FINST_MASK)) |
| 5999 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6000 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6001 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6002 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6003 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6004 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6005 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6006 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6007 | return 0; |
| 6008 | } |
| 6009 | |
| 6010 | /* More interesting part now : we know that we have a complete |
| 6011 | * response which at least looks like HTTP. We have an indicator |
| 6012 | * of each header's length, so we can parse them quickly. |
| 6013 | */ |
| 6014 | |
| 6015 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6016 | 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] | 6017 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6018 | /* |
| 6019 | * 1: get the status code |
| 6020 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6021 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6022 | if (n < 1 || n > 5) |
| 6023 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6024 | /* when the client triggers a 4xx from the server, it's most often due |
| 6025 | * to a missing object or permission. These events should be tracked |
| 6026 | * because if they happen often, it may indicate a brute force or a |
| 6027 | * vulnerability scan. |
| 6028 | */ |
| 6029 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6030 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6031 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6032 | if (objt_server(s->target)) |
| 6033 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6034 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6035 | /* check if the response is HTTP/1.1 or above */ |
| 6036 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6037 | ((rep->buf->p[5] > '1') || |
| 6038 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6039 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6040 | |
| 6041 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6042 | 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] | 6043 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6044 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6045 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6046 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6047 | 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] | 6048 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6049 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6050 | * and 505 are triggered on demand by client request, so we must not |
| 6051 | * count them as server failures. |
| 6052 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6053 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6054 | 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] | 6055 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6056 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6057 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6058 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6059 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6060 | /* |
| 6061 | * 2: check for cacheability. |
| 6062 | */ |
| 6063 | |
| 6064 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6065 | case 100: |
| 6066 | /* |
| 6067 | * We may be facing a 100-continue response, in which case this |
| 6068 | * is not the right response, and we're waiting for the next one. |
| 6069 | * Let's allow this response to go to the client and wait for the |
| 6070 | * next one. |
| 6071 | */ |
| 6072 | hdr_idx_init(&txn->hdr_idx); |
| 6073 | msg->next -= channel_forward(rep, msg->next); |
| 6074 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6075 | txn->status = 0; |
| 6076 | s->logs.t_data = -1; /* was not a response yet */ |
| 6077 | goto next_one; |
| 6078 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6079 | case 200: |
| 6080 | case 203: |
| 6081 | case 206: |
| 6082 | case 300: |
| 6083 | case 301: |
| 6084 | case 410: |
| 6085 | /* RFC2616 @13.4: |
| 6086 | * "A response received with a status code of |
| 6087 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6088 | * by a cache (...) unless a cache-control |
| 6089 | * directive prohibits caching." |
| 6090 | * |
| 6091 | * RFC2616 @9.5: POST method : |
| 6092 | * "Responses to this method are not cacheable, |
| 6093 | * unless the response includes appropriate |
| 6094 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6095 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6096 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6097 | ((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] | 6098 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6099 | break; |
| 6100 | default: |
| 6101 | break; |
| 6102 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6103 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6104 | /* |
| 6105 | * 3: we may need to capture headers |
| 6106 | */ |
| 6107 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6108 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6109 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6110 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6111 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6112 | /* 4: determine the transfer-length. |
| 6113 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 6114 | * the presence of a message-body in a RESPONSE and its transfer length |
| 6115 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6116 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6117 | * All responses to the HEAD request method MUST NOT include a |
| 6118 | * message-body, even though the presence of entity-header fields |
| 6119 | * might lead one to believe they do. All 1xx (informational), 204 |
| 6120 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 6121 | * message-body. All other responses do include a message-body, |
| 6122 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6123 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6124 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 6125 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 6126 | * always terminated by the first empty line after the header fields, |
| 6127 | * regardless of the entity-header fields present in the message. |
| 6128 | * |
| 6129 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 6130 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 6131 | * transfer-length is defined by the use of this transfer-coding. |
| 6132 | * If a Transfer-Encoding header field is present and the "chunked" |
| 6133 | * transfer-coding is not present, the transfer-length is defined by |
| 6134 | * the sender closing the connection. |
| 6135 | * |
| 6136 | * 3. If a Content-Length header field is present, its decimal value in |
| 6137 | * OCTETs represents both the entity-length and the transfer-length. |
| 6138 | * If a message is received with both a Transfer-Encoding header |
| 6139 | * field and a Content-Length header field, the latter MUST be ignored. |
| 6140 | * |
| 6141 | * 4. If the message uses the media type "multipart/byteranges", and |
| 6142 | * the transfer-length is not otherwise specified, then this self- |
| 6143 | * delimiting media type defines the transfer-length. This media |
| 6144 | * type MUST NOT be used unless the sender knows that the recipient |
| 6145 | * can parse it; the presence in a request of a Range header with |
| 6146 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 6147 | * client can parse multipart/byteranges responses. |
| 6148 | * |
| 6149 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6150 | */ |
| 6151 | |
| 6152 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6153 | * 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] | 6154 | * 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] | 6155 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6156 | */ |
| 6157 | if (txn->meth == HTTP_METH_HEAD || |
| 6158 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6159 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6160 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6161 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6162 | goto skip_content_length; |
| 6163 | } |
| 6164 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6165 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6166 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6167 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6168 | 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] | 6169 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6170 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6171 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6172 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6173 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6174 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6175 | break; |
| 6176 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6177 | } |
| 6178 | |
| 6179 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 6180 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6181 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6182 | 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] | 6183 | signed long long cl; |
| 6184 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6185 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6186 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6187 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6188 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6189 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6190 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6191 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6192 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6193 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6194 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6195 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6196 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6197 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6198 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6199 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6200 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6201 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6202 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6203 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6204 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6205 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6206 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6207 | } |
| 6208 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6209 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6210 | select_compression_response_header(s, rep->buf); |
| 6211 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6212 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6213 | /* Now we have to check if we need to modify the Connection header. |
| 6214 | * This is more difficult on the response than it is on the request, |
| 6215 | * because we can have two different HTTP versions and we don't know |
| 6216 | * how the client will interprete a response. For instance, let's say |
| 6217 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6218 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6219 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6220 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6221 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6222 | * indicating how a request MAY be understood by the client. In case |
| 6223 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6224 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6225 | * will fall back to explicit close. Note that we won't take risks with |
| 6226 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6227 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6228 | */ |
| 6229 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6230 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6231 | txn->status == 101)) { |
| 6232 | /* Either we've established an explicit tunnel, or we're |
| 6233 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6234 | * to understand the next protocols. We have to switch to tunnel |
| 6235 | * mode, so that we transfer the request and responses then let |
| 6236 | * this protocol pass unmodified. When we later implement specific |
| 6237 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6238 | * header which contains information about that protocol for |
| 6239 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6240 | */ |
| 6241 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6242 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6243 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6244 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6245 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6246 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6247 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6248 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6249 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6250 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6251 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6252 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6253 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6254 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6255 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6256 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6257 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6258 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6259 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6260 | /* now adjust header transformations depending on current state */ |
| 6261 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6262 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6263 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6264 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6265 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6266 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6267 | else { /* SCL / KAL */ |
| 6268 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6269 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6270 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6271 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6272 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6273 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6274 | http_parse_connection_header(txn, msg, to_del); |
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 | /* Some keep-alive responses are converted to Server-close if |
| 6277 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6278 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6279 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6280 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6281 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6282 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6283 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6284 | } |
| 6285 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6286 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6287 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6288 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6289 | /* end of job, return OK */ |
| 6290 | rep->analysers &= ~an_bit; |
| 6291 | rep->analyse_exp = TICK_ETERNITY; |
| 6292 | channel_auto_close(rep); |
| 6293 | return 1; |
| 6294 | |
| 6295 | abort_keep_alive: |
| 6296 | /* A keep-alive request to the server failed on a network error. |
| 6297 | * The client is required to retry. We need to close without returning |
| 6298 | * any other information so that the client retries. |
| 6299 | */ |
| 6300 | txn->status = 0; |
| 6301 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6302 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6303 | channel_auto_close(rep); |
| 6304 | s->logs.logwait = 0; |
| 6305 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6306 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6307 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6308 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6309 | return 0; |
| 6310 | } |
| 6311 | |
| 6312 | /* This function performs all the processing enabled for the current response. |
| 6313 | * 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] | 6314 | * 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] | 6315 | * other functions. It works like process_request (see indications above). |
| 6316 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6317 | 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] | 6318 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6319 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6320 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6321 | struct http_msg *msg = &txn->rsp; |
| 6322 | struct proxy *cur_proxy; |
| 6323 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6324 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6325 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6326 | 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] | 6327 | now_ms, __FUNCTION__, |
| 6328 | s, |
| 6329 | rep, |
| 6330 | rep->rex, rep->wex, |
| 6331 | rep->flags, |
| 6332 | rep->buf->i, |
| 6333 | rep->analysers); |
| 6334 | |
| 6335 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6336 | return 0; |
| 6337 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6338 | /* The stats applet needs to adjust the Connection header but we don't |
| 6339 | * apply any filter there. |
| 6340 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6341 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6342 | rep->analysers &= ~an_bit; |
| 6343 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6344 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6345 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6346 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6347 | /* |
| 6348 | * We will have to evaluate the filters. |
| 6349 | * As opposed to version 1.2, now they will be evaluated in the |
| 6350 | * filters order and not in the header order. This means that |
| 6351 | * each filter has to be validated among all headers. |
| 6352 | * |
| 6353 | * Filters are tried with ->be first, then with ->fe if it is |
| 6354 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6355 | * |
| 6356 | * Maybe we are in resume condiion. In this case I choose the |
| 6357 | * "struct proxy" which contains the rule list matching the resume |
| 6358 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6359 | * the process with the first one. |
| 6360 | * |
| 6361 | * In fact, I check only correspondance betwwen the current list |
| 6362 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6363 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6364 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6365 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6366 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6367 | else |
| 6368 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6369 | while (1) { |
| 6370 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6371 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6372 | /* evaluate http-response rules */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6373 | if (ret == HTTP_RULE_RES_CONT) |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6374 | 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] | 6375 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6376 | /* we need to be called again. */ |
| 6377 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6378 | channel_dont_close(rep); |
| 6379 | return 0; |
| 6380 | } |
| 6381 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6382 | /* try headers filters */ |
| 6383 | if (rule_set->rsp_exp != NULL) { |
| 6384 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6385 | return_bad_resp: |
| 6386 | if (objt_server(s->target)) { |
| 6387 | objt_server(s->target)->counters.failed_resp++; |
| 6388 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6389 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6390 | s->be->be_counters.failed_resp++; |
| 6391 | return_srv_prx_502: |
| 6392 | rep->analysers = 0; |
| 6393 | txn->status = 502; |
| 6394 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6395 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6396 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6397 | 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] | 6398 | if (!(s->flags & SF_ERR_MASK)) |
| 6399 | s->flags |= SF_ERR_PRXCOND; |
| 6400 | if (!(s->flags & SF_FINST_MASK)) |
| 6401 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6402 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6403 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6404 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6405 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6406 | /* has the response been denied ? */ |
| 6407 | if (txn->flags & TX_SVDENY) { |
| 6408 | if (objt_server(s->target)) |
| 6409 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6410 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6411 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6412 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6413 | if (sess->listener->counters) |
| 6414 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6415 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6416 | goto return_srv_prx_502; |
| 6417 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6418 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6419 | /* add response headers from the rule sets in the same order */ |
| 6420 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6421 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6422 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6423 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6424 | 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] | 6425 | ret = acl_pass(ret); |
| 6426 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6427 | ret = !ret; |
| 6428 | if (!ret) |
| 6429 | continue; |
| 6430 | } |
| 6431 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6432 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6433 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6434 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6435 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6436 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6437 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6438 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6439 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6440 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6441 | /* After this point, this anayzer can't return yield, so we can |
| 6442 | * remove the bit corresponding to this analyzer from the list. |
| 6443 | * |
| 6444 | * Note that the intermediate returns and goto found previously |
| 6445 | * reset the analyzers. |
| 6446 | */ |
| 6447 | rep->analysers &= ~an_bit; |
| 6448 | rep->analyse_exp = TICK_ETERNITY; |
| 6449 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6450 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6451 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6452 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6453 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6454 | /* |
| 6455 | * Now check for a server cookie. |
| 6456 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6457 | 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] | 6458 | (s->be->options & PR_O_CHK_CACHE)) |
| 6459 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6460 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6461 | /* |
| 6462 | * Check for cache-control or pragma headers if required. |
| 6463 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6464 | 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] | 6465 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6466 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6467 | /* |
| 6468 | * Add server cookie in the response if needed |
| 6469 | */ |
| 6470 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6471 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6472 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6473 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6474 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6475 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6476 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6477 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6478 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6479 | /* the server is known, it's not the one the client requested, or the |
| 6480 | * cookie's last seen date needs to be refreshed. We have to |
| 6481 | * insert a set-cookie here, except if we want to insert only on POST |
| 6482 | * requests and this one isn't. Note that servers which don't have cookies |
| 6483 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6484 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6485 | if (!objt_server(s->target)->cookie) { |
| 6486 | chunk_printf(&trash, |
| 6487 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6488 | s->be->cookie_name); |
| 6489 | } |
| 6490 | else { |
| 6491 | 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] | 6492 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6493 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6494 | /* emit last_date, which is mandatory */ |
| 6495 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6496 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6497 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6498 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6499 | if (s->be->cookie_maxlife) { |
| 6500 | /* emit first_date, which is either the original one or |
| 6501 | * the current date. |
| 6502 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6503 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6504 | s30tob64(txn->cookie_first_date ? |
| 6505 | txn->cookie_first_date >> 2 : |
| 6506 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6507 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6508 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6509 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6510 | chunk_appendf(&trash, "; path=/"); |
| 6511 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6512 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6513 | if (s->be->cookie_domain) |
| 6514 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6515 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6516 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6517 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6518 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6519 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6520 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6521 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6522 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6523 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6524 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6525 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6526 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6527 | /* the server did not change, only the date was updated */ |
| 6528 | txn->flags |= TX_SCK_UPDATED; |
| 6529 | else |
| 6530 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6531 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6532 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6533 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6534 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6535 | * 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] | 6536 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6537 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6538 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6539 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6540 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6541 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6542 | "Cache-control: private", 22) < 0)) |
| 6543 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6544 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6545 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6546 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6547 | /* |
| 6548 | * Check if result will be cacheable with a cookie. |
| 6549 | * We'll block the response if security checks have caught |
| 6550 | * nasty things such as a cacheable cookie. |
| 6551 | */ |
| 6552 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6553 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6554 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6555 | /* we're in presence of a cacheable response containing |
| 6556 | * a set-cookie header. We'll block it as requested by |
| 6557 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6558 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6559 | if (objt_server(s->target)) |
| 6560 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6561 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6562 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6563 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6564 | if (sess->listener->counters) |
| 6565 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6566 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6567 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6568 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6569 | send_log(s->be, LOG_ALERT, |
| 6570 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6571 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6572 | goto return_srv_prx_502; |
| 6573 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6574 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6575 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6576 | /* |
| 6577 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6578 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6579 | * 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] | 6580 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6581 | * want to touch any 101 response either since it's switching to another |
| 6582 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6583 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6584 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6585 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6586 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6587 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6588 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6589 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6590 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6591 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6592 | /* we want a keep-alive response here. Keep-alive header |
| 6593 | * required if either side is not 1.1. |
| 6594 | */ |
| 6595 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6596 | want_flags |= TX_CON_KAL_SET; |
| 6597 | } |
| 6598 | else { |
| 6599 | /* we want a close response here. Close header required if |
| 6600 | * the server is 1.1, regardless of the client. |
| 6601 | */ |
| 6602 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6603 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6604 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6605 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6606 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6607 | http_change_connection_header(txn, msg, want_flags); |
| 6608 | } |
| 6609 | |
| 6610 | skip_header_mangling: |
| 6611 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6612 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6613 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
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 the user wants to log as soon as possible, without counting |
| 6616 | * bytes from the server, then this is the right moment. We have |
| 6617 | * to temporarily assign bytes_out to log what we currently have. |
| 6618 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6619 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6620 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6621 | s->logs.bytes_out = txn->rsp.eoh; |
| 6622 | s->do_log(s); |
| 6623 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6624 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6625 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6626 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6627 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6628 | /* This function is an analyser which forwards response body (including chunk |
| 6629 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6630 | * zero byte. The only situation where it must not be called is when we're in |
| 6631 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6632 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6633 | * 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] | 6634 | * 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] | 6635 | * |
| 6636 | * It is capable of compressing response data both in content-length mode and |
| 6637 | * in chunked mode. The state machines follows different flows depending on |
| 6638 | * whether content-length and chunked modes are used, since there are no |
| 6639 | * trailers in content-length : |
| 6640 | * |
| 6641 | * chk-mode cl-mode |
| 6642 | * ,----- BODY -----. |
| 6643 | * / \ |
| 6644 | * V size > 0 V chk-mode |
| 6645 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6646 | * | | size == 0 | last byte | |
| 6647 | * | v final crlf v inspected | |
| 6648 | * | TRAILERS -----------> DONE | |
| 6649 | * | | |
| 6650 | * `----------------------------------------------' |
| 6651 | * |
| 6652 | * Compression only happens in the DATA state, and must be flushed in final |
| 6653 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6654 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6655 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6656 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6657 | 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] | 6658 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6659 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6660 | struct http_txn *txn = s->txn; |
| 6661 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6662 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6663 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6664 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6665 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6666 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6667 | return 0; |
| 6668 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6669 | 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] | 6670 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6671 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6672 | /* Output closed while we were sending data. We must abort and |
| 6673 | * wake the other side up. |
| 6674 | */ |
| 6675 | msg->msg_state = HTTP_MSG_ERROR; |
| 6676 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6677 | return 1; |
| 6678 | } |
| 6679 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6680 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6681 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6682 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6683 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6684 | /* we have msg->sov which points to the first byte of message |
| 6685 | * body, and res->buf.p still points to the beginning of the |
| 6686 | * message. We forward the headers now, as we don't need them |
| 6687 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6688 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6689 | b_adv(res->buf, msg->sov); |
| 6690 | msg->next -= msg->sov; |
| 6691 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6692 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6693 | /* The previous analysers guarantee that the state is somewhere |
| 6694 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6695 | * msg->next are always correct. |
| 6696 | */ |
| 6697 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6698 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6699 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6700 | else |
| 6701 | msg->msg_state = HTTP_MSG_DATA; |
| 6702 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6703 | } |
| 6704 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6705 | if (res->to_forward) { |
| 6706 | /* We can't process the buffer's contents yet */ |
| 6707 | res->flags |= CF_WAKE_WRITE; |
| 6708 | goto missing_data; |
| 6709 | } |
| 6710 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6711 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6712 | /* We need a compression buffer in the DATA state to put the |
| 6713 | * output of compressed data, and in CRLF state to let the |
| 6714 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6715 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6716 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6717 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6718 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6719 | goto aborted_xfer; /* no memory */ |
| 6720 | } |
| 6721 | |
| 6722 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6723 | if (ret < 0) { |
| 6724 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6725 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6726 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6727 | compressing = 1; |
| 6728 | } |
| 6729 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6730 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6731 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6732 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6733 | /* we may have some pending data starting at res->buf->p */ |
| 6734 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6735 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6736 | if (ret < 0) |
| 6737 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6738 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6739 | if (msg->chunk_len) { |
| 6740 | /* input empty or output full */ |
| 6741 | if (res->buf->i > msg->next) |
| 6742 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6743 | goto missing_data; |
| 6744 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6745 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6746 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6747 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6748 | /* output full */ |
| 6749 | res->flags |= CF_WAKE_WRITE; |
| 6750 | goto missing_data; |
| 6751 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6752 | msg->next += msg->chunk_len; |
| 6753 | msg->chunk_len = 0; |
| 6754 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6755 | |
| 6756 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6757 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6758 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6759 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6760 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6761 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6762 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6763 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6764 | |
| 6765 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6766 | /* we want the CRLF after the data */ |
| 6767 | |
| 6768 | ret = http_skip_chunk_crlf(msg); |
| 6769 | if (ret == 0) |
| 6770 | goto missing_data; |
| 6771 | else if (ret < 0) { |
| 6772 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6773 | 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] | 6774 | goto return_bad_res; |
| 6775 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6776 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6777 | |
| 6778 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6779 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6780 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6781 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6782 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6783 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6784 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6785 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6786 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6787 | else if (ret < 0) { |
| 6788 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6789 | 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] | 6790 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6791 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6792 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6793 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6794 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6795 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6796 | if (unlikely(compressing)) { |
| 6797 | /* we need to flush output contents before syncing FSMs */ |
| 6798 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6799 | compressing = 0; |
| 6800 | } |
| 6801 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6802 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6803 | if (ret == 0) |
| 6804 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6805 | else if (ret < 0) { |
| 6806 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6807 | 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] | 6808 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6809 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6810 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6811 | |
| 6812 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6813 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6814 | if (unlikely(compressing)) { |
| 6815 | /* we need to flush output contents before syncing FSMs */ |
| 6816 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6817 | compressing = 0; |
| 6818 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6819 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6820 | /* we may have some pending data starting at res->buf->p |
| 6821 | * such as a last chunk of data or trailers. |
| 6822 | */ |
| 6823 | b_adv(res->buf, msg->next); |
| 6824 | msg->next = 0; |
| 6825 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6826 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6827 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6828 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6829 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6830 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6831 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6832 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6833 | /* some state changes occurred, maybe the analyser |
| 6834 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6835 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6836 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6837 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6838 | /* response errors are most likely due to |
| 6839 | * the client aborting the transfer. |
| 6840 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6841 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6842 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6843 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6844 | 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] | 6845 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6846 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6847 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6848 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6849 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6850 | } |
| 6851 | } |
| 6852 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6853 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6854 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6855 | if (unlikely(compressing)) { |
| 6856 | 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] | 6857 | compressing = 0; |
| 6858 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6859 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6860 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6861 | b_adv(res->buf, msg->next); |
| 6862 | msg->next = 0; |
| 6863 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6864 | } |
| 6865 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6866 | if (res->flags & CF_SHUTW) |
| 6867 | goto aborted_xfer; |
| 6868 | |
| 6869 | /* stop waiting for data if the input is closed before the end. If the |
| 6870 | * client side was already closed, it means that the client has aborted, |
| 6871 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6872 | * server abort. |
| 6873 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6874 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6875 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6876 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6877 | if (!(s->flags & SF_ERR_MASK)) |
| 6878 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6879 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6880 | if (objt_server(s->target)) |
| 6881 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6882 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6883 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6884 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6885 | /* 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] | 6886 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6887 | goto return_bad_res; |
| 6888 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6889 | /* 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] | 6890 | * 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] | 6891 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6892 | * close. |
| 6893 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6894 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6895 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6896 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6897 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6898 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6899 | /* 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] | 6900 | * 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] | 6901 | * 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] | 6902 | * modes are already handled by the stream sock layer. We must not do |
| 6903 | * this in content-length mode because it could present the MSG_MORE |
| 6904 | * flag with the last block of forwarded data, which would cause an |
| 6905 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6906 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6907 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6908 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6909 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6910 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6911 | return 0; |
| 6912 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6913 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6914 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6915 | if (objt_server(s->target)) |
| 6916 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6917 | |
| 6918 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6919 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6920 | 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] | 6921 | compressing = 0; |
| 6922 | } |
| 6923 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6924 | /* we may have some pending data starting at res->buf->p */ |
| 6925 | if (s->comp_algo == NULL) { |
| 6926 | b_adv(res->buf, msg->next); |
| 6927 | msg->next = 0; |
| 6928 | } |
| 6929 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6930 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6931 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6932 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6933 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6934 | 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] | 6935 | if (objt_server(s->target)) |
| 6936 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6937 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6938 | if (!(s->flags & SF_ERR_MASK)) |
| 6939 | s->flags |= SF_ERR_PRXCOND; |
| 6940 | if (!(s->flags & SF_FINST_MASK)) |
| 6941 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6942 | return 0; |
| 6943 | |
| 6944 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6945 | if (unlikely(compressing)) { |
| 6946 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6947 | compressing = 0; |
| 6948 | } |
| 6949 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6950 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6951 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6952 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6953 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6954 | 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] | 6955 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6956 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6957 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6958 | if (objt_server(s->target)) |
| 6959 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6960 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6961 | if (!(s->flags & SF_ERR_MASK)) |
| 6962 | s->flags |= SF_ERR_CLICL; |
| 6963 | if (!(s->flags & SF_FINST_MASK)) |
| 6964 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6965 | return 0; |
| 6966 | } |
| 6967 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6968 | /* Iterate the same filter through all request headers. |
| 6969 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6970 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6971 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6972 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6973 | 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] | 6974 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6975 | char *cur_ptr, *cur_end, *cur_next; |
| 6976 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6977 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6978 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6979 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6980 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6981 | last_hdr = 0; |
| 6982 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6983 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6984 | old_idx = 0; |
| 6985 | |
| 6986 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6987 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6988 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6989 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6990 | (exp->action == ACT_ALLOW || |
| 6991 | exp->action == ACT_DENY || |
| 6992 | exp->action == ACT_TARPIT)) |
| 6993 | return 0; |
| 6994 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6995 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6996 | if (!cur_idx) |
| 6997 | break; |
| 6998 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6999 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7000 | cur_ptr = cur_next; |
| 7001 | cur_end = cur_ptr + cur_hdr->len; |
| 7002 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7003 | |
| 7004 | /* Now we have one header between cur_ptr and cur_end, |
| 7005 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7006 | */ |
| 7007 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7008 | 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] | 7009 | switch (exp->action) { |
| 7010 | case ACT_SETBE: |
| 7011 | /* It is not possible to jump a second time. |
| 7012 | * FIXME: should we return an HTTP/500 here so that |
| 7013 | * the admin knows there's a problem ? |
| 7014 | */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7015 | if (s->be != strm_fe(s)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7016 | break; |
| 7017 | |
| 7018 | /* Swithing Proxy */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7019 | stream_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7020 | last_hdr = 1; |
| 7021 | break; |
| 7022 | |
| 7023 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7024 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7025 | last_hdr = 1; |
| 7026 | break; |
| 7027 | |
| 7028 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7029 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7030 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7031 | break; |
| 7032 | |
| 7033 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7034 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7035 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7036 | break; |
| 7037 | |
| 7038 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7039 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7040 | if (trash.len < 0) |
| 7041 | return -1; |
| 7042 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7043 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7044 | /* FIXME: if the user adds a newline in the replacement, the |
| 7045 | * index will not be recalculated for now, and the new line |
| 7046 | * will not be counted as a new header. |
| 7047 | */ |
| 7048 | |
| 7049 | cur_end += delta; |
| 7050 | cur_next += delta; |
| 7051 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7052 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7053 | break; |
| 7054 | |
| 7055 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7056 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7057 | cur_next += delta; |
| 7058 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7059 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7060 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7061 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7062 | cur_hdr->len = 0; |
| 7063 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7064 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7065 | break; |
| 7066 | |
| 7067 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7068 | } |
| 7069 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7070 | /* keep the link from this header to next one in case of later |
| 7071 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7073 | old_idx = cur_idx; |
| 7074 | } |
| 7075 | return 0; |
| 7076 | } |
| 7077 | |
| 7078 | |
| 7079 | /* Apply the filter to the request line. |
| 7080 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7081 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7082 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7083 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7084 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7085 | int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7086 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7087 | char *cur_ptr, *cur_end; |
| 7088 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7089 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7090 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7091 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7092 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7093 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7094 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7095 | (exp->action == ACT_ALLOW || |
| 7096 | exp->action == ACT_DENY || |
| 7097 | exp->action == ACT_TARPIT)) |
| 7098 | return 0; |
| 7099 | else if (exp->action == ACT_REMOVE) |
| 7100 | return 0; |
| 7101 | |
| 7102 | done = 0; |
| 7103 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7104 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7105 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7106 | |
| 7107 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7108 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7109 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7110 | switch (exp->action) { |
| 7111 | case ACT_SETBE: |
| 7112 | /* It is not possible to jump a second time. |
| 7113 | * FIXME: should we return an HTTP/500 here so that |
| 7114 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7115 | */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7116 | if (s->be != strm_fe(s)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7117 | break; |
| 7118 | |
| 7119 | /* Swithing Proxy */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7120 | stream_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7121 | done = 1; |
| 7122 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7124 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7125 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7126 | done = 1; |
| 7127 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7128 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7129 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7130 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7131 | done = 1; |
| 7132 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7133 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7134 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7135 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7136 | done = 1; |
| 7137 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7138 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7139 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7140 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7141 | if (trash.len < 0) |
| 7142 | return -1; |
| 7143 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7144 | 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] | 7145 | /* FIXME: if the user adds a newline in the replacement, the |
| 7146 | * index will not be recalculated for now, and the new line |
| 7147 | * will not be counted as a new header. |
| 7148 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7149 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7150 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7151 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7152 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7153 | HTTP_MSG_RQMETH, |
| 7154 | cur_ptr, cur_end + 1, |
| 7155 | NULL, NULL); |
| 7156 | if (unlikely(!cur_end)) |
| 7157 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7158 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7159 | /* we have a full request and we know that we have either a CR |
| 7160 | * or an LF at <ptr>. |
| 7161 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7162 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7163 | 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] | 7164 | /* there is no point trying this regex on headers */ |
| 7165 | return 1; |
| 7166 | } |
| 7167 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7168 | return done; |
| 7169 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7170 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7171 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7172 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7173 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7174 | * 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] | 7175 | * 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] | 7176 | * unparsable request. Since it can manage the switch to another backend, it |
| 7177 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7178 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7179 | 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] | 7180 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7181 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7182 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7183 | struct hdr_exp *exp; |
| 7184 | |
| 7185 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7186 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7187 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7188 | /* |
| 7189 | * The interleaving of transformations and verdicts |
| 7190 | * makes it difficult to decide to continue or stop |
| 7191 | * the evaluation. |
| 7192 | */ |
| 7193 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7194 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7195 | break; |
| 7196 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7197 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7198 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7199 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7200 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7201 | |
| 7202 | /* if this filter had a condition, evaluate it now and skip to |
| 7203 | * next filter if the condition does not match. |
| 7204 | */ |
| 7205 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7206 | 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] | 7207 | ret = acl_pass(ret); |
| 7208 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7209 | ret = !ret; |
| 7210 | |
| 7211 | if (!ret) |
| 7212 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7213 | } |
| 7214 | |
| 7215 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7216 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7217 | if (unlikely(ret < 0)) |
| 7218 | return -1; |
| 7219 | |
| 7220 | if (likely(ret == 0)) { |
| 7221 | /* The filter did not match the request, it can be |
| 7222 | * iterated through all headers. |
| 7223 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7224 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7225 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7226 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7227 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7228 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7229 | } |
| 7230 | |
| 7231 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7232 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7233 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7234 | * Try to retrieve the server associated to the appsession. |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7235 | * If the server is found, it's assigned to the stream. |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7236 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7237 | 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] | 7238 | struct http_txn *txn = s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7239 | appsess *asession = NULL; |
| 7240 | char *sessid_temp = NULL; |
| 7241 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7242 | if (len > s->be->appsession_len) { |
| 7243 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7244 | } |
| 7245 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7246 | if (s->be->options2 & PR_O2_AS_REQL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7247 | /* 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] | 7248 | if (txn->sessid != NULL) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7249 | /* 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] | 7250 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7251 | } |
| 7252 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7253 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7254 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7255 | 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] | 7256 | return; |
| 7257 | } |
| 7258 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7259 | memcpy(txn->sessid, buf, len); |
| 7260 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 7264 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7265 | 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] | 7266 | return; |
| 7267 | } |
| 7268 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7269 | memcpy(sessid_temp, buf, len); |
| 7270 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7271 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7272 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7273 | /* free previously allocated memory */ |
| 7274 | pool_free2(apools.sessid, sessid_temp); |
| 7275 | |
| 7276 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7277 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 7278 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7279 | asession->request_count++; |
| 7280 | |
| 7281 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7282 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7283 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7284 | while (srv) { |
| 7285 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7286 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7287 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7288 | (s->flags & SF_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7289 | /* we found the server and it's usable */ |
| 7290 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7291 | 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] | 7292 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7293 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 7294 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7295 | break; |
| 7296 | } else { |
| 7297 | txn->flags &= ~TX_CK_MASK; |
| 7298 | txn->flags |= TX_CK_DOWN; |
| 7299 | } |
| 7300 | } |
| 7301 | srv = srv->next; |
| 7302 | } |
| 7303 | } |
| 7304 | } |
| 7305 | } |
| 7306 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7307 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7308 | * same way as with headers above except that the semi-colon also ends a token. |
| 7309 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7310 | * return a valid result. |
| 7311 | */ |
| 7312 | char *find_cookie_value_end(char *s, const char *e) |
| 7313 | { |
| 7314 | int quoted, qdpair; |
| 7315 | |
| 7316 | quoted = qdpair = 0; |
| 7317 | for (; s < e; s++) { |
| 7318 | if (qdpair) qdpair = 0; |
| 7319 | else if (quoted) { |
| 7320 | if (*s == '\\') qdpair = 1; |
| 7321 | else if (*s == '"') quoted = 0; |
| 7322 | } |
| 7323 | else if (*s == '"') quoted = 1; |
| 7324 | else if (*s == ',' || *s == ';') return s; |
| 7325 | } |
| 7326 | return s; |
| 7327 | } |
| 7328 | |
| 7329 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7330 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7331 | * the first delimiter is updated if required. The function tries as much as |
| 7332 | * possible to respect the following principles : |
| 7333 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7334 | * colon, in which case <next> is simply removed |
| 7335 | * - set exactly one space character after the new first delimiter, unless |
| 7336 | * there are not enough characters in the block being moved to do so. |
| 7337 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7338 | * one. |
| 7339 | * |
| 7340 | * It is the caller's responsibility to ensure that : |
| 7341 | * - <from> points to a valid delimiter or the colon ; |
| 7342 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7343 | * - there are non-space chars before <from> ; |
| 7344 | * - there is a CR/LF at or after <next>. |
| 7345 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7346 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7347 | { |
| 7348 | char *prev = *from; |
| 7349 | |
| 7350 | if (*prev == ':') { |
| 7351 | /* We're removing the first value, preserve the colon and add a |
| 7352 | * space if possible. |
| 7353 | */ |
| 7354 | if (!http_is_crlf[(unsigned char)*next]) |
| 7355 | next++; |
| 7356 | prev++; |
| 7357 | if (prev < next) |
| 7358 | *prev++ = ' '; |
| 7359 | |
| 7360 | while (http_is_spht[(unsigned char)*next]) |
| 7361 | next++; |
| 7362 | } else { |
| 7363 | /* Remove useless spaces before the old delimiter. */ |
| 7364 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7365 | prev--; |
| 7366 | *from = prev; |
| 7367 | |
| 7368 | /* copy the delimiter and if possible a space if we're |
| 7369 | * not at the end of the line. |
| 7370 | */ |
| 7371 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7372 | *prev++ = *next++; |
| 7373 | if (prev + 1 < next) |
| 7374 | *prev++ = ' '; |
| 7375 | while (http_is_spht[(unsigned char)*next]) |
| 7376 | next++; |
| 7377 | } |
| 7378 | } |
| 7379 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7380 | } |
| 7381 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7382 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7383 | * 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] | 7384 | * desirable to call it only when needed. This code is quite complex because |
| 7385 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7386 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7387 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7388 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7389 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7390 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7391 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7392 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7393 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7394 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7395 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7396 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7397 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7398 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7399 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7400 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7401 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7402 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7403 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7404 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7405 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7406 | hdr_beg = hdr_next; |
| 7407 | hdr_end = hdr_beg + cur_hdr->len; |
| 7408 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7409 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7410 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7411 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7412 | * "Cookie:" headers. |
| 7413 | */ |
| 7414 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7415 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7416 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7417 | old_idx = cur_idx; |
| 7418 | continue; |
| 7419 | } |
| 7420 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7421 | del_from = NULL; /* nothing to be deleted */ |
| 7422 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7423 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7424 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7425 | * attributes whose name begin with a '$', and associate them with |
| 7426 | * the right cookie, if we want to delete this cookie. |
| 7427 | * So there are 3 cases for each cookie read : |
| 7428 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7429 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7430 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7431 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7432 | * "special" cookie. |
| 7433 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7434 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7435 | * *MUST* delete it. |
| 7436 | * |
| 7437 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7438 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7439 | * the RFC explicitly allows spaces before it, and not within the |
| 7440 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7441 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7442 | * after the equal sign too, resulting in some (rare) buggy |
| 7443 | * implementations trying to do that. So let's do what servers do. |
| 7444 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7445 | * allowed quoted strings in values, with any possible character |
| 7446 | * after a backslash, including control chars and delimitors, which |
| 7447 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7448 | * within values even without quotes. |
| 7449 | * |
| 7450 | * We have to keep multiple pointers in order to support cookie |
| 7451 | * removal at the beginning, middle or end of header without |
| 7452 | * corrupting the header. All of these headers are valid : |
| 7453 | * |
| 7454 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7455 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7456 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7457 | * | | | | | | | | | |
| 7458 | * | | | | | | | | hdr_end <--+ |
| 7459 | * | | | | | | | +--> next |
| 7460 | * | | | | | | +----> val_end |
| 7461 | * | | | | | +-----------> val_beg |
| 7462 | * | | | | +--------------> equal |
| 7463 | * | | | +----------------> att_end |
| 7464 | * | | +---------------------> att_beg |
| 7465 | * | +--------------------------> prev |
| 7466 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7467 | */ |
| 7468 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7469 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7470 | /* Iterate through all cookies on this line */ |
| 7471 | |
| 7472 | /* find att_beg */ |
| 7473 | att_beg = prev + 1; |
| 7474 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7475 | att_beg++; |
| 7476 | |
| 7477 | /* find att_end : this is the first character after the last non |
| 7478 | * space before the equal. It may be equal to hdr_end. |
| 7479 | */ |
| 7480 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7481 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7482 | while (equal < hdr_end) { |
| 7483 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7484 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7485 | if (http_is_spht[(unsigned char)*equal++]) |
| 7486 | continue; |
| 7487 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7488 | } |
| 7489 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7490 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7491 | * is between <att_beg> and <equal>, both may be identical. |
| 7492 | */ |
| 7493 | |
| 7494 | /* look for end of cookie if there is an equal sign */ |
| 7495 | if (equal < hdr_end && *equal == '=') { |
| 7496 | /* look for the beginning of the value */ |
| 7497 | val_beg = equal + 1; |
| 7498 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7499 | val_beg++; |
| 7500 | |
| 7501 | /* find the end of the value, respecting quotes */ |
| 7502 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7503 | |
| 7504 | /* make val_end point to the first white space or delimitor after the value */ |
| 7505 | val_end = next; |
| 7506 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7507 | val_end--; |
| 7508 | } else { |
| 7509 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7510 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7511 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7512 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7513 | * they will automatically be removed if a header before them is removed, |
| 7514 | * since they're supposed to be linked together. |
| 7515 | */ |
| 7516 | if (*att_beg == '$') |
| 7517 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7518 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7519 | /* Ignore cookies with no equal sign */ |
| 7520 | if (equal == next) { |
| 7521 | /* This is not our cookie, so we must preserve it. But if we already |
| 7522 | * scheduled another cookie for removal, we cannot remove the |
| 7523 | * complete header, but we can remove the previous block itself. |
| 7524 | */ |
| 7525 | preserve_hdr = 1; |
| 7526 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7527 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7528 | val_end += delta; |
| 7529 | next += delta; |
| 7530 | hdr_end += delta; |
| 7531 | hdr_next += delta; |
| 7532 | cur_hdr->len += delta; |
| 7533 | http_msg_move_end(&txn->req, delta); |
| 7534 | prev = del_from; |
| 7535 | del_from = NULL; |
| 7536 | } |
| 7537 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7538 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7539 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7540 | /* if there are spaces around the equal sign, we need to |
| 7541 | * strip them otherwise we'll get trouble for cookie captures, |
| 7542 | * or even for rewrites. Since this happens extremely rarely, |
| 7543 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7544 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7545 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7546 | int stripped_before = 0; |
| 7547 | int stripped_after = 0; |
| 7548 | |
| 7549 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7550 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7551 | equal += stripped_before; |
| 7552 | val_beg += stripped_before; |
| 7553 | } |
| 7554 | |
| 7555 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7556 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7557 | val_beg += stripped_after; |
| 7558 | stripped_before += stripped_after; |
| 7559 | } |
| 7560 | |
| 7561 | val_end += stripped_before; |
| 7562 | next += stripped_before; |
| 7563 | hdr_end += stripped_before; |
| 7564 | hdr_next += stripped_before; |
| 7565 | cur_hdr->len += stripped_before; |
| 7566 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7567 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7568 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7569 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7570 | /* First, let's see if we want to capture this cookie. We check |
| 7571 | * that we don't already have a client side cookie, because we |
| 7572 | * can only capture one. Also as an optimisation, we ignore |
| 7573 | * cookies shorter than the declared name. |
| 7574 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7575 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7576 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7577 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7578 | int log_len = val_end - att_beg; |
| 7579 | |
| 7580 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7581 | Alert("HTTP logging : out of memory.\n"); |
| 7582 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7583 | if (log_len > sess->fe->capture_len) |
| 7584 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7585 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7586 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7587 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7588 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7589 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7590 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7591 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7592 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7593 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7594 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7595 | * For cookies in prefix mode, the form is : |
| 7596 | * |
| 7597 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7598 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7599 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7600 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7601 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7602 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7603 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7604 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7605 | * have the server ID between val_beg and delim, and the original cookie between |
| 7606 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7607 | * |
| 7608 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7609 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7610 | * | || || | |+-> next |
| 7611 | * | || || | +--> val_end |
| 7612 | * | || || +---------> delim |
| 7613 | * | || |+------------> val_beg |
| 7614 | * | || +-------------> att_end = equal |
| 7615 | * | |+-----------------> att_beg |
| 7616 | * | +------------------> prev |
| 7617 | * +-------------------------> hdr_beg |
| 7618 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7619 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7620 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7621 | for (delim = val_beg; delim < val_end; delim++) |
| 7622 | if (*delim == COOKIE_DELIM) |
| 7623 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7624 | } else { |
| 7625 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7626 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7627 | /* Now check if the cookie contains a date field, which would |
| 7628 | * appear after a vertical bar ('|') just after the server name |
| 7629 | * and before the delimiter. |
| 7630 | */ |
| 7631 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7632 | if (vbar1) { |
| 7633 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7634 | * right is the last seen date. It is a base64 encoded |
| 7635 | * 30-bit value representing the UNIX date since the |
| 7636 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7637 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7638 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7639 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7640 | if (val_end - vbar1 >= 5) { |
| 7641 | val = b64tos30(vbar1); |
| 7642 | if (val > 0) |
| 7643 | txn->cookie_last_date = val << 2; |
| 7644 | } |
| 7645 | /* look for a second vertical bar */ |
| 7646 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7647 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7648 | val = b64tos30(vbar1 + 1); |
| 7649 | if (val > 0) |
| 7650 | txn->cookie_first_date = val << 2; |
| 7651 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7652 | } |
| 7653 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7654 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7655 | /* if the cookie has an expiration date and the proxy wants to check |
| 7656 | * it, then we do that now. We first check if the cookie is too old, |
| 7657 | * then only if it has expired. We detect strict overflow because the |
| 7658 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7659 | * in the future are ignored if their offset is beyond one day. This |
| 7660 | * allows an admin to fix timezone issues without expiring everyone |
| 7661 | * and at the same time avoids keeping unwanted side effects for too |
| 7662 | * long. |
| 7663 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7664 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7665 | (((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] | 7666 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7667 | txn->flags &= ~TX_CK_MASK; |
| 7668 | txn->flags |= TX_CK_OLD; |
| 7669 | delim = val_beg; // let's pretend we have not found the cookie |
| 7670 | txn->cookie_first_date = 0; |
| 7671 | txn->cookie_last_date = 0; |
| 7672 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7673 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7674 | (((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] | 7675 | ((signed)(txn->cookie_last_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_EXPIRED; |
| 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 | } |
| 7682 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7683 | /* Here, we'll look for the first running server which supports the cookie. |
| 7684 | * This allows to share a same cookie between several servers, for example |
| 7685 | * to dedicate backup servers to specific servers only. |
| 7686 | * However, to prevent clients from sticking to cookie-less backup server |
| 7687 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7688 | * empty cookies and mark them as invalid. |
| 7689 | * The same behaviour is applied when persistence must be ignored. |
| 7690 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7691 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7692 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7693 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7694 | while (srv) { |
| 7695 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7696 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7697 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7698 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7699 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7700 | /* we found the server and we can use it */ |
| 7701 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7702 | 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] | 7703 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7704 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7705 | break; |
| 7706 | } else { |
| 7707 | /* we found a server, but it's down, |
| 7708 | * mark it as such and go on in case |
| 7709 | * another one is available. |
| 7710 | */ |
| 7711 | txn->flags &= ~TX_CK_MASK; |
| 7712 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7713 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7714 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7715 | srv = srv->next; |
| 7716 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7717 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7718 | 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] | 7719 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7720 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7721 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7722 | txn->flags |= TX_CK_UNUSED; |
| 7723 | else |
| 7724 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7725 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7726 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7727 | /* depending on the cookie mode, we may have to either : |
| 7728 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7729 | * the server never sees it ; |
| 7730 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7731 | * application cookie so that it does not get accidentely removed later, |
| 7732 | * if we're in cookie prefix mode |
| 7733 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7734 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7735 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7736 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7737 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7738 | val_end += delta; |
| 7739 | next += delta; |
| 7740 | hdr_end += delta; |
| 7741 | hdr_next += delta; |
| 7742 | cur_hdr->len += delta; |
| 7743 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7744 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7745 | del_from = NULL; |
| 7746 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7747 | } |
| 7748 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7749 | (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] | 7750 | del_from = prev; |
| 7751 | } |
| 7752 | } else { |
| 7753 | /* This is not our cookie, so we must preserve it. But if we already |
| 7754 | * scheduled another cookie for removal, we cannot remove the |
| 7755 | * complete header, but we can remove the previous block itself. |
| 7756 | */ |
| 7757 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7758 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7760 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7761 | if (att_beg >= del_from) |
| 7762 | att_beg += delta; |
| 7763 | if (att_end >= del_from) |
| 7764 | att_end += delta; |
| 7765 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7766 | val_end += delta; |
| 7767 | next += delta; |
| 7768 | hdr_end += delta; |
| 7769 | hdr_next += delta; |
| 7770 | cur_hdr->len += delta; |
| 7771 | http_msg_move_end(&txn->req, delta); |
| 7772 | prev = del_from; |
| 7773 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7774 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7775 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7776 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7777 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7778 | if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7779 | int cmp_len, value_len; |
| 7780 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7781 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7782 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7783 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7784 | value_begin = att_beg + s->be->appsession_name_len; |
| 7785 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7786 | } else { |
| 7787 | cmp_len = att_end - att_beg; |
| 7788 | value_begin = val_beg; |
| 7789 | value_len = val_end - val_beg; |
| 7790 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7791 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7792 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7793 | if (cmp_len == s->be->appsession_name_len && |
| 7794 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7795 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7796 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7797 | } |
| 7798 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7799 | /* continue with next cookie on this header line */ |
| 7800 | att_beg = next; |
| 7801 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7802 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7803 | /* There are no more cookies on this line. |
| 7804 | * We may still have one (or several) marked for deletion at the |
| 7805 | * end of the line. We must do this now in two ways : |
| 7806 | * - if some cookies must be preserved, we only delete from the |
| 7807 | * mark to the end of line ; |
| 7808 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7809 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7810 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7811 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7812 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7813 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7814 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7815 | cur_hdr->len += delta; |
| 7816 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7817 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7818 | |
| 7819 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7820 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7821 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7822 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7823 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7824 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7825 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7826 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7827 | } |
| 7828 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7829 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7830 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7831 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7832 | } |
| 7833 | |
| 7834 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7835 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7836 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7837 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7838 | 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] | 7839 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | char *cur_ptr, *cur_end, *cur_next; |
| 7841 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7842 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7843 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7844 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7845 | |
| 7846 | last_hdr = 0; |
| 7847 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7848 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7849 | old_idx = 0; |
| 7850 | |
| 7851 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7852 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7854 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7855 | (exp->action == ACT_ALLOW || |
| 7856 | exp->action == ACT_DENY)) |
| 7857 | return 0; |
| 7858 | |
| 7859 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7860 | if (!cur_idx) |
| 7861 | break; |
| 7862 | |
| 7863 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7864 | cur_ptr = cur_next; |
| 7865 | cur_end = cur_ptr + cur_hdr->len; |
| 7866 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7867 | |
| 7868 | /* Now we have one header between cur_ptr and cur_end, |
| 7869 | * and the next header starts at cur_next. |
| 7870 | */ |
| 7871 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7872 | 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] | 7873 | switch (exp->action) { |
| 7874 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7875 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7876 | last_hdr = 1; |
| 7877 | break; |
| 7878 | |
| 7879 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7880 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7881 | last_hdr = 1; |
| 7882 | break; |
| 7883 | |
| 7884 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7885 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7886 | if (trash.len < 0) |
| 7887 | return -1; |
| 7888 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7889 | 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] | 7890 | /* FIXME: if the user adds a newline in the replacement, the |
| 7891 | * index will not be recalculated for now, and the new line |
| 7892 | * will not be counted as a new header. |
| 7893 | */ |
| 7894 | |
| 7895 | cur_end += delta; |
| 7896 | cur_next += delta; |
| 7897 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7898 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7899 | break; |
| 7900 | |
| 7901 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7902 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7903 | cur_next += delta; |
| 7904 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7905 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7906 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7907 | txn->hdr_idx.used--; |
| 7908 | cur_hdr->len = 0; |
| 7909 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7910 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7911 | break; |
| 7912 | |
| 7913 | } |
| 7914 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7915 | |
| 7916 | /* keep the link from this header to next one in case of later |
| 7917 | * removal of next header. |
| 7918 | */ |
| 7919 | old_idx = cur_idx; |
| 7920 | } |
| 7921 | return 0; |
| 7922 | } |
| 7923 | |
| 7924 | |
| 7925 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7926 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7927 | * or -1 if a replacement resulted in an invalid status line. |
| 7928 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7929 | 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] | 7930 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7931 | char *cur_ptr, *cur_end; |
| 7932 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7933 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7934 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7935 | |
| 7936 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7937 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7938 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7939 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7940 | (exp->action == ACT_ALLOW || |
| 7941 | exp->action == ACT_DENY)) |
| 7942 | return 0; |
| 7943 | else if (exp->action == ACT_REMOVE) |
| 7944 | return 0; |
| 7945 | |
| 7946 | done = 0; |
| 7947 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7948 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7949 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7950 | |
| 7951 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7952 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7953 | 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] | 7954 | switch (exp->action) { |
| 7955 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7956 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7957 | done = 1; |
| 7958 | break; |
| 7959 | |
| 7960 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7961 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7962 | done = 1; |
| 7963 | break; |
| 7964 | |
| 7965 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7966 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7967 | if (trash.len < 0) |
| 7968 | return -1; |
| 7969 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7970 | 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] | 7971 | /* FIXME: if the user adds a newline in the replacement, the |
| 7972 | * index will not be recalculated for now, and the new line |
| 7973 | * will not be counted as a new header. |
| 7974 | */ |
| 7975 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7976 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7977 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7978 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7979 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7980 | cur_ptr, cur_end + 1, |
| 7981 | NULL, NULL); |
| 7982 | if (unlikely(!cur_end)) |
| 7983 | return -1; |
| 7984 | |
| 7985 | /* we have a full respnse and we know that we have either a CR |
| 7986 | * or an LF at <ptr>. |
| 7987 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7988 | 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] | 7989 | 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] | 7990 | /* there is no point trying this regex on headers */ |
| 7991 | return 1; |
| 7992 | } |
| 7993 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7994 | return done; |
| 7995 | } |
| 7996 | |
| 7997 | |
| 7998 | |
| 7999 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8000 | * 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] | 8001 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8002 | * unparsable response. |
| 8003 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8004 | 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] | 8005 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8006 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8007 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8008 | struct hdr_exp *exp; |
| 8009 | |
| 8010 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8011 | int ret; |
| 8012 | |
| 8013 | /* |
| 8014 | * The interleaving of transformations and verdicts |
| 8015 | * makes it difficult to decide to continue or stop |
| 8016 | * the evaluation. |
| 8017 | */ |
| 8018 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8019 | if (txn->flags & TX_SVDENY) |
| 8020 | break; |
| 8021 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8022 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8023 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8024 | exp->action == ACT_PASS)) { |
| 8025 | exp = exp->next; |
| 8026 | continue; |
| 8027 | } |
| 8028 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8029 | /* if this filter had a condition, evaluate it now and skip to |
| 8030 | * next filter if the condition does not match. |
| 8031 | */ |
| 8032 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8033 | 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] | 8034 | ret = acl_pass(ret); |
| 8035 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8036 | ret = !ret; |
| 8037 | if (!ret) |
| 8038 | continue; |
| 8039 | } |
| 8040 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8041 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8042 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8043 | if (unlikely(ret < 0)) |
| 8044 | return -1; |
| 8045 | |
| 8046 | if (likely(ret == 0)) { |
| 8047 | /* The filter did not match the response, it can be |
| 8048 | * iterated through all headers. |
| 8049 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8050 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8051 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8052 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8053 | } |
| 8054 | return 0; |
| 8055 | } |
| 8056 | |
| 8057 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8058 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8059 | * 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] | 8060 | * desirable to call it only when needed. This function is also used when we |
| 8061 | * 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] | 8062 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8063 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8064 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8065 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8066 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8067 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8068 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8069 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8070 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8071 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8072 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8073 | /* Iterate through the headers. |
| 8074 | * we start with the start line. |
| 8075 | */ |
| 8076 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8077 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8078 | |
| 8079 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8080 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8081 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8082 | |
| 8083 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8084 | hdr_beg = hdr_next; |
| 8085 | hdr_end = hdr_beg + cur_hdr->len; |
| 8086 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8087 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8088 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8089 | * next header starts at hdr_next. We're only interested in |
| 8090 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8091 | */ |
| 8092 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8093 | is_cookie2 = 0; |
| 8094 | prev = hdr_beg + 10; |
| 8095 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8096 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8097 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8098 | if (!val) { |
| 8099 | old_idx = cur_idx; |
| 8100 | continue; |
| 8101 | } |
| 8102 | is_cookie2 = 1; |
| 8103 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8104 | } |
| 8105 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8106 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8107 | * <prev> points to the colon. |
| 8108 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8109 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8110 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8111 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8112 | * check-cache is enabled) and we are not interested in checking |
| 8113 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8114 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8115 | if (s->be->cookie_name == NULL && |
| 8116 | s->be->appsession_name == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8117 | sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8118 | return; |
| 8119 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8120 | /* OK so now we know we have to process this response cookie. |
| 8121 | * The format of the Set-Cookie header is slightly different |
| 8122 | * from the format of the Cookie header in that it does not |
| 8123 | * support the comma as a cookie delimiter (thus the header |
| 8124 | * cannot be folded) because the Expires attribute described in |
| 8125 | * the original Netscape's spec may contain an unquoted date |
| 8126 | * with a comma inside. We have to live with this because |
| 8127 | * many browsers don't support Max-Age and some browsers don't |
| 8128 | * support quoted strings. However the Set-Cookie2 header is |
| 8129 | * clean. |
| 8130 | * |
| 8131 | * We have to keep multiple pointers in order to support cookie |
| 8132 | * removal at the beginning, middle or end of header without |
| 8133 | * corrupting the header (in case of set-cookie2). A special |
| 8134 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8135 | * fields after the first semi-colon. The <next> pointer points |
| 8136 | * either to the end of line (set-cookie) or next unquoted comma |
| 8137 | * (set-cookie2). All of these headers are valid : |
| 8138 | * |
| 8139 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8140 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8141 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8142 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8143 | * | | | | | | | | | | |
| 8144 | * | | | | | | | | +-> next hdr_end <--+ |
| 8145 | * | | | | | | | +------------> scav |
| 8146 | * | | | | | | +--------------> val_end |
| 8147 | * | | | | | +--------------------> val_beg |
| 8148 | * | | | | +----------------------> equal |
| 8149 | * | | | +------------------------> att_end |
| 8150 | * | | +----------------------------> att_beg |
| 8151 | * | +------------------------------> prev |
| 8152 | * +-----------------------------------------> hdr_beg |
| 8153 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8154 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8155 | for (; prev < hdr_end; prev = next) { |
| 8156 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8157 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8158 | /* find att_beg */ |
| 8159 | att_beg = prev + 1; |
| 8160 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8161 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8162 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8163 | /* find att_end : this is the first character after the last non |
| 8164 | * space before the equal. It may be equal to hdr_end. |
| 8165 | */ |
| 8166 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8167 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8168 | while (equal < hdr_end) { |
| 8169 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8170 | break; |
| 8171 | if (http_is_spht[(unsigned char)*equal++]) |
| 8172 | continue; |
| 8173 | att_end = equal; |
| 8174 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8175 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8176 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8177 | * is between <att_beg> and <equal>, both may be identical. |
| 8178 | */ |
| 8179 | |
| 8180 | /* look for end of cookie if there is an equal sign */ |
| 8181 | if (equal < hdr_end && *equal == '=') { |
| 8182 | /* look for the beginning of the value */ |
| 8183 | val_beg = equal + 1; |
| 8184 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8185 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8186 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8187 | /* find the end of the value, respecting quotes */ |
| 8188 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8189 | |
| 8190 | /* make val_end point to the first white space or delimitor after the value */ |
| 8191 | val_end = next; |
| 8192 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8193 | val_end--; |
| 8194 | } else { |
| 8195 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8196 | val_beg = val_end = next = equal; |
| 8197 | } |
| 8198 | |
| 8199 | if (next < hdr_end) { |
| 8200 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8201 | * a colon or semi-colon before the end. So skip all attr-value |
| 8202 | * pairs and look for the next comma. For Set-Cookie, since |
| 8203 | * commas are permitted in values, skip to the end. |
| 8204 | */ |
| 8205 | if (is_cookie2) |
| 8206 | next = find_hdr_value_end(next, hdr_end); |
| 8207 | else |
| 8208 | next = hdr_end; |
| 8209 | } |
| 8210 | |
| 8211 | /* Now everything is as on the diagram above */ |
| 8212 | |
| 8213 | /* Ignore cookies with no equal sign */ |
| 8214 | if (equal == val_end) |
| 8215 | continue; |
| 8216 | |
| 8217 | /* If there are spaces around the equal sign, we need to |
| 8218 | * strip them otherwise we'll get trouble for cookie captures, |
| 8219 | * or even for rewrites. Since this happens extremely rarely, |
| 8220 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8222 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8223 | int stripped_before = 0; |
| 8224 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8226 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8227 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8228 | equal += stripped_before; |
| 8229 | val_beg += stripped_before; |
| 8230 | } |
| 8231 | |
| 8232 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8233 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8234 | val_beg += stripped_after; |
| 8235 | stripped_before += stripped_after; |
| 8236 | } |
| 8237 | |
| 8238 | val_end += stripped_before; |
| 8239 | next += stripped_before; |
| 8240 | hdr_end += stripped_before; |
| 8241 | hdr_next += stripped_before; |
| 8242 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8243 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8244 | } |
| 8245 | |
| 8246 | /* First, let's see if we want to capture this cookie. We check |
| 8247 | * that we don't already have a server side cookie, because we |
| 8248 | * can only capture one. Also as an optimisation, we ignore |
| 8249 | * cookies shorter than the declared name. |
| 8250 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8251 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8252 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8253 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8254 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8255 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8256 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8257 | Alert("HTTP logging : out of memory.\n"); |
| 8258 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8259 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8260 | if (log_len > sess->fe->capture_len) |
| 8261 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8262 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8263 | txn->srv_cookie[log_len] = 0; |
| 8264 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8265 | } |
| 8266 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8267 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8268 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8269 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8270 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8271 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8272 | /* assume passive cookie by default */ |
| 8273 | txn->flags &= ~TX_SCK_MASK; |
| 8274 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8275 | |
| 8276 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8277 | * this occurrence because we'll insert another one later. |
| 8278 | * 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] | 8279 | * a direct access. |
| 8280 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8281 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8282 | /* The "preserve" flag was set, we don't want to touch the |
| 8283 | * server's cookie. |
| 8284 | */ |
| 8285 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8286 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8287 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8288 | /* this cookie must be deleted */ |
| 8289 | if (*prev == ':' && next == hdr_end) { |
| 8290 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8291 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8292 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8293 | txn->hdr_idx.used--; |
| 8294 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8295 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8296 | hdr_next += delta; |
| 8297 | http_msg_move_end(&txn->rsp, delta); |
| 8298 | /* note: while both invalid now, <next> and <hdr_end> |
| 8299 | * are still equal, so the for() will stop as expected. |
| 8300 | */ |
| 8301 | } else { |
| 8302 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8303 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8304 | next = prev; |
| 8305 | hdr_end += delta; |
| 8306 | hdr_next += delta; |
| 8307 | cur_hdr->len += delta; |
| 8308 | http_msg_move_end(&txn->rsp, delta); |
| 8309 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8310 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8311 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8312 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8313 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8314 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8315 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8316 | * with this server since we know it. |
| 8317 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8318 | 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] | 8319 | next += delta; |
| 8320 | hdr_end += delta; |
| 8321 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8322 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8323 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8324 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8325 | txn->flags &= ~TX_SCK_MASK; |
| 8326 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8327 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8328 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8329 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8330 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8331 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8332 | 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] | 8333 | next += delta; |
| 8334 | hdr_end += delta; |
| 8335 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8336 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8337 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8338 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8339 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8340 | txn->flags &= ~TX_SCK_MASK; |
| 8341 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8342 | } |
| 8343 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 8344 | /* 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] | 8345 | else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8346 | int cmp_len, value_len; |
| 8347 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8348 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8349 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 8350 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 8351 | value_begin = att_beg + s->be->appsession_name_len; |
| 8352 | 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] | 8353 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8354 | cmp_len = att_end - att_beg; |
| 8355 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8356 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8357 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8358 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8359 | if ((cmp_len == s->be->appsession_name_len) && |
| 8360 | (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] | 8361 | /* free a possibly previously allocated memory */ |
| 8362 | pool_free2(apools.sessid, txn->sessid); |
| 8363 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8364 | /* Store the sessid in the stream for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8365 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8366 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8367 | 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] | 8368 | return; |
| 8369 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8370 | memcpy(txn->sessid, value_begin, value_len); |
| 8371 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8372 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8373 | } |
| 8374 | /* that's done for this cookie, check the next one on the same |
| 8375 | * line when next != hdr_end (only if is_cookie2). |
| 8376 | */ |
| 8377 | } |
| 8378 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8379 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8380 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8381 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8382 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8383 | appsess *asession = NULL; |
| 8384 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8385 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8386 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 8387 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8388 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 8389 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8390 | 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] | 8391 | return; |
| 8392 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8393 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 8394 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8395 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 8396 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8397 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8398 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8399 | return; |
| 8400 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8401 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 8402 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8403 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8404 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8405 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 8406 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8407 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 8408 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8409 | return; |
| 8410 | } |
| 8411 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8412 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8413 | |
| 8414 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8415 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8416 | } |
| 8417 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8418 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 8419 | asession->request_count++; |
| 8420 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8421 | } |
| 8422 | |
| 8423 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8424 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8425 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8426 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8427 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8428 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8429 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8430 | char *p1, *p2; |
| 8431 | |
| 8432 | char *cur_ptr, *cur_end, *cur_next; |
| 8433 | int cur_idx; |
| 8434 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8435 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8436 | return; |
| 8437 | |
| 8438 | /* Iterate through the headers. |
| 8439 | * we start with the start line. |
| 8440 | */ |
| 8441 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8442 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8443 | |
| 8444 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8445 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8446 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8447 | |
| 8448 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8449 | cur_ptr = cur_next; |
| 8450 | cur_end = cur_ptr + cur_hdr->len; |
| 8451 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8452 | |
| 8453 | /* We have one full header between cur_ptr and cur_end, and the |
| 8454 | * next header starts at cur_next. We're only interested in |
| 8455 | * "Cookie:" headers. |
| 8456 | */ |
| 8457 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8458 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8459 | if (val) { |
| 8460 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8461 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8462 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8463 | return; |
| 8464 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8465 | } |
| 8466 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8467 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8468 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8469 | continue; |
| 8470 | |
| 8471 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8472 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8473 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8474 | |
| 8475 | if (p1 >= cur_end) /* no more info */ |
| 8476 | continue; |
| 8477 | |
| 8478 | /* p1 is at the beginning of the value */ |
| 8479 | p2 = p1; |
| 8480 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8481 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8482 | p2++; |
| 8483 | |
| 8484 | /* we have a complete value between p1 and p2 */ |
| 8485 | if (p2 < cur_end && *p2 == '=') { |
| 8486 | /* we have something of the form no-cache="set-cookie" */ |
| 8487 | if ((cur_end - p1 >= 21) && |
| 8488 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8489 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8490 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8491 | continue; |
| 8492 | } |
| 8493 | |
| 8494 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8495 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8496 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8497 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8498 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8499 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8500 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8501 | return; |
| 8502 | } |
| 8503 | |
| 8504 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8505 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8506 | continue; |
| 8507 | } |
| 8508 | } |
| 8509 | } |
| 8510 | |
| 8511 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8512 | /* |
| 8513 | * 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] | 8514 | * If the server is found, it's assigned to the stream. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8515 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8516 | 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] | 8517 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8518 | char *end_params, *first_param, *cur_param, *next_param; |
| 8519 | char separator; |
| 8520 | int value_len; |
| 8521 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8522 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8523 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8524 | if (s->be->appsession_name == NULL || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8525 | (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] | 8526 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8527 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8528 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8529 | first_param = NULL; |
| 8530 | switch (mode) { |
| 8531 | case PR_O2_AS_M_PP: |
| 8532 | first_param = memchr(begin, ';', len); |
| 8533 | break; |
| 8534 | case PR_O2_AS_M_QS: |
| 8535 | first_param = memchr(begin, '?', len); |
| 8536 | break; |
| 8537 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8538 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8539 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8540 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8541 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8542 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8543 | switch (mode) { |
| 8544 | case PR_O2_AS_M_PP: |
| 8545 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8546 | end_params = (char *) begin + len; |
| 8547 | } |
| 8548 | separator = ';'; |
| 8549 | break; |
| 8550 | case PR_O2_AS_M_QS: |
| 8551 | end_params = (char *) begin + len; |
| 8552 | separator = '&'; |
| 8553 | break; |
| 8554 | default: |
| 8555 | /* unknown mode, shouldn't happen */ |
| 8556 | return; |
| 8557 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8558 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8559 | cur_param = next_param = end_params; |
| 8560 | while (cur_param > first_param) { |
| 8561 | cur_param--; |
| 8562 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8563 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8564 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8565 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8566 | (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] | 8567 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8568 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8569 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8570 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8571 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8572 | } |
| 8573 | break; |
| 8574 | } |
| 8575 | next_param = cur_param; |
| 8576 | } |
| 8577 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8578 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8579 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8580 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8581 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8582 | } |
| 8583 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8584 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8585 | * 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] | 8586 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8587 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8588 | * 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] | 8589 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8590 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8591 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8592 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8593 | 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] | 8594 | { |
| 8595 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8596 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8597 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8598 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8599 | if (!uri_auth) |
| 8600 | return 0; |
| 8601 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8602 | 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] | 8603 | return 0; |
| 8604 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8605 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8606 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8607 | return 0; |
| 8608 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8609 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8610 | return 0; |
| 8611 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8612 | return 1; |
| 8613 | } |
| 8614 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8615 | /* |
| 8616 | * 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] | 8617 | * By default it tries to report the error position as msg->err_pos. However if |
| 8618 | * this one is not set, it will then report msg->next, which is the last known |
| 8619 | * 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] | 8620 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8621 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8622 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8623 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8624 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8625 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8626 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8627 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8628 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8629 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8630 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8631 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8632 | len1 = MIN(len1, es->len); |
| 8633 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8634 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8635 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8636 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8637 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8638 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8639 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8640 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8641 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8642 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8643 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8644 | es->when = date; // user-visible date |
| 8645 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8646 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8647 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8648 | if (objt_conn(sess->origin)) |
| 8649 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8650 | else |
| 8651 | memset(&es->src, 0, sizeof(es->src)); |
| 8652 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8653 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8654 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8655 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8656 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8657 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8658 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8659 | es->b_out = chn->buf->o; |
| 8660 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8661 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8662 | es->m_clen = msg->chunk_len; |
| 8663 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8664 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8665 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8666 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8667 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8668 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8669 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8670 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8671 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8672 | * 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] | 8673 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8674 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8675 | * 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] | 8676 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8677 | 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] | 8678 | struct hdr_idx *idx, int occ, |
| 8679 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8680 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8681 | struct hdr_ctx local_ctx; |
| 8682 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8683 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8684 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8685 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8686 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8687 | if (!ctx) { |
| 8688 | local_ctx.idx = 0; |
| 8689 | ctx = &local_ctx; |
| 8690 | } |
| 8691 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8692 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8693 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8694 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8695 | occ--; |
| 8696 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8697 | *vptr = ctx->line + ctx->val; |
| 8698 | *vlen = ctx->vlen; |
| 8699 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8700 | } |
| 8701 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8702 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8703 | } |
| 8704 | |
| 8705 | /* negative occurrence, we scan all the list then walk back */ |
| 8706 | if (-occ > MAX_HDR_HISTORY) |
| 8707 | return 0; |
| 8708 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8709 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8710 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8711 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8712 | len_hist[hist_ptr] = ctx->vlen; |
| 8713 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8714 | hist_ptr = 0; |
| 8715 | found++; |
| 8716 | } |
| 8717 | if (-occ > found) |
| 8718 | return 0; |
| 8719 | /* 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] | 8720 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8721 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8722 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8723 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8724 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8725 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8726 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8727 | *vptr = ptr_hist[hist_ptr]; |
| 8728 | *vlen = len_hist[hist_ptr]; |
| 8729 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8730 | } |
| 8731 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8732 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8733 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8734 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8735 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8736 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8737 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8738 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8739 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8740 | * line header values and does not stop at commas. |
| 8741 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8742 | */ |
| 8743 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8744 | struct hdr_idx *idx, int occ, |
| 8745 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8746 | { |
| 8747 | struct hdr_ctx local_ctx; |
| 8748 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8749 | int len_hist[MAX_HDR_HISTORY]; |
| 8750 | unsigned int hist_ptr; |
| 8751 | int found; |
| 8752 | |
| 8753 | if (!ctx) { |
| 8754 | local_ctx.idx = 0; |
| 8755 | ctx = &local_ctx; |
| 8756 | } |
| 8757 | |
| 8758 | if (occ >= 0) { |
| 8759 | /* search from the beginning */ |
| 8760 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8761 | occ--; |
| 8762 | if (occ <= 0) { |
| 8763 | *vptr = ctx->line + ctx->val; |
| 8764 | *vlen = ctx->vlen; |
| 8765 | return 1; |
| 8766 | } |
| 8767 | } |
| 8768 | return 0; |
| 8769 | } |
| 8770 | |
| 8771 | /* negative occurrence, we scan all the list then walk back */ |
| 8772 | if (-occ > MAX_HDR_HISTORY) |
| 8773 | return 0; |
| 8774 | |
| 8775 | found = hist_ptr = 0; |
| 8776 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8777 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8778 | len_hist[hist_ptr] = ctx->vlen; |
| 8779 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8780 | hist_ptr = 0; |
| 8781 | found++; |
| 8782 | } |
| 8783 | if (-occ > found) |
| 8784 | return 0; |
| 8785 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8786 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8787 | */ |
| 8788 | hist_ptr += occ; |
| 8789 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8790 | hist_ptr -= MAX_HDR_HISTORY; |
| 8791 | *vptr = ptr_hist[hist_ptr]; |
| 8792 | *vlen = len_hist[hist_ptr]; |
| 8793 | return 1; |
| 8794 | } |
| 8795 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8796 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8797 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8798 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8799 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8800 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8801 | 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] | 8802 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8803 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8804 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8805 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8806 | 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] | 8807 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8808 | 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] | 8809 | 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] | 8810 | |
| 8811 | for (max = 0; start + max < end; max++) |
| 8812 | if (start[max] == '\r' || start[max] == '\n') |
| 8813 | break; |
| 8814 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8815 | UBOUND(max, trash.size - trash.len - 3); |
| 8816 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8817 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8818 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8819 | } |
| 8820 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8821 | |
| 8822 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8823 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8824 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8825 | * assigned to the stream and returned. |
| 8826 | */ |
| 8827 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8828 | { |
| 8829 | struct http_txn *txn = s->txn; |
| 8830 | |
| 8831 | if (txn) |
| 8832 | return txn; |
| 8833 | |
| 8834 | txn = pool_alloc2(pool2_http_txn); |
| 8835 | if (!txn) |
| 8836 | return txn; |
| 8837 | |
| 8838 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8839 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8840 | if (!txn->hdr_idx.v) { |
| 8841 | pool_free2(pool2_http_txn, txn); |
| 8842 | return NULL; |
| 8843 | } |
| 8844 | |
| 8845 | s->txn = txn; |
| 8846 | return txn; |
| 8847 | } |
| 8848 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8849 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8850 | * 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] | 8851 | * the required fields are properly allocated and that we only need to (re)init |
| 8852 | * them. This should be used before processing any new request. |
| 8853 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8854 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8855 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8856 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8857 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8858 | |
| 8859 | txn->flags = 0; |
| 8860 | txn->status = -1; |
| 8861 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8862 | txn->cookie_first_date = 0; |
| 8863 | txn->cookie_last_date = 0; |
| 8864 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8865 | txn->sessid = NULL; |
| 8866 | txn->srv_cookie = NULL; |
| 8867 | txn->cli_cookie = NULL; |
| 8868 | txn->uri = NULL; |
| 8869 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8870 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8871 | 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] | 8872 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8873 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8874 | 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] | 8875 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8876 | txn->req.chunk_len = 0LL; |
| 8877 | txn->req.body_len = 0LL; |
| 8878 | txn->rsp.chunk_len = 0LL; |
| 8879 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8880 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8881 | 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] | 8882 | txn->req.chn = &s->req; |
| 8883 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8884 | |
| 8885 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8886 | |
| 8887 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8888 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8889 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8890 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8891 | if (txn->hdr_idx.v) |
| 8892 | hdr_idx_init(&txn->hdr_idx); |
| 8893 | } |
| 8894 | |
| 8895 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8896 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8897 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8898 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8899 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8900 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8901 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8902 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8903 | s->comp_algo->end(&s->comp_ctx); |
| 8904 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8905 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8906 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8907 | /* these ones will have been dynamically allocated */ |
| 8908 | pool_free2(pool2_requri, txn->uri); |
| 8909 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8910 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8911 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8912 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8913 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8914 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8915 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8916 | txn->uri = NULL; |
| 8917 | txn->srv_cookie = NULL; |
| 8918 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8919 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8920 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8921 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8922 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8923 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8924 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8925 | } |
| 8926 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8927 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8928 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8929 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8930 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8931 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8932 | } |
| 8933 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8934 | } |
| 8935 | |
| 8936 | /* 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] | 8937 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8938 | { |
| 8939 | http_end_txn(s); |
| 8940 | http_init_txn(s); |
| 8941 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8942 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8943 | * any rulelist match the NULL pointer. |
| 8944 | */ |
| 8945 | s->current_rule_list = NULL; |
| 8946 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8947 | s->be = strm_fe(s); |
| 8948 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8949 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8950 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8951 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8952 | /* re-init store persistence */ |
| 8953 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8954 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8955 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8956 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8957 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8958 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8959 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8960 | /* We must trim any excess data from the response buffer, because we |
| 8961 | * may have blocked an invalid response from a server that we don't |
| 8962 | * want to accidentely forward once we disable the analysers, nor do |
| 8963 | * we want those data to come along with next response. A typical |
| 8964 | * example of such data would be from a buggy server responding to |
| 8965 | * a HEAD with some data, or sending more than the advertised |
| 8966 | * content-length. |
| 8967 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8968 | if (unlikely(s->res.buf->i)) |
| 8969 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8970 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8971 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8972 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8973 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8974 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8975 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8976 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8977 | s->req.rex = TICK_ETERNITY; |
| 8978 | s->req.wex = TICK_ETERNITY; |
| 8979 | s->req.analyse_exp = TICK_ETERNITY; |
| 8980 | s->res.rex = TICK_ETERNITY; |
| 8981 | s->res.wex = TICK_ETERNITY; |
| 8982 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8983 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8984 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8985 | void free_http_res_rules(struct list *r) |
| 8986 | { |
| 8987 | struct http_res_rule *tr, *pr; |
| 8988 | |
| 8989 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8990 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8991 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8992 | free(pr); |
| 8993 | } |
| 8994 | } |
| 8995 | |
| 8996 | void free_http_req_rules(struct list *r) |
| 8997 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8998 | struct http_req_rule *tr, *pr; |
| 8999 | |
| 9000 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9001 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9002 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9003 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9004 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9005 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9006 | free(pr); |
| 9007 | } |
| 9008 | } |
| 9009 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9010 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9011 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9012 | { |
| 9013 | struct http_req_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9014 | struct http_req_action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9015 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9016 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9017 | |
| 9018 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 9019 | if (!rule) { |
| 9020 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9021 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9022 | } |
| 9023 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9024 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9025 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9026 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 9027 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9028 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9029 | int code; |
| 9030 | int hc; |
| 9031 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9032 | rule->action = HTTP_REQ_ACT_DENY; |
| 9033 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9034 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9035 | cur_arg++; |
| 9036 | if (!args[cur_arg]) { |
| 9037 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9038 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9039 | goto out_err; |
| 9040 | } |
| 9041 | |
| 9042 | code = atol(args[cur_arg]); |
| 9043 | cur_arg++; |
| 9044 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9045 | if (http_err_codes[hc] == code) { |
| 9046 | rule->deny_status = hc; |
| 9047 | break; |
| 9048 | } |
| 9049 | } |
| 9050 | |
| 9051 | if (hc >= HTTP_ERR_SIZE) { |
| 9052 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9053 | file, linenum, code); |
| 9054 | } |
| 9055 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9056 | } else if (!strcmp(args[0], "tarpit")) { |
| 9057 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 9058 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9059 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9060 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9061 | cur_arg = 1; |
| 9062 | |
| 9063 | while(*args[cur_arg]) { |
| 9064 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9065 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9066 | cur_arg+=2; |
| 9067 | continue; |
| 9068 | } else |
| 9069 | break; |
| 9070 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9071 | } else if (!strcmp(args[0], "set-nice")) { |
| 9072 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 9073 | cur_arg = 1; |
| 9074 | |
| 9075 | if (!*args[cur_arg] || |
| 9076 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9077 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9078 | file, linenum, args[0]); |
| 9079 | goto out_err; |
| 9080 | } |
| 9081 | rule->arg.nice = atoi(args[cur_arg]); |
| 9082 | if (rule->arg.nice < -1024) |
| 9083 | rule->arg.nice = -1024; |
| 9084 | else if (rule->arg.nice > 1024) |
| 9085 | rule->arg.nice = 1024; |
| 9086 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9087 | } else if (!strcmp(args[0], "set-tos")) { |
| 9088 | #ifdef IP_TOS |
| 9089 | char *err; |
| 9090 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 9091 | cur_arg = 1; |
| 9092 | |
| 9093 | if (!*args[cur_arg] || |
| 9094 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9095 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9096 | file, linenum, args[0]); |
| 9097 | goto out_err; |
| 9098 | } |
| 9099 | |
| 9100 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9101 | if (err && *err != '\0') { |
| 9102 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9103 | file, linenum, err, args[0]); |
| 9104 | goto out_err; |
| 9105 | } |
| 9106 | cur_arg++; |
| 9107 | #else |
| 9108 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9109 | goto out_err; |
| 9110 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9111 | } else if (!strcmp(args[0], "set-mark")) { |
| 9112 | #ifdef SO_MARK |
| 9113 | char *err; |
| 9114 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 9115 | cur_arg = 1; |
| 9116 | |
| 9117 | if (!*args[cur_arg] || |
| 9118 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9119 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9120 | file, linenum, args[0]); |
| 9121 | goto out_err; |
| 9122 | } |
| 9123 | |
| 9124 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9125 | if (err && *err != '\0') { |
| 9126 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9127 | file, linenum, err, args[0]); |
| 9128 | goto out_err; |
| 9129 | } |
| 9130 | cur_arg++; |
| 9131 | global.last_checks |= LSTCHK_NETADM; |
| 9132 | #else |
| 9133 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9134 | goto out_err; |
| 9135 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9136 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9137 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 9138 | cur_arg = 1; |
| 9139 | |
| 9140 | if (!*args[cur_arg] || |
| 9141 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9142 | bad_log_level: |
| 9143 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9144 | file, linenum, args[0]); |
| 9145 | goto out_err; |
| 9146 | } |
| 9147 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9148 | rule->arg.loglevel = -1; |
| 9149 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9150 | goto bad_log_level; |
| 9151 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9152 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9153 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 9154 | cur_arg = 1; |
| 9155 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9156 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9157 | (*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] | 9158 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9159 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9160 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9161 | } |
| 9162 | |
| 9163 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9164 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9165 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9166 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9167 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9168 | 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] | 9169 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9170 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9171 | free(proxy->conf.lfs_file); |
| 9172 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9173 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9174 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9175 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
| 9176 | 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] | 9177 | cur_arg = 1; |
| 9178 | |
| 9179 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9180 | (*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] | 9181 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9182 | file, linenum, args[0]); |
| 9183 | goto out_err; |
| 9184 | } |
| 9185 | |
| 9186 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9187 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9188 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9189 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9190 | error = NULL; |
| 9191 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9192 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9193 | args[cur_arg + 1], error); |
| 9194 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9195 | goto out_err; |
| 9196 | } |
| 9197 | |
| 9198 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9199 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9200 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9201 | file, linenum); |
| 9202 | |
| 9203 | free(proxy->conf.lfs_file); |
| 9204 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9205 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9206 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9207 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9208 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 9209 | cur_arg = 1; |
| 9210 | |
| 9211 | if (!*args[cur_arg] || |
| 9212 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9213 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9214 | file, linenum, args[0]); |
| 9215 | goto out_err; |
| 9216 | } |
| 9217 | |
| 9218 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9219 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9220 | |
| 9221 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9222 | free(proxy->conf.lfs_file); |
| 9223 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9224 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9225 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9226 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9227 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9228 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9229 | struct sample_expr *expr; |
| 9230 | unsigned int where; |
| 9231 | char *err = NULL; |
| 9232 | |
| 9233 | cur_arg = 1; |
| 9234 | proxy->conf.args.ctx = ARGC_TRK; |
| 9235 | |
| 9236 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9237 | if (!expr) { |
| 9238 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9239 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9240 | free(err); |
| 9241 | goto out_err; |
| 9242 | } |
| 9243 | |
| 9244 | where = 0; |
| 9245 | if (proxy->cap & PR_CAP_FE) |
| 9246 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9247 | if (proxy->cap & PR_CAP_BE) |
| 9248 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9249 | |
| 9250 | if (!(expr->fetch->val & where)) { |
| 9251 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9252 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9253 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9254 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9255 | free(expr); |
| 9256 | goto out_err; |
| 9257 | } |
| 9258 | |
| 9259 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9260 | cur_arg++; |
| 9261 | if (!args[cur_arg]) { |
| 9262 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9263 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9264 | free(expr); |
| 9265 | goto out_err; |
| 9266 | } |
| 9267 | /* we copy the table name for now, it will be resolved later */ |
| 9268 | rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9269 | cur_arg++; |
| 9270 | } |
| 9271 | rule->act_prm.trk_ctr.expr = expr; |
| 9272 | rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9273 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9274 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9275 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9276 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9277 | 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] | 9278 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9279 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9280 | goto out_err; |
| 9281 | } |
| 9282 | |
| 9283 | /* this redirect rule might already contain a parsed condition which |
| 9284 | * we'll pass to the http-request rule. |
| 9285 | */ |
| 9286 | rule->action = HTTP_REQ_ACT_REDIR; |
| 9287 | rule->arg.redir = redir; |
| 9288 | rule->cond = redir->cond; |
| 9289 | redir->cond = NULL; |
| 9290 | cur_arg = 2; |
| 9291 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9292 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9293 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9294 | rule->action = HTTP_REQ_ACT_ADD_ACL; |
| 9295 | /* |
| 9296 | * '+ 8' for 'add-acl(' |
| 9297 | * '- 9' for 'add-acl(' + trailing ')' |
| 9298 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9299 | 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] | 9300 | |
| 9301 | cur_arg = 1; |
| 9302 | |
| 9303 | if (!*args[cur_arg] || |
| 9304 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9305 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9306 | file, linenum, args[0]); |
| 9307 | goto out_err; |
| 9308 | } |
| 9309 | |
| 9310 | LIST_INIT(&rule->arg.map.key); |
| 9311 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9312 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9313 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9314 | file, linenum); |
| 9315 | free(proxy->conf.lfs_file); |
| 9316 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9317 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9318 | cur_arg += 1; |
| 9319 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9320 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
| 9321 | rule->action = HTTP_REQ_ACT_DEL_ACL; |
| 9322 | /* |
| 9323 | * '+ 8' for 'del-acl(' |
| 9324 | * '- 9' for 'del-acl(' + trailing ')' |
| 9325 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9326 | 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] | 9327 | |
| 9328 | cur_arg = 1; |
| 9329 | |
| 9330 | if (!*args[cur_arg] || |
| 9331 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9332 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9333 | file, linenum, args[0]); |
| 9334 | goto out_err; |
| 9335 | } |
| 9336 | |
| 9337 | LIST_INIT(&rule->arg.map.key); |
| 9338 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9339 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9340 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9341 | file, linenum); |
| 9342 | free(proxy->conf.lfs_file); |
| 9343 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9344 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9345 | cur_arg += 1; |
| 9346 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9347 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
| 9348 | rule->action = HTTP_REQ_ACT_DEL_MAP; |
| 9349 | /* |
| 9350 | * '+ 8' for 'del-map(' |
| 9351 | * '- 9' for 'del-map(' + trailing ')' |
| 9352 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9353 | 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] | 9354 | |
| 9355 | cur_arg = 1; |
| 9356 | |
| 9357 | if (!*args[cur_arg] || |
| 9358 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9359 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9360 | file, linenum, args[0]); |
| 9361 | goto out_err; |
| 9362 | } |
| 9363 | |
| 9364 | LIST_INIT(&rule->arg.map.key); |
| 9365 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9366 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9367 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9368 | file, linenum); |
| 9369 | free(proxy->conf.lfs_file); |
| 9370 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9371 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9372 | cur_arg += 1; |
| 9373 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9374 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9375 | rule->action = HTTP_REQ_ACT_SET_MAP; |
| 9376 | /* |
| 9377 | * '+ 8' for 'set-map(' |
| 9378 | * '- 9' for 'set-map(' + trailing ')' |
| 9379 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9380 | 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] | 9381 | |
| 9382 | cur_arg = 1; |
| 9383 | |
| 9384 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9385 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9386 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9387 | file, linenum, args[0]); |
| 9388 | goto out_err; |
| 9389 | } |
| 9390 | |
| 9391 | LIST_INIT(&rule->arg.map.key); |
| 9392 | LIST_INIT(&rule->arg.map.value); |
| 9393 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9394 | |
| 9395 | /* key pattern */ |
| 9396 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9397 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9398 | file, linenum); |
| 9399 | |
| 9400 | /* value pattern */ |
| 9401 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9402 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9403 | file, linenum); |
| 9404 | free(proxy->conf.lfs_file); |
| 9405 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9406 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9407 | |
| 9408 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9409 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9410 | char *errmsg = NULL; |
| 9411 | cur_arg = 1; |
| 9412 | /* try in the module list */ |
| 9413 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9414 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9415 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9416 | free(errmsg); |
| 9417 | goto out_err; |
| 9418 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9419 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9420 | 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] | 9421 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9422 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9426 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9427 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9428 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9429 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9430 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9431 | file, linenum, args[0], errmsg); |
| 9432 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9433 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9434 | } |
| 9435 | rule->cond = cond; |
| 9436 | } |
| 9437 | else if (*args[cur_arg]) { |
| 9438 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9439 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9440 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9441 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9442 | } |
| 9443 | |
| 9444 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9445 | out_err: |
| 9446 | free(rule); |
| 9447 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9448 | } |
| 9449 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9450 | /* parse an "http-respose" rule */ |
| 9451 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 9452 | { |
| 9453 | struct http_res_rule *rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9454 | struct http_res_action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9455 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9456 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9457 | |
| 9458 | rule = calloc(1, sizeof(*rule)); |
| 9459 | if (!rule) { |
| 9460 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9461 | goto out_err; |
| 9462 | } |
| 9463 | |
| 9464 | if (!strcmp(args[0], "allow")) { |
| 9465 | rule->action = HTTP_RES_ACT_ALLOW; |
| 9466 | cur_arg = 1; |
| 9467 | } else if (!strcmp(args[0], "deny")) { |
| 9468 | rule->action = HTTP_RES_ACT_DENY; |
| 9469 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9470 | } else if (!strcmp(args[0], "set-nice")) { |
| 9471 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 9472 | cur_arg = 1; |
| 9473 | |
| 9474 | if (!*args[cur_arg] || |
| 9475 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9476 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9477 | file, linenum, args[0]); |
| 9478 | goto out_err; |
| 9479 | } |
| 9480 | rule->arg.nice = atoi(args[cur_arg]); |
| 9481 | if (rule->arg.nice < -1024) |
| 9482 | rule->arg.nice = -1024; |
| 9483 | else if (rule->arg.nice > 1024) |
| 9484 | rule->arg.nice = 1024; |
| 9485 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9486 | } else if (!strcmp(args[0], "set-tos")) { |
| 9487 | #ifdef IP_TOS |
| 9488 | char *err; |
| 9489 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 9490 | cur_arg = 1; |
| 9491 | |
| 9492 | if (!*args[cur_arg] || |
| 9493 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9494 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9495 | file, linenum, args[0]); |
| 9496 | goto out_err; |
| 9497 | } |
| 9498 | |
| 9499 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9500 | if (err && *err != '\0') { |
| 9501 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9502 | file, linenum, err, args[0]); |
| 9503 | goto out_err; |
| 9504 | } |
| 9505 | cur_arg++; |
| 9506 | #else |
| 9507 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9508 | goto out_err; |
| 9509 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9510 | } else if (!strcmp(args[0], "set-mark")) { |
| 9511 | #ifdef SO_MARK |
| 9512 | char *err; |
| 9513 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 9514 | cur_arg = 1; |
| 9515 | |
| 9516 | if (!*args[cur_arg] || |
| 9517 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9518 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9519 | file, linenum, args[0]); |
| 9520 | goto out_err; |
| 9521 | } |
| 9522 | |
| 9523 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9524 | if (err && *err != '\0') { |
| 9525 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9526 | file, linenum, err, args[0]); |
| 9527 | goto out_err; |
| 9528 | } |
| 9529 | cur_arg++; |
| 9530 | global.last_checks |= LSTCHK_NETADM; |
| 9531 | #else |
| 9532 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9533 | goto out_err; |
| 9534 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9535 | } else if (!strcmp(args[0], "set-log-level")) { |
| 9536 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 9537 | cur_arg = 1; |
| 9538 | |
| 9539 | if (!*args[cur_arg] || |
| 9540 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9541 | bad_log_level: |
| 9542 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9543 | file, linenum, args[0]); |
| 9544 | goto out_err; |
| 9545 | } |
| 9546 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9547 | rule->arg.loglevel = -1; |
| 9548 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9549 | goto bad_log_level; |
| 9550 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9551 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 9552 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 9553 | cur_arg = 1; |
| 9554 | |
| 9555 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9556 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9557 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9558 | file, linenum, args[0]); |
| 9559 | goto out_err; |
| 9560 | } |
| 9561 | |
| 9562 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9563 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9564 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9565 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9566 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9567 | 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] | 9568 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9569 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9570 | free(proxy->conf.lfs_file); |
| 9571 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9572 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9573 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9574 | } 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] | 9575 | 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] | 9576 | cur_arg = 1; |
| 9577 | |
| 9578 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9579 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9580 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9581 | file, linenum, args[0]); |
| 9582 | goto out_err; |
| 9583 | } |
| 9584 | |
| 9585 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9586 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9587 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9588 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9589 | error = NULL; |
| 9590 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9591 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9592 | args[cur_arg + 1], error); |
| 9593 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9594 | goto out_err; |
| 9595 | } |
| 9596 | |
| 9597 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9598 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9599 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9600 | file, linenum); |
| 9601 | |
| 9602 | free(proxy->conf.lfs_file); |
| 9603 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9604 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9605 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9606 | } else if (strcmp(args[0], "del-header") == 0) { |
| 9607 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 9608 | cur_arg = 1; |
| 9609 | |
| 9610 | if (!*args[cur_arg] || |
| 9611 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9612 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9613 | file, linenum, args[0]); |
| 9614 | goto out_err; |
| 9615 | } |
| 9616 | |
| 9617 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9618 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9619 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9620 | proxy->conf.args.ctx = ARGC_HRS; |
| 9621 | free(proxy->conf.lfs_file); |
| 9622 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9623 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9624 | cur_arg += 1; |
| 9625 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9626 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
| 9627 | rule->action = HTTP_RES_ACT_ADD_ACL; |
| 9628 | /* |
| 9629 | * '+ 8' for 'add-acl(' |
| 9630 | * '- 9' for 'add-acl(' + trailing ')' |
| 9631 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9632 | 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] | 9633 | |
| 9634 | cur_arg = 1; |
| 9635 | |
| 9636 | if (!*args[cur_arg] || |
| 9637 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9638 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9639 | file, linenum, args[0]); |
| 9640 | goto out_err; |
| 9641 | } |
| 9642 | |
| 9643 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9644 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9645 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9646 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9647 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9648 | free(proxy->conf.lfs_file); |
| 9649 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9650 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9651 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9652 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9653 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9654 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
| 9655 | rule->action = HTTP_RES_ACT_DEL_ACL; |
| 9656 | /* |
| 9657 | * '+ 8' for 'del-acl(' |
| 9658 | * '- 9' for 'del-acl(' + trailing ')' |
| 9659 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9660 | 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] | 9661 | |
| 9662 | cur_arg = 1; |
| 9663 | |
| 9664 | if (!*args[cur_arg] || |
| 9665 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9666 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9667 | file, linenum, args[0]); |
| 9668 | goto out_err; |
| 9669 | } |
| 9670 | |
| 9671 | LIST_INIT(&rule->arg.map.key); |
| 9672 | proxy->conf.args.ctx = ARGC_HRS; |
| 9673 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9674 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9675 | file, linenum); |
| 9676 | free(proxy->conf.lfs_file); |
| 9677 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9678 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9679 | cur_arg += 1; |
| 9680 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9681 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
| 9682 | rule->action = HTTP_RES_ACT_DEL_MAP; |
| 9683 | /* |
| 9684 | * '+ 8' for 'del-map(' |
| 9685 | * '- 9' for 'del-map(' + trailing ')' |
| 9686 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9687 | 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] | 9688 | |
| 9689 | cur_arg = 1; |
| 9690 | |
| 9691 | if (!*args[cur_arg] || |
| 9692 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9693 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9694 | file, linenum, args[0]); |
| 9695 | goto out_err; |
| 9696 | } |
| 9697 | |
| 9698 | LIST_INIT(&rule->arg.map.key); |
| 9699 | proxy->conf.args.ctx = ARGC_HRS; |
| 9700 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9701 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9702 | file, linenum); |
| 9703 | free(proxy->conf.lfs_file); |
| 9704 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9705 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9706 | cur_arg += 1; |
| 9707 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9708 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
| 9709 | rule->action = HTTP_RES_ACT_SET_MAP; |
| 9710 | /* |
| 9711 | * '+ 8' for 'set-map(' |
| 9712 | * '- 9' for 'set-map(' + trailing ')' |
| 9713 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9714 | 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] | 9715 | |
| 9716 | cur_arg = 1; |
| 9717 | |
| 9718 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9719 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9720 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9721 | file, linenum, args[0]); |
| 9722 | goto out_err; |
| 9723 | } |
| 9724 | |
| 9725 | LIST_INIT(&rule->arg.map.key); |
| 9726 | LIST_INIT(&rule->arg.map.value); |
| 9727 | |
| 9728 | proxy->conf.args.ctx = ARGC_HRS; |
| 9729 | |
| 9730 | /* key pattern */ |
| 9731 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9732 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9733 | file, linenum); |
| 9734 | |
| 9735 | /* value pattern */ |
| 9736 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9737 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9738 | file, linenum); |
| 9739 | |
| 9740 | free(proxy->conf.lfs_file); |
| 9741 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9742 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9743 | |
| 9744 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9745 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9746 | char *errmsg = NULL; |
| 9747 | cur_arg = 1; |
| 9748 | /* try in the module list */ |
| 9749 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) { |
| 9750 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9751 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9752 | free(errmsg); |
| 9753 | goto out_err; |
| 9754 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9755 | } else { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9756 | 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] | 9757 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 9758 | goto out_err; |
| 9759 | } |
| 9760 | |
| 9761 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9762 | struct acl_cond *cond; |
| 9763 | char *errmsg = NULL; |
| 9764 | |
| 9765 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9766 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9767 | file, linenum, args[0], errmsg); |
| 9768 | free(errmsg); |
| 9769 | goto out_err; |
| 9770 | } |
| 9771 | rule->cond = cond; |
| 9772 | } |
| 9773 | else if (*args[cur_arg]) { |
| 9774 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9775 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9776 | file, linenum, args[0], args[cur_arg]); |
| 9777 | goto out_err; |
| 9778 | } |
| 9779 | |
| 9780 | return rule; |
| 9781 | out_err: |
| 9782 | free(rule); |
| 9783 | return NULL; |
| 9784 | } |
| 9785 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9786 | /* 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] | 9787 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 9788 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9789 | */ |
| 9790 | 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] | 9791 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9792 | { |
| 9793 | struct redirect_rule *rule; |
| 9794 | int cur_arg; |
| 9795 | int type = REDIRECT_TYPE_NONE; |
| 9796 | int code = 302; |
| 9797 | const char *destination = NULL; |
| 9798 | const char *cookie = NULL; |
| 9799 | int cookie_set = 0; |
| 9800 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9801 | struct acl_cond *cond = NULL; |
| 9802 | |
| 9803 | cur_arg = 0; |
| 9804 | while (*(args[cur_arg])) { |
| 9805 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9806 | if (!*args[cur_arg + 1]) |
| 9807 | goto missing_arg; |
| 9808 | |
| 9809 | type = REDIRECT_TYPE_LOCATION; |
| 9810 | cur_arg++; |
| 9811 | destination = args[cur_arg]; |
| 9812 | } |
| 9813 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9814 | if (!*args[cur_arg + 1]) |
| 9815 | goto missing_arg; |
| 9816 | |
| 9817 | type = REDIRECT_TYPE_PREFIX; |
| 9818 | cur_arg++; |
| 9819 | destination = args[cur_arg]; |
| 9820 | } |
| 9821 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9822 | if (!*args[cur_arg + 1]) |
| 9823 | goto missing_arg; |
| 9824 | |
| 9825 | type = REDIRECT_TYPE_SCHEME; |
| 9826 | cur_arg++; |
| 9827 | destination = args[cur_arg]; |
| 9828 | } |
| 9829 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9830 | if (!*args[cur_arg + 1]) |
| 9831 | goto missing_arg; |
| 9832 | |
| 9833 | cur_arg++; |
| 9834 | cookie = args[cur_arg]; |
| 9835 | cookie_set = 1; |
| 9836 | } |
| 9837 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9838 | if (!*args[cur_arg + 1]) |
| 9839 | goto missing_arg; |
| 9840 | |
| 9841 | cur_arg++; |
| 9842 | cookie = args[cur_arg]; |
| 9843 | cookie_set = 0; |
| 9844 | } |
| 9845 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9846 | if (!*args[cur_arg + 1]) |
| 9847 | goto missing_arg; |
| 9848 | |
| 9849 | cur_arg++; |
| 9850 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9851 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9852 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9853 | "'%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] | 9854 | args[cur_arg - 1], args[cur_arg]); |
| 9855 | return NULL; |
| 9856 | } |
| 9857 | } |
| 9858 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9859 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9860 | } |
| 9861 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9862 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9863 | } |
| 9864 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9865 | strcmp(args[cur_arg], "unless") == 0) { |
| 9866 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9867 | if (!cond) { |
| 9868 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9869 | return NULL; |
| 9870 | } |
| 9871 | break; |
| 9872 | } |
| 9873 | else { |
| 9874 | memprintf(errmsg, |
| 9875 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9876 | args[cur_arg]); |
| 9877 | return NULL; |
| 9878 | } |
| 9879 | cur_arg++; |
| 9880 | } |
| 9881 | |
| 9882 | if (type == REDIRECT_TYPE_NONE) { |
| 9883 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9884 | return NULL; |
| 9885 | } |
| 9886 | |
| 9887 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9888 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9889 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9890 | |
| 9891 | if (!use_fmt) { |
| 9892 | /* old-style static redirect rule */ |
| 9893 | rule->rdr_str = strdup(destination); |
| 9894 | rule->rdr_len = strlen(destination); |
| 9895 | } |
| 9896 | else { |
| 9897 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9898 | |
| 9899 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9900 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9901 | * makes it hard for the user to configure a self-redirection. |
| 9902 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9903 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9904 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9905 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9906 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9907 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9908 | free(curproxy->conf.lfs_file); |
| 9909 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9910 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9911 | } |
| 9912 | } |
| 9913 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9914 | if (cookie) { |
| 9915 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9916 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9917 | */ |
| 9918 | rule->cookie_len = strlen(cookie); |
| 9919 | if (cookie_set) { |
| 9920 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9921 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9922 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9923 | rule->cookie_len += 9; |
| 9924 | } else { |
| 9925 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9926 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9927 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9928 | rule->cookie_len += 20; |
| 9929 | } |
| 9930 | } |
| 9931 | rule->type = type; |
| 9932 | rule->code = code; |
| 9933 | rule->flags = flags; |
| 9934 | LIST_INIT(&rule->list); |
| 9935 | return rule; |
| 9936 | |
| 9937 | missing_arg: |
| 9938 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9939 | return NULL; |
| 9940 | } |
| 9941 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9942 | /************************************************************************/ |
| 9943 | /* The code below is dedicated to ACL parsing and matching */ |
| 9944 | /************************************************************************/ |
| 9945 | |
| 9946 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9947 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9948 | * which means that a request or response is ready. If some data is missing, |
| 9949 | * 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] | 9950 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9951 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9952 | * |
| 9953 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9954 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9955 | * decide whether or not an HTTP message is present ; |
| 9956 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9957 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9958 | * 1 if an HTTP message is ready |
| 9959 | */ |
| 9960 | static int |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9961 | smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9962 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9963 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9964 | struct http_txn *txn; |
| 9965 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9966 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 9967 | /* Note: this function may only be used from places where |
| 9968 | * http_init_txn() has already been done, and implies that <s>, |
| 9969 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 9970 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9971 | */ |
| 9972 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9973 | if (!s) |
| 9974 | return 0; |
| 9975 | txn = s->txn; |
| 9976 | |
| 9977 | if (!txn) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9978 | return 0; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9979 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9980 | |
| 9981 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9982 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9983 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9984 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9985 | /* If the buffer does not leave enough free space at the end, |
| 9986 | * we must first realign it. |
| 9987 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9988 | if (s->req.buf->p > s->req.buf->data && |
| 9989 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9990 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9991 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9992 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9993 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9994 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9995 | |
| 9996 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9997 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9998 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9999 | |
| 10000 | /* Still no valid request ? */ |
| 10001 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10002 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10003 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10004 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10005 | } |
| 10006 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10007 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10008 | return 0; |
| 10009 | } |
| 10010 | |
| 10011 | /* OK we just got a valid HTTP request. We have some minor |
| 10012 | * preparation to perform so that further checks can rely |
| 10013 | * on HTTP tests. |
| 10014 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10015 | |
| 10016 | /* If the request was parsed but was too large, we must absolutely |
| 10017 | * return an error so that it is not processed. At the moment this |
| 10018 | * cannot happen, but if the parsers are to change in the future, |
| 10019 | * we want this check to be maintained. |
| 10020 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10021 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10022 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10023 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10024 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10025 | return 1; |
| 10026 | } |
| 10027 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10028 | 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] | 10029 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10030 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10031 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10032 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10033 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10034 | } |
| 10035 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10036 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10037 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10038 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10039 | |
| 10040 | /* otherwise everything's ready for the request */ |
| 10041 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10042 | else { |
| 10043 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10044 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10045 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10046 | return 0; |
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 | |
| 10050 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10051 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10052 | return 1; |
| 10053 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10054 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 10055 | /* Note: these functinos *do* modify the sample. Even in case of success, at |
| 10056 | * least the type and uint value are modified. |
| 10057 | */ |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10058 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10059 | 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] | 10060 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10061 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10062 | 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] | 10063 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10064 | |
| 10065 | /* 1. Check on METHOD |
| 10066 | * We use the pre-parsed method if it is known, and store its number as an |
| 10067 | * integer. If it is unknown, we use the pointer and the length. |
| 10068 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10069 | 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] | 10070 | { |
| 10071 | int len, meth; |
| 10072 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10073 | len = strlen(text); |
| 10074 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10075 | |
| 10076 | pattern->val.i = meth; |
| 10077 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10078 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10079 | pattern->len = len; |
| 10080 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10081 | else { |
| 10082 | pattern->ptr.str = NULL; |
| 10083 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10084 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10085 | return 1; |
| 10086 | } |
| 10087 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10088 | /* This function fetches the method of current HTTP request and stores |
| 10089 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10090 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10091 | * in <len> and <ptr> is NULL ; |
| 10092 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10093 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10094 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10095 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10096 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10097 | 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] | 10098 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10099 | { |
| 10100 | int meth; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10101 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10102 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10103 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10104 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10105 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10106 | smp->type = SMP_T_METH; |
| 10107 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10108 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10109 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10110 | /* ensure the indexes are not affected */ |
| 10111 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10112 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10113 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 10114 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10115 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10116 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10117 | return 1; |
| 10118 | } |
| 10119 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10120 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10121 | 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] | 10122 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10123 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10124 | struct pattern_list *lst; |
| 10125 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10126 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10127 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10128 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10129 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10130 | /* well-known method */ |
| 10131 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 10132 | if (smp->data.meth.meth == pattern->val.i) |
| 10133 | return pattern; |
| 10134 | else |
| 10135 | continue; |
| 10136 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10137 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10138 | /* Other method, we must compare the strings */ |
| 10139 | if (pattern->len != smp->data.meth.str.len) |
| 10140 | continue; |
| 10141 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10142 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 4de2a94 | 2014-08-28 20:42:57 +0200 | [diff] [blame] | 10143 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) || |
| 10144 | (!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] | 10145 | return pattern; |
| 10146 | } |
| 10147 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10148 | } |
| 10149 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10150 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10151 | 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] | 10152 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10153 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10154 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10155 | char *ptr; |
| 10156 | int len; |
| 10157 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10158 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10159 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10160 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10161 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10162 | |
| 10163 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10164 | if (len <= 0) |
| 10165 | return 0; |
| 10166 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10167 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10168 | smp->data.str.str = ptr; |
| 10169 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10170 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10171 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10172 | return 1; |
| 10173 | } |
| 10174 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10175 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10176 | 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] | 10177 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10178 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10179 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10180 | char *ptr; |
| 10181 | int len; |
| 10182 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10183 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10184 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10185 | txn = strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10186 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10187 | return 0; |
| 10188 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10189 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10190 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10191 | |
| 10192 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10193 | if (len <= 0) |
| 10194 | return 0; |
| 10195 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10196 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10197 | smp->data.str.str = ptr; |
| 10198 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10199 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10200 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10201 | return 1; |
| 10202 | } |
| 10203 | |
| 10204 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10205 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10206 | 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] | 10207 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10208 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10209 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10210 | char *ptr; |
| 10211 | int len; |
| 10212 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10213 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10214 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10215 | txn = strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10216 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10217 | return 0; |
| 10218 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10219 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10220 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10221 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10222 | smp->type = SMP_T_UINT; |
| 10223 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10224 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10225 | return 1; |
| 10226 | } |
| 10227 | |
| 10228 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10229 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10230 | 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] | 10231 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10232 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10233 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10234 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10235 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10236 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10237 | txn = strm->txn; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10238 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10239 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10240 | 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] | 10241 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10242 | return 1; |
| 10243 | } |
| 10244 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10245 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10246 | 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] | 10247 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10248 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10249 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10250 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10251 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10252 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10253 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10254 | txn = strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10255 | 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] | 10256 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10257 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10258 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10259 | smp->type = SMP_T_IPV4; |
| 10260 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10261 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10262 | return 1; |
| 10263 | } |
| 10264 | |
| 10265 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10266 | 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] | 10267 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10268 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10269 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10270 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10271 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10272 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10273 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10274 | txn = strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10275 | 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] | 10276 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10277 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10278 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10279 | smp->type = SMP_T_UINT; |
| 10280 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10281 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10282 | return 1; |
| 10283 | } |
| 10284 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10285 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10286 | * Accepts an optional argument of type string containing the header field name, |
| 10287 | * and an optional argument of type signed or unsigned integer to request an |
| 10288 | * 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] | 10289 | * headers are considered from the first one. It does not stop on commas and |
| 10290 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10291 | */ |
| 10292 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10293 | 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] | 10294 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10295 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10296 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10297 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10298 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10299 | int occ = 0; |
| 10300 | const char *name_str = NULL; |
| 10301 | int name_len = 0; |
| 10302 | |
| 10303 | if (!ctx) { |
| 10304 | /* first call */ |
| 10305 | ctx = &static_hdr_ctx; |
| 10306 | ctx->idx = 0; |
| 10307 | smp->ctx.a[0] = ctx; |
| 10308 | } |
| 10309 | |
| 10310 | if (args) { |
| 10311 | if (args[0].type != ARGT_STR) |
| 10312 | return 0; |
| 10313 | name_str = args[0].data.str.str; |
| 10314 | name_len = args[0].data.str.len; |
| 10315 | |
| 10316 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10317 | occ = args[1].data.uint; |
| 10318 | } |
| 10319 | |
| 10320 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10321 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10322 | idx = &strm->txn->hdr_idx; |
| 10323 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10324 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10325 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10326 | /* search for header from the beginning */ |
| 10327 | ctx->idx = 0; |
| 10328 | |
| 10329 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10330 | /* no explicit occurrence and single fetch => last header by default */ |
| 10331 | occ = -1; |
| 10332 | |
| 10333 | if (!occ) |
| 10334 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10335 | smp->flags |= SMP_F_NOT_LAST; |
| 10336 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10337 | smp->type = SMP_T_STR; |
| 10338 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10339 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 10340 | return 1; |
| 10341 | |
| 10342 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10343 | return 0; |
| 10344 | } |
| 10345 | |
| 10346 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10347 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10348 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10349 | */ |
| 10350 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10351 | 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] | 10352 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10353 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10354 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10355 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10356 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10357 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10358 | const char *name = NULL; |
| 10359 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10360 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10361 | if (args && args->type == ARGT_STR) { |
| 10362 | name = args->data.str.str; |
| 10363 | len = args->data.str.len; |
| 10364 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10365 | |
| 10366 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10367 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10368 | idx = &strm->txn->hdr_idx; |
| 10369 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10370 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10371 | ctx.idx = 0; |
| 10372 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10373 | 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] | 10374 | cnt++; |
| 10375 | |
| 10376 | smp->type = SMP_T_UINT; |
| 10377 | smp->data.uint = cnt; |
| 10378 | smp->flags = SMP_F_VOL_HDR; |
| 10379 | return 1; |
| 10380 | } |
| 10381 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10382 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10383 | 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] | 10384 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10385 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10386 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10387 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10388 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10389 | struct chunk *temp; |
| 10390 | char del = ','; |
| 10391 | |
| 10392 | if (args && args->type == ARGT_STR) |
| 10393 | del = *args[0].data.str.str; |
| 10394 | |
| 10395 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10396 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10397 | idx = &strm->txn->hdr_idx; |
| 10398 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10399 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10400 | temp = get_trash_chunk(); |
| 10401 | |
| 10402 | ctx.idx = 0; |
| 10403 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10404 | if (temp->len) |
| 10405 | temp->str[temp->len++] = del; |
| 10406 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10407 | temp->len += ctx.del; |
| 10408 | } |
| 10409 | |
| 10410 | smp->type = SMP_T_STR; |
| 10411 | smp->data.str.str = temp->str; |
| 10412 | smp->data.str.len = temp->len; |
| 10413 | smp->flags = SMP_F_VOL_HDR; |
| 10414 | return 1; |
| 10415 | } |
| 10416 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10417 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10418 | * Accepts an optional argument of type string containing the header field name, |
| 10419 | * and an optional argument of type signed or unsigned integer to request an |
| 10420 | * 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] | 10421 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10422 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10423 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10424 | 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] | 10425 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10426 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10427 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10428 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10429 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10430 | int occ = 0; |
| 10431 | const char *name_str = NULL; |
| 10432 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10433 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10434 | if (!ctx) { |
| 10435 | /* first call */ |
| 10436 | ctx = &static_hdr_ctx; |
| 10437 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10438 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10439 | } |
| 10440 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10441 | if (args) { |
| 10442 | if (args[0].type != ARGT_STR) |
| 10443 | return 0; |
| 10444 | name_str = args[0].data.str.str; |
| 10445 | name_len = args[0].data.str.len; |
| 10446 | |
| 10447 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 10448 | occ = args[1].data.uint; |
| 10449 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10450 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10451 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10452 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10453 | idx = &strm->txn->hdr_idx; |
| 10454 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10455 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10456 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10457 | /* search for header from the beginning */ |
| 10458 | ctx->idx = 0; |
| 10459 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10460 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10461 | /* no explicit occurrence and single fetch => last header by default */ |
| 10462 | occ = -1; |
| 10463 | |
| 10464 | if (!occ) |
| 10465 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10466 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10467 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10468 | smp->type = SMP_T_STR; |
| 10469 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10470 | 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] | 10471 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10472 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10473 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10474 | return 0; |
| 10475 | } |
| 10476 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10477 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10478 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10479 | */ |
| 10480 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10481 | 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] | 10482 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10483 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10484 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10485 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10486 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10487 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10488 | const char *name = NULL; |
| 10489 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10490 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10491 | if (args && args->type == ARGT_STR) { |
| 10492 | name = args->data.str.str; |
| 10493 | len = args->data.str.len; |
| 10494 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10495 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10496 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10497 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10498 | idx = &strm->txn->hdr_idx; |
| 10499 | msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp; |
| 10500 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10501 | ctx.idx = 0; |
| 10502 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10503 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10504 | cnt++; |
| 10505 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10506 | smp->type = SMP_T_UINT; |
| 10507 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10508 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10509 | return 1; |
| 10510 | } |
| 10511 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10512 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10513 | * takes a mandatory argument of type string and an optional one of type int |
| 10514 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10515 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10516 | */ |
| 10517 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10518 | 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] | 10519 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10520 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10521 | 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] | 10522 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10523 | if (ret > 0) { |
| 10524 | smp->type = SMP_T_UINT; |
| 10525 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10526 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10527 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10528 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10529 | } |
| 10530 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10531 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10532 | * and an optional one of type int to designate a specific occurrence. |
| 10533 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10534 | */ |
| 10535 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10536 | 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] | 10537 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10538 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10539 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10540 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10541 | 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] | 10542 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 10543 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10544 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10545 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10546 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10547 | if (smp->data.str.len < temp->size - 1) { |
| 10548 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 10549 | temp->str[smp->data.str.len] = '\0'; |
| 10550 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 10551 | smp->type = SMP_T_IPV6; |
| 10552 | break; |
| 10553 | } |
| 10554 | } |
| 10555 | } |
| 10556 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10557 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10558 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10559 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10560 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10561 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10562 | } |
| 10563 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10564 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10565 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10566 | */ |
| 10567 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10568 | 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] | 10569 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10570 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10571 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10572 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10573 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10574 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10575 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10576 | txn = strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10577 | 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] | 10578 | ptr = http_get_path(txn); |
| 10579 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10580 | return 0; |
| 10581 | |
| 10582 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10583 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10584 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10585 | |
| 10586 | while (ptr < end && *ptr != '?') |
| 10587 | ptr++; |
| 10588 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10589 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10590 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10591 | return 1; |
| 10592 | } |
| 10593 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10594 | /* This produces a concatenation of the first occurrence of the Host header |
| 10595 | * followed by the path component if it begins with a slash ('/'). This means |
| 10596 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10597 | * If no Host header is found, then the path is returned as-is. The returned |
| 10598 | * 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] | 10599 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10600 | */ |
| 10601 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10602 | 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] | 10603 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10604 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10605 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10606 | char *ptr, *end, *beg; |
| 10607 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10608 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10609 | |
| 10610 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10611 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10612 | txn = strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10613 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10614 | 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] | 10615 | return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10616 | |
| 10617 | /* 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] | 10618 | temp = get_trash_chunk(); |
| 10619 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10620 | smp->type = SMP_T_STR; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10621 | smp->data.str.str = temp->str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10622 | smp->data.str.len = ctx.vlen; |
| 10623 | |
| 10624 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10625 | 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] | 10626 | beg = http_get_path(txn); |
| 10627 | if (!beg) |
| 10628 | beg = end; |
| 10629 | |
| 10630 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10631 | |
| 10632 | if (beg < ptr && *beg == '/') { |
| 10633 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 10634 | smp->data.str.len += ptr - beg; |
| 10635 | } |
| 10636 | |
| 10637 | smp->flags = SMP_F_VOL_1ST; |
| 10638 | return 1; |
| 10639 | } |
| 10640 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10641 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10642 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10643 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10644 | * 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] | 10645 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10646 | * 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] | 10647 | * high-traffic sites without having to store whole paths. |
| 10648 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10649 | int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10650 | 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] | 10651 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10652 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10653 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10654 | struct hdr_ctx ctx; |
| 10655 | unsigned int hash = 0; |
| 10656 | char *ptr, *beg, *end; |
| 10657 | int len; |
| 10658 | |
| 10659 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10660 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10661 | txn = strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10662 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10663 | 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] | 10664 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10665 | ptr = ctx.line + ctx.val; |
| 10666 | len = ctx.vlen; |
| 10667 | while (len--) |
| 10668 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10669 | } |
| 10670 | |
| 10671 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10672 | 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] | 10673 | beg = http_get_path(txn); |
| 10674 | if (!beg) |
| 10675 | beg = end; |
| 10676 | |
| 10677 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10678 | |
| 10679 | if (beg < ptr && *beg == '/') { |
| 10680 | while (beg < ptr) |
| 10681 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10682 | } |
| 10683 | hash = full_hash(hash); |
| 10684 | |
| 10685 | smp->type = SMP_T_UINT; |
| 10686 | smp->data.uint = hash; |
| 10687 | smp->flags = SMP_F_VOL_1ST; |
| 10688 | return 1; |
| 10689 | } |
| 10690 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10691 | /* 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] | 10692 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10693 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10694 | * 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] | 10695 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10696 | * 8 bytes would still work. |
| 10697 | */ |
| 10698 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10699 | 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] | 10700 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10701 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10702 | struct chunk *temp; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 10703 | struct connection *cli_conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10704 | |
| 10705 | if (!cli_conn) |
| 10706 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10707 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10708 | if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10709 | return 0; |
| 10710 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10711 | temp = get_trash_chunk(); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10712 | *(unsigned int *)temp->str = htonl(smp->data.uint); |
| 10713 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10714 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10715 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10716 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10717 | 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] | 10718 | temp->len += 4; |
| 10719 | break; |
| 10720 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10721 | 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] | 10722 | temp->len += 16; |
| 10723 | break; |
| 10724 | default: |
| 10725 | return 0; |
| 10726 | } |
| 10727 | |
| 10728 | smp->data.str = *temp; |
| 10729 | smp->type = SMP_T_BIN; |
| 10730 | return 1; |
| 10731 | } |
| 10732 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10733 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10734 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10735 | * of type string carrying the whole query string. |
| 10736 | */ |
| 10737 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10738 | 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] | 10739 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10740 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10741 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10742 | char *ptr, *end; |
| 10743 | |
| 10744 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10745 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10746 | txn = strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10747 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10748 | end = ptr + txn->req.sl.rq.u_l; |
| 10749 | |
| 10750 | /* look up the '?' */ |
| 10751 | do { |
| 10752 | if (ptr == end) |
| 10753 | return 0; |
| 10754 | } while (*ptr++ != '?'); |
| 10755 | |
| 10756 | smp->type = SMP_T_STR; |
| 10757 | smp->data.str.str = ptr; |
| 10758 | smp->data.str.len = end - ptr; |
| 10759 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10760 | return 1; |
| 10761 | } |
| 10762 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10763 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10764 | 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] | 10765 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10766 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10767 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10768 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10769 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10770 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10771 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10772 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10773 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 10774 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10775 | return 1; |
| 10776 | } |
| 10777 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10778 | /* return a valid test if the current request is the first one on the connection */ |
| 10779 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10780 | 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] | 10781 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10782 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10783 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10784 | smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10785 | return 1; |
| 10786 | } |
| 10787 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10788 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10789 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10790 | 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] | 10791 | 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] | 10792 | { |
| 10793 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10794 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10795 | return 0; |
| 10796 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10797 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10798 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10799 | if (!get_http_auth(strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10800 | return 0; |
| 10801 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10802 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10803 | 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] | 10804 | return 1; |
| 10805 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10806 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10807 | /* Accepts exactly 1 argument of type userlist */ |
| 10808 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10809 | 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] | 10810 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10811 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10812 | if (!args || args->type != ARGT_USR) |
| 10813 | return 0; |
| 10814 | |
| 10815 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10816 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10817 | if (!get_http_auth(strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10818 | return 0; |
| 10819 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10820 | /* if the user does not belong to the userlist or has a wrong password, |
| 10821 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10822 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10823 | */ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10824 | 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] | 10825 | return 0; |
| 10826 | |
| 10827 | /* pat_match_auth() will need the user list */ |
| 10828 | smp->ctx.a[0] = args->data.usr; |
| 10829 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10830 | smp->type = SMP_T_STR; |
| 10831 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10832 | smp->data.str.str = strm->txn->auth.user; |
| 10833 | smp->data.str.len = strlen(strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10834 | |
| 10835 | return 1; |
| 10836 | } |
| 10837 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10838 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10839 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10840 | * the cookie value is returned into *value and *value_l, and the function |
| 10841 | * returns a pointer to the next pointer to search from if the value was found. |
| 10842 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10843 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10844 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10845 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10846 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10847 | * list is expected. |
| 10848 | */ |
| 10849 | static char * |
| 10850 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10851 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10852 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10853 | { |
| 10854 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10855 | char *next; |
| 10856 | |
| 10857 | /* we search at least a cookie name followed by an equal, and more |
| 10858 | * generally something like this : |
| 10859 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10860 | */ |
| 10861 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10862 | /* Iterate through all cookies on this line */ |
| 10863 | |
| 10864 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 10865 | att_beg++; |
| 10866 | |
| 10867 | /* find att_end : this is the first character after the last non |
| 10868 | * space before the equal. It may be equal to hdr_end. |
| 10869 | */ |
| 10870 | equal = att_end = att_beg; |
| 10871 | |
| 10872 | while (equal < hdr_end) { |
| 10873 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10874 | break; |
| 10875 | if (http_is_spht[(unsigned char)*equal++]) |
| 10876 | continue; |
| 10877 | att_end = equal; |
| 10878 | } |
| 10879 | |
| 10880 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10881 | * is between <att_beg> and <equal>, both may be identical. |
| 10882 | */ |
| 10883 | |
| 10884 | /* look for end of cookie if there is an equal sign */ |
| 10885 | if (equal < hdr_end && *equal == '=') { |
| 10886 | /* look for the beginning of the value */ |
| 10887 | val_beg = equal + 1; |
| 10888 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 10889 | val_beg++; |
| 10890 | |
| 10891 | /* find the end of the value, respecting quotes */ |
| 10892 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10893 | |
| 10894 | /* make val_end point to the first white space or delimitor after the value */ |
| 10895 | val_end = next; |
| 10896 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 10897 | val_end--; |
| 10898 | } else { |
| 10899 | val_beg = val_end = next = equal; |
| 10900 | } |
| 10901 | |
| 10902 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10903 | * they will automatically be removed if a header before them is removed, |
| 10904 | * since they're supposed to be linked together. |
| 10905 | */ |
| 10906 | if (*att_beg == '$') |
| 10907 | continue; |
| 10908 | |
| 10909 | /* Ignore cookies with no equal sign */ |
| 10910 | if (equal == next) |
| 10911 | continue; |
| 10912 | |
| 10913 | /* Now we have the cookie name between att_beg and att_end, and |
| 10914 | * its value between val_beg and val_end. |
| 10915 | */ |
| 10916 | |
| 10917 | if (att_end - att_beg == cookie_name_l && |
| 10918 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10919 | /* let's return this value and indicate where to go on from */ |
| 10920 | *value = val_beg; |
| 10921 | *value_l = val_end - val_beg; |
| 10922 | return next + 1; |
| 10923 | } |
| 10924 | |
| 10925 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10926 | if (!list) |
| 10927 | break; |
| 10928 | } |
| 10929 | |
| 10930 | return NULL; |
| 10931 | } |
| 10932 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10933 | /* Fetch a captured HTTP request header. The index is the position of |
| 10934 | * the "capture" option in the configuration file |
| 10935 | */ |
| 10936 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10937 | 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] | 10938 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10939 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 10940 | struct proxy *fe = strm_fe(strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10941 | int idx; |
| 10942 | |
| 10943 | if (!args || args->type != ARGT_UINT) |
| 10944 | return 0; |
| 10945 | |
| 10946 | idx = args->data.uint; |
| 10947 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10948 | 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] | 10949 | return 0; |
| 10950 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10951 | smp->type = SMP_T_STR; |
| 10952 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10953 | smp->data.str.str = strm->req_cap[idx]; |
| 10954 | smp->data.str.len = strlen(strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10955 | |
| 10956 | return 1; |
| 10957 | } |
| 10958 | |
| 10959 | /* Fetch a captured HTTP response header. The index is the position of |
| 10960 | * the "capture" option in the configuration file |
| 10961 | */ |
| 10962 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10963 | 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] | 10964 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10965 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 10966 | struct proxy *fe = strm_fe(strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10967 | int idx; |
| 10968 | |
| 10969 | if (!args || args->type != ARGT_UINT) |
| 10970 | return 0; |
| 10971 | |
| 10972 | idx = args->data.uint; |
| 10973 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10974 | 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] | 10975 | return 0; |
| 10976 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10977 | smp->type = SMP_T_STR; |
| 10978 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10979 | smp->data.str.str = strm->res_cap[idx]; |
| 10980 | smp->data.str.len = strlen(strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10981 | |
| 10982 | return 1; |
| 10983 | } |
| 10984 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10985 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10986 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10987 | 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] | 10988 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10989 | { |
| 10990 | struct chunk *temp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10991 | struct http_txn *txn = strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10992 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10993 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10994 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10995 | return 0; |
| 10996 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10997 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10998 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10999 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11000 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11001 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11002 | temp = get_trash_chunk(); |
| 11003 | temp->str = txn->uri; |
| 11004 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11005 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11006 | smp->type = SMP_T_STR; |
| 11007 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11008 | |
| 11009 | return 1; |
| 11010 | |
| 11011 | } |
| 11012 | |
| 11013 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11014 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11015 | 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] | 11016 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11017 | { |
| 11018 | struct chunk *temp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11019 | struct http_txn *txn = strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11020 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11021 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11022 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11023 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11024 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11025 | ptr = txn->uri; |
| 11026 | |
| 11027 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11028 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11029 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11030 | if (!*ptr) |
| 11031 | return 0; |
| 11032 | |
| 11033 | ptr++; /* skip the space */ |
| 11034 | |
| 11035 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11036 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11037 | if (!ptr) |
| 11038 | return 0; |
| 11039 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11040 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11041 | |
| 11042 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11043 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11044 | smp->type = SMP_T_STR; |
| 11045 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11046 | |
| 11047 | return 1; |
| 11048 | } |
| 11049 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11050 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11051 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11052 | */ |
| 11053 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11054 | 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] | 11055 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11056 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11057 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11058 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11059 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11060 | return 0; |
| 11061 | |
| 11062 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 11063 | smp->data.str.str = "HTTP/1.1"; |
| 11064 | else |
| 11065 | smp->data.str.str = "HTTP/1.0"; |
| 11066 | |
| 11067 | smp->data.str.len = 8; |
| 11068 | smp->type = SMP_T_STR; |
| 11069 | smp->flags = SMP_F_CONST; |
| 11070 | return 1; |
| 11071 | |
| 11072 | } |
| 11073 | |
| 11074 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11075 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11076 | */ |
| 11077 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11078 | 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] | 11079 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11080 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11081 | struct http_txn *txn = strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11082 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11083 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11084 | return 0; |
| 11085 | |
| 11086 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 11087 | smp->data.str.str = "HTTP/1.1"; |
| 11088 | else |
| 11089 | smp->data.str.str = "HTTP/1.0"; |
| 11090 | |
| 11091 | smp->data.str.len = 8; |
| 11092 | smp->type = SMP_T_STR; |
| 11093 | smp->flags = SMP_F_CONST; |
| 11094 | return 1; |
| 11095 | |
| 11096 | } |
| 11097 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11098 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11099 | /* 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] | 11100 | * 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] | 11101 | * 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] | 11102 | * 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] | 11103 | * 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] | 11104 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11105 | * 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] | 11106 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11107 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11108 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11109 | 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] | 11110 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11111 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11112 | struct http_txn *txn; |
| 11113 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11114 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11115 | const struct http_msg *msg; |
| 11116 | const char *hdr_name; |
| 11117 | int hdr_name_len; |
| 11118 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11119 | int occ = 0; |
| 11120 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11121 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11122 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11123 | return 0; |
| 11124 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11125 | if (!ctx) { |
| 11126 | /* first call */ |
| 11127 | ctx = &static_hdr_ctx; |
| 11128 | ctx->idx = 0; |
| 11129 | smp->ctx.a[2] = ctx; |
| 11130 | } |
| 11131 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11132 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11133 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11134 | txn = strm->txn; |
| 11135 | idx = &strm->txn->hdr_idx; |
| 11136 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11137 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11138 | msg = &txn->req; |
| 11139 | hdr_name = "Cookie"; |
| 11140 | hdr_name_len = 6; |
| 11141 | } else { |
| 11142 | msg = &txn->rsp; |
| 11143 | hdr_name = "Set-Cookie"; |
| 11144 | hdr_name_len = 10; |
| 11145 | } |
| 11146 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11147 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 11148 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11149 | occ = -1; |
| 11150 | |
| 11151 | /* OK so basically here, either we want only one value and it's the |
| 11152 | * last one, or we want to iterate over all of them and we fetch the |
| 11153 | * next one. |
| 11154 | */ |
| 11155 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11156 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11157 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11158 | /* search for the header from the beginning, we must first initialize |
| 11159 | * the search parameters. |
| 11160 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11161 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11162 | ctx->idx = 0; |
| 11163 | } |
| 11164 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11165 | smp->flags |= SMP_F_VOL_HDR; |
| 11166 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11167 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11168 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11169 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11170 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11171 | goto out; |
| 11172 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11173 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11174 | continue; |
| 11175 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11176 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11177 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11178 | } |
| 11179 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11180 | smp->type = SMP_T_STR; |
| 11181 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11182 | 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] | 11183 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11184 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11185 | &smp->data.str.str, |
| 11186 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11187 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11188 | found = 1; |
| 11189 | if (occ >= 0) { |
| 11190 | /* one value was returned into smp->data.str.{str,len} */ |
| 11191 | smp->flags |= SMP_F_NOT_LAST; |
| 11192 | return 1; |
| 11193 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11194 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11195 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11196 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11197 | /* all cookie headers and values were scanned. If we're looking for the |
| 11198 | * last occurrence, we may return it now. |
| 11199 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11200 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11201 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11202 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11203 | } |
| 11204 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11205 | /* 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] | 11206 | * 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] | 11207 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11208 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11209 | */ |
| 11210 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11211 | 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] | 11212 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11213 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11214 | struct http_txn *txn; |
| 11215 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11216 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11217 | const struct http_msg *msg; |
| 11218 | const char *hdr_name; |
| 11219 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11220 | int cnt; |
| 11221 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11222 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11223 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11224 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11225 | return 0; |
| 11226 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11227 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11228 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11229 | txn = strm->txn; |
| 11230 | idx = &strm->txn->hdr_idx; |
| 11231 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11232 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11233 | msg = &txn->req; |
| 11234 | hdr_name = "Cookie"; |
| 11235 | hdr_name_len = 6; |
| 11236 | } else { |
| 11237 | msg = &txn->rsp; |
| 11238 | hdr_name = "Set-Cookie"; |
| 11239 | hdr_name_len = 10; |
| 11240 | } |
| 11241 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11242 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11243 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11244 | ctx.idx = 0; |
| 11245 | cnt = 0; |
| 11246 | |
| 11247 | while (1) { |
| 11248 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11249 | if (!val_beg) { |
| 11250 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11251 | break; |
| 11252 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11253 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11254 | continue; |
| 11255 | |
| 11256 | val_beg = ctx.line + ctx.val; |
| 11257 | val_end = val_beg + ctx.vlen; |
| 11258 | } |
| 11259 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11260 | smp->type = SMP_T_STR; |
| 11261 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11262 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11263 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 11264 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11265 | &smp->data.str.str, |
| 11266 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11267 | cnt++; |
| 11268 | } |
| 11269 | } |
| 11270 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 11271 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 11272 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11273 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11274 | return 1; |
| 11275 | } |
| 11276 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11277 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11278 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11279 | */ |
| 11280 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11281 | 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] | 11282 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11283 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11284 | 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] | 11285 | |
| 11286 | if (ret > 0) { |
| 11287 | smp->type = SMP_T_UINT; |
| 11288 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11289 | } |
| 11290 | |
| 11291 | return ret; |
| 11292 | } |
| 11293 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11294 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11295 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11296 | /************************************************************************/ |
| 11297 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11298 | /* |
| 11299 | * Given a path string and its length, find the position of beginning of the |
| 11300 | * query string. Returns NULL if no query string is found in the path. |
| 11301 | * |
| 11302 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11303 | * |
| 11304 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 11305 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11306 | 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] | 11307 | { |
| 11308 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11309 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11310 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11311 | return p ? p + 1 : NULL; |
| 11312 | } |
| 11313 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11314 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11315 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11316 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11317 | } |
| 11318 | |
| 11319 | /* |
| 11320 | * Given a url parameter, find the starting position of the first occurence, |
| 11321 | * or NULL if the parameter is not found. |
| 11322 | * |
| 11323 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11324 | * the function will return query_string+8. |
| 11325 | */ |
| 11326 | static char* |
| 11327 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11328 | char* url_param_name, size_t url_param_name_l, |
| 11329 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11330 | { |
| 11331 | char *pos, *last; |
| 11332 | |
| 11333 | pos = query_string; |
| 11334 | last = query_string + query_string_l - url_param_name_l - 1; |
| 11335 | |
| 11336 | while (pos <= last) { |
| 11337 | if (pos[url_param_name_l] == '=') { |
| 11338 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11339 | return pos; |
| 11340 | pos += url_param_name_l + 1; |
| 11341 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11342 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11343 | pos++; |
| 11344 | pos++; |
| 11345 | } |
| 11346 | return NULL; |
| 11347 | } |
| 11348 | |
| 11349 | /* |
| 11350 | * Given a url parameter name, returns its value and size into *value and |
| 11351 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 11352 | * zero is returned and value/value_l are not touched. |
| 11353 | */ |
| 11354 | static int |
| 11355 | find_url_param_value(char* path, size_t path_l, |
| 11356 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11357 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11358 | { |
| 11359 | char *query_string, *qs_end; |
| 11360 | char *arg_start; |
| 11361 | char *value_start, *value_end; |
| 11362 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11363 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11364 | if (!query_string) |
| 11365 | return 0; |
| 11366 | |
| 11367 | qs_end = path + path_l; |
| 11368 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11369 | url_param_name, url_param_name_l, |
| 11370 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11371 | if (!arg_start) |
| 11372 | return 0; |
| 11373 | |
| 11374 | value_start = arg_start + url_param_name_l + 1; |
| 11375 | value_end = value_start; |
| 11376 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11377 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11378 | value_end++; |
| 11379 | |
| 11380 | *value = value_start; |
| 11381 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11382 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11383 | } |
| 11384 | |
| 11385 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11386 | 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] | 11387 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11388 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11389 | char delim = '?'; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11390 | struct http_msg *msg; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11391 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11392 | if (!args || args[0].type != ARGT_STR || |
| 11393 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11394 | return 0; |
| 11395 | |
| 11396 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11397 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11398 | msg = &strm->txn->req; |
| 11399 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11400 | if (args[1].type) |
| 11401 | delim = *args[1].data.str.str; |
| 11402 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11403 | 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] | 11404 | args->data.str.str, args->data.str.len, |
| 11405 | &smp->data.str.str, &smp->data.str.len, |
| 11406 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11407 | return 0; |
| 11408 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11409 | smp->type = SMP_T_STR; |
| 11410 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11411 | return 1; |
| 11412 | } |
| 11413 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11414 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11415 | * above). |
| 11416 | */ |
| 11417 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11418 | 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] | 11419 | const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11420 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11421 | 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] | 11422 | |
| 11423 | if (ret > 0) { |
| 11424 | smp->type = SMP_T_UINT; |
| 11425 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 11426 | } |
| 11427 | |
| 11428 | return ret; |
| 11429 | } |
| 11430 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11431 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11432 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11433 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11434 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11435 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11436 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11437 | * high-traffic sites without having to store whole paths. |
| 11438 | * this differs from the base32 functions in that it includes the url parameters |
| 11439 | * as well as the path |
| 11440 | */ |
| 11441 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11442 | 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] | 11443 | 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] | 11444 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11445 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11446 | struct hdr_ctx ctx; |
| 11447 | unsigned int hash = 0; |
| 11448 | char *ptr, *beg, *end; |
| 11449 | int len; |
| 11450 | |
| 11451 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11452 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11453 | txn = strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11454 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11455 | 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] | 11456 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11457 | ptr = ctx.line + ctx.val; |
| 11458 | len = ctx.vlen; |
| 11459 | while (len--) |
| 11460 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11461 | } |
| 11462 | |
| 11463 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11464 | 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] | 11465 | beg = http_get_path(txn); |
| 11466 | if (!beg) |
| 11467 | beg = end; |
| 11468 | |
| 11469 | for (ptr = beg; ptr < end ; ptr++); |
| 11470 | |
| 11471 | if (beg < ptr && *beg == '/') { |
| 11472 | while (beg < ptr) |
| 11473 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11474 | } |
| 11475 | hash = full_hash(hash); |
| 11476 | |
| 11477 | smp->type = SMP_T_UINT; |
| 11478 | smp->data.uint = hash; |
| 11479 | smp->flags = SMP_F_VOL_1ST; |
| 11480 | return 1; |
| 11481 | } |
| 11482 | |
| 11483 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11484 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11485 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11486 | * on the source address length. The URL hash is stored before the address so |
| 11487 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11488 | * 8 bytes would still work. |
| 11489 | */ |
| 11490 | static int |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11491 | 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] | 11492 | 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] | 11493 | { |
| 11494 | struct chunk *temp; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 11495 | struct connection *cli_conn = objt_conn(sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11496 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 11497 | 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] | 11498 | return 0; |
| 11499 | |
| 11500 | temp = get_trash_chunk(); |
| 11501 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 11502 | temp->len += sizeof(smp->data.uint); |
| 11503 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11504 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11505 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11506 | 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] | 11507 | temp->len += 4; |
| 11508 | break; |
| 11509 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11510 | 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] | 11511 | temp->len += 16; |
| 11512 | break; |
| 11513 | default: |
| 11514 | return 0; |
| 11515 | } |
| 11516 | |
| 11517 | smp->data.str = *temp; |
| 11518 | smp->type = SMP_T_BIN; |
| 11519 | return 1; |
| 11520 | } |
| 11521 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11522 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11523 | * keyword. These keywords support an optional positive or negative occurrence |
| 11524 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11525 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11526 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11527 | * error message in case of error, that the caller is responsible for freeing. |
| 11528 | * The initial location must either be freeable or NULL. |
| 11529 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11530 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11531 | { |
| 11532 | 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] | 11533 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11534 | return 0; |
| 11535 | } |
| 11536 | return 1; |
| 11537 | } |
| 11538 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11539 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11540 | * adds an optional offset found in args[0] and emits a string representing |
| 11541 | * the date in RFC-1123/5322 format. |
| 11542 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 11543 | 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] | 11544 | struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11545 | { |
| 11546 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11547 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11548 | struct chunk *temp; |
| 11549 | struct tm *tm; |
| 11550 | time_t curr_date = smp->data.uint; |
| 11551 | |
| 11552 | /* add offset */ |
| 11553 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 11554 | curr_date += args[0].data.sint; |
| 11555 | |
| 11556 | tm = gmtime(&curr_date); |
| 11557 | |
| 11558 | temp = get_trash_chunk(); |
| 11559 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11560 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11561 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11562 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11563 | |
| 11564 | smp->data.str = *temp; |
| 11565 | smp->type = SMP_T_STR; |
| 11566 | return 1; |
| 11567 | } |
| 11568 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11569 | /* Match language range with language tag. RFC2616 14.4: |
| 11570 | * |
| 11571 | * A language-range matches a language-tag if it exactly equals |
| 11572 | * the tag, or if it exactly equals a prefix of the tag such |
| 11573 | * that the first tag character following the prefix is "-". |
| 11574 | * |
| 11575 | * Return 1 if the strings match, else return 0. |
| 11576 | */ |
| 11577 | static inline int language_range_match(const char *range, int range_len, |
| 11578 | const char *tag, int tag_len) |
| 11579 | { |
| 11580 | const char *end = range + range_len; |
| 11581 | const char *tend = tag + tag_len; |
| 11582 | while (range < end) { |
| 11583 | if (*range == '-' && tag == tend) |
| 11584 | return 1; |
| 11585 | if (*range != *tag || tag == tend) |
| 11586 | return 0; |
| 11587 | range++; |
| 11588 | tag++; |
| 11589 | } |
| 11590 | /* Return true only if the last char of the tag is matched. */ |
| 11591 | return tag == tend; |
| 11592 | } |
| 11593 | |
| 11594 | /* 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] | 11595 | 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] | 11596 | struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11597 | { |
| 11598 | const char *al = smp->data.str.str; |
| 11599 | const char *end = al + smp->data.str.len; |
| 11600 | const char *token; |
| 11601 | int toklen; |
| 11602 | int qvalue; |
| 11603 | const char *str; |
| 11604 | const char *w; |
| 11605 | int best_q = 0; |
| 11606 | |
| 11607 | /* Set the constant to the sample, because the output of the |
| 11608 | * function will be peek in the constant configuration string. |
| 11609 | */ |
| 11610 | smp->flags |= SMP_F_CONST; |
| 11611 | smp->data.str.size = 0; |
| 11612 | smp->data.str.str = ""; |
| 11613 | smp->data.str.len = 0; |
| 11614 | |
| 11615 | /* Parse the accept language */ |
| 11616 | while (1) { |
| 11617 | |
| 11618 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11619 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11620 | al++; |
| 11621 | if (al >= end) |
| 11622 | break; |
| 11623 | |
| 11624 | /* Start of the fisrt word. */ |
| 11625 | token = al; |
| 11626 | |
| 11627 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11628 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11629 | al++; |
| 11630 | if (al == token) |
| 11631 | goto expect_comma; |
| 11632 | |
| 11633 | /* Length of the token. */ |
| 11634 | toklen = al - token; |
| 11635 | qvalue = 1000; |
| 11636 | |
| 11637 | /* Check if the token exists in the list. If the token not exists, |
| 11638 | * jump to the next token. |
| 11639 | */ |
| 11640 | str = args[0].data.str.str; |
| 11641 | w = str; |
| 11642 | while (1) { |
| 11643 | if (*str == ';' || *str == '\0') { |
| 11644 | if (language_range_match(token, toklen, w, str-w)) |
| 11645 | goto look_for_q; |
| 11646 | if (*str == '\0') |
| 11647 | goto expect_comma; |
| 11648 | w = str + 1; |
| 11649 | } |
| 11650 | str++; |
| 11651 | } |
| 11652 | goto expect_comma; |
| 11653 | |
| 11654 | look_for_q: |
| 11655 | |
| 11656 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11657 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11658 | al++; |
| 11659 | if (al >= end) |
| 11660 | goto process_value; |
| 11661 | |
| 11662 | /* If ',' is found, process the result */ |
| 11663 | if (*al == ',') |
| 11664 | goto process_value; |
| 11665 | |
| 11666 | /* If the character is different from ';', look |
| 11667 | * for the end of the header part in best effort. |
| 11668 | */ |
| 11669 | if (*al != ';') |
| 11670 | goto expect_comma; |
| 11671 | |
| 11672 | /* Assumes that the char is ';', now expect "q=". */ |
| 11673 | al++; |
| 11674 | |
| 11675 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11676 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11677 | al++; |
| 11678 | if (al >= end) |
| 11679 | goto process_value; |
| 11680 | |
| 11681 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11682 | if (*al != 'q') |
| 11683 | goto process_value; |
| 11684 | al++; |
| 11685 | |
| 11686 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11687 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11688 | al++; |
| 11689 | if (al >= end) |
| 11690 | goto process_value; |
| 11691 | |
| 11692 | /* Expect '='. If no '=', continue in best effort */ |
| 11693 | if (*al != '=') |
| 11694 | goto process_value; |
| 11695 | al++; |
| 11696 | |
| 11697 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11698 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11699 | al++; |
| 11700 | if (al >= end) |
| 11701 | goto process_value; |
| 11702 | |
| 11703 | /* Parse the q value. */ |
| 11704 | qvalue = parse_qvalue(al, &al); |
| 11705 | |
| 11706 | process_value: |
| 11707 | |
| 11708 | /* If the new q value is the best q value, then store the associated |
| 11709 | * language in the response. If qvalue is the biggest value (1000), |
| 11710 | * break the process. |
| 11711 | */ |
| 11712 | if (qvalue > best_q) { |
| 11713 | smp->data.str.str = (char *)w; |
| 11714 | smp->data.str.len = str - w; |
| 11715 | if (qvalue >= 1000) |
| 11716 | break; |
| 11717 | best_q = qvalue; |
| 11718 | } |
| 11719 | |
| 11720 | expect_comma: |
| 11721 | |
| 11722 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11723 | while (al < end && *al != ',') |
| 11724 | al++; |
| 11725 | if (al >= end) |
| 11726 | break; |
| 11727 | |
| 11728 | /* Comma is found, jump it and restart the analyzer. */ |
| 11729 | al++; |
| 11730 | } |
| 11731 | |
| 11732 | /* Set default value if required. */ |
| 11733 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 11734 | smp->data.str.str = args[1].data.str.str; |
| 11735 | smp->data.str.len = args[1].data.str.len; |
| 11736 | } |
| 11737 | |
| 11738 | /* Return true only if a matching language was found. */ |
| 11739 | return smp->data.str.len != 0; |
| 11740 | } |
| 11741 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11742 | /* 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] | 11743 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11744 | * the relevant part of the request line accordingly. Then it updates various |
| 11745 | * pointers to the next elements which were moved, and the total buffer length. |
| 11746 | * 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] | 11747 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11748 | * error, though this can be revisited when this code is finally exploited. |
| 11749 | * |
| 11750 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11751 | * query string and 3 to replace uri. |
| 11752 | * |
| 11753 | * In query string case, the mark question '?' must be set at the start of the |
| 11754 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11755 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11756 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11757 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11758 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11759 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11760 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11761 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11762 | int delta; |
| 11763 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11764 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11765 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11766 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11767 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11768 | |
| 11769 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11770 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11771 | txn->req.sl.rq.m_l += delta; |
| 11772 | txn->req.sl.rq.u += delta; |
| 11773 | txn->req.sl.rq.v += delta; |
| 11774 | break; |
| 11775 | |
| 11776 | case 1: // path |
| 11777 | cur_ptr = http_get_path(txn); |
| 11778 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11779 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11780 | |
| 11781 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11782 | 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] | 11783 | cur_end++; |
| 11784 | |
| 11785 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11786 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11787 | txn->req.sl.rq.u_l += delta; |
| 11788 | txn->req.sl.rq.v += delta; |
| 11789 | break; |
| 11790 | |
| 11791 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11792 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11793 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11794 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11795 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11796 | cur_ptr++; |
| 11797 | |
| 11798 | /* skip the question mark or indicate that we must insert it |
| 11799 | * (but only if the format string is not empty then). |
| 11800 | */ |
| 11801 | if (cur_ptr < cur_end) |
| 11802 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11803 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11804 | offset = 0; |
| 11805 | |
| 11806 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11807 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11808 | txn->req.sl.rq.u_l += delta; |
| 11809 | txn->req.sl.rq.v += delta; |
| 11810 | break; |
| 11811 | |
| 11812 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11813 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11814 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11815 | |
| 11816 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11817 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11818 | txn->req.sl.rq.u_l += delta; |
| 11819 | txn->req.sl.rq.v += delta; |
| 11820 | break; |
| 11821 | |
| 11822 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11823 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11824 | } |
| 11825 | |
| 11826 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11827 | 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] | 11828 | txn->req.sl.rq.l += delta; |
| 11829 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11830 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11831 | return 0; |
| 11832 | } |
| 11833 | |
| 11834 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11835 | * builds a string in the trash from the specified format string. It finds |
| 11836 | * the action to be performed in p[2], previously filled by function |
| 11837 | * parse_set_req_line(). The replacement action is excuted by the function |
| 11838 | * http_action_set_req_line_exec(). It always returns 1. If an error occurs |
| 11839 | * the action is canceled, but the rule processing continue. |
| 11840 | */ |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11841 | 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] | 11842 | { |
| 11843 | chunk_reset(&trash); |
| 11844 | |
| 11845 | /* If we have to create a query string, prepare a '?'. */ |
| 11846 | if (*(int *)&rule->arg.act.p[2] == 2) |
| 11847 | trash.str[trash.len++] = '?'; |
| 11848 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]); |
| 11849 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11850 | 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] | 11851 | return 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11852 | } |
| 11853 | |
| 11854 | /* parse an http-request action among : |
| 11855 | * set-method |
| 11856 | * set-path |
| 11857 | * set-query |
| 11858 | * set-uri |
| 11859 | * |
| 11860 | * All of them accept a single argument of type string representing a log-format. |
| 11861 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11862 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 11863 | * It returns 0 on success, < 0 on error. |
| 11864 | */ |
| 11865 | int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err) |
| 11866 | { |
| 11867 | int cur_arg = *orig_arg; |
| 11868 | |
| 11869 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 11870 | |
| 11871 | switch (args[0][4]) { |
| 11872 | case 'm' : |
| 11873 | *(int *)&rule->arg.act.p[2] = 0; |
| 11874 | rule->action_ptr = http_action_set_req_line; |
| 11875 | break; |
| 11876 | case 'p' : |
| 11877 | *(int *)&rule->arg.act.p[2] = 1; |
| 11878 | rule->action_ptr = http_action_set_req_line; |
| 11879 | break; |
| 11880 | case 'q' : |
| 11881 | *(int *)&rule->arg.act.p[2] = 2; |
| 11882 | rule->action_ptr = http_action_set_req_line; |
| 11883 | break; |
| 11884 | case 'u' : |
| 11885 | *(int *)&rule->arg.act.p[2] = 3; |
| 11886 | rule->action_ptr = http_action_set_req_line; |
| 11887 | break; |
| 11888 | default: |
| 11889 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 11890 | return -1; |
| 11891 | } |
| 11892 | |
| 11893 | if (!*args[cur_arg] || |
| 11894 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11895 | memprintf(err, "expects exactly 1 argument <format>"); |
| 11896 | return -1; |
| 11897 | } |
| 11898 | |
| 11899 | LIST_INIT((struct list *)&rule->arg.act.p[0]); |
| 11900 | proxy->conf.args.ctx = ARGC_HRQ; |
| 11901 | parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP, |
| 11902 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 11903 | proxy->conf.args.file, proxy->conf.args.line); |
| 11904 | |
| 11905 | (*orig_arg)++; |
| 11906 | return 0; |
| 11907 | } |
| 11908 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 11909 | /* |
| 11910 | * Return the struct http_req_action_kw associated to a keyword. |
| 11911 | */ |
| 11912 | struct http_req_action_kw *action_http_req_custom(const char *kw) |
| 11913 | { |
| 11914 | if (!LIST_ISEMPTY(&http_req_keywords.list)) { |
| 11915 | struct http_req_action_kw_list *kw_list; |
| 11916 | int i; |
| 11917 | |
| 11918 | list_for_each_entry(kw_list, &http_req_keywords.list, list) { |
| 11919 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11920 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11921 | return &kw_list->kw[i]; |
| 11922 | } |
| 11923 | } |
| 11924 | } |
| 11925 | return NULL; |
| 11926 | } |
| 11927 | |
| 11928 | /* |
| 11929 | * Return the struct http_res_action_kw associated to a keyword. |
| 11930 | */ |
| 11931 | struct http_res_action_kw *action_http_res_custom(const char *kw) |
| 11932 | { |
| 11933 | if (!LIST_ISEMPTY(&http_res_keywords.list)) { |
| 11934 | struct http_res_action_kw_list *kw_list; |
| 11935 | int i; |
| 11936 | |
| 11937 | list_for_each_entry(kw_list, &http_res_keywords.list, list) { |
| 11938 | for (i = 0; kw_list->kw[i].kw != NULL; i++) { |
| 11939 | if (!strcmp(kw, kw_list->kw[i].kw)) |
| 11940 | return &kw_list->kw[i]; |
| 11941 | } |
| 11942 | } |
| 11943 | } |
| 11944 | return NULL; |
| 11945 | } |
| 11946 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11947 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11948 | /* All supported ACL keywords must be declared here. */ |
| 11949 | /************************************************************************/ |
| 11950 | |
| 11951 | /* Note: must not be declared <const> as its list will be overwritten. |
| 11952 | * Please take care of keeping this list alphabetically sorted. |
| 11953 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 11954 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11955 | { "base", "base", PAT_MATCH_STR }, |
| 11956 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 11957 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 11958 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 11959 | { "base_end", "base", PAT_MATCH_END }, |
| 11960 | { "base_len", "base", PAT_MATCH_LEN }, |
| 11961 | { "base_reg", "base", PAT_MATCH_REG }, |
| 11962 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 11963 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11964 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 11965 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 11966 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 11967 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 11968 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 11969 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 11970 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 11971 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 11972 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11973 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 11974 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 11975 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 11976 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 11977 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 11978 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 11979 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 11980 | { "hdr_sub", "req.hdr", 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 | /* these two declarations uses strings with list storage (in place |
| 11983 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 11984 | * and delete functions are relative to the list management. The parse |
| 11985 | * and match method are related to the corresponding fetch methods. This |
| 11986 | * is very particular ACL declaration mode. |
| 11987 | */ |
| 11988 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 11989 | { "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] | 11990 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 11991 | { "path", "path", PAT_MATCH_STR }, |
| 11992 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 11993 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 11994 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 11995 | { "path_end", "path", PAT_MATCH_END }, |
| 11996 | { "path_len", "path", PAT_MATCH_LEN }, |
| 11997 | { "path_reg", "path", PAT_MATCH_REG }, |
| 11998 | { "path_sub", "path", PAT_MATCH_SUB }, |
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 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12001 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12002 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12003 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12004 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12005 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12006 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12007 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12008 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12009 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12010 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
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 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12013 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12014 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12015 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12016 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12017 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12018 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12019 | { "shdr_sub", "res.hdr", 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 | { "url", "url", PAT_MATCH_STR }, |
| 12022 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12023 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12024 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12025 | { "url_end", "url", PAT_MATCH_END }, |
| 12026 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12027 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12028 | { "url_sub", "url", 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 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12031 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12032 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12033 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12034 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12035 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12036 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12037 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12038 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12039 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12040 | }}; |
| 12041 | |
| 12042 | /************************************************************************/ |
| 12043 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12044 | /************************************************************************/ |
| 12045 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12046 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12047 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12048 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12049 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12050 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12051 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12052 | { "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] | 12053 | |
| 12054 | /* retrieve these captures from the HTTP logs */ |
| 12055 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12056 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12057 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12058 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12059 | { "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] | 12060 | { "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] | 12061 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12062 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12063 | * are only here to match the ACL's name, are request-only and are used |
| 12064 | * for ACL compatibility only. |
| 12065 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12066 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12067 | { "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] | 12068 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12069 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12070 | |
| 12071 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12072 | * only here to match the ACL's name, are request-only and are used for |
| 12073 | * ACL compatibility only. |
| 12074 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12075 | { "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] | 12076 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12077 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 12078 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12079 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12080 | { "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] | 12081 | { "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] | 12082 | { "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] | 12083 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12084 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12085 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12086 | |
| 12087 | /* HTTP protocol on the request path */ |
| 12088 | { "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] | 12089 | { "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] | 12090 | |
| 12091 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12092 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12093 | { "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] | 12094 | |
| 12095 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12096 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12097 | { "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] | 12098 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12099 | /* 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] | 12100 | { "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] | 12101 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12102 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12103 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12104 | { "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] | 12105 | { "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] | 12106 | { "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] | 12107 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12108 | { "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] | 12109 | { "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] | 12110 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12111 | |
| 12112 | /* 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] | 12113 | { "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] | 12114 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12115 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12116 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12117 | { "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] | 12118 | { "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] | 12119 | { "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] | 12120 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12121 | { "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] | 12122 | { "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] | 12123 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12124 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12125 | /* 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] | 12126 | { "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] | 12127 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12128 | { "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] | 12129 | { "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] | 12130 | |
| 12131 | /* 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] | 12132 | { "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] | 12133 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12134 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 12135 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 12136 | |
| 12137 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12138 | { "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] | 12139 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12140 | { "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] | 12141 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 12142 | { "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] | 12143 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12144 | { "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] | 12145 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 12146 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12147 | }}; |
| 12148 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12149 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12150 | /************************************************************************/ |
| 12151 | /* All supported converter keywords must be declared here. */ |
| 12152 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12153 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12154 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12155 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 12156 | { "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] | 12157 | { NULL, NULL, 0, 0, 0 }, |
| 12158 | }}; |
| 12159 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12160 | /************************************************************************/ |
| 12161 | /* All supported http-request action keywords must be declared here. */ |
| 12162 | /************************************************************************/ |
| 12163 | struct http_req_action_kw_list http_req_actions = { |
| 12164 | .scope = "http", |
| 12165 | .kw = { |
| 12166 | { "set-method", parse_set_req_line }, |
| 12167 | { "set-path", parse_set_req_line }, |
| 12168 | { "set-query", parse_set_req_line }, |
| 12169 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12170 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12171 | } |
| 12172 | }; |
| 12173 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12174 | __attribute__((constructor)) |
| 12175 | static void __http_protocol_init(void) |
| 12176 | { |
| 12177 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12178 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12179 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12180 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12181 | } |
| 12182 | |
| 12183 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12184 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12185 | * Local variables: |
| 12186 | * c-indent-level: 8 |
| 12187 | * c-basic-offset: 8 |
| 12188 | * End: |
| 12189 | */ |