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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/session.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> |
| 67 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 68 | const char HTTP_100[] = |
| 69 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 70 | |
| 71 | const struct chunk http_100_chunk = { |
| 72 | .str = (char *)&HTTP_100, |
| 73 | .len = sizeof(HTTP_100)-1 |
| 74 | }; |
| 75 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 76 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 77 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 78 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 80 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 81 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 87 | |
| 88 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 89 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 95 | |
| 96 | /* same as 302 except that the browser MUST retry with the same method */ |
| 97 | const char *HTTP_307 = |
| 98 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 99 | "Cache-Control: no-cache\r\n" |
| 100 | "Content-length: 0\r\n" |
| 101 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 102 | |
| 103 | /* same as 301 except that the browser MUST retry with the same method */ |
| 104 | const char *HTTP_308 = |
| 105 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 106 | "Content-length: 0\r\n" |
| 107 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 108 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 110 | const char *HTTP_401_fmt = |
| 111 | "HTTP/1.0 401 Unauthorized\r\n" |
| 112 | "Cache-Control: no-cache\r\n" |
| 113 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 114 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 116 | "\r\n" |
| 117 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 118 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 119 | const char *HTTP_407_fmt = |
| 120 | "HTTP/1.0 407 Unauthorized\r\n" |
| 121 | "Cache-Control: no-cache\r\n" |
| 122 | "Connection: close\r\n" |
| 123 | "Content-Type: text/html\r\n" |
| 124 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 125 | "\r\n" |
| 126 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 127 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | |
| 129 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 130 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | [HTTP_ERR_400] = 400, |
| 132 | [HTTP_ERR_403] = 403, |
| 133 | [HTTP_ERR_408] = 408, |
| 134 | [HTTP_ERR_500] = 500, |
| 135 | [HTTP_ERR_502] = 502, |
| 136 | [HTTP_ERR_503] = 503, |
| 137 | [HTTP_ERR_504] = 504, |
| 138 | }; |
| 139 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 140 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 141 | [HTTP_ERR_200] = |
| 142 | "HTTP/1.0 200 OK\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 148 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 149 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 150 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_403] = |
| 158 | "HTTP/1.0 403 Forbidden\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_408] = |
| 166 | "HTTP/1.0 408 Request Time-out\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_500] = |
| 174 | "HTTP/1.0 500 Server Error\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_502] = |
| 182 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 188 | |
| 189 | [HTTP_ERR_503] = |
| 190 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 191 | "Cache-Control: no-cache\r\n" |
| 192 | "Connection: close\r\n" |
| 193 | "Content-Type: text/html\r\n" |
| 194 | "\r\n" |
| 195 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 196 | |
| 197 | [HTTP_ERR_504] = |
| 198 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 199 | "Cache-Control: no-cache\r\n" |
| 200 | "Connection: close\r\n" |
| 201 | "Content-Type: text/html\r\n" |
| 202 | "\r\n" |
| 203 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 204 | |
| 205 | }; |
| 206 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 207 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 208 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 209 | [STAT_STATUS_DENY] = "DENY", |
| 210 | [STAT_STATUS_DONE] = "DONE", |
| 211 | [STAT_STATUS_ERRP] = "ERRP", |
| 212 | [STAT_STATUS_EXCD] = "EXCD", |
| 213 | [STAT_STATUS_NONE] = "NONE", |
| 214 | [STAT_STATUS_PART] = "PART", |
| 215 | [STAT_STATUS_UNKN] = "UNKN", |
| 216 | }; |
| 217 | |
| 218 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | /* We must put the messages here since GCC cannot initialize consts depending |
| 220 | * on strlen(). |
| 221 | */ |
| 222 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 223 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 224 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 225 | static struct hdr_ctx static_hdr_ctx; |
| 226 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | #define FD_SETS_ARE_BITFIELDS |
| 228 | #ifdef FD_SETS_ARE_BITFIELDS |
| 229 | /* |
| 230 | * This map is used with all the FD_* macros to check whether a particular bit |
| 231 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 232 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 233 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 234 | * exclusively to the macros. |
| 235 | */ |
| 236 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 237 | 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] | 238 | 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] | 239 | |
| 240 | #else |
| 241 | #error "Check if your OS uses bitfields for fd_sets" |
| 242 | #endif |
| 243 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 244 | void init_proto_http() |
| 245 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 246 | int i; |
| 247 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 248 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 250 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 251 | if (!http_err_msgs[msg]) { |
| 252 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 253 | abort(); |
| 254 | } |
| 255 | |
| 256 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 257 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 258 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 259 | |
| 260 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 261 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 262 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 263 | * printable characters above. |
| 264 | */ |
| 265 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 266 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 267 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 268 | for (i = 0; i < 32; i++) { |
| 269 | FD_SET(i, hdr_encode_map); |
| 270 | FD_SET(i, url_encode_map); |
| 271 | } |
| 272 | for (i = 127; i < 256; i++) { |
| 273 | FD_SET(i, hdr_encode_map); |
| 274 | FD_SET(i, url_encode_map); |
| 275 | } |
| 276 | |
| 277 | tmp = "\"#{|}"; |
| 278 | while (*tmp) { |
| 279 | FD_SET(*tmp, hdr_encode_map); |
| 280 | tmp++; |
| 281 | } |
| 282 | |
| 283 | tmp = "\"#"; |
| 284 | while (*tmp) { |
| 285 | FD_SET(*tmp, url_encode_map); |
| 286 | tmp++; |
| 287 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 288 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 289 | /* initialize the http header encoding map. The draft httpbis define the |
| 290 | * header content as: |
| 291 | * |
| 292 | * HTTP-message = start-line |
| 293 | * *( header-field CRLF ) |
| 294 | * CRLF |
| 295 | * [ message-body ] |
| 296 | * header-field = field-name ":" OWS field-value OWS |
| 297 | * field-value = *( field-content / obs-fold ) |
| 298 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 299 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 300 | * field-vchar = VCHAR / obs-text |
| 301 | * VCHAR = %x21-7E |
| 302 | * obs-text = %x80-FF |
| 303 | * |
| 304 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 305 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 306 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 307 | */ |
| 308 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 309 | for (i = 0x00; i <= 0x08; i++) |
| 310 | FD_SET(i, http_encode_map); |
| 311 | for (i = 0x0a; i <= 0x1f; i++) |
| 312 | FD_SET(i, http_encode_map); |
| 313 | FD_SET(0x7f, http_encode_map); |
| 314 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 315 | /* memory allocations */ |
| 316 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 317 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 318 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 320 | /* |
| 321 | * We have 26 list of methods (1 per first letter), each of which can have |
| 322 | * up to 3 entries (2 valid, 1 null). |
| 323 | */ |
| 324 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 325 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 326 | int len; |
| 327 | const char text[8]; |
| 328 | }; |
| 329 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 330 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 331 | ['C' - 'A'] = { |
| 332 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 333 | }, |
| 334 | ['D' - 'A'] = { |
| 335 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 336 | }, |
| 337 | ['G' - 'A'] = { |
| 338 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 339 | }, |
| 340 | ['H' - 'A'] = { |
| 341 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 342 | }, |
| 343 | ['P' - 'A'] = { |
| 344 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 345 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 346 | }, |
| 347 | ['T' - 'A'] = { |
| 348 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 349 | }, |
| 350 | /* rest is empty like this : |
| 351 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 352 | */ |
| 353 | }; |
| 354 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 355 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
| 356 | [HTTP_METH_NONE] = { "", 0 }, |
| 357 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 358 | [HTTP_METH_GET] = { "GET", 3 }, |
| 359 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 360 | [HTTP_METH_POST] = { "POST", 4 }, |
| 361 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 362 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 363 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 364 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 365 | }; |
| 366 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 367 | /* 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] | 368 | * 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] | 369 | * RFC2616 for those chars. |
| 370 | */ |
| 371 | |
| 372 | const char http_is_spht[256] = { |
| 373 | [' '] = 1, ['\t'] = 1, |
| 374 | }; |
| 375 | |
| 376 | const char http_is_crlf[256] = { |
| 377 | ['\r'] = 1, ['\n'] = 1, |
| 378 | }; |
| 379 | |
| 380 | const char http_is_lws[256] = { |
| 381 | [' '] = 1, ['\t'] = 1, |
| 382 | ['\r'] = 1, ['\n'] = 1, |
| 383 | }; |
| 384 | |
| 385 | const char http_is_sep[256] = { |
| 386 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 387 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 388 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 389 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 390 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 391 | }; |
| 392 | |
| 393 | const char http_is_ctl[256] = { |
| 394 | [0 ... 31] = 1, |
| 395 | [127] = 1, |
| 396 | }; |
| 397 | |
| 398 | /* |
| 399 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 400 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 401 | * them correctly. Instead, define every non-CTL char's status. |
| 402 | */ |
| 403 | const char http_is_token[256] = { |
| 404 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 405 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 406 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 407 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 408 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 409 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 410 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 411 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 412 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 413 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 414 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 415 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 416 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 417 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 418 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 419 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 420 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 421 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 422 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 423 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 424 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 425 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 426 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 427 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 428 | }; |
| 429 | |
| 430 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 431 | /* |
| 432 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 433 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 434 | */ |
| 435 | const char http_is_ver_token[256] = { |
| 436 | ['.'] = 1, ['/'] = 1, |
| 437 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 438 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 439 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 440 | }; |
| 441 | |
| 442 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 443 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 444 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 445 | * 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] | 446 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 447 | * of headers is automatically adjusted. The number of bytes added is returned |
| 448 | * on success, otherwise <0 is returned indicating an error. |
| 449 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 450 | 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] | 451 | { |
| 452 | int bytes, len; |
| 453 | |
| 454 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 455 | 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] | 456 | if (!bytes) |
| 457 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 458 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 459 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 460 | } |
| 461 | |
| 462 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 463 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 464 | * 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] | 465 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 466 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 467 | * of headers is automatically adjusted. The number of bytes added is returned |
| 468 | * on success, otherwise <0 is returned indicating an error. |
| 469 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 470 | int http_header_add_tail2(struct http_msg *msg, |
| 471 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 472 | { |
| 473 | int bytes; |
| 474 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 475 | 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] | 476 | if (!bytes) |
| 477 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 478 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 479 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 480 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 481 | |
| 482 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 483 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 484 | * If so, returns the position of the first non-space character relative to |
| 485 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 486 | * to return a pointer to the place after the first space. Returns 0 if the |
| 487 | * header name does not match. Checks are case-insensitive. |
| 488 | */ |
| 489 | int http_header_match2(const char *hdr, const char *end, |
| 490 | const char *name, int len) |
| 491 | { |
| 492 | const char *val; |
| 493 | |
| 494 | if (hdr + len >= end) |
| 495 | return 0; |
| 496 | if (hdr[len] != ':') |
| 497 | return 0; |
| 498 | if (strncasecmp(hdr, name, len) != 0) |
| 499 | return 0; |
| 500 | val = hdr + len + 1; |
| 501 | while (val < end && HTTP_IS_SPHT(*val)) |
| 502 | val++; |
| 503 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 504 | return len + 2; /* we may replace starting from second space */ |
| 505 | return val - hdr; |
| 506 | } |
| 507 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 508 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 509 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 510 | * structure holds everything necessary to use the header and find next |
| 511 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 512 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 513 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 514 | * http_find_header2() except that it is designed to work with full-line headers |
| 515 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 516 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 517 | * header is rescanned. This allows rescanning after a header deletion. |
| 518 | */ |
| 519 | int http_find_full_header2(const char *name, int len, |
| 520 | char *sol, struct hdr_idx *idx, |
| 521 | struct hdr_ctx *ctx) |
| 522 | { |
| 523 | char *eol, *sov; |
| 524 | int cur_idx, old_idx; |
| 525 | |
| 526 | cur_idx = ctx->idx; |
| 527 | if (cur_idx) { |
| 528 | /* We have previously returned a header, let's search another one */ |
| 529 | sol = ctx->line; |
| 530 | eol = sol + idx->v[cur_idx].len; |
| 531 | goto next_hdr; |
| 532 | } |
| 533 | |
| 534 | /* first request for this header */ |
| 535 | sol += hdr_idx_first_pos(idx); |
| 536 | old_idx = 0; |
| 537 | cur_idx = hdr_idx_first_idx(idx); |
| 538 | while (cur_idx) { |
| 539 | eol = sol + idx->v[cur_idx].len; |
| 540 | |
| 541 | if (len == 0) { |
| 542 | /* No argument was passed, we want any header. |
| 543 | * To achieve this, we simply build a fake request. */ |
| 544 | while (sol + len < eol && sol[len] != ':') |
| 545 | len++; |
| 546 | name = sol; |
| 547 | } |
| 548 | |
| 549 | if ((len < eol - sol) && |
| 550 | (sol[len] == ':') && |
| 551 | (strncasecmp(sol, name, len) == 0)) { |
| 552 | ctx->del = len; |
| 553 | sov = sol + len + 1; |
| 554 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 555 | sov++; |
| 556 | |
| 557 | ctx->line = sol; |
| 558 | ctx->prev = old_idx; |
| 559 | ctx->idx = cur_idx; |
| 560 | ctx->val = sov - sol; |
| 561 | ctx->tws = 0; |
| 562 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 563 | eol--; |
| 564 | ctx->tws++; |
| 565 | } |
| 566 | ctx->vlen = eol - sov; |
| 567 | return 1; |
| 568 | } |
| 569 | next_hdr: |
| 570 | sol = eol + idx->v[cur_idx].cr + 1; |
| 571 | old_idx = cur_idx; |
| 572 | cur_idx = idx->v[cur_idx].next; |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 577 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 578 | * par 2.2 for more information. Note that it requires a valid header to return |
| 579 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 580 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 581 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 582 | { |
| 583 | int quoted, qdpair; |
| 584 | |
| 585 | quoted = qdpair = 0; |
| 586 | for (; s < e; s++) { |
| 587 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 588 | else if (quoted) { |
| 589 | if (*s == '\\') qdpair = 1; |
| 590 | else if (*s == '"') quoted = 0; |
| 591 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 592 | else if (*s == '"') quoted = 1; |
| 593 | else if (*s == ',') return s; |
| 594 | } |
| 595 | return s; |
| 596 | } |
| 597 | |
| 598 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 599 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 600 | * structure holds everything necessary to use the header and find next |
| 601 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 602 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 603 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 604 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 605 | * is NULL when searching for a new values of a header, the current header is |
| 606 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 607 | */ |
| 608 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 609 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 610 | struct hdr_ctx *ctx) |
| 611 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | char *eol, *sov; |
| 613 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 614 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 615 | cur_idx = ctx->idx; |
| 616 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 617 | /* We have previously returned a value, let's search |
| 618 | * another one on the same line. |
| 619 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 620 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 621 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 622 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 623 | eol = sol + idx->v[cur_idx].len; |
| 624 | |
| 625 | if (sov >= eol) |
| 626 | /* no more values in this header */ |
| 627 | goto next_hdr; |
| 628 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 629 | /* values remaining for this header, skip the comma but save it |
| 630 | * for later use (eg: for header deletion). |
| 631 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 632 | sov++; |
| 633 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 634 | sov++; |
| 635 | |
| 636 | goto return_hdr; |
| 637 | } |
| 638 | |
| 639 | /* first request for this header */ |
| 640 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 641 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 642 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 643 | while (cur_idx) { |
| 644 | eol = sol + idx->v[cur_idx].len; |
| 645 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 646 | if (len == 0) { |
| 647 | /* No argument was passed, we want any header. |
| 648 | * To achieve this, we simply build a fake request. */ |
| 649 | while (sol + len < eol && sol[len] != ':') |
| 650 | len++; |
| 651 | name = sol; |
| 652 | } |
| 653 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 654 | if ((len < eol - sol) && |
| 655 | (sol[len] == ':') && |
| 656 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 657 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 658 | sov = sol + len + 1; |
| 659 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 660 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 661 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 662 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 663 | ctx->prev = old_idx; |
| 664 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 665 | ctx->idx = cur_idx; |
| 666 | ctx->val = sov - sol; |
| 667 | |
| 668 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 669 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 670 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 671 | eol--; |
| 672 | ctx->tws++; |
| 673 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 674 | ctx->vlen = eol - sov; |
| 675 | return 1; |
| 676 | } |
| 677 | next_hdr: |
| 678 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 679 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 680 | cur_idx = idx->v[cur_idx].next; |
| 681 | } |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 686 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 687 | struct hdr_ctx *ctx) |
| 688 | { |
| 689 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 690 | } |
| 691 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 692 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 693 | * the http_find_header functions. The value is removed, as well as surrounding |
| 694 | * 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] | 695 | * 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] | 696 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 697 | * no more header, so any processing may stop. The ctx is always left in a form |
| 698 | * that can be handled by http_find_header2() to find next occurrence. |
| 699 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 700 | 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] | 701 | { |
| 702 | int cur_idx = ctx->idx; |
| 703 | char *sol = ctx->line; |
| 704 | struct hdr_idx_elem *hdr; |
| 705 | int delta, skip_comma; |
| 706 | |
| 707 | if (!cur_idx) |
| 708 | return 0; |
| 709 | |
| 710 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 711 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 712 | /* 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] | 713 | 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] | 714 | http_msg_move_end(msg, delta); |
| 715 | idx->used--; |
| 716 | hdr->len = 0; /* unused entry */ |
| 717 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 718 | if (idx->tail == ctx->idx) |
| 719 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 720 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 721 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 722 | 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] | 723 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 724 | return ctx->idx; |
| 725 | } |
| 726 | |
| 727 | /* 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] | 728 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 729 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 730 | */ |
| 731 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 732 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 733 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 734 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 735 | NULL, 0); |
| 736 | hdr->len += delta; |
| 737 | http_msg_move_end(msg, delta); |
| 738 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 739 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 740 | return ctx->idx; |
| 741 | } |
| 742 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 743 | /* This function handles a server error at the stream interface level. The |
| 744 | * stream interface is assumed to be already in a closed state. An optional |
| 745 | * message is copied into the input buffer, and an HTTP status code stored. |
| 746 | * 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] | 747 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 748 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 749 | static void http_server_error(struct session *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 750 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 751 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 752 | channel_auto_read(si->ob); |
| 753 | channel_abort(si->ob); |
| 754 | channel_auto_close(si->ob); |
| 755 | channel_erase(si->ob); |
| 756 | channel_auto_close(si->ib); |
| 757 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 758 | if (status > 0 && msg) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 759 | s->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 760 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 761 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 762 | if (!(s->flags & SN_ERR_MASK)) |
| 763 | s->flags |= err; |
| 764 | if (!(s->flags & SN_FINST_MASK)) |
| 765 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 768 | /* This function returns the appropriate error location for the given session |
| 769 | * and message. |
| 770 | */ |
| 771 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 772 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 773 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 774 | if (s->be->errmsg[msgnum].str) |
| 775 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 776 | else if (s->fe->errmsg[msgnum].str) |
| 777 | return &s->fe->errmsg[msgnum]; |
| 778 | else |
| 779 | return &http_err_chunks[msgnum]; |
| 780 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 782 | /* |
| 783 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 784 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 785 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 786 | 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] | 787 | { |
| 788 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 789 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 790 | |
| 791 | m = ((unsigned)*str - 'A'); |
| 792 | |
| 793 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 794 | for (h = http_methods[m]; h->len > 0; h++) { |
| 795 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 796 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 797 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 798 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 799 | }; |
| 800 | return HTTP_METH_OTHER; |
| 801 | } |
| 802 | return HTTP_METH_NONE; |
| 803 | |
| 804 | } |
| 805 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 806 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 807 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 808 | * It is returned otherwise. |
| 809 | */ |
| 810 | static char * |
| 811 | http_get_path(struct http_txn *txn) |
| 812 | { |
| 813 | char *ptr, *end; |
| 814 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 815 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 816 | end = ptr + txn->req.sl.rq.u_l; |
| 817 | |
| 818 | if (ptr >= end) |
| 819 | return NULL; |
| 820 | |
| 821 | /* RFC2616, par. 5.1.2 : |
| 822 | * Request-URI = "*" | absuri | abspath | authority |
| 823 | */ |
| 824 | |
| 825 | if (*ptr == '*') |
| 826 | return NULL; |
| 827 | |
| 828 | if (isalpha((unsigned char)*ptr)) { |
| 829 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 830 | ptr++; |
| 831 | while (ptr < end && |
| 832 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 833 | ptr++; |
| 834 | /* skip '://' */ |
| 835 | if (ptr == end || *ptr++ != ':') |
| 836 | return NULL; |
| 837 | if (ptr == end || *ptr++ != '/') |
| 838 | return NULL; |
| 839 | if (ptr == end || *ptr++ != '/') |
| 840 | return NULL; |
| 841 | } |
| 842 | /* skip [user[:passwd]@]host[:[port]] */ |
| 843 | |
| 844 | while (ptr < end && *ptr != '/') |
| 845 | ptr++; |
| 846 | |
| 847 | if (ptr == end) |
| 848 | return NULL; |
| 849 | |
| 850 | /* OK, we got the '/' ! */ |
| 851 | return ptr; |
| 852 | } |
| 853 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 854 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 855 | * If not found, return NULL. It is returned otherwise. |
| 856 | */ |
| 857 | static char * |
| 858 | http_get_path_from_string(char *str) |
| 859 | { |
| 860 | char *ptr = str; |
| 861 | |
| 862 | /* RFC2616, par. 5.1.2 : |
| 863 | * Request-URI = "*" | absuri | abspath | authority |
| 864 | */ |
| 865 | |
| 866 | if (*ptr == '*') |
| 867 | return NULL; |
| 868 | |
| 869 | if (isalpha((unsigned char)*ptr)) { |
| 870 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 871 | ptr++; |
| 872 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 873 | ptr++; |
| 874 | /* skip '://' */ |
| 875 | if (*ptr == '\0' || *ptr++ != ':') |
| 876 | return NULL; |
| 877 | if (*ptr == '\0' || *ptr++ != '/') |
| 878 | return NULL; |
| 879 | if (*ptr == '\0' || *ptr++ != '/') |
| 880 | return NULL; |
| 881 | } |
| 882 | /* skip [user[:passwd]@]host[:[port]] */ |
| 883 | |
| 884 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 885 | ptr++; |
| 886 | |
| 887 | if (*ptr == '\0' || *ptr == ' ') |
| 888 | return NULL; |
| 889 | |
| 890 | /* OK, we got the '/' ! */ |
| 891 | return ptr; |
| 892 | } |
| 893 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 894 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 895 | * in redirect mode. This may only be called just after the stream interface |
| 896 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 897 | * follow normal proxy processing. NOTE: this function is designed to support |
| 898 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 899 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 900 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 901 | { |
| 902 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 903 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 904 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 905 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 906 | |
| 907 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 908 | trash.len = strlen(HTTP_302); |
| 909 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 910 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 911 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 912 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 913 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 914 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 915 | return; |
| 916 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 917 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 918 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 919 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 920 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 921 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 922 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 923 | /* 3: add the request URI. Since it was already forwarded, we need |
| 924 | * to temporarily rewind the buffer. |
| 925 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 926 | txn = &s->txn; |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 927 | b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 928 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 929 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 930 | 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] | 931 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 932 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 933 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 934 | if (!path) |
| 935 | return; |
| 936 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 937 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 938 | return; |
| 939 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 940 | memcpy(trash.str + trash.len, path, len); |
| 941 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 942 | |
| 943 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 944 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 945 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 946 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 947 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 948 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 949 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 950 | |
| 951 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 952 | si_shutr(si); |
| 953 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 954 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 955 | si->state = SI_ST_CLO; |
| 956 | |
| 957 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 958 | http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 959 | |
| 960 | /* 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] | 961 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 962 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 965 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 966 | * that the server side is closed. Note that err_type is actually a |
| 967 | * bitmask, where almost only aborts may be cumulated with other |
| 968 | * values. We consider that aborted operations are more important |
| 969 | * than timeouts or errors due to the fact that nobody else in the |
| 970 | * logs might explain incomplete retries. All others should avoid |
| 971 | * being cumulated. It should normally not be possible to have multiple |
| 972 | * 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] | 973 | * Note that connection errors appearing on the second request of a keep-alive |
| 974 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 975 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 976 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 977 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 978 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 979 | |
| 980 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 981 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 982 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 983 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 984 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 985 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 986 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 987 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 988 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 989 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 990 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 991 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 992 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 993 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 994 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 995 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 996 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 997 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 998 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 999 | 503, (s->flags & SN_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1000 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1001 | else if (err_type & SI_ET_CONN_RES) |
| 1002 | http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1003 | 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| 1004 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1005 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 1006 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1007 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1010 | extern const char sess_term_cond[8]; |
| 1011 | extern const char sess_fin_state[8]; |
| 1012 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1013 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1014 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1015 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1016 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1017 | /* |
| 1018 | * Capture headers from message starting at <som> according to header list |
| 1019 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1020 | */ |
| 1021 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1022 | char **cap, struct cap_hdr *cap_hdr) |
| 1023 | { |
| 1024 | char *eol, *sol, *col, *sov; |
| 1025 | int cur_idx; |
| 1026 | struct cap_hdr *h; |
| 1027 | int len; |
| 1028 | |
| 1029 | sol = som + hdr_idx_first_pos(idx); |
| 1030 | cur_idx = hdr_idx_first_idx(idx); |
| 1031 | |
| 1032 | while (cur_idx) { |
| 1033 | eol = sol + idx->v[cur_idx].len; |
| 1034 | |
| 1035 | col = sol; |
| 1036 | while (col < eol && *col != ':') |
| 1037 | col++; |
| 1038 | |
| 1039 | sov = col + 1; |
| 1040 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1041 | sov++; |
| 1042 | |
| 1043 | for (h = cap_hdr; h; h = h->next) { |
| 1044 | if ((h->namelen == col - sol) && |
| 1045 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1046 | if (cap[h->index] == NULL) |
| 1047 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1048 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1049 | |
| 1050 | if (cap[h->index] == NULL) { |
| 1051 | Alert("HTTP capture : out of memory.\n"); |
| 1052 | continue; |
| 1053 | } |
| 1054 | |
| 1055 | len = eol - sov; |
| 1056 | if (len > h->len) |
| 1057 | len = h->len; |
| 1058 | |
| 1059 | memcpy(cap[h->index], sov, len); |
| 1060 | cap[h->index][len]=0; |
| 1061 | } |
| 1062 | } |
| 1063 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1064 | cur_idx = idx->v[cur_idx].next; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1069 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1070 | */ |
| 1071 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1072 | |
| 1073 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1074 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1075 | */ |
| 1076 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1077 | ptr++; \ |
| 1078 | if (likely(ptr < end)) \ |
| 1079 | goto good; \ |
| 1080 | else { \ |
| 1081 | state = (st); \ |
| 1082 | goto http_msg_ood; \ |
| 1083 | } \ |
| 1084 | } while (0) |
| 1085 | |
| 1086 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1087 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1088 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1089 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1090 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1091 | * will give undefined results. |
| 1092 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1093 | * and that msg->sol points to the beginning of the response. |
| 1094 | * If a complete line is found (which implies that at least one CR or LF is |
| 1095 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1096 | * returned indicating an incomplete line (which does not mean that parts have |
| 1097 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1098 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1099 | * upon next call. |
| 1100 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1101 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1102 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1103 | * 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] | 1104 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1105 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1106 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1107 | enum ht_state state, const char *ptr, const char *end, |
| 1108 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1109 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1110 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1111 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1112 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1113 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1114 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1115 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1116 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1117 | |
| 1118 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1119 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1120 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1121 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1122 | state = HTTP_MSG_ERROR; |
| 1123 | break; |
| 1124 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1125 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1126 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1127 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1128 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1129 | goto http_msg_rpcode; |
| 1130 | } |
| 1131 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1132 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1133 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1134 | state = HTTP_MSG_ERROR; |
| 1135 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1136 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1137 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1138 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1139 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1140 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1141 | |
| 1142 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1143 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1144 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1145 | } |
| 1146 | |
| 1147 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1148 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1149 | http_msg_rsp_reason: |
| 1150 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1151 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1152 | msg->sl.st.r_l = 0; |
| 1153 | goto http_msg_rpline_eol; |
| 1154 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1155 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1156 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1157 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1158 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1159 | goto http_msg_rpreason; |
| 1160 | } |
| 1161 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1162 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1163 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1164 | goto http_msg_rsp_reason; |
| 1165 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1166 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1167 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1168 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1169 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1170 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1171 | http_msg_rpline_eol: |
| 1172 | /* We have seen the end of line. Note that we do not |
| 1173 | * necessarily have the \n yet, but at least we know that we |
| 1174 | * have EITHER \r OR \n, otherwise the response would not be |
| 1175 | * complete. We can then record the response length and return |
| 1176 | * to the caller which will be able to register it. |
| 1177 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1178 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1179 | return ptr; |
| 1180 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1181 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1182 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1183 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1184 | exit(1); |
| 1185 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1186 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1190 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1191 | if (ret_state) |
| 1192 | *ret_state = state; |
| 1193 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1194 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1195 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1198 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1199 | * This function parses a request line between <ptr> and <end>, starting with |
| 1200 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1201 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1202 | * will give undefined results. |
| 1203 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1204 | * and that msg->sol points to the beginning of the request. |
| 1205 | * If a complete line is found (which implies that at least one CR or LF is |
| 1206 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1207 | * returned indicating an incomplete line (which does not mean that parts have |
| 1208 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1209 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1210 | * upon next call. |
| 1211 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1212 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1213 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1214 | * 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] | 1215 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1216 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1217 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1218 | enum ht_state state, const char *ptr, const char *end, |
| 1219 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1220 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1221 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1222 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1224 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1225 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1226 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1227 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1228 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1229 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1230 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1231 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1232 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1234 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1235 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1236 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1238 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1239 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1240 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1241 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1242 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1243 | msg->sl.rq.v_l = 0; |
| 1244 | goto http_msg_rqline_eol; |
| 1245 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1246 | state = HTTP_MSG_ERROR; |
| 1247 | break; |
| 1248 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1249 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1250 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1251 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1252 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1253 | goto http_msg_rquri; |
| 1254 | } |
| 1255 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1256 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1257 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1258 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1259 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1260 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1261 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1262 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1263 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1264 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1265 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1266 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1267 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1268 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1269 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1270 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1271 | /* non-ASCII chars are forbidden unless option |
| 1272 | * accept-invalid-http-request is enabled in the frontend. |
| 1273 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1274 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1275 | if (msg->err_pos < -1) |
| 1276 | goto invalid_char; |
| 1277 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1278 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1279 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1280 | } |
| 1281 | |
| 1282 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1283 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1284 | goto http_msg_req09_uri_e; |
| 1285 | } |
| 1286 | |
| 1287 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1288 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1289 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1290 | state = HTTP_MSG_ERROR; |
| 1291 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1292 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1293 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1294 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1295 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1296 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1297 | goto http_msg_rqver; |
| 1298 | } |
| 1299 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1300 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1301 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1302 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1304 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1305 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1306 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1307 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1308 | |
| 1309 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1310 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1311 | http_msg_rqline_eol: |
| 1312 | /* We have seen the end of line. Note that we do not |
| 1313 | * necessarily have the \n yet, but at least we know that we |
| 1314 | * have EITHER \r OR \n, otherwise the request would not be |
| 1315 | * complete. We can then record the request length and return |
| 1316 | * to the caller which will be able to register it. |
| 1317 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1318 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1319 | return ptr; |
| 1320 | } |
| 1321 | |
| 1322 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1323 | state = HTTP_MSG_ERROR; |
| 1324 | break; |
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 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1327 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1329 | exit(1); |
| 1330 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1331 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1332 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1333 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1334 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1335 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1336 | if (ret_state) |
| 1337 | *ret_state = state; |
| 1338 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1339 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1342 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1343 | /* |
| 1344 | * Returns the data from Authorization header. Function may be called more |
| 1345 | * than once so data is stored in txn->auth_data. When no header is found |
| 1346 | * 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] | 1347 | * searching again for something we are unable to find anyway. However, if |
| 1348 | * the result if valid, the cache is not reused because we would risk to |
| 1349 | * have the credentials overwritten by another session in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1350 | */ |
| 1351 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1352 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1353 | * set according to global.tune.bufsize. |
| 1354 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1355 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1356 | |
| 1357 | int |
| 1358 | get_http_auth(struct session *s) |
| 1359 | { |
| 1360 | |
| 1361 | struct http_txn *txn = &s->txn; |
| 1362 | struct chunk auth_method; |
| 1363 | struct hdr_ctx ctx; |
| 1364 | char *h, *p; |
| 1365 | int len; |
| 1366 | |
| 1367 | #ifdef DEBUG_AUTH |
| 1368 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1369 | #endif |
| 1370 | |
| 1371 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1372 | return 0; |
| 1373 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1374 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1375 | |
| 1376 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1377 | |
| 1378 | if (txn->flags & TX_USE_PX_CONN) { |
| 1379 | h = "Proxy-Authorization"; |
| 1380 | len = strlen(h); |
| 1381 | } else { |
| 1382 | h = "Authorization"; |
| 1383 | len = strlen(h); |
| 1384 | } |
| 1385 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1386 | 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] | 1387 | return 0; |
| 1388 | |
| 1389 | h = ctx.line + ctx.val; |
| 1390 | |
| 1391 | p = memchr(h, ' ', ctx.vlen); |
| 1392 | if (!p || p == h) |
| 1393 | return 0; |
| 1394 | |
| 1395 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1396 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1397 | |
| 1398 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1399 | |
| 1400 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1401 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1402 | |
| 1403 | if (len < 0) |
| 1404 | return 0; |
| 1405 | |
| 1406 | |
| 1407 | get_http_auth_buff[len] = '\0'; |
| 1408 | |
| 1409 | p = strchr(get_http_auth_buff, ':'); |
| 1410 | |
| 1411 | if (!p) |
| 1412 | return 0; |
| 1413 | |
| 1414 | txn->auth.user = get_http_auth_buff; |
| 1415 | *p = '\0'; |
| 1416 | txn->auth.pass = p+1; |
| 1417 | |
| 1418 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1425 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1426 | /* |
| 1427 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1428 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1429 | * ensuring that the message does not wrap. The function can be preempted |
| 1430 | * everywhere when data are missing and recalled at the exact same location |
| 1431 | * with no information loss. The message may even be realigned between two |
| 1432 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1433 | * 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] | 1434 | * fields. Note that msg->sol will be initialized after completing the first |
| 1435 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1436 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1438 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1440 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1442 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1443 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1444 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1445 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1446 | ptr = buf->p + msg->next; |
| 1447 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1448 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | if (unlikely(ptr >= end)) |
| 1450 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1451 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1453 | /* |
| 1454 | * First, states that are specific to the response only. |
| 1455 | * We check them first so that request and headers are |
| 1456 | * closer to each other (accessed more often). |
| 1457 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1458 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1459 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1460 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1461 | /* we have a start of message, but we have to check |
| 1462 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1463 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1464 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1465 | if (unlikely(ptr != buf->p)) { |
| 1466 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1467 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1468 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1469 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1470 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1471 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1472 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1473 | hdr_idx_init(idx); |
| 1474 | state = HTTP_MSG_RPVER; |
| 1475 | goto http_msg_rpver; |
| 1476 | } |
| 1477 | |
| 1478 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1479 | goto http_msg_invalid; |
| 1480 | |
| 1481 | if (unlikely(*ptr == '\n')) |
| 1482 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1483 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1484 | /* stop here */ |
| 1485 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1486 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1487 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1488 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1489 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1490 | /* stop here */ |
| 1491 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1492 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1493 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1494 | case HTTP_MSG_RPVER_SP: |
| 1495 | case HTTP_MSG_RPCODE: |
| 1496 | case HTTP_MSG_RPCODE_SP: |
| 1497 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1498 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1499 | state, ptr, end, |
| 1500 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1501 | if (unlikely(!ptr)) |
| 1502 | return; |
| 1503 | |
| 1504 | /* we have a full response and we know that we have either a CR |
| 1505 | * or an LF at <ptr>. |
| 1506 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1507 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1508 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1509 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1510 | if (likely(*ptr == '\r')) |
| 1511 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1512 | goto http_msg_rpline_end; |
| 1513 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1514 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1515 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1516 | /* msg->sol must point to the first of CR or LF. */ |
| 1517 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1518 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1519 | /* stop here */ |
| 1520 | |
| 1521 | /* |
| 1522 | * Second, states that are specific to the request only |
| 1523 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1525 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1527 | /* we have a start of message, but we have to check |
| 1528 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1529 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1530 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1531 | if (likely(ptr != buf->p)) { |
| 1532 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1533 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1534 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1535 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1537 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1538 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1539 | state = HTTP_MSG_RQMETH; |
| 1540 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1542 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1543 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1544 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (unlikely(*ptr == '\n')) |
| 1547 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1548 | 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] | 1549 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1550 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1552 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1554 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1555 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1558 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | case HTTP_MSG_RQMETH_SP: |
| 1560 | case HTTP_MSG_RQURI: |
| 1561 | case HTTP_MSG_RQURI_SP: |
| 1562 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1563 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1564 | state, ptr, end, |
| 1565 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | if (unlikely(!ptr)) |
| 1567 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | /* we have a full request and we know that we have either a CR |
| 1570 | * or an LF at <ptr>. |
| 1571 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1572 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1573 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1574 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | if (likely(*ptr == '\r')) |
| 1576 | 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] | 1577 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1578 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1580 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | /* check for HTTP/0.9 request : no version information available. |
| 1582 | * msg->sol must point to the first of CR or LF. |
| 1583 | */ |
| 1584 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1585 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1587 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1588 | 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] | 1589 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1590 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1591 | /* |
| 1592 | * Common states below |
| 1593 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1595 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1596 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1598 | goto http_msg_hdr_name; |
| 1599 | } |
| 1600 | |
| 1601 | if (likely(*ptr == '\r')) |
| 1602 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1603 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1604 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1605 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1606 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | /* assumes msg->sol points to the first char */ |
| 1608 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1609 | 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] | 1610 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1611 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | 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] | 1613 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1614 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1615 | goto http_msg_invalid; |
| 1616 | |
| 1617 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1618 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1619 | |
| 1620 | /* and we still accept this non-token character */ |
| 1621 | 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] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1624 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1625 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1627 | 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] | 1628 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1629 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1630 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1633 | goto http_msg_hdr_val; |
| 1634 | } |
| 1635 | |
| 1636 | if (likely(*ptr == '\r')) |
| 1637 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1638 | goto http_msg_hdr_l1_lf; |
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 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1641 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1643 | 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] | 1644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1646 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1648 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1649 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1650 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | goto http_msg_hdr_l1_sp; |
| 1652 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1653 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1654 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | goto http_msg_complete_header; |
| 1656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1657 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1658 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1659 | /* assumes msg->sol points to the first char, and msg->sov |
| 1660 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | */ |
| 1662 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1663 | 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] | 1664 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1665 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1667 | * real header end in case it ends with lots of LWS, but is this |
| 1668 | * really needed ? |
| 1669 | */ |
| 1670 | if (likely(*ptr == '\r')) |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1672 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1673 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1675 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1677 | 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] | 1678 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1680 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1682 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1683 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1684 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | goto http_msg_hdr_val; |
| 1686 | } |
| 1687 | http_msg_complete_header: |
| 1688 | /* |
| 1689 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1690 | * Assumes msg->sol points to the first char, msg->sov points |
| 1691 | * to the first character of the value and msg->eol to the |
| 1692 | * first CR or LF so we know how the line ends. We insert last |
| 1693 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1695 | 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] | 1696 | idx, idx->tail) < 0)) |
| 1697 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1698 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1699 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1701 | goto http_msg_hdr_name; |
| 1702 | } |
| 1703 | |
| 1704 | if (likely(*ptr == '\r')) |
| 1705 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1706 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1707 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1709 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1710 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1711 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1712 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1713 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1715 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1716 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1717 | msg->eoh = msg->sol; |
| 1718 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1719 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1720 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1722 | |
| 1723 | case HTTP_MSG_ERROR: |
| 1724 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1725 | break; |
| 1726 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1728 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1730 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1731 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1732 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | } |
| 1734 | http_msg_ood: |
| 1735 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1736 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1737 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | http_msg_invalid: |
| 1741 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1742 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1743 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1744 | return; |
| 1745 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1746 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1747 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1748 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1749 | * nothing is done and 1 is returned. |
| 1750 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1751 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1752 | { |
| 1753 | int delta; |
| 1754 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1755 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1756 | |
| 1757 | if (msg->sl.rq.v_l != 0) |
| 1758 | return 1; |
| 1759 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1760 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1761 | if (txn->meth != HTTP_METH_GET) |
| 1762 | return 0; |
| 1763 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1764 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1765 | delta = 0; |
| 1766 | |
| 1767 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1768 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1769 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1770 | } |
| 1771 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1772 | 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] | 1773 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1774 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1775 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1776 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1777 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1778 | NULL, NULL); |
| 1779 | if (unlikely(!cur_end)) |
| 1780 | return 0; |
| 1781 | |
| 1782 | /* we have a full HTTP/1.0 request now and we know that |
| 1783 | * we have either a CR or an LF at <ptr>. |
| 1784 | */ |
| 1785 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1786 | return 1; |
| 1787 | } |
| 1788 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1789 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1790 | * and "keep-alive" values. If we already know that some headers may safely |
| 1791 | * 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] | 1792 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1793 | * - 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] | 1794 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1795 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1796 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1797 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1798 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1799 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1800 | 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] | 1801 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1802 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1803 | const char *hdr_val = "Connection"; |
| 1804 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1805 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1806 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1807 | return; |
| 1808 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1809 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1810 | hdr_val = "Proxy-Connection"; |
| 1811 | hdr_len = 16; |
| 1812 | } |
| 1813 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1814 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1815 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1816 | 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] | 1817 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1818 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1819 | if (to_del & 2) |
| 1820 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1821 | else |
| 1822 | txn->flags |= TX_CON_KAL_SET; |
| 1823 | } |
| 1824 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1825 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1826 | if (to_del & 1) |
| 1827 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1828 | else |
| 1829 | txn->flags |= TX_CON_CLO_SET; |
| 1830 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1831 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1832 | txn->flags |= TX_HDR_CONN_UPG; |
| 1833 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1836 | txn->flags |= TX_HDR_CONN_PRS; |
| 1837 | return; |
| 1838 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1839 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1840 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1841 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1842 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1843 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1844 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1845 | 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] | 1846 | { |
| 1847 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1848 | const char *hdr_val = "Connection"; |
| 1849 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1850 | |
| 1851 | ctx.idx = 0; |
| 1852 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1853 | |
| 1854 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1855 | hdr_val = "Proxy-Connection"; |
| 1856 | hdr_len = 16; |
| 1857 | } |
| 1858 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1859 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1860 | 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] | 1861 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1862 | if (wanted & TX_CON_KAL_SET) |
| 1863 | txn->flags |= TX_CON_KAL_SET; |
| 1864 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1865 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1866 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1867 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1868 | if (wanted & TX_CON_CLO_SET) |
| 1869 | txn->flags |= TX_CON_CLO_SET; |
| 1870 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1871 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1872 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1873 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1874 | |
| 1875 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1876 | return; |
| 1877 | |
| 1878 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1879 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1880 | hdr_val = "Connection: close"; |
| 1881 | hdr_len = 17; |
| 1882 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1883 | hdr_val = "Proxy-Connection: close"; |
| 1884 | hdr_len = 23; |
| 1885 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1886 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1890 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1891 | hdr_val = "Connection: keep-alive"; |
| 1892 | hdr_len = 22; |
| 1893 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1894 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1895 | hdr_len = 28; |
| 1896 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1897 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1898 | } |
| 1899 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1902 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 1903 | * the first byte of data after the chunk size, so that we know we can forward |
| 1904 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 1905 | * the chunk size. That way it is always possible to differentiate between the |
| 1906 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1907 | * 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] | 1908 | * 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] | 1909 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1910 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1911 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1912 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1913 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1914 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1915 | const char *end = buf->data + buf->size; |
| 1916 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1917 | unsigned int chunk = 0; |
| 1918 | |
| 1919 | /* The chunk size is in the following form, though we are only |
| 1920 | * interested in the size and CRLF : |
| 1921 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1922 | */ |
| 1923 | while (1) { |
| 1924 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1925 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1926 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1927 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1928 | if (c < 0) /* not a hex digit anymore */ |
| 1929 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1930 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1931 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1932 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1933 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1934 | chunk = (chunk << 4) + c; |
| 1935 | } |
| 1936 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1937 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1938 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1939 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1940 | |
| 1941 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1942 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1943 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1944 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1945 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1948 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1949 | * for the end of chunk size. |
| 1950 | */ |
| 1951 | while (1) { |
| 1952 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1953 | /* we now have a CR or an LF at ptr */ |
| 1954 | if (likely(*ptr == '\r')) { |
| 1955 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1956 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1957 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1958 | return 0; |
| 1959 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1960 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1961 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1962 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1963 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1964 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1965 | /* done */ |
| 1966 | break; |
| 1967 | } |
| 1968 | else if (*ptr == ';') { |
| 1969 | /* chunk extension, ends at next CRLF */ |
| 1970 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1971 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1972 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1973 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1974 | |
| 1975 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1976 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1977 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1978 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1979 | return 0; |
| 1980 | } |
| 1981 | /* we have a CRLF now, loop above */ |
| 1982 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1983 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1984 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1985 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1986 | } |
| 1987 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1988 | /* 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] | 1989 | * which may or may not be present. We save that into ->next, |
| 1990 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1991 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1992 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1993 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 1994 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1995 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1996 | msg->chunk_len = chunk; |
| 1997 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1998 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1999 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2000 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2001 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2002 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2003 | } |
| 2004 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2005 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2006 | * 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] | 2007 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2008 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2009 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2010 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2011 | * If a parse error is encountered, the function returns < 0 and does not |
| 2012 | * change anything except maybe msg->next. Note that the message must |
| 2013 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2014 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2015 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2016 | * already parsed and not forwarded. It is also important to note that this |
| 2017 | * 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] | 2018 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2019 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2020 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2021 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2022 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2023 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2024 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2025 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2026 | const char *ptr = b_ptr(buf, msg->next); |
| 2027 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2028 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2029 | |
| 2030 | /* scan current line and stop at LF or CRLF */ |
| 2031 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2032 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2033 | return 0; |
| 2034 | |
| 2035 | if (*ptr == '\n') { |
| 2036 | if (!p1) |
| 2037 | p1 = ptr; |
| 2038 | p2 = ptr; |
| 2039 | break; |
| 2040 | } |
| 2041 | |
| 2042 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2043 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2044 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2045 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2046 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2047 | p1 = ptr; |
| 2048 | } |
| 2049 | |
| 2050 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2051 | if (ptr >= buf->data + buf->size) |
| 2052 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /* after LF; point to beginning of next line */ |
| 2056 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2057 | if (p2 >= buf->data + buf->size) |
| 2058 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2059 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2060 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2061 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2062 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2063 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2064 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2065 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2066 | * Everything was scheduled for forwarding, there's nothing |
| 2067 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2068 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2069 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2070 | msg->msg_state = HTTP_MSG_DONE; |
| 2071 | return 1; |
| 2072 | } |
| 2073 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2074 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2078 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2079 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2080 | * 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] | 2081 | * 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] | 2082 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2083 | * not enough data are available, the function does not change anything and |
| 2084 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2085 | * does not change anything. Note: this function is designed to parse wrapped |
| 2086 | * CRLF at the end of the buffer. |
| 2087 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2088 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2089 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2090 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2091 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2092 | int bytes; |
| 2093 | |
| 2094 | /* NB: we'll check data availabilty at the end. It's not a |
| 2095 | * problem because whatever we match first will be checked |
| 2096 | * against the correct length. |
| 2097 | */ |
| 2098 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2099 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2100 | if (*ptr == '\r') { |
| 2101 | bytes++; |
| 2102 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2103 | if (ptr >= buf->data + buf->size) |
| 2104 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2107 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2108 | return 0; |
| 2109 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2110 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2111 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2112 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2113 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2114 | |
| 2115 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2116 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2117 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2118 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2119 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2120 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2121 | return 1; |
| 2122 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2123 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2124 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2125 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2126 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2127 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2128 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2129 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2130 | { |
| 2131 | int q = 1000; |
| 2132 | |
| 2133 | if (!isdigit(*qvalue)) |
| 2134 | goto out; |
| 2135 | q = (*qvalue++ - '0') * 1000; |
| 2136 | |
| 2137 | if (*qvalue++ != '.') |
| 2138 | goto out; |
| 2139 | |
| 2140 | if (!isdigit(*qvalue)) |
| 2141 | goto out; |
| 2142 | q += (*qvalue++ - '0') * 100; |
| 2143 | |
| 2144 | if (!isdigit(*qvalue)) |
| 2145 | goto out; |
| 2146 | q += (*qvalue++ - '0') * 10; |
| 2147 | |
| 2148 | if (!isdigit(*qvalue)) |
| 2149 | goto out; |
| 2150 | q += (*qvalue++ - '0') * 1; |
| 2151 | out: |
| 2152 | if (q > 1000) |
| 2153 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2154 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2155 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2156 | return q; |
| 2157 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2158 | |
| 2159 | /* |
| 2160 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2161 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2162 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2163 | { |
| 2164 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2165 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2166 | struct hdr_ctx ctx; |
| 2167 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2168 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2169 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2170 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2171 | * 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] | 2172 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2173 | */ |
| 2174 | ctx.idx = 0; |
| 2175 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2176 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2177 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2178 | (ctx.vlen < 31 || |
| 2179 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2180 | ctx.line[ctx.val + 30] < '6' || |
| 2181 | (ctx.line[ctx.val + 30] == '6' && |
| 2182 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2183 | s->comp_algo = NULL; |
| 2184 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2185 | } |
| 2186 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2187 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2188 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2189 | int best_q = 0; |
| 2190 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2191 | ctx.idx = 0; |
| 2192 | 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] | 2193 | const char *qval; |
| 2194 | int q; |
| 2195 | int toklen; |
| 2196 | |
| 2197 | /* try to isolate the token from the optional q-value */ |
| 2198 | toklen = 0; |
| 2199 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2200 | toklen++; |
| 2201 | |
| 2202 | qval = ctx.line + ctx.val + toklen; |
| 2203 | while (1) { |
| 2204 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2205 | qval++; |
| 2206 | |
| 2207 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2208 | qval = NULL; |
| 2209 | break; |
| 2210 | } |
| 2211 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2212 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2213 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2214 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2215 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2216 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2217 | qval = NULL; |
| 2218 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2219 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2220 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2221 | break; |
| 2222 | |
| 2223 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2224 | qval++; |
| 2225 | } |
| 2226 | |
| 2227 | /* 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] | 2228 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2229 | |
| 2230 | if (q <= best_q) |
| 2231 | continue; |
| 2232 | |
| 2233 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2234 | if (*(ctx.line + ctx.val) == '*' || |
| 2235 | word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) { |
| 2236 | s->comp_algo = comp_algo; |
| 2237 | best_q = q; |
| 2238 | break; |
| 2239 | } |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2245 | if (s->comp_algo) { |
| 2246 | if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) { |
| 2247 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2248 | ctx.idx = 0; |
| 2249 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2250 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2251 | } |
| 2252 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2253 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2257 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2258 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2259 | if (comp_algo->add_data == identity_add_data) { |
| 2260 | s->comp_algo = comp_algo; |
| 2261 | return 1; |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | * Selects a comression algorithm depending of the server response. |
| 2272 | */ |
| 2273 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2274 | { |
| 2275 | struct http_txn *txn = &s->txn; |
| 2276 | struct http_msg *msg = &txn->rsp; |
| 2277 | struct hdr_ctx ctx; |
| 2278 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2279 | |
| 2280 | /* no common compression algorithm was found in request header */ |
| 2281 | if (s->comp_algo == NULL) |
| 2282 | goto fail; |
| 2283 | |
| 2284 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2285 | 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] | 2286 | goto fail; |
| 2287 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2288 | /* 200 only */ |
| 2289 | if (txn->status != 200) |
| 2290 | goto fail; |
| 2291 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2292 | /* Content-Length is null */ |
| 2293 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2294 | goto fail; |
| 2295 | |
| 2296 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2297 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2298 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2299 | goto fail; |
| 2300 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2301 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2302 | ctx.idx = 0; |
| 2303 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2304 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2305 | goto fail; |
| 2306 | } |
| 2307 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2308 | comp_type = NULL; |
| 2309 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2310 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2311 | * not listed in the "compression type" directive if any. If no content-type was |
| 2312 | * found but configuration requires one, we don't compress either. Backend has |
| 2313 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2314 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2315 | ctx.idx = 0; |
| 2316 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2317 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2318 | goto fail; |
| 2319 | |
| 2320 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2321 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2322 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2323 | if (ctx.vlen >= comp_type->name_len && |
| 2324 | 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] | 2325 | /* this Content-Type should be compressed */ |
| 2326 | break; |
| 2327 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2328 | /* this Content-Type should not be compressed */ |
| 2329 | if (comp_type == NULL) |
| 2330 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2331 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2332 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2333 | else { /* no content-type header */ |
| 2334 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2335 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2338 | /* limit compression rate */ |
| 2339 | if (global.comp_rate_lim > 0) |
| 2340 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2341 | goto fail; |
| 2342 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2343 | /* limit cpu usage */ |
| 2344 | if (idle_pct < compress_min_idle) |
| 2345 | goto fail; |
| 2346 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2347 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2348 | 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] | 2349 | goto fail; |
| 2350 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2351 | s->flags |= SN_COMP_READY; |
| 2352 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2353 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2354 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2355 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2356 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2357 | |
| 2358 | /* add Transfer-Encoding header */ |
| 2359 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2360 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2361 | |
| 2362 | /* |
| 2363 | * Add Content-Encoding header when it's not identity encoding. |
| 2364 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2365 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2366 | * header. |
| 2367 | */ |
| 2368 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2369 | trash.len = 18; |
| 2370 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2371 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2372 | trash.len += s->comp_algo->name_len; |
| 2373 | trash.str[trash.len] = '\0'; |
| 2374 | 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] | 2375 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2376 | return 1; |
| 2377 | |
| 2378 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2379 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2380 | return 0; |
| 2381 | } |
| 2382 | |
| 2383 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2384 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2385 | * processing can continue on next analysers, or zero if it either needs more |
| 2386 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2387 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2388 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2389 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2390 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2391 | int http_wait_for_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2392 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2393 | /* |
| 2394 | * We will parse the partial (or complete) lines. |
| 2395 | * We will check the request syntax, and also join multi-line |
| 2396 | * headers. An index of all the lines will be elaborated while |
| 2397 | * parsing. |
| 2398 | * |
| 2399 | * For the parsing, we use a 28 states FSM. |
| 2400 | * |
| 2401 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2402 | * req->buf->p = beginning of request |
| 2403 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2404 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2405 | * msg->eol = end of current header or line (LF or CRLF) |
| 2406 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2407 | * |
| 2408 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 2409 | * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2410 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2411 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2412 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2413 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2414 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2415 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2416 | struct http_txn *txn = &s->txn; |
| 2417 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2418 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2419 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2420 | DPRINTF(stderr,"[%u] %s: session=%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] | 2421 | now_ms, __FUNCTION__, |
| 2422 | s, |
| 2423 | req, |
| 2424 | req->rex, req->wex, |
| 2425 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2426 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2427 | req->analysers); |
| 2428 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2429 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2430 | s->srv_error = http_return_srv_error; |
| 2431 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2432 | /* There's a protected area at the end of the buffer for rewriting |
| 2433 | * purposes. We don't want to start to parse the request if the |
| 2434 | * protected area is affected, because we may have to move processed |
| 2435 | * data later, which is much more complicated. |
| 2436 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2437 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2438 | if (txn->flags & TX_NOT_FIRST) { |
| 2439 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2440 | 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] | 2441 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2442 | /* 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] | 2443 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2444 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2445 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2446 | return 0; |
| 2447 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2448 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2449 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2450 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2453 | /* Note that we have the same problem with the response ; we |
| 2454 | * may want to send a redirect, error or anything which requires |
| 2455 | * some spare space. So we'll ensure that we have at least |
| 2456 | * maxrewrite bytes available in the response buffer before |
| 2457 | * processing that one. This will only affect pipelined |
| 2458 | * keep-alive requests. |
| 2459 | */ |
| 2460 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2461 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2462 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2463 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2464 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2465 | if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2466 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2467 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2468 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2469 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2470 | s->rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2471 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2472 | return 0; |
| 2473 | } |
| 2474 | } |
| 2475 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2476 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2477 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2480 | /* 1: we might have to print this header in debug mode */ |
| 2481 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2482 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2483 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2484 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2485 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2486 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2487 | /* this is a bit complex : in case of error on the request line, |
| 2488 | * we know that rq.l is still zero, so we display only the part |
| 2489 | * up to the end of the line (truncated by debug_hdr). |
| 2490 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2491 | 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] | 2492 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2493 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2494 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2495 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2496 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2497 | while (cur_idx) { |
| 2498 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2499 | debug_hdr("clihdr", s, sol, eol); |
| 2500 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2501 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2502 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2503 | } |
| 2504 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2505 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2506 | /* |
| 2507 | * Now we quickly check if we have found a full valid request. |
| 2508 | * If not so, we check the FD and buffer states before leaving. |
| 2509 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2510 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2511 | * requests are checked first. When waiting for a second request |
| 2512 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2513 | * we note the error in the session flags but don't set any state. |
| 2514 | * Since the error will be noted there, it will not be counted by |
| 2515 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2516 | * Last, we may increase some tracked counters' http request errors on |
| 2517 | * the cases that are deliberately the client's fault. For instance, |
| 2518 | * a timeout or connection reset is not counted as an error. However |
| 2519 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2520 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2521 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2522 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2523 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2524 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2525 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2526 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2527 | session_inc_http_req_ctr(s); |
| 2528 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2529 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2530 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2531 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2532 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2533 | /* 1: Since we are in header mode, if there's no space |
| 2534 | * left for headers, we won't be able to free more |
| 2535 | * later, so the session will never terminate. We |
| 2536 | * must terminate it now. |
| 2537 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2538 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2539 | /* FIXME: check if URI is set and return Status |
| 2540 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2541 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2542 | session_inc_http_req_ctr(s); |
| 2543 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2544 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2545 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2546 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2547 | goto return_bad_req; |
| 2548 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2551 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2552 | if (!(s->flags & SN_ERR_MASK)) |
| 2553 | s->flags |= SN_ERR_CLICL; |
| 2554 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2555 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2556 | goto failed_keep_alive; |
| 2557 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2558 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2559 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2560 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2561 | session_inc_http_err_ctr(s); |
| 2562 | } |
| 2563 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2564 | txn->status = 400; |
| 2565 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2566 | msg->msg_state = HTTP_MSG_ERROR; |
| 2567 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2568 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2569 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2570 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2571 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2572 | if (s->listener->counters) |
| 2573 | s->listener->counters->failed_req++; |
| 2574 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2575 | if (!(s->flags & SN_FINST_MASK)) |
| 2576 | s->flags |= SN_FINST_R; |
| 2577 | return 0; |
| 2578 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2579 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2580 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2581 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2582 | if (!(s->flags & SN_ERR_MASK)) |
| 2583 | s->flags |= SN_ERR_CLITO; |
| 2584 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2585 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2586 | goto failed_keep_alive; |
| 2587 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2588 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2589 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2590 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2591 | session_inc_http_err_ctr(s); |
| 2592 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2593 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2594 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2595 | msg->msg_state = HTTP_MSG_ERROR; |
| 2596 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2597 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2598 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2599 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2600 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2601 | if (s->listener->counters) |
| 2602 | s->listener->counters->failed_req++; |
| 2603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | if (!(s->flags & SN_FINST_MASK)) |
| 2605 | s->flags |= SN_FINST_R; |
| 2606 | return 0; |
| 2607 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2608 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2609 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2610 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2611 | if (!(s->flags & SN_ERR_MASK)) |
| 2612 | s->flags |= SN_ERR_CLICL; |
| 2613 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2614 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2615 | goto failed_keep_alive; |
| 2616 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2617 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2618 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2619 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2620 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2621 | msg->msg_state = HTTP_MSG_ERROR; |
| 2622 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2623 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2624 | session_inc_http_err_ctr(s); |
| 2625 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2626 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2627 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2628 | if (s->listener->counters) |
| 2629 | s->listener->counters->failed_req++; |
| 2630 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2631 | if (!(s->flags & SN_FINST_MASK)) |
| 2632 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2633 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2634 | } |
| 2635 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2636 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2637 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2638 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2639 | #ifdef TCP_QUICKACK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 2640 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2641 | /* We need more data, we have to re-enable quick-ack in case we |
| 2642 | * previously disabled it, otherwise we might cause the client |
| 2643 | * to delay next data. |
| 2644 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2645 | setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2646 | } |
| 2647 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2648 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2649 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2650 | /* If the client starts to talk, let's fall back to |
| 2651 | * request timeout processing. |
| 2652 | */ |
| 2653 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2654 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2657 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2658 | if (!tick_isset(req->analyse_exp)) { |
| 2659 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2660 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2661 | tick_isset(s->be->timeout.httpka)) |
| 2662 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2663 | else |
| 2664 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2665 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2666 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2667 | /* we're not ready yet */ |
| 2668 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2669 | |
| 2670 | failed_keep_alive: |
| 2671 | /* Here we process low-level errors for keep-alive requests. In |
| 2672 | * short, if the request is not the first one and it experiences |
| 2673 | * a timeout, read error or shutdown, we just silently close so |
| 2674 | * that the client can try again. |
| 2675 | */ |
| 2676 | txn->status = 0; |
| 2677 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2678 | req->analysers = 0; |
| 2679 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2680 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2681 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2682 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2683 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2684 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2685 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2686 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2687 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2688 | * 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] | 2689 | * 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] | 2690 | * 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] | 2691 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2692 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2693 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2694 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2695 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2696 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2697 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2698 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2699 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2700 | /* kill the pending keep-alive timeout */ |
| 2701 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2702 | req->analyse_exp = TICK_ETERNITY; |
| 2703 | } |
| 2704 | |
| 2705 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2706 | /* Maybe we found in invalid header name while we were configured not |
| 2707 | * to block on that, so we have to capture it now. |
| 2708 | */ |
| 2709 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2710 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2711 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2712 | /* |
| 2713 | * 1: identify the method |
| 2714 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2715 | 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] | 2716 | |
| 2717 | /* we can make use of server redirect on GET and HEAD */ |
| 2718 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2719 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2720 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2721 | /* |
| 2722 | * 2: check if the URI matches the monitor_uri. |
| 2723 | * We have to do this for every request which gets in, because |
| 2724 | * the monitor-uri is defined by the frontend. |
| 2725 | */ |
| 2726 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2727 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2728 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2729 | s->fe->monitor_uri, |
| 2730 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2731 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2733 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2734 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2735 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2736 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2737 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2738 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2739 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2740 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2741 | int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2742 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2743 | ret = acl_pass(ret); |
| 2744 | if (cond->pol == ACL_COND_UNLESS) |
| 2745 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2746 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2747 | if (ret) { |
| 2748 | /* we fail this request, let's return 503 service unavail */ |
| 2749 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2750 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2751 | if (!(s->flags & SN_ERR_MASK)) |
| 2752 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2753 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2754 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2755 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2756 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2757 | /* nothing to fail, let's reply normaly */ |
| 2758 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2759 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2760 | if (!(s->flags & SN_ERR_MASK)) |
| 2761 | s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2762 | goto return_prx_cond; |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2767 | * Note: we cannot log anymore if the request has been |
| 2768 | * classified as invalid. |
| 2769 | */ |
| 2770 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2771 | /* we have a complete HTTP request that we must log */ |
| 2772 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2773 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2774 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2775 | if (urilen >= REQURI_LEN) |
| 2776 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2777 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2778 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2779 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2780 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2781 | s->do_log(s); |
| 2782 | } else { |
| 2783 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2784 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2785 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2786 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2787 | /* 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] | 2788 | 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] | 2789 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2790 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2791 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2792 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2793 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2794 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2795 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2796 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2797 | |
| 2798 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2799 | 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] | 2800 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2801 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2802 | * we have what looks like a proxied connection instead of a connection, |
| 2803 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2804 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2805 | * happen to do that despite being non-standard :-( |
| 2806 | * We consider that a request not beginning with either '/' or '*' is |
| 2807 | * a proxied connection, which covers both "scheme://location" and |
| 2808 | * CONNECT ip:port. |
| 2809 | */ |
| 2810 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2811 | 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] | 2812 | txn->flags |= TX_USE_PX_CONN; |
| 2813 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2814 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2815 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2816 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2817 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2818 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2819 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2820 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2821 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2822 | /* 6: determine the transfer-length. |
| 2823 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2824 | * the presence of a message-body in a REQUEST and its transfer length |
| 2825 | * must be determined that way (in order of precedence) : |
| 2826 | * 1. The presence of a message-body in a request is signaled by the |
| 2827 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2828 | * in the request's header fields. When a request message contains |
| 2829 | * both a message-body of non-zero length and a method that does |
| 2830 | * not define any semantics for that request message-body, then an |
| 2831 | * origin server SHOULD either ignore the message-body or respond |
| 2832 | * with an appropriate error message (e.g., 413). A proxy or |
| 2833 | * gateway, when presented the same request, SHOULD either forward |
| 2834 | * the request inbound with the message- body or ignore the |
| 2835 | * message-body when determining a response. |
| 2836 | * |
| 2837 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2838 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2839 | * transfer-length is defined by the use of this transfer-coding. |
| 2840 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2841 | * transfer-coding is not present, the transfer-length is defined |
| 2842 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2843 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2844 | * 3. If a Content-Length header field is present, its decimal value in |
| 2845 | * OCTETs represents both the entity-length and the transfer-length. |
| 2846 | * If a message is received with both a Transfer-Encoding header |
| 2847 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2848 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2849 | * 4. By the server closing the connection. (Closing the connection |
| 2850 | * cannot be used to indicate the end of a request body, since that |
| 2851 | * would leave no possibility for the server to send back a response.) |
| 2852 | * |
| 2853 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2854 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2855 | * it is terminated by closing the connection. When the "chunked" |
| 2856 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2857 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2858 | */ |
| 2859 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2860 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2861 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2862 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2863 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2864 | 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] | 2865 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2866 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2867 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2868 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2869 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2870 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2871 | break; |
| 2872 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2873 | } |
| 2874 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2875 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2876 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2877 | 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] | 2878 | signed long long cl; |
| 2879 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2880 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2881 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2882 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2883 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2884 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2885 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2886 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2887 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2888 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2889 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2890 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2891 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2892 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2893 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2894 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2895 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2896 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2897 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2898 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2899 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2900 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2901 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2904 | /* bodyless requests have a known length */ |
| 2905 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2906 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2907 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2908 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2909 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2910 | req->analyse_exp = TICK_ETERNITY; |
| 2911 | return 1; |
| 2912 | |
| 2913 | return_bad_req: |
| 2914 | /* We centralize bad requests processing here */ |
| 2915 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2916 | /* we detected a parsing error. We want to archive this request |
| 2917 | * in the dedicated proxy area for later troubleshooting. |
| 2918 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2919 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2923 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2924 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2925 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2926 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2927 | if (s->listener->counters) |
| 2928 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2929 | |
| 2930 | return_prx_cond: |
| 2931 | if (!(s->flags & SN_ERR_MASK)) |
| 2932 | s->flags |= SN_ERR_PRXCOND; |
| 2933 | if (!(s->flags & SN_FINST_MASK)) |
| 2934 | s->flags |= SN_FINST_R; |
| 2935 | |
| 2936 | req->analysers = 0; |
| 2937 | req->analyse_exp = TICK_ETERNITY; |
| 2938 | return 0; |
| 2939 | } |
| 2940 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2941 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2942 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2943 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2944 | * and program a response message if something was unexpected. It cannot fail |
| 2945 | * 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] | 2946 | * 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] | 2947 | * s->target which is supposed to already point to the stats applet. The caller |
| 2948 | * is expected to have already assigned an appctx to the session. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2949 | */ |
| 2950 | int http_handle_stats(struct session *s, struct channel *req) |
| 2951 | { |
| 2952 | struct stats_admin_rule *stats_admin_rule; |
| 2953 | struct stream_interface *si = s->rep->prod; |
| 2954 | struct http_txn *txn = &s->txn; |
| 2955 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2956 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2957 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2958 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2959 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2960 | appctx = si_appctx(si); |
| 2961 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2962 | appctx->st1 = appctx->st2 = 0; |
| 2963 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2964 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 2965 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD)) |
| 2966 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2967 | |
| 2968 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2969 | lookup = uri + uri_auth->uri_len; |
| 2970 | |
| 2971 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2972 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2973 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2974 | break; |
| 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | if (uri_auth->refresh) { |
| 2979 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2980 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2981 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2982 | break; |
| 2983 | } |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2988 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2989 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2990 | break; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2995 | if (memcmp(h, ";st=", 4) == 0) { |
| 2996 | int i; |
| 2997 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2998 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2999 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3000 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3001 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3002 | break; |
| 3003 | } |
| 3004 | } |
| 3005 | break; |
| 3006 | } |
| 3007 | } |
| 3008 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3009 | appctx->ctx.stats.scope_str = 0; |
| 3010 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3011 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3012 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3013 | int itx = 0; |
| 3014 | const char *h2; |
| 3015 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3016 | const char *err; |
| 3017 | |
| 3018 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3019 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3020 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3021 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3022 | itx++; |
| 3023 | h++; |
| 3024 | } |
| 3025 | |
| 3026 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3027 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3028 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3029 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3030 | /* 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] | 3031 | memcpy(scope_txt, h2, itx); |
| 3032 | scope_txt[itx] = '\0'; |
| 3033 | err = invalid_char(scope_txt); |
| 3034 | if (err) { |
| 3035 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3036 | appctx->ctx.stats.scope_str = 0; |
| 3037 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3038 | } |
| 3039 | break; |
| 3040 | } |
| 3041 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3042 | |
| 3043 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3044 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3045 | int ret = 1; |
| 3046 | |
| 3047 | if (stats_admin_rule->cond) { |
| 3048 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3049 | ret = acl_pass(ret); |
| 3050 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3051 | ret = !ret; |
| 3052 | } |
| 3053 | |
| 3054 | if (ret) { |
| 3055 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3056 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3057 | break; |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3062 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3063 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3064 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3065 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3066 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3067 | */ |
| 3068 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3069 | struct hdr_ctx ctx; |
| 3070 | ctx.idx = 0; |
| 3071 | /* Expect is allowed in 1.1, look for it */ |
| 3072 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 3073 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3074 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3075 | } |
| 3076 | } |
| 3077 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3078 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3079 | } |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3080 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3081 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3082 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3083 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3084 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3085 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3086 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3087 | else { |
| 3088 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3089 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3090 | } |
| 3091 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3092 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3093 | return 1; |
| 3094 | } |
| 3095 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3096 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3097 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3098 | */ |
| 3099 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3100 | { |
| 3101 | #ifdef IP_TOS |
| 3102 | if (from.ss_family == AF_INET) |
| 3103 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3104 | #endif |
| 3105 | #ifdef IPV6_TCLASS |
| 3106 | if (from.ss_family == AF_INET6) { |
| 3107 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3108 | /* v4-mapped addresses need IP_TOS */ |
| 3109 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3110 | else |
| 3111 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3112 | } |
| 3113 | #endif |
| 3114 | } |
| 3115 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3116 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3117 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3118 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3119 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3120 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3121 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3122 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3123 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3124 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3125 | struct connection *cli_conn; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3126 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3127 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3128 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3129 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3130 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3131 | continue; |
| 3132 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3133 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3134 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3135 | int ret; |
| 3136 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3137 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3138 | ret = acl_pass(ret); |
| 3139 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3140 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3141 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3142 | |
| 3143 | if (!ret) /* condition not matched */ |
| 3144 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3147 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3148 | switch (rule->action) { |
| 3149 | case HTTP_REQ_ACT_ALLOW: |
| 3150 | return NULL; /* "allow" rules are OK */ |
| 3151 | |
| 3152 | case HTTP_REQ_ACT_DENY: |
| 3153 | txn->flags |= TX_CLDENY; |
| 3154 | return rule; |
| 3155 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3156 | case HTTP_REQ_ACT_TARPIT: |
| 3157 | txn->flags |= TX_CLTARPIT; |
| 3158 | return rule; |
| 3159 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3160 | case HTTP_REQ_ACT_AUTH: |
| 3161 | return rule; |
| 3162 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3163 | case HTTP_REQ_ACT_REDIR: |
| 3164 | return rule; |
| 3165 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3166 | case HTTP_REQ_ACT_SET_NICE: |
| 3167 | s->task->nice = rule->arg.nice; |
| 3168 | break; |
| 3169 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3170 | case HTTP_REQ_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3171 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3172 | 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] | 3173 | break; |
| 3174 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3175 | case HTTP_REQ_ACT_SET_MARK: |
| 3176 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3177 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3178 | 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] | 3179 | #endif |
| 3180 | break; |
| 3181 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3182 | case HTTP_REQ_ACT_SET_LOGL: |
| 3183 | s->logs.level = rule->arg.loglevel; |
| 3184 | break; |
| 3185 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3186 | case HTTP_REQ_ACT_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3187 | case HTTP_REQ_ACT_SET_HDR: |
| 3188 | ctx.idx = 0; |
| 3189 | /* remove all occurrences of the header */ |
| 3190 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3191 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3192 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3193 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3194 | if (rule->action == HTTP_REQ_ACT_DEL_HDR) |
| 3195 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3196 | /* now fall through to header addition */ |
| 3197 | |
| 3198 | case HTTP_REQ_ACT_ADD_HDR: |
| 3199 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3200 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3201 | trash.len = rule->arg.hdr_add.name_len; |
| 3202 | trash.str[trash.len++] = ':'; |
| 3203 | trash.str[trash.len++] = ' '; |
| 3204 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3205 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3206 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3207 | } |
| 3208 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3209 | |
| 3210 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3211 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3212 | } |
| 3213 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3214 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3215 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3216 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3217 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3218 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3219 | * rule. |
| 3220 | */ |
| 3221 | static struct http_res_rule * |
| 3222 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3223 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3224 | struct connection *cli_conn; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3225 | struct http_res_rule *rule; |
| 3226 | struct hdr_ctx ctx; |
| 3227 | |
| 3228 | list_for_each_entry(rule, rules, list) { |
| 3229 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3230 | continue; |
| 3231 | |
| 3232 | /* check optional condition */ |
| 3233 | if (rule->cond) { |
| 3234 | int ret; |
| 3235 | |
| 3236 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3237 | ret = acl_pass(ret); |
| 3238 | |
| 3239 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3240 | ret = !ret; |
| 3241 | |
| 3242 | if (!ret) /* condition not matched */ |
| 3243 | continue; |
| 3244 | } |
| 3245 | |
| 3246 | |
| 3247 | switch (rule->action) { |
| 3248 | case HTTP_RES_ACT_ALLOW: |
| 3249 | return NULL; /* "allow" rules are OK */ |
| 3250 | |
| 3251 | case HTTP_RES_ACT_DENY: |
| 3252 | txn->flags |= TX_SVDENY; |
| 3253 | return rule; |
| 3254 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3255 | case HTTP_RES_ACT_SET_NICE: |
| 3256 | s->task->nice = rule->arg.nice; |
| 3257 | break; |
| 3258 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3259 | case HTTP_RES_ACT_SET_TOS: |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3260 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3261 | 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] | 3262 | break; |
| 3263 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3264 | case HTTP_RES_ACT_SET_MARK: |
| 3265 | #ifdef SO_MARK |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 3266 | if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3267 | 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] | 3268 | #endif |
| 3269 | break; |
| 3270 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3271 | case HTTP_RES_ACT_SET_LOGL: |
| 3272 | s->logs.level = rule->arg.loglevel; |
| 3273 | break; |
| 3274 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3275 | case HTTP_RES_ACT_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3276 | case HTTP_RES_ACT_SET_HDR: |
| 3277 | ctx.idx = 0; |
| 3278 | /* remove all occurrences of the header */ |
| 3279 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3280 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3281 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3282 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 3283 | if (rule->action == HTTP_RES_ACT_DEL_HDR) |
| 3284 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3285 | /* now fall through to header addition */ |
| 3286 | |
| 3287 | case HTTP_RES_ACT_ADD_HDR: |
| 3288 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3289 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3290 | trash.len = rule->arg.hdr_add.name_len; |
| 3291 | trash.str[trash.len++] = ':'; |
| 3292 | trash.str[trash.len++] = ' '; |
| 3293 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3294 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3295 | break; |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | /* we reached the end of the rules, nothing to report */ |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | |
| 3303 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3304 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3305 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3306 | * as too large a request to build a valid response. |
| 3307 | */ |
| 3308 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3309 | { |
| 3310 | struct http_msg *msg = &txn->req; |
| 3311 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3312 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* build redirect message */ |
| 3315 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3316 | case 308: |
| 3317 | msg_fmt = HTTP_308; |
| 3318 | break; |
| 3319 | case 307: |
| 3320 | msg_fmt = HTTP_307; |
| 3321 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3322 | case 303: |
| 3323 | msg_fmt = HTTP_303; |
| 3324 | break; |
| 3325 | case 301: |
| 3326 | msg_fmt = HTTP_301; |
| 3327 | break; |
| 3328 | case 302: |
| 3329 | default: |
| 3330 | msg_fmt = HTTP_302; |
| 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3335 | return 0; |
| 3336 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3337 | location = trash.str + trash.len; |
| 3338 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3339 | switch(rule->type) { |
| 3340 | case REDIRECT_TYPE_SCHEME: { |
| 3341 | const char *path; |
| 3342 | const char *host; |
| 3343 | struct hdr_ctx ctx; |
| 3344 | int pathlen; |
| 3345 | int hostlen; |
| 3346 | |
| 3347 | host = ""; |
| 3348 | hostlen = 0; |
| 3349 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3350 | 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] | 3351 | host = ctx.line + ctx.val; |
| 3352 | hostlen = ctx.vlen; |
| 3353 | } |
| 3354 | |
| 3355 | path = http_get_path(txn); |
| 3356 | /* build message using path */ |
| 3357 | if (path) { |
| 3358 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3359 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3360 | int qs = 0; |
| 3361 | while (qs < pathlen) { |
| 3362 | if (path[qs] == '?') { |
| 3363 | pathlen = qs; |
| 3364 | break; |
| 3365 | } |
| 3366 | qs++; |
| 3367 | } |
| 3368 | } |
| 3369 | } else { |
| 3370 | path = "/"; |
| 3371 | pathlen = 1; |
| 3372 | } |
| 3373 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3374 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3375 | /* check if we can add scheme + "://" + host + path */ |
| 3376 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3377 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3378 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3379 | /* add scheme */ |
| 3380 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3381 | trash.len += rule->rdr_len; |
| 3382 | } |
| 3383 | else { |
| 3384 | /* add scheme with executing log format */ |
| 3385 | 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] | 3386 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3387 | /* check if we can add scheme + "://" + host + path */ |
| 3388 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 3389 | return 0; |
| 3390 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3391 | /* add "://" */ |
| 3392 | memcpy(trash.str + trash.len, "://", 3); |
| 3393 | trash.len += 3; |
| 3394 | |
| 3395 | /* add host */ |
| 3396 | memcpy(trash.str + trash.len, host, hostlen); |
| 3397 | trash.len += hostlen; |
| 3398 | |
| 3399 | /* add path */ |
| 3400 | memcpy(trash.str + trash.len, path, pathlen); |
| 3401 | trash.len += pathlen; |
| 3402 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3403 | /* 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] | 3404 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3405 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3406 | if (trash.len > trash.size - 5) |
| 3407 | return 0; |
| 3408 | trash.str[trash.len] = '/'; |
| 3409 | trash.len++; |
| 3410 | } |
| 3411 | |
| 3412 | break; |
| 3413 | } |
| 3414 | case REDIRECT_TYPE_PREFIX: { |
| 3415 | const char *path; |
| 3416 | int pathlen; |
| 3417 | |
| 3418 | path = http_get_path(txn); |
| 3419 | /* build message using path */ |
| 3420 | if (path) { |
| 3421 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3422 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3423 | int qs = 0; |
| 3424 | while (qs < pathlen) { |
| 3425 | if (path[qs] == '?') { |
| 3426 | pathlen = qs; |
| 3427 | break; |
| 3428 | } |
| 3429 | qs++; |
| 3430 | } |
| 3431 | } |
| 3432 | } else { |
| 3433 | path = "/"; |
| 3434 | pathlen = 1; |
| 3435 | } |
| 3436 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3437 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3438 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3439 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3440 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3441 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3442 | * add anything, otherwise it makes it hard for the user to |
| 3443 | * configure a self-redirection. |
| 3444 | */ |
| 3445 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3446 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3447 | trash.len += rule->rdr_len; |
| 3448 | } |
| 3449 | } |
| 3450 | else { |
| 3451 | /* add prefix with executing log format */ |
| 3452 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 3453 | |
| 3454 | /* Check length */ |
| 3455 | if (trash.len + pathlen > trash.size - 4) |
| 3456 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | /* add path */ |
| 3460 | memcpy(trash.str + trash.len, path, pathlen); |
| 3461 | trash.len += pathlen; |
| 3462 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3463 | /* 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] | 3464 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3465 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3466 | if (trash.len > trash.size - 5) |
| 3467 | return 0; |
| 3468 | trash.str[trash.len] = '/'; |
| 3469 | trash.len++; |
| 3470 | } |
| 3471 | |
| 3472 | break; |
| 3473 | } |
| 3474 | case REDIRECT_TYPE_LOCATION: |
| 3475 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3476 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3477 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3478 | return 0; |
| 3479 | |
| 3480 | /* add location */ |
| 3481 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3482 | trash.len += rule->rdr_len; |
| 3483 | } |
| 3484 | else { |
| 3485 | /* add location with executing log format */ |
| 3486 | 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] | 3487 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3488 | /* Check left length */ |
| 3489 | if (trash.len > trash.size - 4) |
| 3490 | return 0; |
| 3491 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3492 | break; |
| 3493 | } |
| 3494 | |
| 3495 | if (rule->cookie_len) { |
| 3496 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3497 | trash.len += 14; |
| 3498 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3499 | trash.len += rule->cookie_len; |
| 3500 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3501 | trash.len += 2; |
| 3502 | } |
| 3503 | |
| 3504 | /* add end of headers and the keep-alive/close status. |
| 3505 | * We may choose to set keep-alive if the Location begins |
| 3506 | * with a slash, because the client will come back to the |
| 3507 | * same server. |
| 3508 | */ |
| 3509 | txn->status = rule->code; |
| 3510 | /* let's log the request time */ |
| 3511 | s->logs.tv_request = now; |
| 3512 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3513 | if (*location == '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3514 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3515 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3516 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3517 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3518 | /* keep-alive possible */ |
| 3519 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3520 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3521 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3522 | trash.len += 30; |
| 3523 | } else { |
| 3524 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3525 | trash.len += 24; |
| 3526 | } |
| 3527 | } |
| 3528 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3529 | trash.len += 4; |
| 3530 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3531 | /* "eat" the request */ |
| 3532 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
Willy Tarreau | 6d8bac7 | 2014-04-25 12:19:32 +0200 | [diff] [blame] | 3533 | msg->next -= msg->sov; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3534 | msg->sov = 0; |
| 3535 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3536 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3537 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3538 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3539 | } else { |
| 3540 | /* keep-alive not possible */ |
| 3541 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3542 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3543 | trash.len += 29; |
| 3544 | } else { |
| 3545 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3546 | trash.len += 23; |
| 3547 | } |
| 3548 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3549 | txn->req.chn->analysers = 0; |
| 3550 | } |
| 3551 | |
| 3552 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3553 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3554 | if (!(s->flags & SN_FINST_MASK)) |
| 3555 | s->flags |= SN_FINST_R; |
| 3556 | |
| 3557 | return 1; |
| 3558 | } |
| 3559 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3560 | /* This stream analyser runs all HTTP request processing which is common to |
| 3561 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3562 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3563 | * 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] | 3564 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3565 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3566 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3567 | int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3568 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3569 | struct http_txn *txn = &s->txn; |
| 3570 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3571 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3572 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3573 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3574 | struct cond_wordlist *wl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3575 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3576 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3577 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3578 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3579 | return 0; |
| 3580 | } |
| 3581 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3582 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3583 | req->analyse_exp = TICK_ETERNITY; |
| 3584 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3585 | DPRINTF(stderr,"[%u] %s: session=%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] | 3586 | now_ms, __FUNCTION__, |
| 3587 | s, |
| 3588 | req, |
| 3589 | req->rex, req->wex, |
| 3590 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3591 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3592 | req->analysers); |
| 3593 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3594 | /* first check whether we have some ACLs set to block this request */ |
| 3595 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3596 | int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3597 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3598 | ret = acl_pass(ret); |
| 3599 | if (cond->pol == ACL_COND_UNLESS) |
| 3600 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3601 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3602 | if (ret) { |
| 3603 | txn->status = 403; |
| 3604 | /* let's log the request time */ |
| 3605 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3606 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3607 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3608 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3609 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3610 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3611 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3612 | /* just in case we have some per-backend tracking */ |
| 3613 | session_inc_be_http_req_ctr(s); |
| 3614 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3615 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3616 | http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3617 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3618 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3619 | if (!http_req_last_rule) { |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3620 | if (stats_check_uri(s->rep->prod, txn, px)) { |
| 3621 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3622 | if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) { |
| 3623 | txn->status = 500; |
| 3624 | s->logs.tv_request = now; |
| 3625 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3626 | |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 3627 | if (!(s->flags & SN_ERR_MASK)) |
| 3628 | s->flags |= SN_ERR_RESOURCE; |
| 3629 | goto return_prx_cond; |
| 3630 | } |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3631 | /* parse the whole stats request and extract the relevant information */ |
| 3632 | http_handle_stats(s, req); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3633 | http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3634 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3635 | } |
| 3636 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3637 | /* only apply req{,i}{rep/deny/tarpit} if the request was not yet |
| 3638 | * blocked by an http-request rule. |
| 3639 | */ |
| 3640 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3641 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3642 | goto return_bad_req; |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3643 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3644 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3645 | /* return a 403 if either rule has blocked */ |
| 3646 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3647 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3648 | txn->status = 403; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3649 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3650 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3651 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3652 | s->fe->fe_counters.denied_req++; |
| 3653 | if (s->fe != s->be) |
| 3654 | s->be->be_counters.denied_req++; |
| 3655 | if (s->listener->counters) |
| 3656 | s->listener->counters->denied_req++; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3657 | goto return_prx_cond; |
| 3658 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3659 | |
| 3660 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3661 | * which may be the same as the connect timeout if unspecified. |
| 3662 | * If unset, then set it to zero because we really want it to |
| 3663 | * eventually expire. We build the tarpit as an analyser. |
| 3664 | */ |
| 3665 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3666 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3667 | /* wipe the request out so that we can drop the connection early |
| 3668 | * if the client closes first. |
| 3669 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3670 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3671 | req->analysers = 0; /* remove switching rules etc... */ |
| 3672 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3673 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3674 | if (!req->analyse_exp) |
| 3675 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3676 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3677 | s->fe->fe_counters.denied_req++; |
| 3678 | if (s->fe != s->be) |
| 3679 | s->be->be_counters.denied_req++; |
| 3680 | if (s->listener->counters) |
| 3681 | s->listener->counters->denied_req++; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3682 | return 1; |
| 3683 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3684 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3685 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3686 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3687 | * only change if both the request and the config reference something else. |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3688 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3689 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3690 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3691 | * if FE and BE have the same settings (common). The method consists in |
| 3692 | * checking if options changed between the two calls (implying that either |
| 3693 | * one is non-null, or one of them is non-null and we are there for the first |
| 3694 | * time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3695 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3696 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3697 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3698 | ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) { |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3699 | int tmp = TX_CON_WANT_KAL; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3700 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3701 | if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 3702 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 3703 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 3704 | tmp = TX_CON_WANT_TUN; |
| 3705 | |
| 3706 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3707 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3708 | tmp = TX_CON_WANT_TUN; |
| 3709 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3710 | |
| 3711 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 3712 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 3713 | /* option httpclose + server_close => forceclose */ |
| 3714 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 3715 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 3716 | tmp = TX_CON_WANT_CLO; |
| 3717 | else |
| 3718 | tmp = TX_CON_WANT_SCL; |
| 3719 | } |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3720 | |
| 3721 | if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
| 3722 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3723 | tmp = TX_CON_WANT_CLO; |
| 3724 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3725 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3726 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3727 | |
Willy Tarreau | 416ce61 | 2014-01-31 15:45:34 +0100 | [diff] [blame] | 3728 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 3729 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3730 | /* parse the Connection header and possibly clean it */ |
| 3731 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3732 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3733 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3734 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3735 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3736 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3737 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3738 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3739 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3740 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3741 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3742 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3743 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3744 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3745 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3746 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3747 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3748 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3749 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3750 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3751 | /* we can be blocked here because the request needs to be authenticated, |
| 3752 | * either to pass or to access stats. |
| 3753 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3754 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 3755 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3756 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3757 | if (!realm) |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3758 | realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3759 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3760 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3761 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3762 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3763 | /* on 401 we still count one error, because normal browsing |
| 3764 | * won't significantly increase the counter but brute force |
| 3765 | * attempts will. |
| 3766 | */ |
| 3767 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3768 | goto return_prx_cond; |
| 3769 | } |
| 3770 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3771 | /* add request headers from the rule sets in the same order */ |
| 3772 | list_for_each_entry(wl, &px->req_add, list) { |
| 3773 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3774 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3775 | ret = acl_pass(ret); |
| 3776 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3777 | ret = !ret; |
| 3778 | if (!ret) |
| 3779 | continue; |
| 3780 | } |
| 3781 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3782 | 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] | 3783 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3787 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3788 | goto return_bad_req; |
| 3789 | req->analyse_exp = TICK_ETERNITY; |
| 3790 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3791 | } |
| 3792 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3793 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3794 | /* process the stats request now */ |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3795 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3796 | s->fe->fe_counters.intercepted_req++; |
| 3797 | |
| 3798 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3799 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
| 3800 | if (!(s->flags & SN_FINST_MASK)) |
| 3801 | s->flags |= SN_FINST_R; |
| 3802 | |
| 3803 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3804 | |
| 3805 | /* we may want to compress the stats page */ |
| 3806 | if (s->fe->comp || s->be->comp) |
| 3807 | select_compression_request_header(s, req->buf); |
| 3808 | |
| 3809 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 3810 | req->analysers = AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3811 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3812 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3813 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3814 | /* check whether we have some ACLs set to redirect this request */ |
| 3815 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3816 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3817 | int ret; |
| 3818 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3819 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3820 | ret = acl_pass(ret); |
| 3821 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3822 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3823 | if (!ret) |
| 3824 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3825 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3826 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3827 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3828 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3829 | req->analyse_exp = TICK_ETERNITY; |
| 3830 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3831 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3832 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3833 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3834 | * If this happens, then the data will not come immediately, so we must |
| 3835 | * send all what we have without waiting. Note that due to the small gain |
| 3836 | * 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] | 3837 | * 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] | 3838 | * itself once used. |
| 3839 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3840 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3841 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3842 | /* that's OK for us now, let's move on to next analysers */ |
| 3843 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3844 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3845 | return_bad_req: |
| 3846 | /* We centralize bad requests processing here */ |
| 3847 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3848 | /* we detected a parsing error. We want to archive this request |
| 3849 | * in the dedicated proxy area for later troubleshooting. |
| 3850 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3851 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3852 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3853 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3854 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3855 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3856 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3857 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3858 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3859 | if (s->listener->counters) |
| 3860 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3861 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3862 | return_prx_cond: |
| 3863 | if (!(s->flags & SN_ERR_MASK)) |
| 3864 | s->flags |= SN_ERR_PRXCOND; |
| 3865 | if (!(s->flags & SN_FINST_MASK)) |
| 3866 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3867 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3868 | req->analysers = 0; |
| 3869 | req->analyse_exp = TICK_ETERNITY; |
| 3870 | return 0; |
| 3871 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3872 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3873 | /* This function performs all the processing enabled for the current request. |
| 3874 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3875 | * needs more data, encounters an error, or wants to immediately abort the |
| 3876 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3877 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3878 | int http_process_request(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3879 | { |
| 3880 | struct http_txn *txn = &s->txn; |
| 3881 | struct http_msg *msg = &txn->req; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3882 | struct connection *cli_conn = objt_conn(req->prod->end); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3883 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3884 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3885 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3886 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3887 | return 0; |
| 3888 | } |
| 3889 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3890 | DPRINTF(stderr,"[%u] %s: session=%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] | 3891 | now_ms, __FUNCTION__, |
| 3892 | s, |
| 3893 | req, |
| 3894 | req->rex, req->wex, |
| 3895 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3896 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3897 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3898 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3899 | if (s->fe->comp || s->be->comp) |
| 3900 | select_compression_request_header(s, req->buf); |
| 3901 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3902 | /* |
| 3903 | * Right now, we know that we have processed the entire headers |
| 3904 | * and that unwanted requests have been filtered out. We can do |
| 3905 | * whatever we want with the remaining request. Also, now we |
| 3906 | * may have separate values for ->fe, ->be. |
| 3907 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3908 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3909 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3910 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3911 | * incoming request. Note that this requires that a connection is |
| 3912 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3913 | */ |
| 3914 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3915 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3916 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3917 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3918 | /* Note that for now we don't reuse existing proxy connections */ |
| 3919 | if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3920 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3921 | txn->status = 500; |
| 3922 | req->analysers = 0; |
| 3923 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
| 3924 | |
| 3925 | if (!(s->flags & SN_ERR_MASK)) |
| 3926 | s->flags |= SN_ERR_RESOURCE; |
| 3927 | if (!(s->flags & SN_FINST_MASK)) |
| 3928 | s->flags |= SN_FINST_R; |
| 3929 | |
| 3930 | return 0; |
| 3931 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3932 | |
| 3933 | path = http_get_path(txn); |
| 3934 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3935 | 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] | 3936 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3937 | /* if the path was found, we have to remove everything between |
| 3938 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3939 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3940 | * u_l characters by a single "/". |
| 3941 | */ |
| 3942 | if (path) { |
| 3943 | char *cur_ptr = req->buf->p; |
| 3944 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3945 | int delta; |
| 3946 | |
| 3947 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3948 | http_msg_move_end(&txn->req, delta); |
| 3949 | cur_end += delta; |
| 3950 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3951 | goto return_bad_req; |
| 3952 | } |
| 3953 | else { |
| 3954 | char *cur_ptr = req->buf->p; |
| 3955 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3956 | int delta; |
| 3957 | |
| 3958 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3959 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3960 | http_msg_move_end(&txn->req, delta); |
| 3961 | cur_end += delta; |
| 3962 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3963 | goto return_bad_req; |
| 3964 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3965 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3966 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3967 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3968 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3969 | * Note that doing so might move headers in the request, but |
| 3970 | * the fields will stay coherent and the URI will not move. |
| 3971 | * This should only be performed in the backend. |
| 3972 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3973 | if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3974 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3975 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3976 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3977 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3978 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3979 | * so let's do the same now. |
| 3980 | */ |
| 3981 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3982 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3983 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3984 | 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] | 3985 | } |
| 3986 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3987 | /* add unique-id if "header-unique-id" is specified */ |
| 3988 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3989 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 3990 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 3991 | goto return_bad_req; |
| 3992 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3993 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3994 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3995 | |
| 3996 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3997 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3998 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3999 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4000 | 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] | 4001 | goto return_bad_req; |
| 4002 | } |
| 4003 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4004 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4005 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4006 | * asks for it. |
| 4007 | */ |
| 4008 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4009 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4010 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 4011 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 4012 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4013 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4014 | /* The header is set to be added only if none is present |
| 4015 | * and we found it, so don't do anything. |
| 4016 | */ |
| 4017 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4018 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4019 | /* Add an X-Forwarded-For header unless the source IP is |
| 4020 | * in the 'except' network range. |
| 4021 | */ |
| 4022 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4023 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4024 | != s->fe->except_net.s_addr) && |
| 4025 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4026 | (((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] | 4027 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4028 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4029 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4030 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4031 | |
| 4032 | /* Note: we rely on the backend to get the header name to be used for |
| 4033 | * x-forwarded-for, because the header is really meant for the backends. |
| 4034 | * However, if the backend did not specify any option, we have to rely |
| 4035 | * on the frontend's header name. |
| 4036 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4037 | if (s->be->fwdfor_hdr_len) { |
| 4038 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4039 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4040 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4041 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4042 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4043 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4044 | 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] | 4045 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4046 | 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] | 4047 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4048 | } |
| 4049 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4050 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4051 | /* FIXME: for the sake of completeness, we should also support |
| 4052 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4053 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4054 | int len; |
| 4055 | char pn[INET6_ADDRSTRLEN]; |
| 4056 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4057 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4058 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4059 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4060 | /* Note: we rely on the backend to get the header name to be used for |
| 4061 | * x-forwarded-for, because the header is really meant for the backends. |
| 4062 | * However, if the backend did not specify any option, we have to rely |
| 4063 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4064 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4065 | if (s->be->fwdfor_hdr_len) { |
| 4066 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4067 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4068 | } else { |
| 4069 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4070 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4071 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4072 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4073 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4074 | 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] | 4075 | goto return_bad_req; |
| 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4080 | * 10: add X-Original-To if either the frontend or the backend |
| 4081 | * asks for it. |
| 4082 | */ |
| 4083 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 4084 | |
| 4085 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4086 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4087 | /* Add an X-Original-To header unless the destination IP is |
| 4088 | * in the 'except' network range. |
| 4089 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4090 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4091 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4092 | if (cli_conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4093 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4094 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4095 | != s->fe->except_to.s_addr) && |
| 4096 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4097 | (((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] | 4098 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4099 | int len; |
| 4100 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4101 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4102 | |
| 4103 | /* Note: we rely on the backend to get the header name to be used for |
| 4104 | * x-original-to, because the header is really meant for the backends. |
| 4105 | * However, if the backend did not specify any option, we have to rely |
| 4106 | * on the frontend's header name. |
| 4107 | */ |
| 4108 | if (s->be->orgto_hdr_len) { |
| 4109 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4110 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4111 | } else { |
| 4112 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4113 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4114 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4115 | 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] | 4116 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4117 | 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] | 4118 | goto return_bad_req; |
| 4119 | } |
| 4120 | } |
| 4121 | } |
| 4122 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4123 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4124 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4125 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4126 | * "Upgrade" is present in the Connection header. |
| 4127 | */ |
| 4128 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4129 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4130 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4131 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4132 | unsigned int want_flags = 0; |
| 4133 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4134 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4135 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4136 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 4137 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4138 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4139 | want_flags |= TX_CON_CLO_SET; |
| 4140 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4141 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4142 | ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
| 4143 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4144 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4145 | want_flags |= TX_CON_KAL_SET; |
| 4146 | } |
| 4147 | |
| 4148 | 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] | 4149 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4150 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4151 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4152 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4153 | /* If we have no server assigned yet and we're balancing on url_param |
| 4154 | * with a POST request, we may be interested in checking the body for |
| 4155 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4156 | */ |
| 4157 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4158 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4159 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4160 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4161 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4162 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4163 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4164 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4165 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4166 | #ifdef TCP_QUICKACK |
| 4167 | /* We expect some data from the client. Unless we know for sure |
| 4168 | * we already have a full request, we have to re-enable quick-ack |
| 4169 | * in case we previously disabled it, otherwise we might cause |
| 4170 | * the client to delay further data. |
| 4171 | */ |
| 4172 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4173 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4174 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4175 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4176 | 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] | 4177 | #endif |
| 4178 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4179 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4180 | /************************************************************* |
| 4181 | * OK, that's finished for the headers. We have done what we * |
| 4182 | * could. Let's switch to the DATA state. * |
| 4183 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4184 | req->analyse_exp = TICK_ETERNITY; |
| 4185 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4186 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4187 | /* if the server closes the connection, we want to immediately react |
| 4188 | * and close the socket to save packets and syscalls. |
| 4189 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4190 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4191 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4192 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4193 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4194 | /* OK let's go on with the BODY now */ |
| 4195 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4196 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4197 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4198 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4199 | /* we detected a parsing error. We want to archive this request |
| 4200 | * in the dedicated proxy area for later troubleshooting. |
| 4201 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4202 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4203 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4204 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4205 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4206 | txn->status = 400; |
| 4207 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4208 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4209 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4210 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4211 | if (s->listener->counters) |
| 4212 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4213 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4214 | if (!(s->flags & SN_ERR_MASK)) |
| 4215 | s->flags |= SN_ERR_PRXCOND; |
| 4216 | if (!(s->flags & SN_FINST_MASK)) |
| 4217 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4218 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4219 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4220 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4221 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4222 | * returns zero, at the beginning because it prevents any other processing |
| 4223 | * from occurring, and at the end because it terminates the request. |
| 4224 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4225 | int http_process_tarpit(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4226 | { |
| 4227 | struct http_txn *txn = &s->txn; |
| 4228 | |
| 4229 | /* This connection is being tarpitted. The CLIENT side has |
| 4230 | * already set the connect expiration date to the right |
| 4231 | * timeout. We just have to check that the client is still |
| 4232 | * there and that the timeout has not expired. |
| 4233 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4234 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4235 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4236 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4237 | return 0; |
| 4238 | |
| 4239 | /* We will set the queue timer to the time spent, just for |
| 4240 | * logging purposes. We fake a 500 server error, so that the |
| 4241 | * attacker will not suspect his connection has been tarpitted. |
| 4242 | * It will not cause trouble to the logs because we can exclude |
| 4243 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4244 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4245 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4246 | |
| 4247 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4248 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4249 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4250 | |
| 4251 | req->analysers = 0; |
| 4252 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4253 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4254 | if (!(s->flags & SN_ERR_MASK)) |
| 4255 | s->flags |= SN_ERR_PRXCOND; |
| 4256 | if (!(s->flags & SN_FINST_MASK)) |
| 4257 | s->flags |= SN_FINST_T; |
| 4258 | return 0; |
| 4259 | } |
| 4260 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4261 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4262 | * for either the buffer to be full, or the full advertised contents to have |
| 4263 | * reached the buffer. It must only be called after the standard HTTP request |
| 4264 | * processing has occurred, because it expects the request to be parsed and will |
| 4265 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4266 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4267 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4268 | * 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] | 4269 | */ |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4270 | int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4271 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4272 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4273 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4274 | |
| 4275 | /* We have to parse the HTTP request body to find any required data. |
| 4276 | * "balance url_param check_post" should have been the only way to get |
| 4277 | * into this. We were brought here after HTTP header analysis, so all |
| 4278 | * related structures are ready. |
| 4279 | */ |
| 4280 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4281 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4282 | /* This is the first call */ |
| 4283 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4284 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4285 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4286 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4287 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4288 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4289 | */ |
| 4290 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4291 | struct hdr_ctx ctx; |
| 4292 | ctx.idx = 0; |
| 4293 | /* Expect is allowed in 1.1, look for it */ |
| 4294 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4295 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 4296 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 4297 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4298 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4299 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4300 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4301 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4302 | /* 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] | 4303 | * req->buf->p still points to the beginning of the message. We |
| 4304 | * must save the body in msg->next because it survives buffer |
| 4305 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4306 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4307 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4308 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4309 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4310 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4311 | else |
| 4312 | msg->msg_state = HTTP_MSG_DATA; |
| 4313 | } |
| 4314 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4315 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4316 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 4317 | if (req->buf->i - msg->sov < msg->body_len) |
| 4318 | goto missing_data; |
| 4319 | |
| 4320 | /* OK we have everything we need now */ |
| 4321 | goto http_end; |
| 4322 | } |
| 4323 | |
| 4324 | /* OK here we're parsing a chunked-encoded message */ |
| 4325 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4326 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4327 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4328 | * 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] | 4329 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4330 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4331 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4332 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4333 | if (!ret) |
| 4334 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4335 | else if (ret < 0) { |
| 4336 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4337 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4338 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4339 | } |
| 4340 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4341 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4342 | * 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] | 4343 | * 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] | 4344 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4345 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4346 | goto http_end; |
| 4347 | |
Willy Tarreau | 226071e | 2014-04-10 11:55:45 +0200 | [diff] [blame] | 4348 | 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] | 4349 | goto http_end; |
| 4350 | |
| 4351 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4352 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4353 | * we have the maximum we can expect. |
| 4354 | */ |
| 4355 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4356 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4357 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4358 | 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] | 4359 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4360 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4361 | |
| 4362 | if (!(s->flags & SN_ERR_MASK)) |
| 4363 | s->flags |= SN_ERR_CLITO; |
| 4364 | if (!(s->flags & SN_FINST_MASK)) |
| 4365 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4366 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4367 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4368 | |
| 4369 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4370 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4371 | /* Not enough data. We'll re-use the http-request |
| 4372 | * timeout here. Ideally, we should set the timeout |
| 4373 | * relative to the accept() date. We just set the |
| 4374 | * request timeout once at the beginning of the |
| 4375 | * request. |
| 4376 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4377 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4378 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4379 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4380 | return 0; |
| 4381 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4382 | |
| 4383 | http_end: |
| 4384 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4385 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4386 | req->analysers &= ~an_bit; |
| 4387 | req->analyse_exp = TICK_ETERNITY; |
| 4388 | return 1; |
| 4389 | |
| 4390 | return_bad_req: /* let's centralize all bad requests */ |
| 4391 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4392 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4393 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4394 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4395 | if (!(s->flags & SN_ERR_MASK)) |
| 4396 | s->flags |= SN_ERR_PRXCOND; |
| 4397 | if (!(s->flags & SN_FINST_MASK)) |
| 4398 | s->flags |= SN_FINST_R; |
| 4399 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4400 | return_err_msg: |
| 4401 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4402 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4403 | if (s->listener->counters) |
| 4404 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4405 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4406 | } |
| 4407 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4408 | /* send a server's name with an outgoing request over an established connection. |
| 4409 | * Note: this function is designed to be called once the request has been scheduled |
| 4410 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4411 | * proceeding. |
| 4412 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4413 | 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] | 4414 | |
| 4415 | struct hdr_ctx ctx; |
| 4416 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4417 | char *hdr_name = be->server_id_hdr_name; |
| 4418 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4419 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4420 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4421 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4422 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4423 | ctx.idx = 0; |
| 4424 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4425 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4426 | if (old_o) { |
| 4427 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4428 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4429 | txn->req.next += old_o; |
| 4430 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4431 | } |
| 4432 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4433 | old_i = chn->buf->i; |
| 4434 | 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] | 4435 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4436 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4440 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4441 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4442 | hdr_val += hdr_name_len; |
| 4443 | *hdr_val++ = ':'; |
| 4444 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4445 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4446 | 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] | 4447 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4448 | if (old_o) { |
| 4449 | /* If this was a forwarded request, we must readjust the amount of |
| 4450 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4451 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4452 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4453 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4454 | old_o += chn->buf->i - old_i; |
| 4455 | b_adv(chn->buf, old_o); |
| 4456 | txn->req.next -= old_o; |
| 4457 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4458 | } |
| 4459 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4460 | return 0; |
| 4461 | } |
| 4462 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4463 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4464 | * right now but it works. |
| 4465 | */ |
| 4466 | void http_end_txn_clean_session(struct session *s) |
| 4467 | { |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4468 | int prev_status = s->txn.status; |
| 4469 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4470 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4471 | * server's connections. We need a safer way to reinitialize buffer |
| 4472 | * flags. We also need a more accurate method for computing per-request |
| 4473 | * data. |
| 4474 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4475 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4476 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4477 | * to the server. |
| 4478 | */ |
| 4479 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4480 | !si_conn_ready(s->req->cons)) { |
| 4481 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4482 | si_shutr(s->req->cons); |
| 4483 | si_shutw(s->req->cons); |
| 4484 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4485 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4486 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4487 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4488 | if (unlikely(s->srv_conn)) |
| 4489 | sess_change_server(s, NULL); |
| 4490 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4491 | |
| 4492 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4493 | session_process_counters(s); |
Willy Tarreau | f333834 | 2014-01-28 21:40:28 +0100 | [diff] [blame] | 4494 | session_stop_content_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4495 | |
| 4496 | if (s->txn.status) { |
| 4497 | int n; |
| 4498 | |
| 4499 | n = s->txn.status / 100; |
| 4500 | if (n < 1 || n > 5) |
| 4501 | n = 0; |
| 4502 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4503 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4504 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4505 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4506 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4507 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4508 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4509 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4510 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4511 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4512 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4513 | s->be->be_counters.p.http.comp_rsp++; |
| 4514 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4518 | s->logs.bytes_in -= s->req->buf->i; |
| 4519 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4520 | |
| 4521 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4522 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4523 | !(s->flags & SN_MONITOR) && |
| 4524 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4525 | s->do_log(s); |
| 4526 | } |
| 4527 | |
| 4528 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4529 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4530 | tv_zero(&s->logs.tv_request); |
| 4531 | s->logs.t_queue = -1; |
| 4532 | s->logs.t_connect = -1; |
| 4533 | s->logs.t_data = -1; |
| 4534 | s->logs.t_close = 0; |
| 4535 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4536 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4537 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4538 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4539 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4540 | |
| 4541 | if (s->pend_pos) |
| 4542 | pendconn_free(s->pend_pos); |
| 4543 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4544 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4545 | if (s->flags & SN_CURR_SESS) { |
| 4546 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4547 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4548 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4549 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4550 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4551 | } |
| 4552 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4553 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4554 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4555 | /* only release our endpoint if we don't intend to reuse the |
| 4556 | * connection. |
| 4557 | */ |
| 4558 | if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
| 4559 | !si_conn_ready(s->req->cons)) { |
| 4560 | si_release_endpoint(s->req->cons); |
| 4561 | } |
| 4562 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4563 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4564 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4565 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4566 | s->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | c920096 | 2013-12-31 23:03:09 +0100 | [diff] [blame] | 4567 | s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4568 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4569 | s->rep->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); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4570 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 4571 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4572 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4573 | s->txn.meth = 0; |
| 4574 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4575 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4576 | |
| 4577 | if (prev_status == 401 || prev_status == 407) { |
| 4578 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4579 | * a chance of being able to send the visitor again to the same |
| 4580 | * server over the same connection. This is required by some |
| 4581 | * broken protocols such as NTLM, and anyway whenever there is |
| 4582 | * an opportunity for sending the challenge to the proper place, |
| 4583 | * it's better to do it (at least it helps with debugging). |
| 4584 | */ |
| 4585 | s->txn.flags |= TX_PREFER_LAST; |
| 4586 | } |
| 4587 | |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4588 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4589 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4590 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4591 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4592 | s->req->flags |= CF_NEVER_WAIT; |
| 4593 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4594 | } |
| 4595 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4596 | /* if the request buffer is not empty, it means we're |
| 4597 | * about to process another request, so send pending |
| 4598 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4599 | * Just don't do this if the buffer is close to be full, |
| 4600 | * because the request will wait for it to flush a little |
| 4601 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4602 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4603 | if (s->req->buf->i) { |
| 4604 | if (s->rep->buf->o && |
| 4605 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4606 | bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4607 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4608 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4609 | |
| 4610 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4611 | channel_auto_read(s->req); |
| 4612 | channel_auto_close(s->req); |
| 4613 | channel_auto_read(s->rep); |
| 4614 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4615 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 4616 | /* we're in keep-alive with an idle connection, monitor it */ |
| 4617 | si_idle_conn(s->req->cons); |
| 4618 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4619 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4620 | s->rep->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | |
| 4624 | /* This function updates the request state machine according to the response |
| 4625 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4626 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4627 | * it is only used to find when a request/response couple is complete. Both |
| 4628 | * this function and its equivalent should loop until both return zero. It |
| 4629 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4630 | */ |
| 4631 | int http_sync_req_state(struct session *s) |
| 4632 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4633 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4634 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4635 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4636 | unsigned int old_state = txn->req.msg_state; |
| 4637 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4638 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4639 | return 0; |
| 4640 | |
| 4641 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4642 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4643 | * We can shut the read side unless we want to abort_on_close, |
| 4644 | * or we have a POST request. The issue with POST requests is |
| 4645 | * that some browsers still send a CRLF after the request, and |
| 4646 | * this CRLF must be read so that it does not remain in the kernel |
| 4647 | * buffers, otherwise a close could cause an RST on some systems |
| 4648 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4649 | * Note that if we're using keep-alive on the client side, we'd |
| 4650 | * rather poll now and keep the polling enabled for the whole |
| 4651 | * session's life than enabling/disabling it between each |
| 4652 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4653 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4654 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4655 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4656 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4657 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4658 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4659 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4660 | /* if the server closes the connection, we want to immediately react |
| 4661 | * and close the socket to save packets and syscalls. |
| 4662 | */ |
| 4663 | chn->cons->flags |= SI_FL_NOHALF; |
| 4664 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4665 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4666 | goto wait_other_side; |
| 4667 | |
| 4668 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4669 | /* The server has not finished to respond, so we |
| 4670 | * don't want to move in order not to upset it. |
| 4671 | */ |
| 4672 | goto wait_other_side; |
| 4673 | } |
| 4674 | |
| 4675 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4676 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4677 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4678 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4679 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4680 | goto wait_other_side; |
| 4681 | } |
| 4682 | |
| 4683 | /* When we get here, it means that both the request and the |
| 4684 | * response have finished receiving. Depending on the connection |
| 4685 | * mode, we'll have to wait for the last bytes to leave in either |
| 4686 | * direction, and sometimes for a close to be effective. |
| 4687 | */ |
| 4688 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4689 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4690 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4691 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4692 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4693 | } |
| 4694 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4695 | /* Option forceclose is set, or either side wants to close, |
| 4696 | * let's enforce it now that we're not expecting any new |
| 4697 | * data to come. The caller knows the session is complete |
| 4698 | * once both states are CLOSED. |
| 4699 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4700 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4701 | channel_shutr_now(chn); |
| 4702 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4703 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4704 | } |
| 4705 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4706 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4707 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4708 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4709 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4710 | channel_auto_read(chn); |
| 4711 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4712 | chn->flags |= CF_NEVER_WAIT; |
| 4713 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4714 | } |
| 4715 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4716 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4717 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4718 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4719 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4720 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4721 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4722 | goto http_msg_closing; |
| 4723 | } |
| 4724 | else { |
| 4725 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4726 | goto http_msg_closed; |
| 4727 | } |
| 4728 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4729 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4733 | http_msg_closing: |
| 4734 | /* nothing else to forward, just waiting for the output buffer |
| 4735 | * to be empty and for the shutw_now to take effect. |
| 4736 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4737 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4738 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4739 | goto http_msg_closed; |
| 4740 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4741 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4742 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4743 | goto wait_other_side; |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4748 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4749 | /* see above in MSG_DONE why we only do this in these states */ |
| 4750 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4751 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4752 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4753 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4754 | goto wait_other_side; |
| 4755 | } |
| 4756 | |
| 4757 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4758 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | |
| 4762 | /* This function updates the response state machine according to the request |
| 4763 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4764 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4765 | * it is only used to find when a request/response couple is complete. Both |
| 4766 | * this function and its equivalent should loop until both return zero. It |
| 4767 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4768 | */ |
| 4769 | int http_sync_res_state(struct session *s) |
| 4770 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4771 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4772 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4773 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4774 | unsigned int old_state = txn->rsp.msg_state; |
| 4775 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4776 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4777 | return 0; |
| 4778 | |
| 4779 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4780 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4781 | * still monitor the server connection for a possible close |
| 4782 | * while the request is being uploaded, so we don't disable |
| 4783 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4784 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4785 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4786 | |
| 4787 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4788 | goto wait_other_side; |
| 4789 | |
| 4790 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4791 | /* The client seems to still be sending data, probably |
| 4792 | * because we got an error response during an upload. |
| 4793 | * We have the choice of either breaking the connection |
| 4794 | * or letting it pass through. Let's do the later. |
| 4795 | */ |
| 4796 | goto wait_other_side; |
| 4797 | } |
| 4798 | |
| 4799 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4800 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4801 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4802 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4803 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4804 | goto wait_other_side; |
| 4805 | } |
| 4806 | |
| 4807 | /* When we get here, it means that both the request and the |
| 4808 | * response have finished receiving. Depending on the connection |
| 4809 | * mode, we'll have to wait for the last bytes to leave in either |
| 4810 | * direction, and sometimes for a close to be effective. |
| 4811 | */ |
| 4812 | |
| 4813 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4814 | /* Server-close mode : shut read and wait for the request |
| 4815 | * side to close its output buffer. The caller will detect |
| 4816 | * when we're in DONE and the other is in CLOSED and will |
| 4817 | * catch that for the final cleanup. |
| 4818 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4819 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4820 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4821 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4822 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4823 | /* Option forceclose is set, or either side wants to close, |
| 4824 | * let's enforce it now that we're not expecting any new |
| 4825 | * data to come. The caller knows the session is complete |
| 4826 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4827 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4828 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4829 | channel_shutr_now(chn); |
| 4830 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4831 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4832 | } |
| 4833 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4834 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4835 | * need to forward remaining data. Keep-alive will need to monitor |
| 4836 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4837 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4838 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4839 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4840 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4841 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4844 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4845 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4846 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4847 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4848 | goto http_msg_closing; |
| 4849 | } |
| 4850 | else { |
| 4851 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4852 | goto http_msg_closed; |
| 4853 | } |
| 4854 | } |
| 4855 | goto wait_other_side; |
| 4856 | } |
| 4857 | |
| 4858 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4859 | http_msg_closing: |
| 4860 | /* nothing else to forward, just waiting for the output buffer |
| 4861 | * to be empty and for the shutw_now to take effect. |
| 4862 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4863 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4864 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4865 | goto http_msg_closed; |
| 4866 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4867 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4868 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4869 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4870 | if (objt_server(s->target)) |
| 4871 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4872 | goto wait_other_side; |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4877 | http_msg_closed: |
| 4878 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4879 | bi_erase(chn); |
| 4880 | channel_auto_close(chn); |
| 4881 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4882 | goto wait_other_side; |
| 4883 | } |
| 4884 | |
| 4885 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4886 | /* We force the response to leave immediately if we're waiting for the |
| 4887 | * other side, since there is no pending shutdown to push it out. |
| 4888 | */ |
| 4889 | if (!channel_is_empty(chn)) |
| 4890 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4891 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | |
| 4895 | /* Resync the request and response state machines. Return 1 if either state |
| 4896 | * changes. |
| 4897 | */ |
| 4898 | int http_resync_states(struct session *s) |
| 4899 | { |
| 4900 | struct http_txn *txn = &s->txn; |
| 4901 | int old_req_state = txn->req.msg_state; |
| 4902 | int old_res_state = txn->rsp.msg_state; |
| 4903 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4904 | http_sync_req_state(s); |
| 4905 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4906 | if (!http_sync_res_state(s)) |
| 4907 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4908 | if (!http_sync_req_state(s)) |
| 4909 | break; |
| 4910 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4911 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4912 | /* OK, both state machines agree on a compatible state. |
| 4913 | * There are a few cases we're interested in : |
| 4914 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4915 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4916 | * directions, so let's simply disable both analysers. |
| 4917 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4918 | * request. |
| 4919 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4920 | * with server-close mode means we've completed one request and we |
| 4921 | * must re-initialize the server connection. |
| 4922 | */ |
| 4923 | |
| 4924 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4925 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4926 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4927 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4928 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4929 | channel_auto_close(s->req); |
| 4930 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4931 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4932 | channel_auto_close(s->rep); |
| 4933 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4934 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4935 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4936 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4937 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4938 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4939 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4940 | channel_auto_close(s->rep); |
| 4941 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4942 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4943 | channel_abort(s->req); |
| 4944 | channel_auto_close(s->req); |
| 4945 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4946 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4947 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4948 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4949 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4950 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4951 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4952 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4953 | /* server-close/keep-alive: terminate this transaction, |
| 4954 | * possibly killing the server connection and reinitialize |
| 4955 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4956 | */ |
| 4957 | http_end_txn_clean_session(s); |
| 4958 | } |
| 4959 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4960 | return txn->req.msg_state != old_req_state || |
| 4961 | txn->rsp.msg_state != old_res_state; |
| 4962 | } |
| 4963 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4964 | /* This function is an analyser which forwards request body (including chunk |
| 4965 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4966 | * zero byte. The only situation where it must not be called is when we're in |
| 4967 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4968 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4969 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4970 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4971 | * 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] | 4972 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4973 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4974 | int http_request_forward_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4975 | { |
| 4976 | struct http_txn *txn = &s->txn; |
| 4977 | struct http_msg *msg = &s->txn.req; |
| 4978 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4979 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4980 | return 0; |
| 4981 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4982 | 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] | 4983 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4984 | /* Output closed while we were sending data. We must abort and |
| 4985 | * wake the other side up. |
| 4986 | */ |
| 4987 | msg->msg_state = HTTP_MSG_ERROR; |
| 4988 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4989 | return 1; |
| 4990 | } |
| 4991 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4992 | /* Note that we don't have to send 100-continue back because we don't |
| 4993 | * need the data to complete our job, and it's up to the server to |
| 4994 | * decide whether to return 100, 417 or anything else in return of |
| 4995 | * an "Expect: 100-continue" header. |
| 4996 | */ |
| 4997 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 4998 | if (msg->sov) { |
| 4999 | /* we have msg->sov which points to the first byte of message |
| 5000 | * body, and req->buf.p still points to the beginning of the |
| 5001 | * message. We forward the headers now, as we don't need them |
| 5002 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5003 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5004 | b_adv(req->buf, msg->sov); |
| 5005 | msg->next -= msg->sov; |
| 5006 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5007 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5008 | /* The previous analysers guarantee that the state is somewhere |
| 5009 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5010 | * msg->next are always correct. |
| 5011 | */ |
| 5012 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5013 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5014 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5015 | else |
| 5016 | msg->msg_state = HTTP_MSG_DATA; |
| 5017 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5018 | } |
| 5019 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5020 | /* Some post-connect processing might want us to refrain from starting to |
| 5021 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5022 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5023 | */ |
| 5024 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 5025 | if (!(s->rep->flags & CF_READ_ATTACHED)) { |
| 5026 | channel_auto_connect(req); |
| 5027 | goto missing_data; |
| 5028 | } |
| 5029 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5030 | } |
| 5031 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5032 | /* in most states, we should abort in case of early close */ |
| 5033 | channel_auto_close(req); |
| 5034 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5035 | if (req->to_forward) { |
| 5036 | /* We can't process the buffer's contents yet */ |
| 5037 | req->flags |= CF_WAKE_WRITE; |
| 5038 | goto missing_data; |
| 5039 | } |
| 5040 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5041 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5042 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5043 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5044 | /* we may have some pending data starting at req->buf->p */ |
| 5045 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5046 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5047 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5048 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5049 | msg->next += msg->chunk_len; |
| 5050 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5053 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5054 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5055 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5056 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5057 | } |
| 5058 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5059 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5060 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5061 | * TRAILERS state. |
| 5062 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5063 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5064 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5065 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5066 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5067 | else if (ret < 0) { |
| 5068 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5069 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5070 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5071 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5072 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5073 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5074 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5075 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5076 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5077 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5078 | |
| 5079 | if (ret == 0) |
| 5080 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5081 | else if (ret < 0) { |
| 5082 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5083 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5084 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5085 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5086 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5087 | /* we're in MSG_CHUNK_SIZE now */ |
| 5088 | } |
| 5089 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5090 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5091 | |
| 5092 | if (ret == 0) |
| 5093 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5094 | else if (ret < 0) { |
| 5095 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5096 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5097 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5098 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5099 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5100 | /* we're in HTTP_MSG_DONE now */ |
| 5101 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5102 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5103 | int old_state = msg->msg_state; |
| 5104 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5105 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5106 | |
| 5107 | /* we may have some pending data starting at req->buf->p |
| 5108 | * such as last chunk of data or trailers. |
| 5109 | */ |
| 5110 | b_adv(req->buf, msg->next); |
| 5111 | msg->next = 0; |
| 5112 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5113 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5114 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5115 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5116 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5117 | if (http_resync_states(s)) { |
| 5118 | /* some state changes occurred, maybe the analyser |
| 5119 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5120 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5121 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5122 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5123 | /* request errors are most likely due to |
| 5124 | * the server aborting the transfer. |
| 5125 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5126 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5127 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5128 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5129 | http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5130 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5131 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5132 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5133 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5134 | |
| 5135 | /* If "option abortonclose" is set on the backend, we |
| 5136 | * want to monitor the client's connection and forward |
| 5137 | * any shutdown notification to the server, which will |
| 5138 | * decide whether to close or to go on processing the |
| 5139 | * request. |
| 5140 | */ |
| 5141 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5142 | channel_auto_read(req); |
| 5143 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5144 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5145 | else if (s->txn.meth == HTTP_METH_POST) { |
| 5146 | /* POST requests may require to read extra CRLF |
| 5147 | * sent by broken browsers and which could cause |
| 5148 | * an RST to be sent upon close on some systems |
| 5149 | * (eg: Linux). |
| 5150 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5151 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5152 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5153 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5154 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5155 | } |
| 5156 | } |
| 5157 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5158 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5159 | /* we may have some pending data starting at req->buf->p */ |
| 5160 | b_adv(req->buf, msg->next); |
| 5161 | msg->next = 0; |
| 5162 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5163 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5164 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5165 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5166 | if (!(s->flags & SN_ERR_MASK)) |
| 5167 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5168 | if (!(s->flags & SN_FINST_MASK)) { |
| 5169 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5170 | s->flags |= SN_FINST_H; |
| 5171 | else |
| 5172 | s->flags |= SN_FINST_D; |
| 5173 | } |
| 5174 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5175 | s->fe->fe_counters.cli_aborts++; |
| 5176 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5177 | if (objt_server(s->target)) |
| 5178 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5179 | |
| 5180 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5181 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5182 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5183 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5184 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5185 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5186 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5187 | /* 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] | 5188 | * 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] | 5189 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5190 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5191 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5192 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5193 | /* 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] | 5194 | * 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] | 5195 | * 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] | 5196 | * modes are already handled by the stream sock layer. We must not do |
| 5197 | * this in content-length mode because it could present the MSG_MORE |
| 5198 | * flag with the last block of forwarded data, which would cause an |
| 5199 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5200 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5201 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5202 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5203 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5204 | return 0; |
| 5205 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5206 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5207 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5208 | if (s->listener->counters) |
| 5209 | s->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5210 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5211 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5212 | /* we may have some pending data starting at req->buf->p */ |
| 5213 | b_adv(req->buf, msg->next); |
| 5214 | msg->next = 0; |
| 5215 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5216 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5217 | if (txn->status) { |
| 5218 | /* Note: we don't send any error if some data were already sent */ |
| 5219 | stream_int_retnclose(req->prod, NULL); |
| 5220 | } else { |
| 5221 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5222 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5223 | } |
| 5224 | req->analysers = 0; |
| 5225 | s->rep->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] | 5226 | |
| 5227 | if (!(s->flags & SN_ERR_MASK)) |
| 5228 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5229 | if (!(s->flags & SN_FINST_MASK)) { |
| 5230 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5231 | s->flags |= SN_FINST_H; |
| 5232 | else |
| 5233 | s->flags |= SN_FINST_D; |
| 5234 | } |
| 5235 | return 0; |
| 5236 | |
| 5237 | aborted_xfer: |
| 5238 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5239 | if (txn->status) { |
| 5240 | /* Note: we don't send any error if some data were already sent */ |
| 5241 | stream_int_retnclose(req->prod, NULL); |
| 5242 | } else { |
| 5243 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5244 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5245 | } |
| 5246 | req->analysers = 0; |
| 5247 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5248 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5249 | s->fe->fe_counters.srv_aborts++; |
| 5250 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5251 | if (objt_server(s->target)) |
| 5252 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5253 | |
| 5254 | if (!(s->flags & SN_ERR_MASK)) |
| 5255 | s->flags |= SN_ERR_SRVCL; |
| 5256 | if (!(s->flags & SN_FINST_MASK)) { |
| 5257 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5258 | s->flags |= SN_FINST_H; |
| 5259 | else |
| 5260 | s->flags |= SN_FINST_D; |
| 5261 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5262 | return 0; |
| 5263 | } |
| 5264 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5265 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5266 | * processing can continue on next analysers, or zero if it either needs more |
| 5267 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5268 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5269 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5270 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5271 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5272 | int http_wait_for_response(struct session *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5273 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5274 | struct http_txn *txn = &s->txn; |
| 5275 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5276 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5277 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5278 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5279 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5280 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5281 | DPRINTF(stderr,"[%u] %s: session=%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] | 5282 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5283 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5284 | rep, |
| 5285 | rep->rex, rep->wex, |
| 5286 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5287 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5288 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5289 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5290 | /* |
| 5291 | * Now parse the partial (or complete) lines. |
| 5292 | * We will check the response syntax, and also join multi-line |
| 5293 | * headers. An index of all the lines will be elaborated while |
| 5294 | * parsing. |
| 5295 | * |
| 5296 | * For the parsing, we use a 28 states FSM. |
| 5297 | * |
| 5298 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5299 | * rep->buf->p = beginning of response |
| 5300 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5301 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5302 | * msg->eol = end of current header or line (LF or CRLF) |
| 5303 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5304 | */ |
| 5305 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5306 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5307 | /* There's a protected area at the end of the buffer for rewriting |
| 5308 | * purposes. We don't want to start to parse the request if the |
| 5309 | * protected area is affected, because we may have to move processed |
| 5310 | * data later, which is much more complicated. |
| 5311 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5312 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5313 | if (unlikely(!channel_reserved(rep))) { |
| 5314 | /* some data has still not left the buffer, wake us once that's done */ |
| 5315 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5316 | goto abort_response; |
| 5317 | channel_dont_close(rep); |
| 5318 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5319 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5320 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5321 | } |
| 5322 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5323 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5324 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5325 | buffer_slow_realign(rep->buf); |
| 5326 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5327 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5328 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5329 | } |
| 5330 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5331 | /* 1: we might have to print this header in debug mode */ |
| 5332 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5333 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5334 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5335 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5336 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5337 | sol = rep->buf->p; |
| 5338 | 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] | 5339 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5340 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5341 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5342 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5343 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5344 | while (cur_idx) { |
| 5345 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5346 | debug_hdr("srvhdr", s, sol, eol); |
| 5347 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5348 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5349 | } |
| 5350 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5351 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5352 | /* |
| 5353 | * Now we quickly check if we have found a full valid response. |
| 5354 | * If not so, we check the FD and buffer states before leaving. |
| 5355 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5356 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5357 | * responses are checked first. |
| 5358 | * |
| 5359 | * Depending on whether the client is still there or not, we |
| 5360 | * may send an error response back or not. Note that normally |
| 5361 | * we should only check for HTTP status there, and check I/O |
| 5362 | * errors somewhere else. |
| 5363 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5364 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5365 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5366 | /* Invalid response */ |
| 5367 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5368 | /* we detected a parsing error. We want to archive this response |
| 5369 | * in the dedicated proxy area for later troubleshooting. |
| 5370 | */ |
| 5371 | hdr_response_bad: |
| 5372 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5373 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5374 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5375 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5376 | if (objt_server(s->target)) { |
| 5377 | objt_server(s->target)->counters.failed_resp++; |
| 5378 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5379 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5380 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5381 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5382 | rep->analysers = 0; |
| 5383 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5384 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5385 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5386 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5387 | |
| 5388 | if (!(s->flags & SN_ERR_MASK)) |
| 5389 | s->flags |= SN_ERR_PRXCOND; |
| 5390 | if (!(s->flags & SN_FINST_MASK)) |
| 5391 | s->flags |= SN_FINST_H; |
| 5392 | |
| 5393 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5394 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5395 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5396 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5397 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5398 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5399 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5400 | goto hdr_response_bad; |
| 5401 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5402 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5403 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5404 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5405 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5406 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5407 | else if (txn->flags & TX_NOT_FIRST) |
| 5408 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5409 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5410 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5411 | if (objt_server(s->target)) { |
| 5412 | objt_server(s->target)->counters.failed_resp++; |
| 5413 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5414 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5415 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5416 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5417 | rep->analysers = 0; |
| 5418 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5419 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5420 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5421 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5422 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5423 | if (!(s->flags & SN_ERR_MASK)) |
| 5424 | s->flags |= SN_ERR_SRVCL; |
| 5425 | if (!(s->flags & SN_FINST_MASK)) |
| 5426 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5427 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5428 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5429 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5430 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5431 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5432 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5433 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5434 | else if (txn->flags & TX_NOT_FIRST) |
| 5435 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5436 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5437 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5438 | if (objt_server(s->target)) { |
| 5439 | objt_server(s->target)->counters.failed_resp++; |
| 5440 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5441 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5442 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5443 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5444 | rep->analysers = 0; |
| 5445 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5446 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5447 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5448 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5449 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5450 | if (!(s->flags & SN_ERR_MASK)) |
| 5451 | s->flags |= SN_ERR_SRVTO; |
| 5452 | if (!(s->flags & SN_FINST_MASK)) |
| 5453 | s->flags |= SN_FINST_H; |
| 5454 | return 0; |
| 5455 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5456 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5457 | /* client abort with an abortonclose */ |
| 5458 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5459 | s->fe->fe_counters.cli_aborts++; |
| 5460 | s->be->be_counters.cli_aborts++; |
| 5461 | if (objt_server(s->target)) |
| 5462 | objt_server(s->target)->counters.cli_aborts++; |
| 5463 | |
| 5464 | rep->analysers = 0; |
| 5465 | channel_auto_close(rep); |
| 5466 | |
| 5467 | txn->status = 400; |
| 5468 | bi_erase(rep); |
| 5469 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5470 | |
| 5471 | if (!(s->flags & SN_ERR_MASK)) |
| 5472 | s->flags |= SN_ERR_CLICL; |
| 5473 | if (!(s->flags & SN_FINST_MASK)) |
| 5474 | s->flags |= SN_FINST_H; |
| 5475 | |
| 5476 | /* process_session() will take care of the error */ |
| 5477 | return 0; |
| 5478 | } |
| 5479 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5480 | /* 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] | 5481 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5482 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5483 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5484 | else if (txn->flags & TX_NOT_FIRST) |
| 5485 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5486 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5487 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5488 | if (objt_server(s->target)) { |
| 5489 | objt_server(s->target)->counters.failed_resp++; |
| 5490 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5491 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5492 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5493 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5494 | rep->analysers = 0; |
| 5495 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5496 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5497 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5498 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5499 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5500 | if (!(s->flags & SN_ERR_MASK)) |
| 5501 | s->flags |= SN_ERR_SRVCL; |
| 5502 | if (!(s->flags & SN_FINST_MASK)) |
| 5503 | s->flags |= SN_FINST_H; |
| 5504 | return 0; |
| 5505 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5506 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5507 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5508 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5509 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5510 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5511 | else if (txn->flags & TX_NOT_FIRST) |
| 5512 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5513 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5514 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5515 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5516 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5517 | |
| 5518 | if (!(s->flags & SN_ERR_MASK)) |
| 5519 | s->flags |= SN_ERR_CLICL; |
| 5520 | if (!(s->flags & SN_FINST_MASK)) |
| 5521 | s->flags |= SN_FINST_H; |
| 5522 | |
| 5523 | /* process_session() will take care of the error */ |
| 5524 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5525 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5526 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5527 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5528 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5529 | return 0; |
| 5530 | } |
| 5531 | |
| 5532 | /* More interesting part now : we know that we have a complete |
| 5533 | * response which at least looks like HTTP. We have an indicator |
| 5534 | * of each header's length, so we can parse them quickly. |
| 5535 | */ |
| 5536 | |
| 5537 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5538 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5539 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5540 | /* |
| 5541 | * 1: get the status code |
| 5542 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5543 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5544 | if (n < 1 || n > 5) |
| 5545 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5546 | /* when the client triggers a 4xx from the server, it's most often due |
| 5547 | * to a missing object or permission. These events should be tracked |
| 5548 | * because if they happen often, it may indicate a brute force or a |
| 5549 | * vulnerability scan. |
| 5550 | */ |
| 5551 | if (n == 4) |
| 5552 | session_inc_http_err_ctr(s); |
| 5553 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5554 | if (objt_server(s->target)) |
| 5555 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5556 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5557 | /* check if the response is HTTP/1.1 or above */ |
| 5558 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5559 | ((rep->buf->p[5] > '1') || |
| 5560 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5561 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5562 | |
| 5563 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5564 | 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] | 5565 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5566 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5567 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5568 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5569 | 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] | 5570 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5571 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5572 | * and 505 are triggered on demand by client request, so we must not |
| 5573 | * count them as server failures. |
| 5574 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5575 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5576 | 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] | 5577 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5578 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5579 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5580 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5581 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5582 | /* |
| 5583 | * 2: check for cacheability. |
| 5584 | */ |
| 5585 | |
| 5586 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5587 | case 100: |
| 5588 | /* |
| 5589 | * We may be facing a 100-continue response, in which case this |
| 5590 | * is not the right response, and we're waiting for the next one. |
| 5591 | * Let's allow this response to go to the client and wait for the |
| 5592 | * next one. |
| 5593 | */ |
| 5594 | hdr_idx_init(&txn->hdr_idx); |
| 5595 | msg->next -= channel_forward(rep, msg->next); |
| 5596 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5597 | txn->status = 0; |
| 5598 | s->logs.t_data = -1; /* was not a response yet */ |
| 5599 | goto next_one; |
| 5600 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5601 | case 200: |
| 5602 | case 203: |
| 5603 | case 206: |
| 5604 | case 300: |
| 5605 | case 301: |
| 5606 | case 410: |
| 5607 | /* RFC2616 @13.4: |
| 5608 | * "A response received with a status code of |
| 5609 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5610 | * by a cache (...) unless a cache-control |
| 5611 | * directive prohibits caching." |
| 5612 | * |
| 5613 | * RFC2616 @9.5: POST method : |
| 5614 | * "Responses to this method are not cacheable, |
| 5615 | * unless the response includes appropriate |
| 5616 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5617 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5618 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5619 | ((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] | 5620 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5621 | break; |
| 5622 | default: |
| 5623 | break; |
| 5624 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5625 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5626 | /* |
| 5627 | * 3: we may need to capture headers |
| 5628 | */ |
| 5629 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5630 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5631 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5632 | txn->rsp.cap, s->fe->rsp_cap); |
| 5633 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5634 | /* 4: determine the transfer-length. |
| 5635 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5636 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5637 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5638 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5639 | * All responses to the HEAD request method MUST NOT include a |
| 5640 | * message-body, even though the presence of entity-header fields |
| 5641 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5642 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5643 | * message-body. All other responses do include a message-body, |
| 5644 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5645 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5646 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5647 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5648 | * always terminated by the first empty line after the header fields, |
| 5649 | * regardless of the entity-header fields present in the message. |
| 5650 | * |
| 5651 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5652 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5653 | * transfer-length is defined by the use of this transfer-coding. |
| 5654 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5655 | * transfer-coding is not present, the transfer-length is defined by |
| 5656 | * the sender closing the connection. |
| 5657 | * |
| 5658 | * 3. If a Content-Length header field is present, its decimal value in |
| 5659 | * OCTETs represents both the entity-length and the transfer-length. |
| 5660 | * If a message is received with both a Transfer-Encoding header |
| 5661 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5662 | * |
| 5663 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5664 | * the transfer-length is not otherwise specified, then this self- |
| 5665 | * delimiting media type defines the transfer-length. This media |
| 5666 | * type MUST NOT be used unless the sender knows that the recipient |
| 5667 | * can parse it; the presence in a request of a Range header with |
| 5668 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5669 | * client can parse multipart/byteranges responses. |
| 5670 | * |
| 5671 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5672 | */ |
| 5673 | |
| 5674 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5675 | * 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] | 5676 | * 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] | 5677 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5678 | */ |
| 5679 | if (txn->meth == HTTP_METH_HEAD || |
| 5680 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5681 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5682 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5683 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5684 | goto skip_content_length; |
| 5685 | } |
| 5686 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5687 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5688 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5689 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5690 | 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] | 5691 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5692 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5693 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5694 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5695 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5696 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5697 | break; |
| 5698 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5702 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5703 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5704 | 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] | 5705 | signed long long cl; |
| 5706 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5707 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5708 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5709 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5710 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5711 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5712 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5713 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5714 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5715 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5716 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5717 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5718 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5719 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5720 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5721 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5722 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5723 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5724 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5725 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5726 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5727 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5728 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5729 | } |
| 5730 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5731 | if (s->fe->comp || s->be->comp) |
| 5732 | select_compression_response_header(s, rep->buf); |
| 5733 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5734 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5735 | /* Now we have to check if we need to modify the Connection header. |
| 5736 | * This is more difficult on the response than it is on the request, |
| 5737 | * because we can have two different HTTP versions and we don't know |
| 5738 | * how the client will interprete a response. For instance, let's say |
| 5739 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5740 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5741 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5742 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5743 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5744 | * indicating how a request MAY be understood by the client. In case |
| 5745 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5746 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5747 | * will fall back to explicit close. Note that we won't take risks with |
| 5748 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5749 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5750 | */ |
| 5751 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5752 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5753 | txn->status == 101)) { |
| 5754 | /* Either we've established an explicit tunnel, or we're |
| 5755 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5756 | * to understand the next protocols. We have to switch to tunnel |
| 5757 | * mode, so that we transfer the request and responses then let |
| 5758 | * this protocol pass unmodified. When we later implement specific |
| 5759 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5760 | * header which contains information about that protocol for |
| 5761 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5762 | */ |
| 5763 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5764 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5765 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5766 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5767 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5768 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5769 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5770 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5771 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5772 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5773 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5774 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5775 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5776 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5777 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5778 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5779 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5780 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5781 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5782 | /* now adjust header transformations depending on current state */ |
| 5783 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5784 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5785 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5786 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5787 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5788 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5789 | else { /* SCL / KAL */ |
| 5790 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5791 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5792 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5793 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5794 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5795 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5796 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5797 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5798 | /* Some keep-alive responses are converted to Server-close if |
| 5799 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5800 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5801 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5802 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5803 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5804 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5805 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5806 | } |
| 5807 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5808 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5809 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5810 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5811 | /* end of job, return OK */ |
| 5812 | rep->analysers &= ~an_bit; |
| 5813 | rep->analyse_exp = TICK_ETERNITY; |
| 5814 | channel_auto_close(rep); |
| 5815 | return 1; |
| 5816 | |
| 5817 | abort_keep_alive: |
| 5818 | /* A keep-alive request to the server failed on a network error. |
| 5819 | * The client is required to retry. We need to close without returning |
| 5820 | * any other information so that the client retries. |
| 5821 | */ |
| 5822 | txn->status = 0; |
| 5823 | rep->analysers = 0; |
| 5824 | s->req->analysers = 0; |
| 5825 | channel_auto_close(rep); |
| 5826 | s->logs.logwait = 0; |
| 5827 | s->logs.level = 0; |
| 5828 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 5829 | bi_erase(rep); |
| 5830 | stream_int_retnclose(rep->cons, NULL); |
| 5831 | return 0; |
| 5832 | } |
| 5833 | |
| 5834 | /* This function performs all the processing enabled for the current response. |
| 5835 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5836 | * and updates s->rep->analysers. It might make sense to explode it into several |
| 5837 | * other functions. It works like process_request (see indications above). |
| 5838 | */ |
| 5839 | int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px) |
| 5840 | { |
| 5841 | struct http_txn *txn = &s->txn; |
| 5842 | struct http_msg *msg = &txn->rsp; |
| 5843 | struct proxy *cur_proxy; |
| 5844 | struct cond_wordlist *wl; |
| 5845 | struct http_res_rule *http_res_last_rule = NULL; |
| 5846 | |
| 5847 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 5848 | now_ms, __FUNCTION__, |
| 5849 | s, |
| 5850 | rep, |
| 5851 | rep->rex, rep->wex, |
| 5852 | rep->flags, |
| 5853 | rep->buf->i, |
| 5854 | rep->analysers); |
| 5855 | |
| 5856 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5857 | return 0; |
| 5858 | |
| 5859 | rep->analysers &= ~an_bit; |
| 5860 | rep->analyse_exp = TICK_ETERNITY; |
| 5861 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5862 | /* The stats applet needs to adjust the Connection header but we don't |
| 5863 | * apply any filter there. |
| 5864 | */ |
| 5865 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) |
| 5866 | goto skip_filters; |
| 5867 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5868 | /* |
| 5869 | * We will have to evaluate the filters. |
| 5870 | * As opposed to version 1.2, now they will be evaluated in the |
| 5871 | * filters order and not in the header order. This means that |
| 5872 | * each filter has to be validated among all headers. |
| 5873 | * |
| 5874 | * Filters are tried with ->be first, then with ->fe if it is |
| 5875 | * different from ->be. |
| 5876 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5877 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5878 | cur_proxy = s->be; |
| 5879 | while (1) { |
| 5880 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5881 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5882 | /* evaluate http-response rules */ |
| 5883 | if (!http_res_last_rule) |
| 5884 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5886 | /* try headers filters */ |
| 5887 | if (rule_set->rsp_exp != NULL) { |
| 5888 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5889 | return_bad_resp: |
| 5890 | if (objt_server(s->target)) { |
| 5891 | objt_server(s->target)->counters.failed_resp++; |
| 5892 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5893 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5894 | s->be->be_counters.failed_resp++; |
| 5895 | return_srv_prx_502: |
| 5896 | rep->analysers = 0; |
| 5897 | txn->status = 502; |
| 5898 | s->logs.t_data = -1; /* was not a valid response */ |
| 5899 | rep->prod->flags |= SI_FL_NOLINGER; |
| 5900 | bi_erase(rep); |
| 5901 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
| 5902 | if (!(s->flags & SN_ERR_MASK)) |
| 5903 | s->flags |= SN_ERR_PRXCOND; |
| 5904 | if (!(s->flags & SN_FINST_MASK)) |
| 5905 | s->flags |= SN_FINST_H; |
| 5906 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5907 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5908 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5909 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5910 | /* has the response been denied ? */ |
| 5911 | if (txn->flags & TX_SVDENY) { |
| 5912 | if (objt_server(s->target)) |
| 5913 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5914 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5915 | s->be->be_counters.denied_resp++; |
| 5916 | s->fe->fe_counters.denied_resp++; |
| 5917 | if (s->listener->counters) |
| 5918 | s->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5919 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5920 | goto return_srv_prx_502; |
| 5921 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5922 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5923 | /* add response headers from the rule sets in the same order */ |
| 5924 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
| 5925 | if (txn->status < 200) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5926 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5927 | if (wl->cond) { |
| 5928 | int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 5929 | ret = acl_pass(ret); |
| 5930 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5931 | ret = !ret; |
| 5932 | if (!ret) |
| 5933 | continue; |
| 5934 | } |
| 5935 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5936 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5937 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5938 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5939 | /* check whether we're already working on the frontend */ |
| 5940 | if (cur_proxy == s->fe) |
| 5941 | break; |
| 5942 | cur_proxy = s->fe; |
| 5943 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5944 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5945 | /* OK that's all we can do for 1xx responses */ |
| 5946 | if (unlikely(txn->status < 200)) |
| 5947 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5948 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5949 | /* |
| 5950 | * Now check for a server cookie. |
| 5951 | */ |
| 5952 | if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name || |
| 5953 | (s->be->options & PR_O_CHK_CACHE)) |
| 5954 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5955 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5956 | /* |
| 5957 | * Check for cache-control or pragma headers if required. |
| 5958 | */ |
| 5959 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
| 5960 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5961 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5962 | /* |
| 5963 | * Add server cookie in the response if needed |
| 5964 | */ |
| 5965 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5966 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 5967 | (!(s->flags & SN_DIRECT) || |
| 5968 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5969 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5970 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5971 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5972 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5973 | !(s->flags & SN_IGNORE_PRST)) { |
| 5974 | /* the server is known, it's not the one the client requested, or the |
| 5975 | * cookie's last seen date needs to be refreshed. We have to |
| 5976 | * insert a set-cookie here, except if we want to insert only on POST |
| 5977 | * requests and this one isn't. Note that servers which don't have cookies |
| 5978 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5979 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5980 | if (!objt_server(s->target)->cookie) { |
| 5981 | chunk_printf(&trash, |
| 5982 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5983 | s->be->cookie_name); |
| 5984 | } |
| 5985 | else { |
| 5986 | 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] | 5987 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5988 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5989 | /* emit last_date, which is mandatory */ |
| 5990 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5991 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5992 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5993 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5994 | if (s->be->cookie_maxlife) { |
| 5995 | /* emit first_date, which is either the original one or |
| 5996 | * the current date. |
| 5997 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5998 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5999 | s30tob64(txn->cookie_first_date ? |
| 6000 | txn->cookie_first_date >> 2 : |
| 6001 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6002 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6003 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6004 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6005 | chunk_appendf(&trash, "; path=/"); |
| 6006 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6007 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6008 | if (s->be->cookie_domain) |
| 6009 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6010 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6011 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6012 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6013 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6014 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6015 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6016 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6017 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6018 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6019 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6020 | txn->flags &= ~TX_SCK_MASK; |
| 6021 | if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT)) |
| 6022 | /* the server did not change, only the date was updated */ |
| 6023 | txn->flags |= TX_SCK_UPDATED; |
| 6024 | else |
| 6025 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6026 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6027 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6028 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6029 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6030 | * 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] | 6031 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6032 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6033 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6034 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6035 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6036 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6037 | "Cache-control: private", 22) < 0)) |
| 6038 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6039 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6040 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6041 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6042 | /* |
| 6043 | * Check if result will be cacheable with a cookie. |
| 6044 | * We'll block the response if security checks have caught |
| 6045 | * nasty things such as a cacheable cookie. |
| 6046 | */ |
| 6047 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6048 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6049 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6050 | /* we're in presence of a cacheable response containing |
| 6051 | * a set-cookie header. We'll block it as requested by |
| 6052 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6053 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6054 | if (objt_server(s->target)) |
| 6055 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6056 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6057 | s->be->be_counters.denied_resp++; |
| 6058 | s->fe->fe_counters.denied_resp++; |
| 6059 | if (s->listener->counters) |
| 6060 | s->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6061 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6062 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6063 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6064 | send_log(s->be, LOG_ALERT, |
| 6065 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6066 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6067 | goto return_srv_prx_502; |
| 6068 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6069 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6070 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6071 | /* |
| 6072 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6073 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6074 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 6075 | * if anything but "Upgrade" is present in the Connection header. |
| 6076 | */ |
| 6077 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 6078 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 6079 | ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 6080 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6081 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6082 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6083 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6084 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6085 | /* we want a keep-alive response here. Keep-alive header |
| 6086 | * required if either side is not 1.1. |
| 6087 | */ |
| 6088 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6089 | want_flags |= TX_CON_KAL_SET; |
| 6090 | } |
| 6091 | else { |
| 6092 | /* we want a close response here. Close header required if |
| 6093 | * the server is 1.1, regardless of the client. |
| 6094 | */ |
| 6095 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6096 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6097 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6098 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6099 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6100 | http_change_connection_header(txn, msg, want_flags); |
| 6101 | } |
| 6102 | |
| 6103 | skip_header_mangling: |
| 6104 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6105 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6106 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6107 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6108 | /* if the user wants to log as soon as possible, without counting |
| 6109 | * bytes from the server, then this is the right moment. We have |
| 6110 | * to temporarily assign bytes_out to log what we currently have. |
| 6111 | */ |
| 6112 | if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 6113 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6114 | s->logs.bytes_out = txn->rsp.eoh; |
| 6115 | s->do_log(s); |
| 6116 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6117 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6118 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6119 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6120 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6121 | /* This function is an analyser which forwards response body (including chunk |
| 6122 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6123 | * zero byte. The only situation where it must not be called is when we're in |
| 6124 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6125 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6126 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 6127 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 6128 | * |
| 6129 | * It is capable of compressing response data both in content-length mode and |
| 6130 | * in chunked mode. The state machines follows different flows depending on |
| 6131 | * whether content-length and chunked modes are used, since there are no |
| 6132 | * trailers in content-length : |
| 6133 | * |
| 6134 | * chk-mode cl-mode |
| 6135 | * ,----- BODY -----. |
| 6136 | * / \ |
| 6137 | * V size > 0 V chk-mode |
| 6138 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6139 | * | | size == 0 | last byte | |
| 6140 | * | v final crlf v inspected | |
| 6141 | * | TRAILERS -----------> DONE | |
| 6142 | * | | |
| 6143 | * `----------------------------------------------' |
| 6144 | * |
| 6145 | * Compression only happens in the DATA state, and must be flushed in final |
| 6146 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6147 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6148 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6149 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6150 | int http_response_forward_body(struct session *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6151 | { |
| 6152 | struct http_txn *txn = &s->txn; |
| 6153 | struct http_msg *msg = &s->txn.rsp; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6154 | static struct buffer *tmpbuf = NULL; |
| 6155 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6156 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6157 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6158 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6159 | return 0; |
| 6160 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6161 | 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] | 6162 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 6163 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6164 | /* Output closed while we were sending data. We must abort and |
| 6165 | * wake the other side up. |
| 6166 | */ |
| 6167 | msg->msg_state = HTTP_MSG_ERROR; |
| 6168 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6169 | return 1; |
| 6170 | } |
| 6171 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6172 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6173 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6174 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6175 | if (msg->sov) { |
| 6176 | /* we have msg->sov which points to the first byte of message |
| 6177 | * body, and res->buf.p still points to the beginning of the |
| 6178 | * message. We forward the headers now, as we don't need them |
| 6179 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6180 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6181 | b_adv(res->buf, msg->sov); |
| 6182 | msg->next -= msg->sov; |
| 6183 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6184 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6185 | /* The previous analysers guarantee that the state is somewhere |
| 6186 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6187 | * msg->next are always correct. |
| 6188 | */ |
| 6189 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6190 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6191 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6192 | else |
| 6193 | msg->msg_state = HTTP_MSG_DATA; |
| 6194 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6195 | } |
| 6196 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6197 | if (res->to_forward) { |
| 6198 | /* We can't process the buffer's contents yet */ |
| 6199 | res->flags |= CF_WAKE_WRITE; |
| 6200 | goto missing_data; |
| 6201 | } |
| 6202 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6203 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6204 | /* We need a compression buffer in the DATA state to put the |
| 6205 | * output of compressed data, and in CRLF state to let the |
| 6206 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6207 | */ |
| 6208 | if (unlikely(tmpbuf == NULL)) { |
| 6209 | /* this is the first time we need the compression buffer */ |
| 6210 | tmpbuf = pool_alloc2(pool2_buffer); |
| 6211 | if (tmpbuf == NULL) |
| 6212 | goto aborted_xfer; /* no memory */ |
| 6213 | } |
| 6214 | |
| 6215 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6216 | if (ret < 0) { |
| 6217 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6218 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6219 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6220 | compressing = 1; |
| 6221 | } |
| 6222 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6223 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6224 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6225 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6226 | /* we may have some pending data starting at res->buf->p */ |
| 6227 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 6228 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6229 | if (ret < 0) |
| 6230 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6231 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6232 | if (msg->chunk_len) { |
| 6233 | /* input empty or output full */ |
| 6234 | if (res->buf->i > msg->next) |
| 6235 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6236 | goto missing_data; |
| 6237 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6238 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6239 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 6240 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 6241 | /* output full */ |
| 6242 | res->flags |= CF_WAKE_WRITE; |
| 6243 | goto missing_data; |
| 6244 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6245 | msg->next += msg->chunk_len; |
| 6246 | msg->chunk_len = 0; |
| 6247 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6248 | |
| 6249 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6250 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6251 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6252 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6253 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6254 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6255 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6256 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6257 | |
| 6258 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6259 | /* we want the CRLF after the data */ |
| 6260 | |
| 6261 | ret = http_skip_chunk_crlf(msg); |
| 6262 | if (ret == 0) |
| 6263 | goto missing_data; |
| 6264 | else if (ret < 0) { |
| 6265 | if (msg->err_pos >= 0) |
| 6266 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6267 | goto return_bad_res; |
| 6268 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6269 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6270 | |
| 6271 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6272 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 6273 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6274 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6275 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6276 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6277 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6278 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6279 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6280 | else if (ret < 0) { |
| 6281 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6282 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6283 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6284 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6285 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6286 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6287 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6288 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6289 | if (unlikely(compressing)) { |
| 6290 | /* we need to flush output contents before syncing FSMs */ |
| 6291 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6292 | compressing = 0; |
| 6293 | } |
| 6294 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6295 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6296 | if (ret == 0) |
| 6297 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6298 | else if (ret < 0) { |
| 6299 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6300 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6301 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6302 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6303 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6304 | |
| 6305 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6306 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6307 | if (unlikely(compressing)) { |
| 6308 | /* we need to flush output contents before syncing FSMs */ |
| 6309 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6310 | compressing = 0; |
| 6311 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6312 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6313 | /* we may have some pending data starting at res->buf->p |
| 6314 | * such as a last chunk of data or trailers. |
| 6315 | */ |
| 6316 | b_adv(res->buf, msg->next); |
| 6317 | msg->next = 0; |
| 6318 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6319 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6320 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6321 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6322 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6323 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6324 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6325 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6326 | /* some state changes occurred, maybe the analyser |
| 6327 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6328 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6329 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6330 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6331 | /* response errors are most likely due to |
| 6332 | * the client aborting the transfer. |
| 6333 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6334 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6335 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6336 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6337 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6338 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6339 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6340 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6341 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6342 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6343 | } |
| 6344 | } |
| 6345 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6346 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6347 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6348 | if (unlikely(compressing)) { |
| 6349 | 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] | 6350 | compressing = 0; |
| 6351 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6352 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6353 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 6354 | b_adv(res->buf, msg->next); |
| 6355 | msg->next = 0; |
| 6356 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6357 | } |
| 6358 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6359 | if (res->flags & CF_SHUTW) |
| 6360 | goto aborted_xfer; |
| 6361 | |
| 6362 | /* stop waiting for data if the input is closed before the end. If the |
| 6363 | * client side was already closed, it means that the client has aborted, |
| 6364 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6365 | * server abort. |
| 6366 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6367 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | b2c6a78 | 2014-04-23 20:29:01 +0200 | [diff] [blame] | 6368 | if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6369 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6370 | if (!(s->flags & SN_ERR_MASK)) |
| 6371 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6372 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6373 | if (objt_server(s->target)) |
| 6374 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6375 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6376 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6377 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6378 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6379 | if (!s->req->analysers) |
| 6380 | goto return_bad_res; |
| 6381 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6382 | /* 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] | 6383 | * 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] | 6384 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6385 | * close. |
| 6386 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6387 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6388 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6389 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6390 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6391 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6392 | /* 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] | 6393 | * 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] | 6394 | * 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] | 6395 | * modes are already handled by the stream sock layer. We must not do |
| 6396 | * this in content-length mode because it could present the MSG_MORE |
| 6397 | * flag with the last block of forwarded data, which would cause an |
| 6398 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6399 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6400 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6401 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6402 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6403 | /* the session handler will take care of timeouts and errors */ |
| 6404 | return 0; |
| 6405 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6406 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6407 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6408 | if (objt_server(s->target)) |
| 6409 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6410 | |
| 6411 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 6412 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 6413 | 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] | 6414 | compressing = 0; |
| 6415 | } |
| 6416 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 6417 | /* we may have some pending data starting at res->buf->p */ |
| 6418 | if (s->comp_algo == NULL) { |
| 6419 | b_adv(res->buf, msg->next); |
| 6420 | msg->next = 0; |
| 6421 | } |
| 6422 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6423 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6424 | /* don't send any error message as we're in the body */ |
| 6425 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6426 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6427 | 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] | 6428 | if (objt_server(s->target)) |
| 6429 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6430 | |
| 6431 | if (!(s->flags & SN_ERR_MASK)) |
| 6432 | s->flags |= SN_ERR_PRXCOND; |
| 6433 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6434 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6435 | return 0; |
| 6436 | |
| 6437 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 6438 | if (unlikely(compressing)) { |
| 6439 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 6440 | compressing = 0; |
| 6441 | } |
| 6442 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6443 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6444 | /* don't send any error message as we're in the body */ |
| 6445 | stream_int_retnclose(res->cons, NULL); |
| 6446 | res->analysers = 0; |
| 6447 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6448 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6449 | s->fe->fe_counters.cli_aborts++; |
| 6450 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6451 | if (objt_server(s->target)) |
| 6452 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6453 | |
| 6454 | if (!(s->flags & SN_ERR_MASK)) |
| 6455 | s->flags |= SN_ERR_CLICL; |
| 6456 | if (!(s->flags & SN_FINST_MASK)) |
| 6457 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6458 | return 0; |
| 6459 | } |
| 6460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6461 | /* Iterate the same filter through all request headers. |
| 6462 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6463 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6464 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6465 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6466 | int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6467 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6468 | char term; |
| 6469 | char *cur_ptr, *cur_end, *cur_next; |
| 6470 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6471 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6472 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6473 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6475 | last_hdr = 0; |
| 6476 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6477 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6478 | old_idx = 0; |
| 6479 | |
| 6480 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6481 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6482 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6483 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6484 | (exp->action == ACT_ALLOW || |
| 6485 | exp->action == ACT_DENY || |
| 6486 | exp->action == ACT_TARPIT)) |
| 6487 | return 0; |
| 6488 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6489 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6490 | if (!cur_idx) |
| 6491 | break; |
| 6492 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6493 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6494 | cur_ptr = cur_next; |
| 6495 | cur_end = cur_ptr + cur_hdr->len; |
| 6496 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6497 | |
| 6498 | /* Now we have one header between cur_ptr and cur_end, |
| 6499 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6500 | */ |
| 6501 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6502 | /* The annoying part is that pattern matching needs |
| 6503 | * that we modify the contents to null-terminate all |
| 6504 | * strings before testing them. |
| 6505 | */ |
| 6506 | |
| 6507 | term = *cur_end; |
| 6508 | *cur_end = '\0'; |
| 6509 | |
| 6510 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6511 | switch (exp->action) { |
| 6512 | case ACT_SETBE: |
| 6513 | /* It is not possible to jump a second time. |
| 6514 | * FIXME: should we return an HTTP/500 here so that |
| 6515 | * the admin knows there's a problem ? |
| 6516 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6517 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6518 | break; |
| 6519 | |
| 6520 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6521 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6522 | last_hdr = 1; |
| 6523 | break; |
| 6524 | |
| 6525 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6526 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6527 | last_hdr = 1; |
| 6528 | break; |
| 6529 | |
| 6530 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6531 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6532 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6533 | break; |
| 6534 | |
| 6535 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6536 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6537 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6538 | break; |
| 6539 | |
| 6540 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6541 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6542 | 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] | 6543 | /* FIXME: if the user adds a newline in the replacement, the |
| 6544 | * index will not be recalculated for now, and the new line |
| 6545 | * will not be counted as a new header. |
| 6546 | */ |
| 6547 | |
| 6548 | cur_end += delta; |
| 6549 | cur_next += delta; |
| 6550 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6551 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6552 | break; |
| 6553 | |
| 6554 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6555 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6556 | cur_next += delta; |
| 6557 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6558 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6559 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6560 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6561 | cur_hdr->len = 0; |
| 6562 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6563 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6564 | break; |
| 6565 | |
| 6566 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6567 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6568 | if (cur_end) |
| 6569 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6570 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6571 | /* keep the link from this header to next one in case of later |
| 6572 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6573 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6574 | old_idx = cur_idx; |
| 6575 | } |
| 6576 | return 0; |
| 6577 | } |
| 6578 | |
| 6579 | |
| 6580 | /* Apply the filter to the request line. |
| 6581 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6582 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6583 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6584 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6585 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6586 | int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6587 | { |
| 6588 | char term; |
| 6589 | char *cur_ptr, *cur_end; |
| 6590 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6591 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6592 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6593 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6594 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6595 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6596 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6597 | (exp->action == ACT_ALLOW || |
| 6598 | exp->action == ACT_DENY || |
| 6599 | exp->action == ACT_TARPIT)) |
| 6600 | return 0; |
| 6601 | else if (exp->action == ACT_REMOVE) |
| 6602 | return 0; |
| 6603 | |
| 6604 | done = 0; |
| 6605 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6606 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6607 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6608 | |
| 6609 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6610 | |
| 6611 | /* The annoying part is that pattern matching needs |
| 6612 | * that we modify the contents to null-terminate all |
| 6613 | * strings before testing them. |
| 6614 | */ |
| 6615 | |
| 6616 | term = *cur_end; |
| 6617 | *cur_end = '\0'; |
| 6618 | |
| 6619 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6620 | switch (exp->action) { |
| 6621 | case ACT_SETBE: |
| 6622 | /* It is not possible to jump a second time. |
| 6623 | * FIXME: should we return an HTTP/500 here so that |
| 6624 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6625 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6626 | if (s->be != s->fe) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6627 | break; |
| 6628 | |
| 6629 | /* Swithing Proxy */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6630 | session_set_backend(s, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6631 | done = 1; |
| 6632 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6634 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6635 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6636 | done = 1; |
| 6637 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6638 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6639 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6640 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6641 | done = 1; |
| 6642 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6643 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6644 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6645 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6646 | done = 1; |
| 6647 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6648 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6649 | case ACT_REPLACE: |
| 6650 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6651 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6652 | 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] | 6653 | /* FIXME: if the user adds a newline in the replacement, the |
| 6654 | * index will not be recalculated for now, and the new line |
| 6655 | * will not be counted as a new header. |
| 6656 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6657 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6658 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6659 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6660 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6661 | HTTP_MSG_RQMETH, |
| 6662 | cur_ptr, cur_end + 1, |
| 6663 | NULL, NULL); |
| 6664 | if (unlikely(!cur_end)) |
| 6665 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6666 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6667 | /* we have a full request and we know that we have either a CR |
| 6668 | * or an LF at <ptr>. |
| 6669 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6670 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6671 | 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] | 6672 | /* there is no point trying this regex on headers */ |
| 6673 | return 1; |
| 6674 | } |
| 6675 | } |
| 6676 | *cur_end = term; /* restore the string terminator */ |
| 6677 | return done; |
| 6678 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6679 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6680 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6681 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6682 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6683 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6684 | * 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] | 6685 | * unparsable request. Since it can manage the switch to another backend, it |
| 6686 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6687 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6688 | int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6689 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6690 | struct http_txn *txn = &s->txn; |
| 6691 | struct hdr_exp *exp; |
| 6692 | |
| 6693 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6694 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6695 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6696 | /* |
| 6697 | * The interleaving of transformations and verdicts |
| 6698 | * makes it difficult to decide to continue or stop |
| 6699 | * the evaluation. |
| 6700 | */ |
| 6701 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6702 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6703 | break; |
| 6704 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6705 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6706 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6707 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6708 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6709 | |
| 6710 | /* if this filter had a condition, evaluate it now and skip to |
| 6711 | * next filter if the condition does not match. |
| 6712 | */ |
| 6713 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6714 | ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6715 | ret = acl_pass(ret); |
| 6716 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6717 | ret = !ret; |
| 6718 | |
| 6719 | if (!ret) |
| 6720 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6724 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6725 | if (unlikely(ret < 0)) |
| 6726 | return -1; |
| 6727 | |
| 6728 | if (likely(ret == 0)) { |
| 6729 | /* The filter did not match the request, it can be |
| 6730 | * iterated through all headers. |
| 6731 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6732 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6733 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6734 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6735 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6739 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6740 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6741 | * Try to retrieve the server associated to the appsession. |
| 6742 | * If the server is found, it's assigned to the session. |
| 6743 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6744 | void manage_client_side_appsession(struct session *s, const char *buf, int len) { |
| 6745 | struct http_txn *txn = &s->txn; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6746 | appsess *asession = NULL; |
| 6747 | char *sessid_temp = NULL; |
| 6748 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6749 | if (len > s->be->appsession_len) { |
| 6750 | len = s->be->appsession_len; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6751 | } |
| 6752 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6753 | if (s->be->options2 & PR_O2_AS_REQL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6754 | /* request-learn option is enabled : store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6755 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6756 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6757 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6758 | } |
| 6759 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6760 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6761 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6762 | 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] | 6763 | return; |
| 6764 | } |
| 6765 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6766 | memcpy(txn->sessid, buf, len); |
| 6767 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6768 | } |
| 6769 | |
| 6770 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6771 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6772 | 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] | 6773 | return; |
| 6774 | } |
| 6775 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6776 | memcpy(sessid_temp, buf, len); |
| 6777 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6778 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6779 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6780 | /* free previously allocated memory */ |
| 6781 | pool_free2(apools.sessid, sessid_temp); |
| 6782 | |
| 6783 | if (asession != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6784 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
| 6785 | if (!(s->be->options2 & PR_O2_AS_REQL)) |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6786 | asession->request_count++; |
| 6787 | |
| 6788 | if (asession->serverid != NULL) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6789 | struct server *srv = s->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6790 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6791 | while (srv) { |
| 6792 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6793 | if ((srv->state & SRV_RUNNING) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6794 | (s->be->options & PR_O_PERSIST) || |
| 6795 | (s->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6796 | /* we found the server and it's usable */ |
| 6797 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6798 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6799 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 6800 | s->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6801 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6802 | break; |
| 6803 | } else { |
| 6804 | txn->flags &= ~TX_CK_MASK; |
| 6805 | txn->flags |= TX_CK_DOWN; |
| 6806 | } |
| 6807 | } |
| 6808 | srv = srv->next; |
| 6809 | } |
| 6810 | } |
| 6811 | } |
| 6812 | } |
| 6813 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6814 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6815 | * same way as with headers above except that the semi-colon also ends a token. |
| 6816 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6817 | * return a valid result. |
| 6818 | */ |
| 6819 | char *find_cookie_value_end(char *s, const char *e) |
| 6820 | { |
| 6821 | int quoted, qdpair; |
| 6822 | |
| 6823 | quoted = qdpair = 0; |
| 6824 | for (; s < e; s++) { |
| 6825 | if (qdpair) qdpair = 0; |
| 6826 | else if (quoted) { |
| 6827 | if (*s == '\\') qdpair = 1; |
| 6828 | else if (*s == '"') quoted = 0; |
| 6829 | } |
| 6830 | else if (*s == '"') quoted = 1; |
| 6831 | else if (*s == ',' || *s == ';') return s; |
| 6832 | } |
| 6833 | return s; |
| 6834 | } |
| 6835 | |
| 6836 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6837 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6838 | * the first delimiter is updated if required. The function tries as much as |
| 6839 | * possible to respect the following principles : |
| 6840 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6841 | * colon, in which case <next> is simply removed |
| 6842 | * - set exactly one space character after the new first delimiter, unless |
| 6843 | * there are not enough characters in the block being moved to do so. |
| 6844 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6845 | * one. |
| 6846 | * |
| 6847 | * It is the caller's responsibility to ensure that : |
| 6848 | * - <from> points to a valid delimiter or the colon ; |
| 6849 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6850 | * - there are non-space chars before <from> ; |
| 6851 | * - there is a CR/LF at or after <next>. |
| 6852 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6853 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6854 | { |
| 6855 | char *prev = *from; |
| 6856 | |
| 6857 | if (*prev == ':') { |
| 6858 | /* We're removing the first value, preserve the colon and add a |
| 6859 | * space if possible. |
| 6860 | */ |
| 6861 | if (!http_is_crlf[(unsigned char)*next]) |
| 6862 | next++; |
| 6863 | prev++; |
| 6864 | if (prev < next) |
| 6865 | *prev++ = ' '; |
| 6866 | |
| 6867 | while (http_is_spht[(unsigned char)*next]) |
| 6868 | next++; |
| 6869 | } else { |
| 6870 | /* Remove useless spaces before the old delimiter. */ |
| 6871 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6872 | prev--; |
| 6873 | *from = prev; |
| 6874 | |
| 6875 | /* copy the delimiter and if possible a space if we're |
| 6876 | * not at the end of the line. |
| 6877 | */ |
| 6878 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6879 | *prev++ = *next++; |
| 6880 | if (prev + 1 < next) |
| 6881 | *prev++ = ' '; |
| 6882 | while (http_is_spht[(unsigned char)*next]) |
| 6883 | next++; |
| 6884 | } |
| 6885 | } |
| 6886 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6887 | } |
| 6888 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6889 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6890 | * 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] | 6891 | * desirable to call it only when needed. This code is quite complex because |
| 6892 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6893 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6894 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6895 | void manage_client_side_cookies(struct session *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6896 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6897 | struct http_txn *txn = &s->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6898 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6899 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6900 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6901 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6902 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6903 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6904 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6905 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6906 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6907 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6908 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6909 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6910 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6911 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6912 | hdr_beg = hdr_next; |
| 6913 | hdr_end = hdr_beg + cur_hdr->len; |
| 6914 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6915 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6916 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6917 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6918 | * "Cookie:" headers. |
| 6919 | */ |
| 6920 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6921 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6922 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6923 | old_idx = cur_idx; |
| 6924 | continue; |
| 6925 | } |
| 6926 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6927 | del_from = NULL; /* nothing to be deleted */ |
| 6928 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6929 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6930 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6931 | * attributes whose name begin with a '$', and associate them with |
| 6932 | * the right cookie, if we want to delete this cookie. |
| 6933 | * So there are 3 cases for each cookie read : |
| 6934 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6935 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6936 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6937 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6938 | * "special" cookie. |
| 6939 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6940 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6941 | * *MUST* delete it. |
| 6942 | * |
| 6943 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6944 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6945 | * the RFC explicitly allows spaces before it, and not within the |
| 6946 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6947 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6948 | * after the equal sign too, resulting in some (rare) buggy |
| 6949 | * implementations trying to do that. So let's do what servers do. |
| 6950 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6951 | * allowed quoted strings in values, with any possible character |
| 6952 | * after a backslash, including control chars and delimitors, which |
| 6953 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6954 | * within values even without quotes. |
| 6955 | * |
| 6956 | * We have to keep multiple pointers in order to support cookie |
| 6957 | * removal at the beginning, middle or end of header without |
| 6958 | * corrupting the header. All of these headers are valid : |
| 6959 | * |
| 6960 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6961 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6962 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6963 | * | | | | | | | | | |
| 6964 | * | | | | | | | | hdr_end <--+ |
| 6965 | * | | | | | | | +--> next |
| 6966 | * | | | | | | +----> val_end |
| 6967 | * | | | | | +-----------> val_beg |
| 6968 | * | | | | +--------------> equal |
| 6969 | * | | | +----------------> att_end |
| 6970 | * | | +---------------------> att_beg |
| 6971 | * | +--------------------------> prev |
| 6972 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6973 | */ |
| 6974 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6975 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6976 | /* Iterate through all cookies on this line */ |
| 6977 | |
| 6978 | /* find att_beg */ |
| 6979 | att_beg = prev + 1; |
| 6980 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6981 | att_beg++; |
| 6982 | |
| 6983 | /* find att_end : this is the first character after the last non |
| 6984 | * space before the equal. It may be equal to hdr_end. |
| 6985 | */ |
| 6986 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6987 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6988 | while (equal < hdr_end) { |
| 6989 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6990 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6991 | if (http_is_spht[(unsigned char)*equal++]) |
| 6992 | continue; |
| 6993 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6994 | } |
| 6995 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6996 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6997 | * is between <att_beg> and <equal>, both may be identical. |
| 6998 | */ |
| 6999 | |
| 7000 | /* look for end of cookie if there is an equal sign */ |
| 7001 | if (equal < hdr_end && *equal == '=') { |
| 7002 | /* look for the beginning of the value */ |
| 7003 | val_beg = equal + 1; |
| 7004 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7005 | val_beg++; |
| 7006 | |
| 7007 | /* find the end of the value, respecting quotes */ |
| 7008 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7009 | |
| 7010 | /* make val_end point to the first white space or delimitor after the value */ |
| 7011 | val_end = next; |
| 7012 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7013 | val_end--; |
| 7014 | } else { |
| 7015 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7016 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7017 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7018 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7019 | * they will automatically be removed if a header before them is removed, |
| 7020 | * since they're supposed to be linked together. |
| 7021 | */ |
| 7022 | if (*att_beg == '$') |
| 7023 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7024 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7025 | /* Ignore cookies with no equal sign */ |
| 7026 | if (equal == next) { |
| 7027 | /* This is not our cookie, so we must preserve it. But if we already |
| 7028 | * scheduled another cookie for removal, we cannot remove the |
| 7029 | * complete header, but we can remove the previous block itself. |
| 7030 | */ |
| 7031 | preserve_hdr = 1; |
| 7032 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7033 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7034 | val_end += delta; |
| 7035 | next += delta; |
| 7036 | hdr_end += delta; |
| 7037 | hdr_next += delta; |
| 7038 | cur_hdr->len += delta; |
| 7039 | http_msg_move_end(&txn->req, delta); |
| 7040 | prev = del_from; |
| 7041 | del_from = NULL; |
| 7042 | } |
| 7043 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7044 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7045 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7046 | /* if there are spaces around the equal sign, we need to |
| 7047 | * strip them otherwise we'll get trouble for cookie captures, |
| 7048 | * or even for rewrites. Since this happens extremely rarely, |
| 7049 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7050 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7051 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7052 | int stripped_before = 0; |
| 7053 | int stripped_after = 0; |
| 7054 | |
| 7055 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7056 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7057 | equal += stripped_before; |
| 7058 | val_beg += stripped_before; |
| 7059 | } |
| 7060 | |
| 7061 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7062 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7063 | val_beg += stripped_after; |
| 7064 | stripped_before += stripped_after; |
| 7065 | } |
| 7066 | |
| 7067 | val_end += stripped_before; |
| 7068 | next += stripped_before; |
| 7069 | hdr_end += stripped_before; |
| 7070 | hdr_next += stripped_before; |
| 7071 | cur_hdr->len += stripped_before; |
| 7072 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7073 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7074 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7075 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7076 | /* First, let's see if we want to capture this cookie. We check |
| 7077 | * that we don't already have a client side cookie, because we |
| 7078 | * can only capture one. Also as an optimisation, we ignore |
| 7079 | * cookies shorter than the declared name. |
| 7080 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7081 | if (s->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7082 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7083 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7084 | int log_len = val_end - att_beg; |
| 7085 | |
| 7086 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7087 | Alert("HTTP logging : out of memory.\n"); |
| 7088 | } else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7089 | if (log_len > s->fe->capture_len) |
| 7090 | log_len = s->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7091 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7092 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7093 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7094 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7095 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7096 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7097 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7098 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7099 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7100 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7101 | * For cookies in prefix mode, the form is : |
| 7102 | * |
| 7103 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7104 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7105 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7106 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7107 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7108 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7109 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7110 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7111 | * have the server ID between val_beg and delim, and the original cookie between |
| 7112 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7113 | * |
| 7114 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7115 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7116 | * | || || | |+-> next |
| 7117 | * | || || | +--> val_end |
| 7118 | * | || || +---------> delim |
| 7119 | * | || |+------------> val_beg |
| 7120 | * | || +-------------> att_end = equal |
| 7121 | * | |+-----------------> att_beg |
| 7122 | * | +------------------> prev |
| 7123 | * +-------------------------> hdr_beg |
| 7124 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7125 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7126 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7127 | for (delim = val_beg; delim < val_end; delim++) |
| 7128 | if (*delim == COOKIE_DELIM) |
| 7129 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7130 | } else { |
| 7131 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7132 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7133 | /* Now check if the cookie contains a date field, which would |
| 7134 | * appear after a vertical bar ('|') just after the server name |
| 7135 | * and before the delimiter. |
| 7136 | */ |
| 7137 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7138 | if (vbar1) { |
| 7139 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7140 | * right is the last seen date. It is a base64 encoded |
| 7141 | * 30-bit value representing the UNIX date since the |
| 7142 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7143 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7144 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7145 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7146 | if (val_end - vbar1 >= 5) { |
| 7147 | val = b64tos30(vbar1); |
| 7148 | if (val > 0) |
| 7149 | txn->cookie_last_date = val << 2; |
| 7150 | } |
| 7151 | /* look for a second vertical bar */ |
| 7152 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7153 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7154 | val = b64tos30(vbar1 + 1); |
| 7155 | if (val > 0) |
| 7156 | txn->cookie_first_date = val << 2; |
| 7157 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7158 | } |
| 7159 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7160 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7161 | /* if the cookie has an expiration date and the proxy wants to check |
| 7162 | * it, then we do that now. We first check if the cookie is too old, |
| 7163 | * then only if it has expired. We detect strict overflow because the |
| 7164 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7165 | * in the future are ignored if their offset is beyond one day. This |
| 7166 | * allows an admin to fix timezone issues without expiring everyone |
| 7167 | * and at the same time avoids keeping unwanted side effects for too |
| 7168 | * long. |
| 7169 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7170 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7171 | (((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] | 7172 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7173 | txn->flags &= ~TX_CK_MASK; |
| 7174 | txn->flags |= TX_CK_OLD; |
| 7175 | delim = val_beg; // let's pretend we have not found the cookie |
| 7176 | txn->cookie_first_date = 0; |
| 7177 | txn->cookie_last_date = 0; |
| 7178 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7179 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7180 | (((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] | 7181 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7182 | txn->flags &= ~TX_CK_MASK; |
| 7183 | txn->flags |= TX_CK_EXPIRED; |
| 7184 | delim = val_beg; // let's pretend we have not found the cookie |
| 7185 | txn->cookie_first_date = 0; |
| 7186 | txn->cookie_last_date = 0; |
| 7187 | } |
| 7188 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7189 | /* Here, we'll look for the first running server which supports the cookie. |
| 7190 | * This allows to share a same cookie between several servers, for example |
| 7191 | * to dedicate backup servers to specific servers only. |
| 7192 | * However, to prevent clients from sticking to cookie-less backup server |
| 7193 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7194 | * empty cookies and mark them as invalid. |
| 7195 | * The same behaviour is applied when persistence must be ignored. |
| 7196 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7197 | if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7198 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7199 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7200 | while (srv) { |
| 7201 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7202 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 7203 | if ((srv->state & SRV_RUNNING) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7204 | (s->be->options & PR_O_PERSIST) || |
| 7205 | (s->flags & SN_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7206 | /* we found the server and we can use it */ |
| 7207 | txn->flags &= ~TX_CK_MASK; |
| 7208 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7209 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 7210 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7211 | break; |
| 7212 | } else { |
| 7213 | /* we found a server, but it's down, |
| 7214 | * mark it as such and go on in case |
| 7215 | * another one is available. |
| 7216 | */ |
| 7217 | txn->flags &= ~TX_CK_MASK; |
| 7218 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7219 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7220 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7221 | srv = srv->next; |
| 7222 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7223 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7224 | 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] | 7225 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7226 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7227 | if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7228 | txn->flags |= TX_CK_UNUSED; |
| 7229 | else |
| 7230 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7231 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7232 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7233 | /* depending on the cookie mode, we may have to either : |
| 7234 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7235 | * the server never sees it ; |
| 7236 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7237 | * application cookie so that it does not get accidentely removed later, |
| 7238 | * if we're in cookie prefix mode |
| 7239 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7240 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7241 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7242 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7243 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7244 | val_end += delta; |
| 7245 | next += delta; |
| 7246 | hdr_end += delta; |
| 7247 | hdr_next += delta; |
| 7248 | cur_hdr->len += delta; |
| 7249 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7250 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7251 | del_from = NULL; |
| 7252 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7253 | } |
| 7254 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7255 | (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] | 7256 | del_from = prev; |
| 7257 | } |
| 7258 | } else { |
| 7259 | /* This is not our cookie, so we must preserve it. But if we already |
| 7260 | * scheduled another cookie for removal, we cannot remove the |
| 7261 | * complete header, but we can remove the previous block itself. |
| 7262 | */ |
| 7263 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7264 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7265 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7266 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7267 | if (att_beg >= del_from) |
| 7268 | att_beg += delta; |
| 7269 | if (att_end >= del_from) |
| 7270 | att_end += delta; |
| 7271 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7272 | val_end += delta; |
| 7273 | next += delta; |
| 7274 | hdr_end += delta; |
| 7275 | hdr_next += delta; |
| 7276 | cur_hdr->len += delta; |
| 7277 | http_msg_move_end(&txn->req, delta); |
| 7278 | prev = del_from; |
| 7279 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7280 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7281 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7282 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7283 | /* Look for the appsession cookie unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7284 | if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7285 | int cmp_len, value_len; |
| 7286 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7287 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7288 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7289 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7290 | value_begin = att_beg + s->be->appsession_name_len; |
| 7291 | value_len = val_end - att_beg - s->be->appsession_name_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7292 | } else { |
| 7293 | cmp_len = att_end - att_beg; |
| 7294 | value_begin = val_beg; |
| 7295 | value_len = val_end - val_beg; |
| 7296 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7297 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7298 | /* let's see if the cookie is our appcookie */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7299 | if (cmp_len == s->be->appsession_name_len && |
| 7300 | memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) { |
| 7301 | manage_client_side_appsession(s, value_begin, value_len); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7302 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7303 | } |
| 7304 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7305 | /* continue with next cookie on this header line */ |
| 7306 | att_beg = next; |
| 7307 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7308 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7309 | /* There are no more cookies on this line. |
| 7310 | * We may still have one (or several) marked for deletion at the |
| 7311 | * end of the line. We must do this now in two ways : |
| 7312 | * - if some cookies must be preserved, we only delete from the |
| 7313 | * mark to the end of line ; |
| 7314 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7315 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7316 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7317 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7318 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7319 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7320 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7321 | cur_hdr->len += delta; |
| 7322 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7323 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7324 | |
| 7325 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7326 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7327 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7328 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7329 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7330 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7331 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7332 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7333 | } |
| 7334 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7335 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7336 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7337 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7338 | } |
| 7339 | |
| 7340 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7341 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7342 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7343 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7344 | int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7345 | { |
| 7346 | char term; |
| 7347 | char *cur_ptr, *cur_end, *cur_next; |
| 7348 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7349 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7350 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7351 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7352 | |
| 7353 | last_hdr = 0; |
| 7354 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7355 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7356 | old_idx = 0; |
| 7357 | |
| 7358 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7359 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7360 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7361 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7362 | (exp->action == ACT_ALLOW || |
| 7363 | exp->action == ACT_DENY)) |
| 7364 | return 0; |
| 7365 | |
| 7366 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7367 | if (!cur_idx) |
| 7368 | break; |
| 7369 | |
| 7370 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7371 | cur_ptr = cur_next; |
| 7372 | cur_end = cur_ptr + cur_hdr->len; |
| 7373 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7374 | |
| 7375 | /* Now we have one header between cur_ptr and cur_end, |
| 7376 | * and the next header starts at cur_next. |
| 7377 | */ |
| 7378 | |
| 7379 | /* The annoying part is that pattern matching needs |
| 7380 | * that we modify the contents to null-terminate all |
| 7381 | * strings before testing them. |
| 7382 | */ |
| 7383 | |
| 7384 | term = *cur_end; |
| 7385 | *cur_end = '\0'; |
| 7386 | |
| 7387 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7388 | switch (exp->action) { |
| 7389 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7390 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7391 | last_hdr = 1; |
| 7392 | break; |
| 7393 | |
| 7394 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7395 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7396 | last_hdr = 1; |
| 7397 | break; |
| 7398 | |
| 7399 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7400 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7401 | 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] | 7402 | /* FIXME: if the user adds a newline in the replacement, the |
| 7403 | * index will not be recalculated for now, and the new line |
| 7404 | * will not be counted as a new header. |
| 7405 | */ |
| 7406 | |
| 7407 | cur_end += delta; |
| 7408 | cur_next += delta; |
| 7409 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7410 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7411 | break; |
| 7412 | |
| 7413 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7414 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7415 | cur_next += delta; |
| 7416 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7417 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7418 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7419 | txn->hdr_idx.used--; |
| 7420 | cur_hdr->len = 0; |
| 7421 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7422 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7423 | break; |
| 7424 | |
| 7425 | } |
| 7426 | } |
| 7427 | if (cur_end) |
| 7428 | *cur_end = term; /* restore the string terminator */ |
| 7429 | |
| 7430 | /* keep the link from this header to next one in case of later |
| 7431 | * removal of next header. |
| 7432 | */ |
| 7433 | old_idx = cur_idx; |
| 7434 | } |
| 7435 | return 0; |
| 7436 | } |
| 7437 | |
| 7438 | |
| 7439 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7440 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7441 | * or -1 if a replacement resulted in an invalid status line. |
| 7442 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7443 | int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7444 | { |
| 7445 | char term; |
| 7446 | char *cur_ptr, *cur_end; |
| 7447 | int done; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7448 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7449 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7450 | |
| 7451 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7452 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7453 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7454 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7455 | (exp->action == ACT_ALLOW || |
| 7456 | exp->action == ACT_DENY)) |
| 7457 | return 0; |
| 7458 | else if (exp->action == ACT_REMOVE) |
| 7459 | return 0; |
| 7460 | |
| 7461 | done = 0; |
| 7462 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7463 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7464 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7465 | |
| 7466 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7467 | |
| 7468 | /* The annoying part is that pattern matching needs |
| 7469 | * that we modify the contents to null-terminate all |
| 7470 | * strings before testing them. |
| 7471 | */ |
| 7472 | |
| 7473 | term = *cur_end; |
| 7474 | *cur_end = '\0'; |
| 7475 | |
| 7476 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7477 | switch (exp->action) { |
| 7478 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7479 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7480 | done = 1; |
| 7481 | break; |
| 7482 | |
| 7483 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7484 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7485 | done = 1; |
| 7486 | break; |
| 7487 | |
| 7488 | case ACT_REPLACE: |
| 7489 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7490 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7491 | 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] | 7492 | /* FIXME: if the user adds a newline in the replacement, the |
| 7493 | * index will not be recalculated for now, and the new line |
| 7494 | * will not be counted as a new header. |
| 7495 | */ |
| 7496 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7497 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7498 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7499 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7500 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7501 | cur_ptr, cur_end + 1, |
| 7502 | NULL, NULL); |
| 7503 | if (unlikely(!cur_end)) |
| 7504 | return -1; |
| 7505 | |
| 7506 | /* we have a full respnse and we know that we have either a CR |
| 7507 | * or an LF at <ptr>. |
| 7508 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7509 | 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] | 7510 | 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] | 7511 | /* there is no point trying this regex on headers */ |
| 7512 | return 1; |
| 7513 | } |
| 7514 | } |
| 7515 | *cur_end = term; /* restore the string terminator */ |
| 7516 | return done; |
| 7517 | } |
| 7518 | |
| 7519 | |
| 7520 | |
| 7521 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7522 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7523 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7524 | * unparsable response. |
| 7525 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7526 | int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7527 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7528 | struct http_txn *txn = &s->txn; |
| 7529 | struct hdr_exp *exp; |
| 7530 | |
| 7531 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7532 | int ret; |
| 7533 | |
| 7534 | /* |
| 7535 | * The interleaving of transformations and verdicts |
| 7536 | * makes it difficult to decide to continue or stop |
| 7537 | * the evaluation. |
| 7538 | */ |
| 7539 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7540 | if (txn->flags & TX_SVDENY) |
| 7541 | break; |
| 7542 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7543 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7544 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7545 | exp->action == ACT_PASS)) { |
| 7546 | exp = exp->next; |
| 7547 | continue; |
| 7548 | } |
| 7549 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7550 | /* if this filter had a condition, evaluate it now and skip to |
| 7551 | * next filter if the condition does not match. |
| 7552 | */ |
| 7553 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7554 | ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7555 | ret = acl_pass(ret); |
| 7556 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7557 | ret = !ret; |
| 7558 | if (!ret) |
| 7559 | continue; |
| 7560 | } |
| 7561 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7562 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7563 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7564 | if (unlikely(ret < 0)) |
| 7565 | return -1; |
| 7566 | |
| 7567 | if (likely(ret == 0)) { |
| 7568 | /* The filter did not match the response, it can be |
| 7569 | * iterated through all headers. |
| 7570 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7571 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7572 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7573 | } |
| 7574 | return 0; |
| 7575 | } |
| 7576 | |
| 7577 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7578 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7579 | * 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] | 7580 | * desirable to call it only when needed. This function is also used when we |
| 7581 | * 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] | 7582 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7583 | void manage_server_side_cookies(struct session *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7584 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7585 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7586 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7587 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7588 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7589 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7590 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7591 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7592 | /* Iterate through the headers. |
| 7593 | * we start with the start line. |
| 7594 | */ |
| 7595 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7596 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7597 | |
| 7598 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7599 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7600 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7601 | |
| 7602 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7603 | hdr_beg = hdr_next; |
| 7604 | hdr_end = hdr_beg + cur_hdr->len; |
| 7605 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7606 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7607 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7608 | * next header starts at hdr_next. We're only interested in |
| 7609 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7610 | */ |
| 7611 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7612 | is_cookie2 = 0; |
| 7613 | prev = hdr_beg + 10; |
| 7614 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7615 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7616 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7617 | if (!val) { |
| 7618 | old_idx = cur_idx; |
| 7619 | continue; |
| 7620 | } |
| 7621 | is_cookie2 = 1; |
| 7622 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7623 | } |
| 7624 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7625 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7626 | * <prev> points to the colon. |
| 7627 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7628 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7629 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7630 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7631 | * check-cache is enabled) and we are not interested in checking |
| 7632 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7633 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7634 | if (s->be->cookie_name == NULL && |
| 7635 | s->be->appsession_name == NULL && |
| 7636 | s->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7637 | return; |
| 7638 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7639 | /* OK so now we know we have to process this response cookie. |
| 7640 | * The format of the Set-Cookie header is slightly different |
| 7641 | * from the format of the Cookie header in that it does not |
| 7642 | * support the comma as a cookie delimiter (thus the header |
| 7643 | * cannot be folded) because the Expires attribute described in |
| 7644 | * the original Netscape's spec may contain an unquoted date |
| 7645 | * with a comma inside. We have to live with this because |
| 7646 | * many browsers don't support Max-Age and some browsers don't |
| 7647 | * support quoted strings. However the Set-Cookie2 header is |
| 7648 | * clean. |
| 7649 | * |
| 7650 | * We have to keep multiple pointers in order to support cookie |
| 7651 | * removal at the beginning, middle or end of header without |
| 7652 | * corrupting the header (in case of set-cookie2). A special |
| 7653 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7654 | * fields after the first semi-colon. The <next> pointer points |
| 7655 | * either to the end of line (set-cookie) or next unquoted comma |
| 7656 | * (set-cookie2). All of these headers are valid : |
| 7657 | * |
| 7658 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7659 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7660 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7661 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7662 | * | | | | | | | | | | |
| 7663 | * | | | | | | | | +-> next hdr_end <--+ |
| 7664 | * | | | | | | | +------------> scav |
| 7665 | * | | | | | | +--------------> val_end |
| 7666 | * | | | | | +--------------------> val_beg |
| 7667 | * | | | | +----------------------> equal |
| 7668 | * | | | +------------------------> att_end |
| 7669 | * | | +----------------------------> att_beg |
| 7670 | * | +------------------------------> prev |
| 7671 | * +-----------------------------------------> hdr_beg |
| 7672 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7673 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7674 | for (; prev < hdr_end; prev = next) { |
| 7675 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7676 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7677 | /* find att_beg */ |
| 7678 | att_beg = prev + 1; |
| 7679 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7680 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7681 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7682 | /* find att_end : this is the first character after the last non |
| 7683 | * space before the equal. It may be equal to hdr_end. |
| 7684 | */ |
| 7685 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7686 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7687 | while (equal < hdr_end) { |
| 7688 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7689 | break; |
| 7690 | if (http_is_spht[(unsigned char)*equal++]) |
| 7691 | continue; |
| 7692 | att_end = equal; |
| 7693 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7694 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7695 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7696 | * is between <att_beg> and <equal>, both may be identical. |
| 7697 | */ |
| 7698 | |
| 7699 | /* look for end of cookie if there is an equal sign */ |
| 7700 | if (equal < hdr_end && *equal == '=') { |
| 7701 | /* look for the beginning of the value */ |
| 7702 | val_beg = equal + 1; |
| 7703 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7704 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7705 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7706 | /* find the end of the value, respecting quotes */ |
| 7707 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7708 | |
| 7709 | /* make val_end point to the first white space or delimitor after the value */ |
| 7710 | val_end = next; |
| 7711 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7712 | val_end--; |
| 7713 | } else { |
| 7714 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7715 | val_beg = val_end = next = equal; |
| 7716 | } |
| 7717 | |
| 7718 | if (next < hdr_end) { |
| 7719 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7720 | * a colon or semi-colon before the end. So skip all attr-value |
| 7721 | * pairs and look for the next comma. For Set-Cookie, since |
| 7722 | * commas are permitted in values, skip to the end. |
| 7723 | */ |
| 7724 | if (is_cookie2) |
| 7725 | next = find_hdr_value_end(next, hdr_end); |
| 7726 | else |
| 7727 | next = hdr_end; |
| 7728 | } |
| 7729 | |
| 7730 | /* Now everything is as on the diagram above */ |
| 7731 | |
| 7732 | /* Ignore cookies with no equal sign */ |
| 7733 | if (equal == val_end) |
| 7734 | continue; |
| 7735 | |
| 7736 | /* If there are spaces around the equal sign, we need to |
| 7737 | * strip them otherwise we'll get trouble for cookie captures, |
| 7738 | * or even for rewrites. Since this happens extremely rarely, |
| 7739 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7740 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7741 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7742 | int stripped_before = 0; |
| 7743 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7744 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7745 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7746 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7747 | equal += stripped_before; |
| 7748 | val_beg += stripped_before; |
| 7749 | } |
| 7750 | |
| 7751 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7752 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7753 | val_beg += stripped_after; |
| 7754 | stripped_before += stripped_after; |
| 7755 | } |
| 7756 | |
| 7757 | val_end += stripped_before; |
| 7758 | next += stripped_before; |
| 7759 | hdr_end += stripped_before; |
| 7760 | hdr_next += stripped_before; |
| 7761 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7762 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | /* First, let's see if we want to capture this cookie. We check |
| 7766 | * that we don't already have a server side cookie, because we |
| 7767 | * can only capture one. Also as an optimisation, we ignore |
| 7768 | * cookies shorter than the declared name. |
| 7769 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7770 | if (s->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7771 | txn->srv_cookie == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7772 | (val_end - att_beg >= s->fe->capture_namelen) && |
| 7773 | memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7774 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7775 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7776 | Alert("HTTP logging : out of memory.\n"); |
| 7777 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7778 | else { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7779 | if (log_len > s->fe->capture_len) |
| 7780 | log_len = s->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7781 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7782 | txn->srv_cookie[log_len] = 0; |
| 7783 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7784 | } |
| 7785 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7786 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7787 | /* now check if we need to process it for persistence */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7788 | if (!(s->flags & SN_IGNORE_PRST) && |
| 7789 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7790 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7791 | /* assume passive cookie by default */ |
| 7792 | txn->flags &= ~TX_SCK_MASK; |
| 7793 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7794 | |
| 7795 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7796 | * this occurrence because we'll insert another one later. |
| 7797 | * 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] | 7798 | * a direct access. |
| 7799 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7800 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7801 | /* The "preserve" flag was set, we don't want to touch the |
| 7802 | * server's cookie. |
| 7803 | */ |
| 7804 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7805 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 7806 | ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7807 | /* this cookie must be deleted */ |
| 7808 | if (*prev == ':' && next == hdr_end) { |
| 7809 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7810 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7811 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7812 | txn->hdr_idx.used--; |
| 7813 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7814 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7815 | hdr_next += delta; |
| 7816 | http_msg_move_end(&txn->rsp, delta); |
| 7817 | /* note: while both invalid now, <next> and <hdr_end> |
| 7818 | * are still equal, so the for() will stop as expected. |
| 7819 | */ |
| 7820 | } else { |
| 7821 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7822 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7823 | next = prev; |
| 7824 | hdr_end += delta; |
| 7825 | hdr_next += delta; |
| 7826 | cur_hdr->len += delta; |
| 7827 | http_msg_move_end(&txn->rsp, delta); |
| 7828 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7829 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7830 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7831 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7832 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7833 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7834 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7835 | * with this server since we know it. |
| 7836 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7837 | 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] | 7838 | next += delta; |
| 7839 | hdr_end += delta; |
| 7840 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7841 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7842 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7843 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7844 | txn->flags &= ~TX_SCK_MASK; |
| 7845 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7846 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7847 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7848 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7849 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7850 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7851 | 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] | 7852 | next += delta; |
| 7853 | hdr_end += delta; |
| 7854 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7855 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7856 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7857 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7858 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7859 | txn->flags &= ~TX_SCK_MASK; |
| 7860 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7861 | } |
| 7862 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7863 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7864 | else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7865 | int cmp_len, value_len; |
| 7866 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7867 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7868 | if (s->be->options2 & PR_O2_AS_PFX) { |
| 7869 | cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len); |
| 7870 | value_begin = att_beg + s->be->appsession_name_len; |
| 7871 | 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] | 7872 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7873 | cmp_len = att_end - att_beg; |
| 7874 | value_begin = val_beg; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7875 | value_len = MIN(s->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7876 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7877 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7878 | if ((cmp_len == s->be->appsession_name_len) && |
| 7879 | (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] | 7880 | /* free a possibly previously allocated memory */ |
| 7881 | pool_free2(apools.sessid, txn->sessid); |
| 7882 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7883 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7884 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7885 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7886 | 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] | 7887 | return; |
| 7888 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7889 | memcpy(txn->sessid, value_begin, value_len); |
| 7890 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7891 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7892 | } |
| 7893 | /* that's done for this cookie, check the next one on the same |
| 7894 | * line when next != hdr_end (only if is_cookie2). |
| 7895 | */ |
| 7896 | } |
| 7897 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7898 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7899 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7900 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7901 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7902 | appsess *asession = NULL; |
| 7903 | /* only do insert, if lookup fails */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7904 | asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7905 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7906 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7907 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7908 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7909 | 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] | 7910 | return; |
| 7911 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7912 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7913 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7914 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7915 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7916 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7917 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7918 | return; |
| 7919 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7920 | memcpy(asession->sessid, txn->sessid, s->be->appsession_len); |
| 7921 | asession->sessid[s->be->appsession_len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7922 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7923 | server_id_len = strlen(objt_server(s->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7924 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7925 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7926 | send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7927 | s->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7928 | return; |
| 7929 | } |
| 7930 | asession->serverid[0] = '\0'; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7931 | memcpy(asession->serverid, objt_server(s->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7932 | |
| 7933 | asession->request_count = 0; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7934 | appsession_hash_insert(&(s->be->htbl_proxy), asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7935 | } |
| 7936 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7937 | asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7938 | asession->request_count++; |
| 7939 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7940 | } |
| 7941 | |
| 7942 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7943 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7944 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7945 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7946 | void check_response_for_cacheability(struct session *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7947 | { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7948 | struct http_txn *txn = &s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7949 | char *p1, *p2; |
| 7950 | |
| 7951 | char *cur_ptr, *cur_end, *cur_next; |
| 7952 | int cur_idx; |
| 7953 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7954 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7955 | return; |
| 7956 | |
| 7957 | /* Iterate through the headers. |
| 7958 | * we start with the start line. |
| 7959 | */ |
| 7960 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7961 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7962 | |
| 7963 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7964 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7965 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7966 | |
| 7967 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7968 | cur_ptr = cur_next; |
| 7969 | cur_end = cur_ptr + cur_hdr->len; |
| 7970 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7971 | |
| 7972 | /* We have one full header between cur_ptr and cur_end, and the |
| 7973 | * next header starts at cur_next. We're only interested in |
| 7974 | * "Cookie:" headers. |
| 7975 | */ |
| 7976 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7977 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7978 | if (val) { |
| 7979 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7980 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7981 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7982 | return; |
| 7983 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7984 | } |
| 7985 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7986 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7987 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7988 | continue; |
| 7989 | |
| 7990 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7991 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7992 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7993 | |
| 7994 | if (p1 >= cur_end) /* no more info */ |
| 7995 | continue; |
| 7996 | |
| 7997 | /* p1 is at the beginning of the value */ |
| 7998 | p2 = p1; |
| 7999 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8000 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8001 | p2++; |
| 8002 | |
| 8003 | /* we have a complete value between p1 and p2 */ |
| 8004 | if (p2 < cur_end && *p2 == '=') { |
| 8005 | /* we have something of the form no-cache="set-cookie" */ |
| 8006 | if ((cur_end - p1 >= 21) && |
| 8007 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8008 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8009 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8010 | continue; |
| 8011 | } |
| 8012 | |
| 8013 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8014 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8015 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8016 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8017 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8018 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8019 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8020 | return; |
| 8021 | } |
| 8022 | |
| 8023 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8024 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8025 | continue; |
| 8026 | } |
| 8027 | } |
| 8028 | } |
| 8029 | |
| 8030 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8031 | /* |
| 8032 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 8033 | * If the server is found, it's assigned to the session. |
| 8034 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8035 | void get_srv_from_appsession(struct session *s, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8036 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8037 | char *end_params, *first_param, *cur_param, *next_param; |
| 8038 | char separator; |
| 8039 | int value_len; |
| 8040 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8041 | int mode = s->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8042 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8043 | if (s->be->appsession_name == NULL || |
| 8044 | (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] | 8045 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8046 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8047 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8048 | first_param = NULL; |
| 8049 | switch (mode) { |
| 8050 | case PR_O2_AS_M_PP: |
| 8051 | first_param = memchr(begin, ';', len); |
| 8052 | break; |
| 8053 | case PR_O2_AS_M_QS: |
| 8054 | first_param = memchr(begin, '?', len); |
| 8055 | break; |
| 8056 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8057 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8058 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8059 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8060 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8061 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8062 | switch (mode) { |
| 8063 | case PR_O2_AS_M_PP: |
| 8064 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 8065 | end_params = (char *) begin + len; |
| 8066 | } |
| 8067 | separator = ';'; |
| 8068 | break; |
| 8069 | case PR_O2_AS_M_QS: |
| 8070 | end_params = (char *) begin + len; |
| 8071 | separator = '&'; |
| 8072 | break; |
| 8073 | default: |
| 8074 | /* unknown mode, shouldn't happen */ |
| 8075 | return; |
| 8076 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8077 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8078 | cur_param = next_param = end_params; |
| 8079 | while (cur_param > first_param) { |
| 8080 | cur_param--; |
| 8081 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 8082 | /* let's see if this is the appsession parameter */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8083 | if ((cur_param + s->be->appsession_name_len + 1 < next_param) && |
| 8084 | ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') && |
| 8085 | (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] | 8086 | /* Cool... it's the right one */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8087 | cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 8088 | value_len = MIN(s->be->appsession_len, next_param - cur_param); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8089 | if (value_len > 0) { |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8090 | manage_client_side_appsession(s, cur_param, value_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 8091 | } |
| 8092 | break; |
| 8093 | } |
| 8094 | next_param = cur_param; |
| 8095 | } |
| 8096 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8097 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 8098 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8099 | appsession_hash_dump(&(s->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8100 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8101 | } |
| 8102 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8103 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8104 | * 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] | 8105 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8106 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8107 | * 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] | 8108 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8109 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8110 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8111 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8112 | 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] | 8113 | { |
| 8114 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8115 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8116 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8117 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8118 | if (!uri_auth) |
| 8119 | return 0; |
| 8120 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8121 | 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] | 8122 | return 0; |
| 8123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8124 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8125 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8126 | return 0; |
| 8127 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8128 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8129 | return 0; |
| 8130 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8131 | return 1; |
| 8132 | } |
| 8133 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8134 | /* |
| 8135 | * 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] | 8136 | * By default it tries to report the error position as msg->err_pos. However if |
| 8137 | * this one is not set, it will then report msg->next, which is the last known |
| 8138 | * 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] | 8139 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8140 | */ |
| 8141 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8142 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8143 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8144 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8145 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8146 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8147 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8148 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8149 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8150 | len1 = MIN(len1, es->len); |
| 8151 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8152 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8153 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8154 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8155 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8156 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8157 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8158 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8159 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8160 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8161 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8162 | es->when = date; // user-visible date |
| 8163 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8164 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8165 | es->oe = other_end; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8166 | if (objt_conn(s->req->prod->end)) |
| 8167 | es->src = __objt_conn(s->req->prod->end)->addr.from; |
| 8168 | else |
| 8169 | memset(&es->src, 0, sizeof(es->src)); |
| 8170 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8171 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8172 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8173 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8174 | es->s_flags = s->flags; |
| 8175 | es->t_flags = s->txn.flags; |
| 8176 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8177 | es->b_out = chn->buf->o; |
| 8178 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8179 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8180 | es->m_clen = msg->chunk_len; |
| 8181 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8182 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8183 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8184 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8185 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8186 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8187 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8188 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8189 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8190 | * 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] | 8191 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8192 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8193 | * 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] | 8194 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8195 | 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] | 8196 | struct hdr_idx *idx, int occ, |
| 8197 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8198 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8199 | struct hdr_ctx local_ctx; |
| 8200 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8201 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8202 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8203 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8204 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8205 | if (!ctx) { |
| 8206 | local_ctx.idx = 0; |
| 8207 | ctx = &local_ctx; |
| 8208 | } |
| 8209 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8210 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8211 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8212 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8213 | occ--; |
| 8214 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8215 | *vptr = ctx->line + ctx->val; |
| 8216 | *vlen = ctx->vlen; |
| 8217 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8218 | } |
| 8219 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8220 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | /* negative occurrence, we scan all the list then walk back */ |
| 8224 | if (-occ > MAX_HDR_HISTORY) |
| 8225 | return 0; |
| 8226 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8227 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8228 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8229 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8230 | len_hist[hist_ptr] = ctx->vlen; |
| 8231 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8232 | hist_ptr = 0; |
| 8233 | found++; |
| 8234 | } |
| 8235 | if (-occ > found) |
| 8236 | return 0; |
| 8237 | /* 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] | 8238 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8239 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8240 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8241 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8242 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8243 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8244 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8245 | *vptr = ptr_hist[hist_ptr]; |
| 8246 | *vlen = len_hist[hist_ptr]; |
| 8247 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8248 | } |
| 8249 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8250 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8251 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8252 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8253 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8254 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8255 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8256 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8257 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8258 | * line header values and does not stop at commas. |
| 8259 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8260 | */ |
| 8261 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8262 | struct hdr_idx *idx, int occ, |
| 8263 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8264 | { |
| 8265 | struct hdr_ctx local_ctx; |
| 8266 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8267 | int len_hist[MAX_HDR_HISTORY]; |
| 8268 | unsigned int hist_ptr; |
| 8269 | int found; |
| 8270 | |
| 8271 | if (!ctx) { |
| 8272 | local_ctx.idx = 0; |
| 8273 | ctx = &local_ctx; |
| 8274 | } |
| 8275 | |
| 8276 | if (occ >= 0) { |
| 8277 | /* search from the beginning */ |
| 8278 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8279 | occ--; |
| 8280 | if (occ <= 0) { |
| 8281 | *vptr = ctx->line + ctx->val; |
| 8282 | *vlen = ctx->vlen; |
| 8283 | return 1; |
| 8284 | } |
| 8285 | } |
| 8286 | return 0; |
| 8287 | } |
| 8288 | |
| 8289 | /* negative occurrence, we scan all the list then walk back */ |
| 8290 | if (-occ > MAX_HDR_HISTORY) |
| 8291 | return 0; |
| 8292 | |
| 8293 | found = hist_ptr = 0; |
| 8294 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8295 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8296 | len_hist[hist_ptr] = ctx->vlen; |
| 8297 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8298 | hist_ptr = 0; |
| 8299 | found++; |
| 8300 | } |
| 8301 | if (-occ > found) |
| 8302 | return 0; |
| 8303 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8304 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8305 | */ |
| 8306 | hist_ptr += occ; |
| 8307 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8308 | hist_ptr -= MAX_HDR_HISTORY; |
| 8309 | *vptr = ptr_hist[hist_ptr]; |
| 8310 | *vlen = len_hist[hist_ptr]; |
| 8311 | return 1; |
| 8312 | } |
| 8313 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8314 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8315 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8316 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8317 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8318 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8319 | void debug_hdr(const char *dir, struct session *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8320 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8321 | int max; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8322 | 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] | 8323 | dir, |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8324 | objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1, |
| 8325 | objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8326 | |
| 8327 | for (max = 0; start + max < end; max++) |
| 8328 | if (start[max] == '\r' || start[max] == '\n') |
| 8329 | break; |
| 8330 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8331 | UBOUND(max, trash.size - trash.len - 3); |
| 8332 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8333 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8334 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8335 | } |
| 8336 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8337 | /* |
| 8338 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8339 | * the required fields are properly allocated and that we only need to (re)init |
| 8340 | * them. This should be used before processing any new request. |
| 8341 | */ |
| 8342 | void http_init_txn(struct session *s) |
| 8343 | { |
| 8344 | struct http_txn *txn = &s->txn; |
| 8345 | struct proxy *fe = s->fe; |
| 8346 | |
| 8347 | txn->flags = 0; |
| 8348 | txn->status = -1; |
| 8349 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8350 | txn->cookie_first_date = 0; |
| 8351 | txn->cookie_last_date = 0; |
| 8352 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8353 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8354 | 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] | 8355 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8356 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8357 | 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] | 8358 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8359 | txn->req.chunk_len = 0LL; |
| 8360 | txn->req.body_len = 0LL; |
| 8361 | txn->rsp.chunk_len = 0LL; |
| 8362 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8363 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8364 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 8365 | txn->req.chn = s->req; |
| 8366 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8367 | |
| 8368 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8369 | |
| 8370 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8371 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8372 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8373 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8374 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8375 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8376 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8377 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8378 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8379 | |
| 8380 | if (txn->hdr_idx.v) |
| 8381 | hdr_idx_init(&txn->hdr_idx); |
| 8382 | } |
| 8383 | |
| 8384 | /* to be used at the end of a transaction */ |
| 8385 | void http_end_txn(struct session *s) |
| 8386 | { |
| 8387 | struct http_txn *txn = &s->txn; |
| 8388 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8389 | /* release any possible compression context */ |
| 8390 | if (s->flags & SN_COMP_READY) |
| 8391 | s->comp_algo->end(&s->comp_ctx); |
| 8392 | s->comp_algo = NULL; |
| 8393 | s->flags &= ~SN_COMP_READY; |
| 8394 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8395 | /* these ones will have been dynamically allocated */ |
| 8396 | pool_free2(pool2_requri, txn->uri); |
| 8397 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8398 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8399 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8400 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8401 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8402 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8403 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8404 | txn->uri = NULL; |
| 8405 | txn->srv_cookie = NULL; |
| 8406 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8407 | |
| 8408 | if (txn->req.cap) { |
| 8409 | struct cap_hdr *h; |
| 8410 | for (h = s->fe->req_cap; h; h = h->next) |
| 8411 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8412 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8413 | } |
| 8414 | |
| 8415 | if (txn->rsp.cap) { |
| 8416 | struct cap_hdr *h; |
| 8417 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8418 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8419 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8420 | } |
| 8421 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8422 | } |
| 8423 | |
| 8424 | /* to be used at the end of a transaction to prepare a new one */ |
| 8425 | void http_reset_txn(struct session *s) |
| 8426 | { |
| 8427 | http_end_txn(s); |
| 8428 | http_init_txn(s); |
| 8429 | |
| 8430 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8431 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8432 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8433 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8434 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8435 | /* re-init store persistence */ |
| 8436 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8437 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8438 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8439 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8440 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8441 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8442 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8443 | /* We must trim any excess data from the response buffer, because we |
| 8444 | * may have blocked an invalid response from a server that we don't |
| 8445 | * want to accidentely forward once we disable the analysers, nor do |
| 8446 | * we want those data to come along with next response. A typical |
| 8447 | * example of such data would be from a buggy server responding to |
| 8448 | * a HEAD with some data, or sending more than the advertised |
| 8449 | * content-length. |
| 8450 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8451 | if (unlikely(s->rep->buf->i)) |
| 8452 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8453 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8454 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8455 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8456 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8457 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8458 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8459 | |
| 8460 | s->req->rex = TICK_ETERNITY; |
| 8461 | s->req->wex = TICK_ETERNITY; |
| 8462 | s->req->analyse_exp = TICK_ETERNITY; |
| 8463 | s->rep->rex = TICK_ETERNITY; |
| 8464 | s->rep->wex = TICK_ETERNITY; |
| 8465 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8466 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8467 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8468 | void free_http_req_rules(struct list *r) { |
| 8469 | struct http_req_rule *tr, *pr; |
| 8470 | |
| 8471 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8472 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8473 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8474 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8475 | |
| 8476 | free(pr); |
| 8477 | } |
| 8478 | } |
| 8479 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8480 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8481 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8482 | { |
| 8483 | struct http_req_rule *rule; |
| 8484 | int cur_arg; |
| 8485 | |
| 8486 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8487 | if (!rule) { |
| 8488 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8489 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8490 | } |
| 8491 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8492 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8493 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8494 | cur_arg = 1; |
| 8495 | } else if (!strcmp(args[0], "deny")) { |
| 8496 | rule->action = HTTP_REQ_ACT_DENY; |
| 8497 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8498 | } else if (!strcmp(args[0], "tarpit")) { |
| 8499 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8500 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8501 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8502 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8503 | cur_arg = 1; |
| 8504 | |
| 8505 | while(*args[cur_arg]) { |
| 8506 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8507 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8508 | cur_arg+=2; |
| 8509 | continue; |
| 8510 | } else |
| 8511 | break; |
| 8512 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8513 | } else if (!strcmp(args[0], "set-nice")) { |
| 8514 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8515 | cur_arg = 1; |
| 8516 | |
| 8517 | if (!*args[cur_arg] || |
| 8518 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8519 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8520 | file, linenum, args[0]); |
| 8521 | goto out_err; |
| 8522 | } |
| 8523 | rule->arg.nice = atoi(args[cur_arg]); |
| 8524 | if (rule->arg.nice < -1024) |
| 8525 | rule->arg.nice = -1024; |
| 8526 | else if (rule->arg.nice > 1024) |
| 8527 | rule->arg.nice = 1024; |
| 8528 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8529 | } else if (!strcmp(args[0], "set-tos")) { |
| 8530 | #ifdef IP_TOS |
| 8531 | char *err; |
| 8532 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8533 | cur_arg = 1; |
| 8534 | |
| 8535 | if (!*args[cur_arg] || |
| 8536 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8537 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8538 | file, linenum, args[0]); |
| 8539 | goto out_err; |
| 8540 | } |
| 8541 | |
| 8542 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8543 | if (err && *err != '\0') { |
| 8544 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8545 | file, linenum, err, args[0]); |
| 8546 | goto out_err; |
| 8547 | } |
| 8548 | cur_arg++; |
| 8549 | #else |
| 8550 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8551 | goto out_err; |
| 8552 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8553 | } else if (!strcmp(args[0], "set-mark")) { |
| 8554 | #ifdef SO_MARK |
| 8555 | char *err; |
| 8556 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8557 | cur_arg = 1; |
| 8558 | |
| 8559 | if (!*args[cur_arg] || |
| 8560 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8561 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8562 | file, linenum, args[0]); |
| 8563 | goto out_err; |
| 8564 | } |
| 8565 | |
| 8566 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8567 | if (err && *err != '\0') { |
| 8568 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8569 | file, linenum, err, args[0]); |
| 8570 | goto out_err; |
| 8571 | } |
| 8572 | cur_arg++; |
| 8573 | global.last_checks |= LSTCHK_NETADM; |
| 8574 | #else |
| 8575 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8576 | goto out_err; |
| 8577 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8578 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8579 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8580 | cur_arg = 1; |
| 8581 | |
| 8582 | if (!*args[cur_arg] || |
| 8583 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8584 | bad_log_level: |
| 8585 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8586 | file, linenum, args[0]); |
| 8587 | goto out_err; |
| 8588 | } |
| 8589 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8590 | rule->arg.loglevel = -1; |
| 8591 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8592 | goto bad_log_level; |
| 8593 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8594 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8595 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8596 | cur_arg = 1; |
| 8597 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8598 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8599 | (*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] | 8600 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8601 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8602 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8603 | } |
| 8604 | |
| 8605 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8606 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8607 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8608 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8609 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8610 | 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] | 8611 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8612 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8613 | free(proxy->conf.lfs_file); |
| 8614 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8615 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8616 | cur_arg += 2; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8617 | } else if (strcmp(args[0], "del-header") == 0) { |
| 8618 | rule->action = HTTP_REQ_ACT_DEL_HDR; |
| 8619 | cur_arg = 1; |
| 8620 | |
| 8621 | if (!*args[cur_arg] || |
| 8622 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8623 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8624 | file, linenum, args[0]); |
| 8625 | goto out_err; |
| 8626 | } |
| 8627 | |
| 8628 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8629 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8630 | |
| 8631 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8632 | free(proxy->conf.lfs_file); |
| 8633 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8634 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8635 | cur_arg += 1; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8636 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8637 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8638 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8639 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8640 | 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] | 8641 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8642 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8643 | goto out_err; |
| 8644 | } |
| 8645 | |
| 8646 | /* this redirect rule might already contain a parsed condition which |
| 8647 | * we'll pass to the http-request rule. |
| 8648 | */ |
| 8649 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8650 | rule->arg.redir = redir; |
| 8651 | rule->cond = redir->cond; |
| 8652 | redir->cond = NULL; |
| 8653 | cur_arg = 2; |
| 8654 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8655 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8656 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8657 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8658 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8659 | } |
| 8660 | |
| 8661 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8662 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8663 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8664 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8665 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8666 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8667 | file, linenum, args[0], errmsg); |
| 8668 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8669 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8670 | } |
| 8671 | rule->cond = cond; |
| 8672 | } |
| 8673 | else if (*args[cur_arg]) { |
| 8674 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8675 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8676 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8677 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8678 | } |
| 8679 | |
| 8680 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8681 | out_err: |
| 8682 | free(rule); |
| 8683 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8684 | } |
| 8685 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8686 | /* parse an "http-respose" rule */ |
| 8687 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8688 | { |
| 8689 | struct http_res_rule *rule; |
| 8690 | int cur_arg; |
| 8691 | |
| 8692 | rule = calloc(1, sizeof(*rule)); |
| 8693 | if (!rule) { |
| 8694 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8695 | goto out_err; |
| 8696 | } |
| 8697 | |
| 8698 | if (!strcmp(args[0], "allow")) { |
| 8699 | rule->action = HTTP_RES_ACT_ALLOW; |
| 8700 | cur_arg = 1; |
| 8701 | } else if (!strcmp(args[0], "deny")) { |
| 8702 | rule->action = HTTP_RES_ACT_DENY; |
| 8703 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8704 | } else if (!strcmp(args[0], "set-nice")) { |
| 8705 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 8706 | cur_arg = 1; |
| 8707 | |
| 8708 | if (!*args[cur_arg] || |
| 8709 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8710 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8711 | file, linenum, args[0]); |
| 8712 | goto out_err; |
| 8713 | } |
| 8714 | rule->arg.nice = atoi(args[cur_arg]); |
| 8715 | if (rule->arg.nice < -1024) |
| 8716 | rule->arg.nice = -1024; |
| 8717 | else if (rule->arg.nice > 1024) |
| 8718 | rule->arg.nice = 1024; |
| 8719 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8720 | } else if (!strcmp(args[0], "set-tos")) { |
| 8721 | #ifdef IP_TOS |
| 8722 | char *err; |
| 8723 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 8724 | cur_arg = 1; |
| 8725 | |
| 8726 | if (!*args[cur_arg] || |
| 8727 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8728 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8729 | file, linenum, args[0]); |
| 8730 | goto out_err; |
| 8731 | } |
| 8732 | |
| 8733 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8734 | if (err && *err != '\0') { |
| 8735 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8736 | file, linenum, err, args[0]); |
| 8737 | goto out_err; |
| 8738 | } |
| 8739 | cur_arg++; |
| 8740 | #else |
| 8741 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8742 | goto out_err; |
| 8743 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8744 | } else if (!strcmp(args[0], "set-mark")) { |
| 8745 | #ifdef SO_MARK |
| 8746 | char *err; |
| 8747 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 8748 | cur_arg = 1; |
| 8749 | |
| 8750 | if (!*args[cur_arg] || |
| 8751 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8752 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8753 | file, linenum, args[0]); |
| 8754 | goto out_err; |
| 8755 | } |
| 8756 | |
| 8757 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8758 | if (err && *err != '\0') { |
| 8759 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8760 | file, linenum, err, args[0]); |
| 8761 | goto out_err; |
| 8762 | } |
| 8763 | cur_arg++; |
| 8764 | global.last_checks |= LSTCHK_NETADM; |
| 8765 | #else |
| 8766 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8767 | goto out_err; |
| 8768 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8769 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8770 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 8771 | cur_arg = 1; |
| 8772 | |
| 8773 | if (!*args[cur_arg] || |
| 8774 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8775 | bad_log_level: |
| 8776 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8777 | file, linenum, args[0]); |
| 8778 | goto out_err; |
| 8779 | } |
| 8780 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8781 | rule->arg.loglevel = -1; |
| 8782 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 8783 | goto bad_log_level; |
| 8784 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8785 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8786 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 8787 | cur_arg = 1; |
| 8788 | |
| 8789 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8790 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8791 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8792 | file, linenum, args[0]); |
| 8793 | goto out_err; |
| 8794 | } |
| 8795 | |
| 8796 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8797 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8798 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8799 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8800 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8801 | 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] | 8802 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 8803 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8804 | free(proxy->conf.lfs_file); |
| 8805 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8806 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8807 | cur_arg += 2; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8808 | } else if (strcmp(args[0], "del-header") == 0) { |
| 8809 | rule->action = HTTP_RES_ACT_DEL_HDR; |
| 8810 | cur_arg = 1; |
| 8811 | |
| 8812 | if (!*args[cur_arg] || |
| 8813 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8814 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8815 | file, linenum, args[0]); |
| 8816 | goto out_err; |
| 8817 | } |
| 8818 | |
| 8819 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8820 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8821 | |
| 8822 | proxy->conf.args.ctx = ARGC_HRS; |
| 8823 | free(proxy->conf.lfs_file); |
| 8824 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8825 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8826 | cur_arg += 1; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8827 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8828 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n", |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8829 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 8830 | goto out_err; |
| 8831 | } |
| 8832 | |
| 8833 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8834 | struct acl_cond *cond; |
| 8835 | char *errmsg = NULL; |
| 8836 | |
| 8837 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8838 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8839 | file, linenum, args[0], errmsg); |
| 8840 | free(errmsg); |
| 8841 | goto out_err; |
| 8842 | } |
| 8843 | rule->cond = cond; |
| 8844 | } |
| 8845 | else if (*args[cur_arg]) { |
| 8846 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8847 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8848 | file, linenum, args[0], args[cur_arg]); |
| 8849 | goto out_err; |
| 8850 | } |
| 8851 | |
| 8852 | return rule; |
| 8853 | out_err: |
| 8854 | free(rule); |
| 8855 | return NULL; |
| 8856 | } |
| 8857 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8858 | /* 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] | 8859 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 8860 | * dynamic log-format rule instead of a static string. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8861 | */ |
| 8862 | 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] | 8863 | const char **args, char **errmsg, int use_fmt) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8864 | { |
| 8865 | struct redirect_rule *rule; |
| 8866 | int cur_arg; |
| 8867 | int type = REDIRECT_TYPE_NONE; |
| 8868 | int code = 302; |
| 8869 | const char *destination = NULL; |
| 8870 | const char *cookie = NULL; |
| 8871 | int cookie_set = 0; |
| 8872 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8873 | struct acl_cond *cond = NULL; |
| 8874 | |
| 8875 | cur_arg = 0; |
| 8876 | while (*(args[cur_arg])) { |
| 8877 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8878 | if (!*args[cur_arg + 1]) |
| 8879 | goto missing_arg; |
| 8880 | |
| 8881 | type = REDIRECT_TYPE_LOCATION; |
| 8882 | cur_arg++; |
| 8883 | destination = args[cur_arg]; |
| 8884 | } |
| 8885 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8886 | if (!*args[cur_arg + 1]) |
| 8887 | goto missing_arg; |
| 8888 | |
| 8889 | type = REDIRECT_TYPE_PREFIX; |
| 8890 | cur_arg++; |
| 8891 | destination = args[cur_arg]; |
| 8892 | } |
| 8893 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8894 | if (!*args[cur_arg + 1]) |
| 8895 | goto missing_arg; |
| 8896 | |
| 8897 | type = REDIRECT_TYPE_SCHEME; |
| 8898 | cur_arg++; |
| 8899 | destination = args[cur_arg]; |
| 8900 | } |
| 8901 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8902 | if (!*args[cur_arg + 1]) |
| 8903 | goto missing_arg; |
| 8904 | |
| 8905 | cur_arg++; |
| 8906 | cookie = args[cur_arg]; |
| 8907 | cookie_set = 1; |
| 8908 | } |
| 8909 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8910 | if (!*args[cur_arg + 1]) |
| 8911 | goto missing_arg; |
| 8912 | |
| 8913 | cur_arg++; |
| 8914 | cookie = args[cur_arg]; |
| 8915 | cookie_set = 0; |
| 8916 | } |
| 8917 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8918 | if (!*args[cur_arg + 1]) |
| 8919 | goto missing_arg; |
| 8920 | |
| 8921 | cur_arg++; |
| 8922 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8923 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8924 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8925 | "'%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] | 8926 | args[cur_arg - 1], args[cur_arg]); |
| 8927 | return NULL; |
| 8928 | } |
| 8929 | } |
| 8930 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8931 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8932 | } |
| 8933 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8934 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8935 | } |
| 8936 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8937 | strcmp(args[cur_arg], "unless") == 0) { |
| 8938 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8939 | if (!cond) { |
| 8940 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8941 | return NULL; |
| 8942 | } |
| 8943 | break; |
| 8944 | } |
| 8945 | else { |
| 8946 | memprintf(errmsg, |
| 8947 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8948 | args[cur_arg]); |
| 8949 | return NULL; |
| 8950 | } |
| 8951 | cur_arg++; |
| 8952 | } |
| 8953 | |
| 8954 | if (type == REDIRECT_TYPE_NONE) { |
| 8955 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8956 | return NULL; |
| 8957 | } |
| 8958 | |
| 8959 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8960 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 8961 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8962 | |
| 8963 | if (!use_fmt) { |
| 8964 | /* old-style static redirect rule */ |
| 8965 | rule->rdr_str = strdup(destination); |
| 8966 | rule->rdr_len = strlen(destination); |
| 8967 | } |
| 8968 | else { |
| 8969 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8970 | |
| 8971 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 8972 | * if prefix == "/", we don't want to add anything, otherwise it |
| 8973 | * makes it hard for the user to configure a self-redirection. |
| 8974 | */ |
| 8975 | proxy->conf.args.ctx = ARGC_RDR; |
| 8976 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 8977 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 8978 | (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 8979 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8980 | free(curproxy->conf.lfs_file); |
| 8981 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 8982 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8983 | } |
| 8984 | } |
| 8985 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8986 | if (cookie) { |
| 8987 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8988 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8989 | */ |
| 8990 | rule->cookie_len = strlen(cookie); |
| 8991 | if (cookie_set) { |
| 8992 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8993 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8994 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8995 | rule->cookie_len += 9; |
| 8996 | } else { |
| 8997 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8998 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8999 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9000 | rule->cookie_len += 20; |
| 9001 | } |
| 9002 | } |
| 9003 | rule->type = type; |
| 9004 | rule->code = code; |
| 9005 | rule->flags = flags; |
| 9006 | LIST_INIT(&rule->list); |
| 9007 | return rule; |
| 9008 | |
| 9009 | missing_arg: |
| 9010 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9011 | return NULL; |
| 9012 | } |
| 9013 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9014 | /************************************************************************/ |
| 9015 | /* The code below is dedicated to ACL parsing and matching */ |
| 9016 | /************************************************************************/ |
| 9017 | |
| 9018 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9019 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9020 | * which means that a request or response is ready. If some data is missing, |
| 9021 | * 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] | 9022 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9023 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9024 | * |
| 9025 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9026 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9027 | * decide whether or not an HTTP message is present ; |
| 9028 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9029 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9030 | * 1 if an HTTP message is ready |
| 9031 | */ |
| 9032 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9033 | smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9034 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9035 | { |
| 9036 | struct http_txn *txn = l7; |
| 9037 | struct http_msg *msg = &txn->req; |
| 9038 | |
| 9039 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9040 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9041 | */ |
| 9042 | |
| 9043 | if (unlikely(!s || !txn)) |
| 9044 | return 0; |
| 9045 | |
| 9046 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9047 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9048 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9049 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9050 | if (unlikely(!s->req)) |
| 9051 | return 0; |
| 9052 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9053 | /* If the buffer does not leave enough free space at the end, |
| 9054 | * we must first realign it. |
| 9055 | */ |
| 9056 | if (s->req->buf->p > s->req->buf->data && |
| 9057 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 9058 | buffer_slow_realign(s->req->buf); |
| 9059 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9060 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9061 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9062 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9063 | |
| 9064 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9065 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9066 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9067 | |
| 9068 | /* Still no valid request ? */ |
| 9069 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9070 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9071 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9072 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9073 | } |
| 9074 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9075 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9076 | return 0; |
| 9077 | } |
| 9078 | |
| 9079 | /* OK we just got a valid HTTP request. We have some minor |
| 9080 | * preparation to perform so that further checks can rely |
| 9081 | * on HTTP tests. |
| 9082 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9083 | |
| 9084 | /* If the request was parsed but was too large, we must absolutely |
| 9085 | * return an error so that it is not processed. At the moment this |
| 9086 | * cannot happen, but if the parsers are to change in the future, |
| 9087 | * we want this check to be maintained. |
| 9088 | */ |
| 9089 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 9090 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 9091 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9092 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9093 | return 1; |
| 9094 | } |
| 9095 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9096 | 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] | 9097 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 9098 | s->flags |= SN_REDIRECTABLE; |
| 9099 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9100 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9101 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9102 | } |
| 9103 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9104 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9105 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9106 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9107 | |
| 9108 | /* otherwise everything's ready for the request */ |
| 9109 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9110 | else { |
| 9111 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9112 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9113 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9114 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9115 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9116 | } |
| 9117 | |
| 9118 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9119 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9120 | return 1; |
| 9121 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9122 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9123 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9124 | do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0) |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9125 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9126 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9127 | do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0) |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9128 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9129 | |
| 9130 | /* 1. Check on METHOD |
| 9131 | * We use the pre-parsed method if it is known, and store its number as an |
| 9132 | * integer. If it is unknown, we use the pointer and the length. |
| 9133 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9134 | static int pat_parse_meth(const char *text, struct pattern *pattern, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9135 | { |
| 9136 | int len, meth; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 9137 | struct chunk *trash; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9138 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9139 | len = strlen(text); |
| 9140 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9141 | |
| 9142 | pattern->val.i = meth; |
| 9143 | if (meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9144 | trash = get_trash_chunk(); |
| 9145 | if (trash->size < len) { |
| 9146 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 9147 | len, trash->size); |
| 9148 | return 0; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 9149 | } |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 9150 | pattern->ptr.str = trash->str; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9151 | pattern->len = len; |
| 9152 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9153 | else { |
| 9154 | pattern->ptr.str = NULL; |
| 9155 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9156 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9157 | return 1; |
| 9158 | } |
| 9159 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9160 | /* This function fetches the method of current HTTP request and stores |
| 9161 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9162 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9163 | * in <len> and <ptr> is NULL ; |
| 9164 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9165 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9166 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9167 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9168 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9169 | smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9170 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9171 | { |
| 9172 | int meth; |
| 9173 | struct http_txn *txn = l7; |
| 9174 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9175 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9176 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9177 | meth = txn->meth; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9178 | smp->type = SMP_T_METH; |
| 9179 | smp->data.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9180 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9181 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9182 | /* ensure the indexes are not affected */ |
| 9183 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9184 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9185 | smp->data.meth.str.len = txn->req.sl.rq.m_l; |
| 9186 | smp->data.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9187 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9188 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9189 | return 1; |
| 9190 | } |
| 9191 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9192 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9193 | 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] | 9194 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9195 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9196 | struct pattern_list *lst; |
| 9197 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9198 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9199 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9200 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9201 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9202 | /* well-known method */ |
| 9203 | if (pattern->val.i != HTTP_METH_OTHER) { |
| 9204 | if (smp->data.meth.meth == pattern->val.i) |
| 9205 | return pattern; |
| 9206 | else |
| 9207 | continue; |
| 9208 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9209 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9210 | /* Other method, we must compare the strings */ |
| 9211 | if (pattern->len != smp->data.meth.str.len) |
| 9212 | continue; |
| 9213 | |
| 9214 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 9215 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) || |
| 9216 | (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0)) |
| 9217 | return pattern; |
| 9218 | } |
| 9219 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9220 | } |
| 9221 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9222 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9223 | smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9224 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9225 | { |
| 9226 | struct http_txn *txn = l7; |
| 9227 | char *ptr; |
| 9228 | int len; |
| 9229 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9230 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9231 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9232 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9233 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9234 | |
| 9235 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9236 | if (len <= 0) |
| 9237 | return 0; |
| 9238 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9239 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9240 | smp->data.str.str = ptr; |
| 9241 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9242 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9243 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9244 | return 1; |
| 9245 | } |
| 9246 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9247 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9248 | smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9249 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9250 | { |
| 9251 | struct http_txn *txn = l7; |
| 9252 | char *ptr; |
| 9253 | int len; |
| 9254 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9255 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9256 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9257 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9258 | return 0; |
| 9259 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9260 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9261 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9262 | |
| 9263 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9264 | if (len <= 0) |
| 9265 | return 0; |
| 9266 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9267 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9268 | smp->data.str.str = ptr; |
| 9269 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9270 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9271 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9272 | return 1; |
| 9273 | } |
| 9274 | |
| 9275 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9276 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9277 | smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9278 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9279 | { |
| 9280 | struct http_txn *txn = l7; |
| 9281 | char *ptr; |
| 9282 | int len; |
| 9283 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9284 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9285 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9286 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9287 | return 0; |
| 9288 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9289 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9290 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9291 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9292 | smp->type = SMP_T_UINT; |
| 9293 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9294 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9295 | return 1; |
| 9296 | } |
| 9297 | |
| 9298 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9299 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9300 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9301 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9302 | { |
| 9303 | struct http_txn *txn = l7; |
| 9304 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9305 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9306 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9307 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9308 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9309 | 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] | 9310 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9311 | return 1; |
| 9312 | } |
| 9313 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9314 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9315 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9316 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9317 | { |
| 9318 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9319 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9320 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9321 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9322 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9323 | 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] | 9324 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9325 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9326 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9327 | smp->type = SMP_T_IPV4; |
| 9328 | smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9329 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9330 | return 1; |
| 9331 | } |
| 9332 | |
| 9333 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9334 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9335 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9336 | { |
| 9337 | struct http_txn *txn = l7; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9338 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9339 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9340 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9341 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9342 | 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] | 9343 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9344 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9345 | |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9346 | smp->type = SMP_T_UINT; |
| 9347 | smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9348 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9349 | return 1; |
| 9350 | } |
| 9351 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9352 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9353 | * Accepts an optional argument of type string containing the header field name, |
| 9354 | * and an optional argument of type signed or unsigned integer to request an |
| 9355 | * 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] | 9356 | * headers are considered from the first one. It does not stop on commas and |
| 9357 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9358 | */ |
| 9359 | static int |
| 9360 | smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9361 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9362 | { |
| 9363 | struct http_txn *txn = l7; |
| 9364 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9365 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 9366 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9367 | int occ = 0; |
| 9368 | const char *name_str = NULL; |
| 9369 | int name_len = 0; |
| 9370 | |
| 9371 | if (!ctx) { |
| 9372 | /* first call */ |
| 9373 | ctx = &static_hdr_ctx; |
| 9374 | ctx->idx = 0; |
| 9375 | smp->ctx.a[0] = ctx; |
| 9376 | } |
| 9377 | |
| 9378 | if (args) { |
| 9379 | if (args[0].type != ARGT_STR) |
| 9380 | return 0; |
| 9381 | name_str = args[0].data.str.str; |
| 9382 | name_len = args[0].data.str.len; |
| 9383 | |
| 9384 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9385 | occ = args[1].data.uint; |
| 9386 | } |
| 9387 | |
| 9388 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9389 | |
| 9390 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9391 | /* search for header from the beginning */ |
| 9392 | ctx->idx = 0; |
| 9393 | |
| 9394 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9395 | /* no explicit occurrence and single fetch => last header by default */ |
| 9396 | occ = -1; |
| 9397 | |
| 9398 | if (!occ) |
| 9399 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9400 | smp->flags |= SMP_F_NOT_LAST; |
| 9401 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9402 | smp->type = SMP_T_STR; |
| 9403 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9404 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 9405 | return 1; |
| 9406 | |
| 9407 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9408 | return 0; |
| 9409 | } |
| 9410 | |
| 9411 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9412 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9413 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9414 | */ |
| 9415 | static int |
| 9416 | smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9417 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9418 | { |
| 9419 | struct http_txn *txn = l7; |
| 9420 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9421 | struct hdr_ctx ctx; |
| 9422 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9423 | int cnt; |
| 9424 | |
| 9425 | if (!args || args->type != ARGT_STR) |
| 9426 | return 0; |
| 9427 | |
| 9428 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9429 | |
| 9430 | ctx.idx = 0; |
| 9431 | cnt = 0; |
| 9432 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 9433 | cnt++; |
| 9434 | |
| 9435 | smp->type = SMP_T_UINT; |
| 9436 | smp->data.uint = cnt; |
| 9437 | smp->flags = SMP_F_VOL_HDR; |
| 9438 | return 1; |
| 9439 | } |
| 9440 | |
| 9441 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9442 | * Accepts an optional argument of type string containing the header field name, |
| 9443 | * and an optional argument of type signed or unsigned integer to request an |
| 9444 | * 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] | 9445 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9446 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9447 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9448 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9449 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9450 | { |
| 9451 | struct http_txn *txn = l7; |
| 9452 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9453 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9454 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9455 | int occ = 0; |
| 9456 | const char *name_str = NULL; |
| 9457 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9458 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9459 | if (!ctx) { |
| 9460 | /* first call */ |
| 9461 | ctx = &static_hdr_ctx; |
| 9462 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9463 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9464 | } |
| 9465 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9466 | if (args) { |
| 9467 | if (args[0].type != ARGT_STR) |
| 9468 | return 0; |
| 9469 | name_str = args[0].data.str.str; |
| 9470 | name_len = args[0].data.str.len; |
| 9471 | |
| 9472 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9473 | occ = args[1].data.uint; |
| 9474 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9475 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9476 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9477 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9478 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9479 | /* search for header from the beginning */ |
| 9480 | ctx->idx = 0; |
| 9481 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9482 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9483 | /* no explicit occurrence and single fetch => last header by default */ |
| 9484 | occ = -1; |
| 9485 | |
| 9486 | if (!occ) |
| 9487 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9488 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9489 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9490 | smp->type = SMP_T_STR; |
| 9491 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9492 | 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] | 9493 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9494 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9495 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9496 | return 0; |
| 9497 | } |
| 9498 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9499 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9500 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9501 | */ |
| 9502 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9503 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9504 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9505 | { |
| 9506 | struct http_txn *txn = l7; |
| 9507 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9508 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9509 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9510 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9511 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9512 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9513 | return 0; |
| 9514 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9515 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9516 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9517 | ctx.idx = 0; |
| 9518 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9519 | while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9520 | cnt++; |
| 9521 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9522 | smp->type = SMP_T_UINT; |
| 9523 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9524 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9525 | return 1; |
| 9526 | } |
| 9527 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9528 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9529 | * takes a mandatory argument of type string and an optional one of type int |
| 9530 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9531 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9532 | */ |
| 9533 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9534 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9535 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9536 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9537 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9538 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9539 | if (ret > 0) { |
| 9540 | smp->type = SMP_T_UINT; |
| 9541 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9542 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9543 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9544 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9545 | } |
| 9546 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9547 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9548 | * and an optional one of type int to designate a specific occurrence. |
| 9549 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9550 | */ |
| 9551 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9552 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9553 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9554 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9555 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9556 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9557 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9558 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 9559 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9560 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9561 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9562 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9563 | if (smp->data.str.len < temp->size - 1) { |
| 9564 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 9565 | temp->str[smp->data.str.len] = '\0'; |
| 9566 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 9567 | smp->type = SMP_T_IPV6; |
| 9568 | break; |
| 9569 | } |
| 9570 | } |
| 9571 | } |
| 9572 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9573 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9574 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9575 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9576 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9577 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9578 | } |
| 9579 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9580 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9581 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9582 | */ |
| 9583 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9584 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9585 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9586 | { |
| 9587 | struct http_txn *txn = l7; |
| 9588 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9589 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9590 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9591 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9592 | 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] | 9593 | ptr = http_get_path(txn); |
| 9594 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9595 | return 0; |
| 9596 | |
| 9597 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9598 | smp->type = SMP_T_STR; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9599 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9600 | |
| 9601 | while (ptr < end && *ptr != '?') |
| 9602 | ptr++; |
| 9603 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9604 | smp->data.str.len = ptr - smp->data.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9605 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9606 | return 1; |
| 9607 | } |
| 9608 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9609 | /* This produces a concatenation of the first occurrence of the Host header |
| 9610 | * followed by the path component if it begins with a slash ('/'). This means |
| 9611 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 9612 | * If no Host header is found, then the path is returned as-is. The returned |
| 9613 | * 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] | 9614 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9615 | */ |
| 9616 | static int |
| 9617 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9618 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9619 | { |
| 9620 | struct http_txn *txn = l7; |
| 9621 | char *ptr, *end, *beg; |
| 9622 | struct hdr_ctx ctx; |
| 9623 | |
| 9624 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9625 | |
| 9626 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 9627 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9628 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9629 | |
| 9630 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9631 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9632 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9633 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9634 | smp->data.str.len = ctx.vlen; |
| 9635 | |
| 9636 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 9637 | 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] | 9638 | beg = http_get_path(txn); |
| 9639 | if (!beg) |
| 9640 | beg = end; |
| 9641 | |
| 9642 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9643 | |
| 9644 | if (beg < ptr && *beg == '/') { |
| 9645 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 9646 | smp->data.str.len += ptr - beg; |
| 9647 | } |
| 9648 | |
| 9649 | smp->flags = SMP_F_VOL_1ST; |
| 9650 | return 1; |
| 9651 | } |
| 9652 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9653 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 9654 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 9655 | * This means that '*' will not be added, resulting in exactly the first Host |
| 9656 | * 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] | 9657 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 9658 | * 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] | 9659 | * high-traffic sites without having to store whole paths. |
| 9660 | */ |
| 9661 | static int |
| 9662 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9663 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9664 | { |
| 9665 | struct http_txn *txn = l7; |
| 9666 | struct hdr_ctx ctx; |
| 9667 | unsigned int hash = 0; |
| 9668 | char *ptr, *beg, *end; |
| 9669 | int len; |
| 9670 | |
| 9671 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9672 | |
| 9673 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 9674 | 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] | 9675 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 9676 | ptr = ctx.line + ctx.val; |
| 9677 | len = ctx.vlen; |
| 9678 | while (len--) |
| 9679 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 9680 | } |
| 9681 | |
| 9682 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 9683 | 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] | 9684 | beg = http_get_path(txn); |
| 9685 | if (!beg) |
| 9686 | beg = end; |
| 9687 | |
| 9688 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9689 | |
| 9690 | if (beg < ptr && *beg == '/') { |
| 9691 | while (beg < ptr) |
| 9692 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 9693 | } |
| 9694 | hash = full_hash(hash); |
| 9695 | |
| 9696 | smp->type = SMP_T_UINT; |
| 9697 | smp->data.uint = hash; |
| 9698 | smp->flags = SMP_F_VOL_1ST; |
| 9699 | return 1; |
| 9700 | } |
| 9701 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9702 | /* 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] | 9703 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 9704 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 9705 | * 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] | 9706 | * that in environments where IPv6 is insignificant, truncating the output to |
| 9707 | * 8 bytes would still work. |
| 9708 | */ |
| 9709 | static int |
| 9710 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9711 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9712 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9713 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9714 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
| 9715 | |
| 9716 | if (!cli_conn) |
| 9717 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9718 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9719 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9720 | return 0; |
| 9721 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9722 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9723 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 9724 | temp->len += sizeof(smp->data.uint); |
| 9725 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9726 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9727 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9728 | 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] | 9729 | temp->len += 4; |
| 9730 | break; |
| 9731 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 9732 | 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] | 9733 | temp->len += 16; |
| 9734 | break; |
| 9735 | default: |
| 9736 | return 0; |
| 9737 | } |
| 9738 | |
| 9739 | smp->data.str = *temp; |
| 9740 | smp->type = SMP_T_BIN; |
| 9741 | return 1; |
| 9742 | } |
| 9743 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9744 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9745 | smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9746 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9747 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9748 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9749 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9750 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9751 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9752 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9753 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9754 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9755 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9756 | return 1; |
| 9757 | } |
| 9758 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9759 | /* return a valid test if the current request is the first one on the connection */ |
| 9760 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9761 | smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9762 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9763 | { |
| 9764 | if (!s) |
| 9765 | return 0; |
| 9766 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9767 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9768 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9769 | return 1; |
| 9770 | } |
| 9771 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9772 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9773 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9774 | smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9775 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9776 | { |
| 9777 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9778 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9779 | return 0; |
| 9780 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9781 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9782 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9783 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9784 | return 0; |
| 9785 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9786 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9787 | smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9788 | return 1; |
| 9789 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9790 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9791 | /* Accepts exactly 1 argument of type userlist */ |
| 9792 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9793 | smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9794 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9795 | { |
| 9796 | |
| 9797 | if (!args || args->type != ARGT_USR) |
| 9798 | return 0; |
| 9799 | |
| 9800 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9801 | |
| 9802 | if (!get_http_auth(l4)) |
| 9803 | return 0; |
| 9804 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9805 | /* if the user does not belong to the userlist or has a wrong password, |
| 9806 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9807 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9808 | */ |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9809 | if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass)) |
| 9810 | return 0; |
| 9811 | |
| 9812 | /* pat_match_auth() will need the user list */ |
| 9813 | smp->ctx.a[0] = args->data.usr; |
| 9814 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9815 | smp->type = SMP_T_STR; |
| 9816 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 9817 | smp->data.str.str = l4->txn.auth.user; |
| 9818 | smp->data.str.len = strlen(l4->txn.auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9819 | |
| 9820 | return 1; |
| 9821 | } |
| 9822 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9823 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9824 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9825 | * the cookie value is returned into *value and *value_l, and the function |
| 9826 | * returns a pointer to the next pointer to search from if the value was found. |
| 9827 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9828 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9829 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9830 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9831 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9832 | * list is expected. |
| 9833 | */ |
| 9834 | static char * |
| 9835 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9836 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9837 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9838 | { |
| 9839 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9840 | char *next; |
| 9841 | |
| 9842 | /* we search at least a cookie name followed by an equal, and more |
| 9843 | * generally something like this : |
| 9844 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9845 | */ |
| 9846 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9847 | /* Iterate through all cookies on this line */ |
| 9848 | |
| 9849 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9850 | att_beg++; |
| 9851 | |
| 9852 | /* find att_end : this is the first character after the last non |
| 9853 | * space before the equal. It may be equal to hdr_end. |
| 9854 | */ |
| 9855 | equal = att_end = att_beg; |
| 9856 | |
| 9857 | while (equal < hdr_end) { |
| 9858 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9859 | break; |
| 9860 | if (http_is_spht[(unsigned char)*equal++]) |
| 9861 | continue; |
| 9862 | att_end = equal; |
| 9863 | } |
| 9864 | |
| 9865 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9866 | * is between <att_beg> and <equal>, both may be identical. |
| 9867 | */ |
| 9868 | |
| 9869 | /* look for end of cookie if there is an equal sign */ |
| 9870 | if (equal < hdr_end && *equal == '=') { |
| 9871 | /* look for the beginning of the value */ |
| 9872 | val_beg = equal + 1; |
| 9873 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9874 | val_beg++; |
| 9875 | |
| 9876 | /* find the end of the value, respecting quotes */ |
| 9877 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9878 | |
| 9879 | /* make val_end point to the first white space or delimitor after the value */ |
| 9880 | val_end = next; |
| 9881 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9882 | val_end--; |
| 9883 | } else { |
| 9884 | val_beg = val_end = next = equal; |
| 9885 | } |
| 9886 | |
| 9887 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9888 | * they will automatically be removed if a header before them is removed, |
| 9889 | * since they're supposed to be linked together. |
| 9890 | */ |
| 9891 | if (*att_beg == '$') |
| 9892 | continue; |
| 9893 | |
| 9894 | /* Ignore cookies with no equal sign */ |
| 9895 | if (equal == next) |
| 9896 | continue; |
| 9897 | |
| 9898 | /* Now we have the cookie name between att_beg and att_end, and |
| 9899 | * its value between val_beg and val_end. |
| 9900 | */ |
| 9901 | |
| 9902 | if (att_end - att_beg == cookie_name_l && |
| 9903 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9904 | /* let's return this value and indicate where to go on from */ |
| 9905 | *value = val_beg; |
| 9906 | *value_l = val_end - val_beg; |
| 9907 | return next + 1; |
| 9908 | } |
| 9909 | |
| 9910 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9911 | if (!list) |
| 9912 | break; |
| 9913 | } |
| 9914 | |
| 9915 | return NULL; |
| 9916 | } |
| 9917 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9918 | /* Fetch a captured HTTP request header. The index is the position of |
| 9919 | * the "capture" option in the configuration file |
| 9920 | */ |
| 9921 | static int |
| 9922 | smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9923 | const struct arg *args, struct sample *smp, const char *kw) |
| 9924 | { |
| 9925 | struct proxy *fe = l4->fe; |
| 9926 | struct http_txn *txn = l7; |
| 9927 | int idx; |
| 9928 | |
| 9929 | if (!args || args->type != ARGT_UINT) |
| 9930 | return 0; |
| 9931 | |
| 9932 | idx = args->data.uint; |
| 9933 | |
| 9934 | if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL) |
| 9935 | return 0; |
| 9936 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9937 | smp->type = SMP_T_STR; |
| 9938 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9939 | smp->data.str.str = txn->req.cap[idx]; |
| 9940 | smp->data.str.len = strlen(txn->req.cap[idx]); |
| 9941 | |
| 9942 | return 1; |
| 9943 | } |
| 9944 | |
| 9945 | /* Fetch a captured HTTP response header. The index is the position of |
| 9946 | * the "capture" option in the configuration file |
| 9947 | */ |
| 9948 | static int |
| 9949 | smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9950 | const struct arg *args, struct sample *smp, const char *kw) |
| 9951 | { |
| 9952 | struct proxy *fe = l4->fe; |
| 9953 | struct http_txn *txn = l7; |
| 9954 | int idx; |
| 9955 | |
| 9956 | if (!args || args->type != ARGT_UINT) |
| 9957 | return 0; |
| 9958 | |
| 9959 | idx = args->data.uint; |
| 9960 | |
| 9961 | if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL) |
| 9962 | return 0; |
| 9963 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9964 | smp->type = SMP_T_STR; |
| 9965 | smp->flags |= SMP_F_CONST; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 9966 | smp->data.str.str = txn->rsp.cap[idx]; |
| 9967 | smp->data.str.len = strlen(txn->rsp.cap[idx]); |
| 9968 | |
| 9969 | return 1; |
| 9970 | } |
| 9971 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9972 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 9973 | static int |
| 9974 | smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9975 | const struct arg *args, struct sample *smp, const char *kw) |
| 9976 | { |
| 9977 | struct chunk *temp; |
| 9978 | struct http_txn *txn = l7; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9979 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9980 | |
| 9981 | if (!txn->uri) |
| 9982 | return 0; |
| 9983 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9984 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9985 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9986 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 9987 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9988 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 9989 | temp = get_trash_chunk(); |
| 9990 | temp->str = txn->uri; |
| 9991 | temp->len = ptr - txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9992 | smp->data.str = *temp; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9993 | smp->type = SMP_T_STR; |
| 9994 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 9995 | |
| 9996 | return 1; |
| 9997 | |
| 9998 | } |
| 9999 | |
| 10000 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10001 | static int |
| 10002 | smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10003 | const struct arg *args, struct sample *smp, const char *kw) |
| 10004 | { |
| 10005 | struct chunk *temp; |
| 10006 | struct http_txn *txn = l7; |
| 10007 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10008 | |
| 10009 | if (!txn->uri) |
| 10010 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10011 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10012 | ptr = txn->uri; |
| 10013 | |
| 10014 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10015 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10016 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10017 | if (!*ptr) |
| 10018 | return 0; |
| 10019 | |
| 10020 | ptr++; /* skip the space */ |
| 10021 | |
| 10022 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10023 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10024 | if (!ptr) |
| 10025 | return 0; |
| 10026 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10027 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10028 | |
| 10029 | smp->data.str = *temp; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10030 | smp->data.str.len = ptr - temp->str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10031 | smp->type = SMP_T_STR; |
| 10032 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10033 | |
| 10034 | return 1; |
| 10035 | } |
| 10036 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10037 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10038 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10039 | */ |
| 10040 | static int |
| 10041 | smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10042 | const struct arg *args, struct sample *smp, const char *kw) |
| 10043 | { |
| 10044 | struct http_txn *txn = l7; |
| 10045 | |
| 10046 | if (txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 10047 | return 0; |
| 10048 | |
| 10049 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 10050 | smp->data.str.str = "HTTP/1.1"; |
| 10051 | else |
| 10052 | smp->data.str.str = "HTTP/1.0"; |
| 10053 | |
| 10054 | smp->data.str.len = 8; |
| 10055 | smp->type = SMP_T_STR; |
| 10056 | smp->flags = SMP_F_CONST; |
| 10057 | return 1; |
| 10058 | |
| 10059 | } |
| 10060 | |
| 10061 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10062 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10063 | */ |
| 10064 | static int |
| 10065 | smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 10066 | const struct arg *args, struct sample *smp, const char *kw) |
| 10067 | { |
| 10068 | struct http_txn *txn = l7; |
| 10069 | |
| 10070 | if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 10071 | return 0; |
| 10072 | |
| 10073 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 10074 | smp->data.str.str = "HTTP/1.1"; |
| 10075 | else |
| 10076 | smp->data.str.str = "HTTP/1.0"; |
| 10077 | |
| 10078 | smp->data.str.len = 8; |
| 10079 | smp->type = SMP_T_STR; |
| 10080 | smp->flags = SMP_F_CONST; |
| 10081 | return 1; |
| 10082 | |
| 10083 | } |
| 10084 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10085 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10086 | /* 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] | 10087 | * 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] | 10088 | * 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] | 10089 | * 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] | 10090 | * 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] | 10091 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10092 | * that no iterating is desired, then only last value is fetched if any. |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10093 | * The returned sample is of type CSTR. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10094 | */ |
| 10095 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10096 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10097 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10098 | { |
| 10099 | struct http_txn *txn = l7; |
| 10100 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10101 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10102 | const struct http_msg *msg; |
| 10103 | const char *hdr_name; |
| 10104 | int hdr_name_len; |
| 10105 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10106 | int occ = 0; |
| 10107 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10108 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10109 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10110 | return 0; |
| 10111 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10112 | if (!ctx) { |
| 10113 | /* first call */ |
| 10114 | ctx = &static_hdr_ctx; |
| 10115 | ctx->idx = 0; |
| 10116 | smp->ctx.a[2] = ctx; |
| 10117 | } |
| 10118 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10119 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10120 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10121 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10122 | msg = &txn->req; |
| 10123 | hdr_name = "Cookie"; |
| 10124 | hdr_name_len = 6; |
| 10125 | } else { |
| 10126 | msg = &txn->rsp; |
| 10127 | hdr_name = "Set-Cookie"; |
| 10128 | hdr_name_len = 10; |
| 10129 | } |
| 10130 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10131 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 10132 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10133 | occ = -1; |
| 10134 | |
| 10135 | /* OK so basically here, either we want only one value and it's the |
| 10136 | * last one, or we want to iterate over all of them and we fetch the |
| 10137 | * next one. |
| 10138 | */ |
| 10139 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10140 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10141 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10142 | /* search for the header from the beginning, we must first initialize |
| 10143 | * the search parameters. |
| 10144 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10145 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10146 | ctx->idx = 0; |
| 10147 | } |
| 10148 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10149 | smp->flags |= SMP_F_VOL_HDR; |
| 10150 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10151 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10152 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10153 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10154 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10155 | goto out; |
| 10156 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10157 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10158 | continue; |
| 10159 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10160 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10161 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10162 | } |
| 10163 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10164 | smp->type = SMP_T_STR; |
| 10165 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10166 | 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] | 10167 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10168 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10169 | &smp->data.str.str, |
| 10170 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10171 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10172 | found = 1; |
| 10173 | if (occ >= 0) { |
| 10174 | /* one value was returned into smp->data.str.{str,len} */ |
| 10175 | smp->flags |= SMP_F_NOT_LAST; |
| 10176 | return 1; |
| 10177 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10178 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10179 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10180 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10181 | /* all cookie headers and values were scanned. If we're looking for the |
| 10182 | * last occurrence, we may return it now. |
| 10183 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10184 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10185 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10186 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10187 | } |
| 10188 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10189 | /* 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] | 10190 | * 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] | 10191 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10192 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10193 | */ |
| 10194 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10195 | smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10196 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10197 | { |
| 10198 | struct http_txn *txn = l7; |
| 10199 | struct hdr_idx *idx = &txn->hdr_idx; |
| 10200 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10201 | const struct http_msg *msg; |
| 10202 | const char *hdr_name; |
| 10203 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10204 | int cnt; |
| 10205 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10206 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10207 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10208 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10209 | return 0; |
| 10210 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10211 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10212 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10213 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10214 | msg = &txn->req; |
| 10215 | hdr_name = "Cookie"; |
| 10216 | hdr_name_len = 6; |
| 10217 | } else { |
| 10218 | msg = &txn->rsp; |
| 10219 | hdr_name = "Set-Cookie"; |
| 10220 | hdr_name_len = 10; |
| 10221 | } |
| 10222 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10223 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10224 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10225 | ctx.idx = 0; |
| 10226 | cnt = 0; |
| 10227 | |
| 10228 | while (1) { |
| 10229 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10230 | if (!val_beg) { |
| 10231 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10232 | break; |
| 10233 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10234 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10235 | continue; |
| 10236 | |
| 10237 | val_beg = ctx.line + ctx.val; |
| 10238 | val_end = val_beg + ctx.vlen; |
| 10239 | } |
| 10240 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10241 | smp->type = SMP_T_STR; |
| 10242 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10243 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10244 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10245 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10246 | &smp->data.str.str, |
| 10247 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10248 | cnt++; |
| 10249 | } |
| 10250 | } |
| 10251 | |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10252 | smp->type = SMP_T_UINT; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10253 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10254 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10255 | return 1; |
| 10256 | } |
| 10257 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10258 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10259 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10260 | */ |
| 10261 | static int |
| 10262 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10263 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10264 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10265 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10266 | |
| 10267 | if (ret > 0) { |
| 10268 | smp->type = SMP_T_UINT; |
| 10269 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10270 | } |
| 10271 | |
| 10272 | return ret; |
| 10273 | } |
| 10274 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10275 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10276 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10277 | /************************************************************************/ |
| 10278 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10279 | /* |
| 10280 | * Given a path string and its length, find the position of beginning of the |
| 10281 | * query string. Returns NULL if no query string is found in the path. |
| 10282 | * |
| 10283 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10284 | * |
| 10285 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 10286 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10287 | 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] | 10288 | { |
| 10289 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10290 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10291 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10292 | return p ? p + 1 : NULL; |
| 10293 | } |
| 10294 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10295 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10296 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10297 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10298 | } |
| 10299 | |
| 10300 | /* |
| 10301 | * Given a url parameter, find the starting position of the first occurence, |
| 10302 | * or NULL if the parameter is not found. |
| 10303 | * |
| 10304 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10305 | * the function will return query_string+8. |
| 10306 | */ |
| 10307 | static char* |
| 10308 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10309 | char* url_param_name, size_t url_param_name_l, |
| 10310 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10311 | { |
| 10312 | char *pos, *last; |
| 10313 | |
| 10314 | pos = query_string; |
| 10315 | last = query_string + query_string_l - url_param_name_l - 1; |
| 10316 | |
| 10317 | while (pos <= last) { |
| 10318 | if (pos[url_param_name_l] == '=') { |
| 10319 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10320 | return pos; |
| 10321 | pos += url_param_name_l + 1; |
| 10322 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10323 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10324 | pos++; |
| 10325 | pos++; |
| 10326 | } |
| 10327 | return NULL; |
| 10328 | } |
| 10329 | |
| 10330 | /* |
| 10331 | * Given a url parameter name, returns its value and size into *value and |
| 10332 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 10333 | * zero is returned and value/value_l are not touched. |
| 10334 | */ |
| 10335 | static int |
| 10336 | find_url_param_value(char* path, size_t path_l, |
| 10337 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10338 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10339 | { |
| 10340 | char *query_string, *qs_end; |
| 10341 | char *arg_start; |
| 10342 | char *value_start, *value_end; |
| 10343 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10344 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10345 | if (!query_string) |
| 10346 | return 0; |
| 10347 | |
| 10348 | qs_end = path + path_l; |
| 10349 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10350 | url_param_name, url_param_name_l, |
| 10351 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10352 | if (!arg_start) |
| 10353 | return 0; |
| 10354 | |
| 10355 | value_start = arg_start + url_param_name_l + 1; |
| 10356 | value_end = value_start; |
| 10357 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10358 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10359 | value_end++; |
| 10360 | |
| 10361 | *value = value_start; |
| 10362 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 10363 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10364 | } |
| 10365 | |
| 10366 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10367 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10368 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10369 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10370 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10371 | struct http_txn *txn = l7; |
| 10372 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10373 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10374 | if (!args || args[0].type != ARGT_STR || |
| 10375 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10376 | return 0; |
| 10377 | |
| 10378 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10379 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10380 | if (args[1].type) |
| 10381 | delim = *args[1].data.str.str; |
| 10382 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10383 | 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] | 10384 | args->data.str.str, args->data.str.len, |
| 10385 | &smp->data.str.str, &smp->data.str.len, |
| 10386 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10387 | return 0; |
| 10388 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10389 | smp->type = SMP_T_STR; |
| 10390 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10391 | return 1; |
| 10392 | } |
| 10393 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10394 | /* Return the signed integer value for the specified url parameter (see url_param |
| 10395 | * above). |
| 10396 | */ |
| 10397 | static int |
| 10398 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10399 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10400 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10401 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10402 | |
| 10403 | if (ret > 0) { |
| 10404 | smp->type = SMP_T_UINT; |
| 10405 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10406 | } |
| 10407 | |
| 10408 | return ret; |
| 10409 | } |
| 10410 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10411 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10412 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10413 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10414 | * entry. If no Host header is found, then the path is used. The resulting value |
| 10415 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 10416 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 10417 | * high-traffic sites without having to store whole paths. |
| 10418 | * this differs from the base32 functions in that it includes the url parameters |
| 10419 | * as well as the path |
| 10420 | */ |
| 10421 | static int |
| 10422 | smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10423 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10424 | { |
| 10425 | struct http_txn *txn = l7; |
| 10426 | struct hdr_ctx ctx; |
| 10427 | unsigned int hash = 0; |
| 10428 | char *ptr, *beg, *end; |
| 10429 | int len; |
| 10430 | |
| 10431 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10432 | |
| 10433 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10434 | 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] | 10435 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10436 | ptr = ctx.line + ctx.val; |
| 10437 | len = ctx.vlen; |
| 10438 | while (len--) |
| 10439 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10440 | } |
| 10441 | |
| 10442 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10443 | 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] | 10444 | beg = http_get_path(txn); |
| 10445 | if (!beg) |
| 10446 | beg = end; |
| 10447 | |
| 10448 | for (ptr = beg; ptr < end ; ptr++); |
| 10449 | |
| 10450 | if (beg < ptr && *beg == '/') { |
| 10451 | while (beg < ptr) |
| 10452 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10453 | } |
| 10454 | hash = full_hash(hash); |
| 10455 | |
| 10456 | smp->type = SMP_T_UINT; |
| 10457 | smp->data.uint = hash; |
| 10458 | smp->flags = SMP_F_VOL_1ST; |
| 10459 | return 1; |
| 10460 | } |
| 10461 | |
| 10462 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 10463 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10464 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 10465 | * on the source address length. The URL hash is stored before the address so |
| 10466 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10467 | * 8 bytes would still work. |
| 10468 | */ |
| 10469 | static int |
| 10470 | smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10471 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10472 | { |
| 10473 | struct chunk *temp; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10474 | struct connection *cli_conn = objt_conn(l4->si[0].end); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10475 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10476 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10477 | return 0; |
| 10478 | |
| 10479 | temp = get_trash_chunk(); |
| 10480 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10481 | temp->len += sizeof(smp->data.uint); |
| 10482 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10483 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10484 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10485 | 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] | 10486 | temp->len += 4; |
| 10487 | break; |
| 10488 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10489 | 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] | 10490 | temp->len += 16; |
| 10491 | break; |
| 10492 | default: |
| 10493 | return 0; |
| 10494 | } |
| 10495 | |
| 10496 | smp->data.str = *temp; |
| 10497 | smp->type = SMP_T_BIN; |
| 10498 | return 1; |
| 10499 | } |
| 10500 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10501 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 10502 | * keyword. These keywords support an optional positive or negative occurrence |
| 10503 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 10504 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 10505 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 10506 | * error message in case of error, that the caller is responsible for freeing. |
| 10507 | * The initial location must either be freeable or NULL. |
| 10508 | */ |
| 10509 | static int val_hdr(struct arg *arg, char **err_msg) |
| 10510 | { |
| 10511 | 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] | 10512 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10513 | return 0; |
| 10514 | } |
| 10515 | return 1; |
| 10516 | } |
| 10517 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10518 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 10519 | * adds an optional offset found in args[0] and emits a string representing |
| 10520 | * the date in RFC-1123/5322 format. |
| 10521 | */ |
| 10522 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 10523 | { |
| 10524 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 10525 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 10526 | struct chunk *temp; |
| 10527 | struct tm *tm; |
| 10528 | time_t curr_date = smp->data.uint; |
| 10529 | |
| 10530 | /* add offset */ |
| 10531 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 10532 | curr_date += args[0].data.sint; |
| 10533 | |
| 10534 | tm = gmtime(&curr_date); |
| 10535 | |
| 10536 | temp = get_trash_chunk(); |
| 10537 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 10538 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 10539 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 10540 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 10541 | |
| 10542 | smp->data.str = *temp; |
| 10543 | smp->type = SMP_T_STR; |
| 10544 | return 1; |
| 10545 | } |
| 10546 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 10547 | /* Match language range with language tag. RFC2616 14.4: |
| 10548 | * |
| 10549 | * A language-range matches a language-tag if it exactly equals |
| 10550 | * the tag, or if it exactly equals a prefix of the tag such |
| 10551 | * that the first tag character following the prefix is "-". |
| 10552 | * |
| 10553 | * Return 1 if the strings match, else return 0. |
| 10554 | */ |
| 10555 | static inline int language_range_match(const char *range, int range_len, |
| 10556 | const char *tag, int tag_len) |
| 10557 | { |
| 10558 | const char *end = range + range_len; |
| 10559 | const char *tend = tag + tag_len; |
| 10560 | while (range < end) { |
| 10561 | if (*range == '-' && tag == tend) |
| 10562 | return 1; |
| 10563 | if (*range != *tag || tag == tend) |
| 10564 | return 0; |
| 10565 | range++; |
| 10566 | tag++; |
| 10567 | } |
| 10568 | /* Return true only if the last char of the tag is matched. */ |
| 10569 | return tag == tend; |
| 10570 | } |
| 10571 | |
| 10572 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
| 10573 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp) |
| 10574 | { |
| 10575 | const char *al = smp->data.str.str; |
| 10576 | const char *end = al + smp->data.str.len; |
| 10577 | const char *token; |
| 10578 | int toklen; |
| 10579 | int qvalue; |
| 10580 | const char *str; |
| 10581 | const char *w; |
| 10582 | int best_q = 0; |
| 10583 | |
| 10584 | /* Set the constant to the sample, because the output of the |
| 10585 | * function will be peek in the constant configuration string. |
| 10586 | */ |
| 10587 | smp->flags |= SMP_F_CONST; |
| 10588 | smp->data.str.size = 0; |
| 10589 | smp->data.str.str = ""; |
| 10590 | smp->data.str.len = 0; |
| 10591 | |
| 10592 | /* Parse the accept language */ |
| 10593 | while (1) { |
| 10594 | |
| 10595 | /* Jump spaces, quit if the end is detected. */ |
| 10596 | while (al < end && isspace(*al)) |
| 10597 | al++; |
| 10598 | if (al >= end) |
| 10599 | break; |
| 10600 | |
| 10601 | /* Start of the fisrt word. */ |
| 10602 | token = al; |
| 10603 | |
| 10604 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
| 10605 | while (al < end && *al != ';' && *al != ',' && !isspace(*al)) |
| 10606 | al++; |
| 10607 | if (al == token) |
| 10608 | goto expect_comma; |
| 10609 | |
| 10610 | /* Length of the token. */ |
| 10611 | toklen = al - token; |
| 10612 | qvalue = 1000; |
| 10613 | |
| 10614 | /* Check if the token exists in the list. If the token not exists, |
| 10615 | * jump to the next token. |
| 10616 | */ |
| 10617 | str = args[0].data.str.str; |
| 10618 | w = str; |
| 10619 | while (1) { |
| 10620 | if (*str == ';' || *str == '\0') { |
| 10621 | if (language_range_match(token, toklen, w, str-w)) |
| 10622 | goto look_for_q; |
| 10623 | if (*str == '\0') |
| 10624 | goto expect_comma; |
| 10625 | w = str + 1; |
| 10626 | } |
| 10627 | str++; |
| 10628 | } |
| 10629 | goto expect_comma; |
| 10630 | |
| 10631 | look_for_q: |
| 10632 | |
| 10633 | /* Jump spaces, quit if the end is detected. */ |
| 10634 | while (al < end && isspace(*al)) |
| 10635 | al++; |
| 10636 | if (al >= end) |
| 10637 | goto process_value; |
| 10638 | |
| 10639 | /* If ',' is found, process the result */ |
| 10640 | if (*al == ',') |
| 10641 | goto process_value; |
| 10642 | |
| 10643 | /* If the character is different from ';', look |
| 10644 | * for the end of the header part in best effort. |
| 10645 | */ |
| 10646 | if (*al != ';') |
| 10647 | goto expect_comma; |
| 10648 | |
| 10649 | /* Assumes that the char is ';', now expect "q=". */ |
| 10650 | al++; |
| 10651 | |
| 10652 | /* Jump spaces, process value if the end is detected. */ |
| 10653 | while (al < end && isspace(*al)) |
| 10654 | al++; |
| 10655 | if (al >= end) |
| 10656 | goto process_value; |
| 10657 | |
| 10658 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 10659 | if (*al != 'q') |
| 10660 | goto process_value; |
| 10661 | al++; |
| 10662 | |
| 10663 | /* Jump spaces, process value if the end is detected. */ |
| 10664 | while (al < end && isspace(*al)) |
| 10665 | al++; |
| 10666 | if (al >= end) |
| 10667 | goto process_value; |
| 10668 | |
| 10669 | /* Expect '='. If no '=', continue in best effort */ |
| 10670 | if (*al != '=') |
| 10671 | goto process_value; |
| 10672 | al++; |
| 10673 | |
| 10674 | /* Jump spaces, process value if the end is detected. */ |
| 10675 | while (al < end && isspace(*al)) |
| 10676 | al++; |
| 10677 | if (al >= end) |
| 10678 | goto process_value; |
| 10679 | |
| 10680 | /* Parse the q value. */ |
| 10681 | qvalue = parse_qvalue(al, &al); |
| 10682 | |
| 10683 | process_value: |
| 10684 | |
| 10685 | /* If the new q value is the best q value, then store the associated |
| 10686 | * language in the response. If qvalue is the biggest value (1000), |
| 10687 | * break the process. |
| 10688 | */ |
| 10689 | if (qvalue > best_q) { |
| 10690 | smp->data.str.str = (char *)w; |
| 10691 | smp->data.str.len = str - w; |
| 10692 | if (qvalue >= 1000) |
| 10693 | break; |
| 10694 | best_q = qvalue; |
| 10695 | } |
| 10696 | |
| 10697 | expect_comma: |
| 10698 | |
| 10699 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 10700 | while (al < end && *al != ',') |
| 10701 | al++; |
| 10702 | if (al >= end) |
| 10703 | break; |
| 10704 | |
| 10705 | /* Comma is found, jump it and restart the analyzer. */ |
| 10706 | al++; |
| 10707 | } |
| 10708 | |
| 10709 | /* Set default value if required. */ |
| 10710 | if (smp->data.str.len == 0 && args[1].type == ARGT_STR) { |
| 10711 | smp->data.str.str = args[1].data.str.str; |
| 10712 | smp->data.str.len = args[1].data.str.len; |
| 10713 | } |
| 10714 | |
| 10715 | /* Return true only if a matching language was found. */ |
| 10716 | return smp->data.str.len != 0; |
| 10717 | } |
| 10718 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10719 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10720 | /* All supported ACL keywords must be declared here. */ |
| 10721 | /************************************************************************/ |
| 10722 | |
| 10723 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10724 | * Please take care of keeping this list alphabetically sorted. |
| 10725 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10726 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10727 | { "base", "base", PAT_MATCH_STR }, |
| 10728 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 10729 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 10730 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 10731 | { "base_end", "base", PAT_MATCH_END }, |
| 10732 | { "base_len", "base", PAT_MATCH_LEN }, |
| 10733 | { "base_reg", "base", PAT_MATCH_REG }, |
| 10734 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10735 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10736 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 10737 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 10738 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 10739 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 10740 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 10741 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 10742 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 10743 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10744 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10745 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 10746 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 10747 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 10748 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 10749 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 10750 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 10751 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 10752 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10753 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10754 | /* these two declarations uses strings with list storage (in place |
| 10755 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 10756 | * and delete functions are relative to the list management. The parse |
| 10757 | * and match method are related to the corresponding fetch methods. This |
| 10758 | * is very particular ACL declaration mode. |
| 10759 | */ |
| 10760 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 10761 | { "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] | 10762 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10763 | { "path", "path", PAT_MATCH_STR }, |
| 10764 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 10765 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 10766 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 10767 | { "path_end", "path", PAT_MATCH_END }, |
| 10768 | { "path_len", "path", PAT_MATCH_LEN }, |
| 10769 | { "path_reg", "path", PAT_MATCH_REG }, |
| 10770 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10771 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10772 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 10773 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10774 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10775 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 10776 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 10777 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 10778 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 10779 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 10780 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 10781 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 10782 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10783 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10784 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 10785 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 10786 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 10787 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 10788 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 10789 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 10790 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 10791 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10792 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10793 | { "url", "url", PAT_MATCH_STR }, |
| 10794 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 10795 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 10796 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 10797 | { "url_end", "url", PAT_MATCH_END }, |
| 10798 | { "url_len", "url", PAT_MATCH_LEN }, |
| 10799 | { "url_reg", "url", PAT_MATCH_REG }, |
| 10800 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10801 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10802 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 10803 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 10804 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 10805 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 10806 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 10807 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 10808 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 10809 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10810 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 10811 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10812 | }}; |
| 10813 | |
| 10814 | /************************************************************************/ |
| 10815 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10816 | /************************************************************************/ |
| 10817 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10818 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10819 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10820 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10821 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 10822 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10823 | /* capture are allocated and are permanent in the session */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10824 | { "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] | 10825 | |
| 10826 | /* retrieve these captures from the HTTP logs */ |
| 10827 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 10828 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 10829 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 10830 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10831 | { "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] | 10832 | { "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] | 10833 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10834 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 10835 | * are only here to match the ACL's name, are request-only and are used |
| 10836 | * for ACL compatibility only. |
| 10837 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10838 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 10839 | { "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] | 10840 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10841 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10842 | |
| 10843 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 10844 | * only here to match the ACL's name, are request-only and are used for |
| 10845 | * ACL compatibility only. |
| 10846 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10847 | { "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] | 10848 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10849 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10850 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10851 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 10852 | { "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] | 10853 | { "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] | 10854 | { "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] | 10855 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10856 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10857 | |
| 10858 | /* HTTP protocol on the request path */ |
| 10859 | { "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] | 10860 | { "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] | 10861 | |
| 10862 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10863 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 10864 | { "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] | 10865 | |
| 10866 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10867 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 10868 | { "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] | 10869 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10870 | /* 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] | 10871 | { "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] | 10872 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10873 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10874 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10875 | { "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] | 10876 | { "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] | 10877 | { "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] | 10878 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10879 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10880 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10881 | |
| 10882 | /* 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] | 10883 | { "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] | 10884 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10885 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10886 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10887 | { "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] | 10888 | { "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] | 10889 | { "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] | 10890 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10891 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10892 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10893 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10894 | /* 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] | 10895 | { "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] | 10896 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10897 | { "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] | 10898 | { "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] | 10899 | |
| 10900 | /* 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] | 10901 | { "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] | 10902 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10903 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10904 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10905 | |
| 10906 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10907 | { "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] | 10908 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10909 | { "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] | 10910 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10911 | { "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] | 10912 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 10913 | { "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] | 10914 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10915 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10916 | }}; |
| 10917 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10918 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10919 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 10920 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 10921 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR}, |
| 10922 | { "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] | 10923 | { NULL, NULL, 0, 0, 0 }, |
| 10924 | }}; |
| 10925 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10926 | __attribute__((constructor)) |
| 10927 | static void __http_protocol_init(void) |
| 10928 | { |
| 10929 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10930 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10931 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10932 | } |
| 10933 | |
| 10934 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 10935 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 10936 | * Local variables: |
| 10937 | * c-indent-level: 8 |
| 10938 | * c-basic-offset: 8 |
| 10939 | * End: |
| 10940 | */ |