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> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 57 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 61 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 62 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 64 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | #include <proto/task.h> |
| 66 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 67 | const char HTTP_100[] = |
| 68 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 69 | |
| 70 | const struct chunk http_100_chunk = { |
| 71 | .str = (char *)&HTTP_100, |
| 72 | .len = sizeof(HTTP_100)-1 |
| 73 | }; |
| 74 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 75 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 78 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 79 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 80 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 81 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 82 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 85 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 86 | |
| 87 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 88 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 90 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the same method */ |
| 96 | const char *HTTP_307 = |
| 97 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 98 | "Cache-Control: no-cache\r\n" |
| 99 | "Content-length: 0\r\n" |
| 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
| 102 | /* same as 301 except that the browser MUST retry with the same method */ |
| 103 | const char *HTTP_308 = |
| 104 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 105 | "Content-length: 0\r\n" |
| 106 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 107 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 109 | const char *HTTP_401_fmt = |
| 110 | "HTTP/1.0 401 Unauthorized\r\n" |
| 111 | "Cache-Control: no-cache\r\n" |
| 112 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 113 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 115 | "\r\n" |
| 116 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 117 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 118 | const char *HTTP_407_fmt = |
| 119 | "HTTP/1.0 407 Unauthorized\r\n" |
| 120 | "Cache-Control: no-cache\r\n" |
| 121 | "Connection: close\r\n" |
| 122 | "Content-Type: text/html\r\n" |
| 123 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 124 | "\r\n" |
| 125 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 126 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 127 | |
| 128 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 129 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | [HTTP_ERR_400] = 400, |
| 131 | [HTTP_ERR_403] = 403, |
| 132 | [HTTP_ERR_408] = 408, |
| 133 | [HTTP_ERR_500] = 500, |
| 134 | [HTTP_ERR_502] = 502, |
| 135 | [HTTP_ERR_503] = 503, |
| 136 | [HTTP_ERR_504] = 504, |
| 137 | }; |
| 138 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 139 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 140 | [HTTP_ERR_200] = |
| 141 | "HTTP/1.0 200 OK\r\n" |
| 142 | "Cache-Control: no-cache\r\n" |
| 143 | "Connection: close\r\n" |
| 144 | "Content-Type: text/html\r\n" |
| 145 | "\r\n" |
| 146 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 147 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 148 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 149 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 150 | "Cache-Control: no-cache\r\n" |
| 151 | "Connection: close\r\n" |
| 152 | "Content-Type: text/html\r\n" |
| 153 | "\r\n" |
| 154 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 155 | |
| 156 | [HTTP_ERR_403] = |
| 157 | "HTTP/1.0 403 Forbidden\r\n" |
| 158 | "Cache-Control: no-cache\r\n" |
| 159 | "Connection: close\r\n" |
| 160 | "Content-Type: text/html\r\n" |
| 161 | "\r\n" |
| 162 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 163 | |
| 164 | [HTTP_ERR_408] = |
| 165 | "HTTP/1.0 408 Request Time-out\r\n" |
| 166 | "Cache-Control: no-cache\r\n" |
| 167 | "Connection: close\r\n" |
| 168 | "Content-Type: text/html\r\n" |
| 169 | "\r\n" |
| 170 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 171 | |
| 172 | [HTTP_ERR_500] = |
| 173 | "HTTP/1.0 500 Server Error\r\n" |
| 174 | "Cache-Control: no-cache\r\n" |
| 175 | "Connection: close\r\n" |
| 176 | "Content-Type: text/html\r\n" |
| 177 | "\r\n" |
| 178 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 179 | |
| 180 | [HTTP_ERR_502] = |
| 181 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 182 | "Cache-Control: no-cache\r\n" |
| 183 | "Connection: close\r\n" |
| 184 | "Content-Type: text/html\r\n" |
| 185 | "\r\n" |
| 186 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 187 | |
| 188 | [HTTP_ERR_503] = |
| 189 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 190 | "Cache-Control: no-cache\r\n" |
| 191 | "Connection: close\r\n" |
| 192 | "Content-Type: text/html\r\n" |
| 193 | "\r\n" |
| 194 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 195 | |
| 196 | [HTTP_ERR_504] = |
| 197 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 198 | "Cache-Control: no-cache\r\n" |
| 199 | "Connection: close\r\n" |
| 200 | "Content-Type: text/html\r\n" |
| 201 | "\r\n" |
| 202 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 203 | |
| 204 | }; |
| 205 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 206 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 207 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 208 | [STAT_STATUS_DENY] = "DENY", |
| 209 | [STAT_STATUS_DONE] = "DONE", |
| 210 | [STAT_STATUS_ERRP] = "ERRP", |
| 211 | [STAT_STATUS_EXCD] = "EXCD", |
| 212 | [STAT_STATUS_NONE] = "NONE", |
| 213 | [STAT_STATUS_PART] = "PART", |
| 214 | [STAT_STATUS_UNKN] = "UNKN", |
| 215 | }; |
| 216 | |
| 217 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 218 | /* We must put the messages here since GCC cannot initialize consts depending |
| 219 | * on strlen(). |
| 220 | */ |
| 221 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 222 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 223 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 224 | static struct hdr_ctx static_hdr_ctx; |
| 225 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 226 | #define FD_SETS_ARE_BITFIELDS |
| 227 | #ifdef FD_SETS_ARE_BITFIELDS |
| 228 | /* |
| 229 | * This map is used with all the FD_* macros to check whether a particular bit |
| 230 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 231 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 232 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 233 | * exclusively to the macros. |
| 234 | */ |
| 235 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 236 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 237 | |
| 238 | #else |
| 239 | #error "Check if your OS uses bitfields for fd_sets" |
| 240 | #endif |
| 241 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 242 | void init_proto_http() |
| 243 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 244 | int i; |
| 245 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 246 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 247 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 248 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 249 | if (!http_err_msgs[msg]) { |
| 250 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 251 | abort(); |
| 252 | } |
| 253 | |
| 254 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 255 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 256 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 257 | |
| 258 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 259 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 260 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 261 | * printable characters above. |
| 262 | */ |
| 263 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 264 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 265 | for (i = 0; i < 32; i++) { |
| 266 | FD_SET(i, hdr_encode_map); |
| 267 | FD_SET(i, url_encode_map); |
| 268 | } |
| 269 | for (i = 127; i < 256; i++) { |
| 270 | FD_SET(i, hdr_encode_map); |
| 271 | FD_SET(i, url_encode_map); |
| 272 | } |
| 273 | |
| 274 | tmp = "\"#{|}"; |
| 275 | while (*tmp) { |
| 276 | FD_SET(*tmp, hdr_encode_map); |
| 277 | tmp++; |
| 278 | } |
| 279 | |
| 280 | tmp = "\"#"; |
| 281 | while (*tmp) { |
| 282 | FD_SET(*tmp, url_encode_map); |
| 283 | tmp++; |
| 284 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 285 | |
| 286 | /* memory allocations */ |
| 287 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 288 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 289 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 291 | /* |
| 292 | * We have 26 list of methods (1 per first letter), each of which can have |
| 293 | * up to 3 entries (2 valid, 1 null). |
| 294 | */ |
| 295 | struct http_method_desc { |
| 296 | http_meth_t meth; |
| 297 | int len; |
| 298 | const char text[8]; |
| 299 | }; |
| 300 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 301 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 302 | ['C' - 'A'] = { |
| 303 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 304 | }, |
| 305 | ['D' - 'A'] = { |
| 306 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 307 | }, |
| 308 | ['G' - 'A'] = { |
| 309 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 310 | }, |
| 311 | ['H' - 'A'] = { |
| 312 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 313 | }, |
| 314 | ['P' - 'A'] = { |
| 315 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 316 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 317 | }, |
| 318 | ['T' - 'A'] = { |
| 319 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 320 | }, |
| 321 | /* rest is empty like this : |
| 322 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 323 | */ |
| 324 | }; |
| 325 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 326 | /* 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] | 327 | * 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] | 328 | * RFC2616 for those chars. |
| 329 | */ |
| 330 | |
| 331 | const char http_is_spht[256] = { |
| 332 | [' '] = 1, ['\t'] = 1, |
| 333 | }; |
| 334 | |
| 335 | const char http_is_crlf[256] = { |
| 336 | ['\r'] = 1, ['\n'] = 1, |
| 337 | }; |
| 338 | |
| 339 | const char http_is_lws[256] = { |
| 340 | [' '] = 1, ['\t'] = 1, |
| 341 | ['\r'] = 1, ['\n'] = 1, |
| 342 | }; |
| 343 | |
| 344 | const char http_is_sep[256] = { |
| 345 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 346 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 347 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 348 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 349 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 350 | }; |
| 351 | |
| 352 | const char http_is_ctl[256] = { |
| 353 | [0 ... 31] = 1, |
| 354 | [127] = 1, |
| 355 | }; |
| 356 | |
| 357 | /* |
| 358 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 359 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 360 | * them correctly. Instead, define every non-CTL char's status. |
| 361 | */ |
| 362 | const char http_is_token[256] = { |
| 363 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 364 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 365 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 366 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 367 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 368 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 369 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 370 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 371 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 372 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 373 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 374 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 375 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 376 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 377 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 378 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 379 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 380 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 381 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 382 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 383 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 384 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 385 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 386 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 387 | }; |
| 388 | |
| 389 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 390 | /* |
| 391 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 392 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 393 | */ |
| 394 | const char http_is_ver_token[256] = { |
| 395 | ['.'] = 1, ['/'] = 1, |
| 396 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 397 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 398 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 399 | }; |
| 400 | |
| 401 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 403 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 404 | */ |
| 405 | #if defined(DEBUG_FSM) |
| 406 | static void http_silent_debug(int line, struct session *s) |
| 407 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 408 | chunk_printf(&trash, |
| 409 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n", |
| 410 | line, |
| 411 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 412 | s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags); |
| 413 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 414 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 415 | chunk_printf(&trash, |
| 416 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n", |
| 417 | line, |
| 418 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 419 | s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward); |
| 420 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 421 | } |
| 422 | #else |
| 423 | #define http_silent_debug(l,s) do { } while (0) |
| 424 | #endif |
| 425 | |
| 426 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 427 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 428 | * 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] | 429 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 430 | * of headers is automatically adjusted. The number of bytes added is returned |
| 431 | * on success, otherwise <0 is returned indicating an error. |
| 432 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 433 | 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] | 434 | { |
| 435 | int bytes, len; |
| 436 | |
| 437 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 438 | 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] | 439 | if (!bytes) |
| 440 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 441 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 442 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 443 | } |
| 444 | |
| 445 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 446 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 447 | * 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] | 448 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 449 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 450 | * of headers is automatically adjusted. The number of bytes added is returned |
| 451 | * on success, otherwise <0 is returned indicating an error. |
| 452 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 453 | int http_header_add_tail2(struct http_msg *msg, |
| 454 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 455 | { |
| 456 | int bytes; |
| 457 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 458 | 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] | 459 | if (!bytes) |
| 460 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 461 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 462 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 463 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | |
| 465 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 466 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 467 | * If so, returns the position of the first non-space character relative to |
| 468 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 469 | * to return a pointer to the place after the first space. Returns 0 if the |
| 470 | * header name does not match. Checks are case-insensitive. |
| 471 | */ |
| 472 | int http_header_match2(const char *hdr, const char *end, |
| 473 | const char *name, int len) |
| 474 | { |
| 475 | const char *val; |
| 476 | |
| 477 | if (hdr + len >= end) |
| 478 | return 0; |
| 479 | if (hdr[len] != ':') |
| 480 | return 0; |
| 481 | if (strncasecmp(hdr, name, len) != 0) |
| 482 | return 0; |
| 483 | val = hdr + len + 1; |
| 484 | while (val < end && HTTP_IS_SPHT(*val)) |
| 485 | val++; |
| 486 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 487 | return len + 2; /* we may replace starting from second space */ |
| 488 | return val - hdr; |
| 489 | } |
| 490 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 491 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 492 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 493 | * structure holds everything necessary to use the header and find next |
| 494 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 495 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 496 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 497 | * http_find_header2() except that it is designed to work with full-line headers |
| 498 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 499 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 500 | * header is rescanned. This allows rescanning after a header deletion. |
| 501 | */ |
| 502 | int http_find_full_header2(const char *name, int len, |
| 503 | char *sol, struct hdr_idx *idx, |
| 504 | struct hdr_ctx *ctx) |
| 505 | { |
| 506 | char *eol, *sov; |
| 507 | int cur_idx, old_idx; |
| 508 | |
| 509 | cur_idx = ctx->idx; |
| 510 | if (cur_idx) { |
| 511 | /* We have previously returned a header, let's search another one */ |
| 512 | sol = ctx->line; |
| 513 | eol = sol + idx->v[cur_idx].len; |
| 514 | goto next_hdr; |
| 515 | } |
| 516 | |
| 517 | /* first request for this header */ |
| 518 | sol += hdr_idx_first_pos(idx); |
| 519 | old_idx = 0; |
| 520 | cur_idx = hdr_idx_first_idx(idx); |
| 521 | while (cur_idx) { |
| 522 | eol = sol + idx->v[cur_idx].len; |
| 523 | |
| 524 | if (len == 0) { |
| 525 | /* No argument was passed, we want any header. |
| 526 | * To achieve this, we simply build a fake request. */ |
| 527 | while (sol + len < eol && sol[len] != ':') |
| 528 | len++; |
| 529 | name = sol; |
| 530 | } |
| 531 | |
| 532 | if ((len < eol - sol) && |
| 533 | (sol[len] == ':') && |
| 534 | (strncasecmp(sol, name, len) == 0)) { |
| 535 | ctx->del = len; |
| 536 | sov = sol + len + 1; |
| 537 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 538 | sov++; |
| 539 | |
| 540 | ctx->line = sol; |
| 541 | ctx->prev = old_idx; |
| 542 | ctx->idx = cur_idx; |
| 543 | ctx->val = sov - sol; |
| 544 | ctx->tws = 0; |
| 545 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 546 | eol--; |
| 547 | ctx->tws++; |
| 548 | } |
| 549 | ctx->vlen = eol - sov; |
| 550 | return 1; |
| 551 | } |
| 552 | next_hdr: |
| 553 | sol = eol + idx->v[cur_idx].cr + 1; |
| 554 | old_idx = cur_idx; |
| 555 | cur_idx = idx->v[cur_idx].next; |
| 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 560 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 561 | * par 2.2 for more information. Note that it requires a valid header to return |
| 562 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 563 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 564 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | { |
| 566 | int quoted, qdpair; |
| 567 | |
| 568 | quoted = qdpair = 0; |
| 569 | for (; s < e; s++) { |
| 570 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 571 | else if (quoted) { |
| 572 | if (*s == '\\') qdpair = 1; |
| 573 | else if (*s == '"') quoted = 0; |
| 574 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 575 | else if (*s == '"') quoted = 1; |
| 576 | else if (*s == ',') return s; |
| 577 | } |
| 578 | return s; |
| 579 | } |
| 580 | |
| 581 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 582 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 583 | * structure holds everything necessary to use the header and find next |
| 584 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 585 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 586 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 587 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 588 | * is NULL when searching for a new values of a header, the current header is |
| 589 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 590 | */ |
| 591 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 592 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 593 | struct hdr_ctx *ctx) |
| 594 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 595 | char *eol, *sov; |
| 596 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 597 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 598 | cur_idx = ctx->idx; |
| 599 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 600 | /* We have previously returned a value, let's search |
| 601 | * another one on the same line. |
| 602 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 603 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 604 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 605 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 606 | eol = sol + idx->v[cur_idx].len; |
| 607 | |
| 608 | if (sov >= eol) |
| 609 | /* no more values in this header */ |
| 610 | goto next_hdr; |
| 611 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | /* values remaining for this header, skip the comma but save it |
| 613 | * for later use (eg: for header deletion). |
| 614 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 615 | sov++; |
| 616 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 617 | sov++; |
| 618 | |
| 619 | goto return_hdr; |
| 620 | } |
| 621 | |
| 622 | /* first request for this header */ |
| 623 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 625 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 626 | while (cur_idx) { |
| 627 | eol = sol + idx->v[cur_idx].len; |
| 628 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 629 | if (len == 0) { |
| 630 | /* No argument was passed, we want any header. |
| 631 | * To achieve this, we simply build a fake request. */ |
| 632 | while (sol + len < eol && sol[len] != ':') |
| 633 | len++; |
| 634 | name = sol; |
| 635 | } |
| 636 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 637 | if ((len < eol - sol) && |
| 638 | (sol[len] == ':') && |
| 639 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 640 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 641 | sov = sol + len + 1; |
| 642 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 643 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 644 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 645 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 646 | ctx->prev = old_idx; |
| 647 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 648 | ctx->idx = cur_idx; |
| 649 | ctx->val = sov - sol; |
| 650 | |
| 651 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 652 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 653 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 654 | eol--; |
| 655 | ctx->tws++; |
| 656 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 657 | ctx->vlen = eol - sov; |
| 658 | return 1; |
| 659 | } |
| 660 | next_hdr: |
| 661 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 662 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 663 | cur_idx = idx->v[cur_idx].next; |
| 664 | } |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 669 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 670 | struct hdr_ctx *ctx) |
| 671 | { |
| 672 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 673 | } |
| 674 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 675 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 676 | * the http_find_header functions. The value is removed, as well as surrounding |
| 677 | * 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] | 678 | * 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] | 679 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 680 | * no more header, so any processing may stop. The ctx is always left in a form |
| 681 | * that can be handled by http_find_header2() to find next occurrence. |
| 682 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 683 | 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] | 684 | { |
| 685 | int cur_idx = ctx->idx; |
| 686 | char *sol = ctx->line; |
| 687 | struct hdr_idx_elem *hdr; |
| 688 | int delta, skip_comma; |
| 689 | |
| 690 | if (!cur_idx) |
| 691 | return 0; |
| 692 | |
| 693 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 694 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 695 | /* 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] | 696 | 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] | 697 | http_msg_move_end(msg, delta); |
| 698 | idx->used--; |
| 699 | hdr->len = 0; /* unused entry */ |
| 700 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 701 | if (idx->tail == ctx->idx) |
| 702 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 703 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 704 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 705 | 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] | 706 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 707 | return ctx->idx; |
| 708 | } |
| 709 | |
| 710 | /* 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] | 711 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 712 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 713 | */ |
| 714 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 715 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 716 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 717 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 718 | NULL, 0); |
| 719 | hdr->len += delta; |
| 720 | http_msg_move_end(msg, delta); |
| 721 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 722 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 723 | return ctx->idx; |
| 724 | } |
| 725 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 726 | /* This function handles a server error at the stream interface level. The |
| 727 | * stream interface is assumed to be already in a closed state. An optional |
| 728 | * message is copied into the input buffer, and an HTTP status code stored. |
| 729 | * 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] | 730 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 731 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 732 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 733 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 734 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 735 | channel_auto_read(si->ob); |
| 736 | channel_abort(si->ob); |
| 737 | channel_auto_close(si->ob); |
| 738 | channel_erase(si->ob); |
| 739 | channel_auto_close(si->ib); |
| 740 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 741 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 742 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 743 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 744 | } |
| 745 | if (!(t->flags & SN_ERR_MASK)) |
| 746 | t->flags |= err; |
| 747 | if (!(t->flags & SN_FINST_MASK)) |
| 748 | t->flags |= finst; |
| 749 | } |
| 750 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 751 | /* This function returns the appropriate error location for the given session |
| 752 | * and message. |
| 753 | */ |
| 754 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 755 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 756 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 757 | if (s->be->errmsg[msgnum].str) |
| 758 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 759 | else if (s->fe->errmsg[msgnum].str) |
| 760 | return &s->fe->errmsg[msgnum]; |
| 761 | else |
| 762 | return &http_err_chunks[msgnum]; |
| 763 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 764 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 765 | /* |
| 766 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 767 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 768 | */ |
| 769 | static http_meth_t find_http_meth(const char *str, const int len) |
| 770 | { |
| 771 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 772 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 773 | |
| 774 | m = ((unsigned)*str - 'A'); |
| 775 | |
| 776 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 777 | for (h = http_methods[m]; h->len > 0; h++) { |
| 778 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 779 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 780 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 781 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 782 | }; |
| 783 | return HTTP_METH_OTHER; |
| 784 | } |
| 785 | return HTTP_METH_NONE; |
| 786 | |
| 787 | } |
| 788 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 789 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 790 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 791 | * It is returned otherwise. |
| 792 | */ |
| 793 | static char * |
| 794 | http_get_path(struct http_txn *txn) |
| 795 | { |
| 796 | char *ptr, *end; |
| 797 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 798 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 799 | end = ptr + txn->req.sl.rq.u_l; |
| 800 | |
| 801 | if (ptr >= end) |
| 802 | return NULL; |
| 803 | |
| 804 | /* RFC2616, par. 5.1.2 : |
| 805 | * Request-URI = "*" | absuri | abspath | authority |
| 806 | */ |
| 807 | |
| 808 | if (*ptr == '*') |
| 809 | return NULL; |
| 810 | |
| 811 | if (isalpha((unsigned char)*ptr)) { |
| 812 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 813 | ptr++; |
| 814 | while (ptr < end && |
| 815 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 816 | ptr++; |
| 817 | /* skip '://' */ |
| 818 | if (ptr == end || *ptr++ != ':') |
| 819 | return NULL; |
| 820 | if (ptr == end || *ptr++ != '/') |
| 821 | return NULL; |
| 822 | if (ptr == end || *ptr++ != '/') |
| 823 | return NULL; |
| 824 | } |
| 825 | /* skip [user[:passwd]@]host[:[port]] */ |
| 826 | |
| 827 | while (ptr < end && *ptr != '/') |
| 828 | ptr++; |
| 829 | |
| 830 | if (ptr == end) |
| 831 | return NULL; |
| 832 | |
| 833 | /* OK, we got the '/' ! */ |
| 834 | return ptr; |
| 835 | } |
| 836 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 837 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 838 | * in redirect mode. This may only be called just after the stream interface |
| 839 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 840 | * follow normal proxy processing. NOTE: this function is designed to support |
| 841 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 842 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 843 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 844 | { |
| 845 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 846 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 847 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 848 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 849 | |
| 850 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 851 | trash.len = strlen(HTTP_302); |
| 852 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 853 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 854 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 855 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 856 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 857 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 858 | return; |
| 859 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 860 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 861 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 862 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 863 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 864 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 865 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 866 | /* 3: add the request URI. Since it was already forwarded, we need |
| 867 | * to temporarily rewind the buffer. |
| 868 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 869 | txn = &s->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 870 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 871 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 872 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 873 | 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] | 874 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 875 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 876 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 877 | if (!path) |
| 878 | return; |
| 879 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 880 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 881 | return; |
| 882 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 883 | memcpy(trash.str + trash.len, path, len); |
| 884 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 885 | |
| 886 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 887 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 888 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 889 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 890 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 891 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 892 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 893 | |
| 894 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 895 | si_shutr(si); |
| 896 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 897 | si->err_type = SI_ET_NONE; |
| 898 | si->err_loc = NULL; |
| 899 | si->state = SI_ST_CLO; |
| 900 | |
| 901 | /* send the message */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 902 | 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] | 903 | |
| 904 | /* 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] | 905 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 908 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 909 | * that the server side is closed. Note that err_type is actually a |
| 910 | * bitmask, where almost only aborts may be cumulated with other |
| 911 | * values. We consider that aborted operations are more important |
| 912 | * than timeouts or errors due to the fact that nobody else in the |
| 913 | * logs might explain incomplete retries. All others should avoid |
| 914 | * being cumulated. It should normally not be possible to have multiple |
| 915 | * aborts at once, but just in case, the first one in sequence is reported. |
| 916 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 917 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 918 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 919 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 920 | |
| 921 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 922 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 923 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 924 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 925 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 926 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 927 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 928 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 929 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 930 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 931 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 932 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 933 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 934 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 935 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 936 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 937 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 938 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 939 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 940 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 941 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 944 | extern const char sess_term_cond[8]; |
| 945 | extern const char sess_fin_state[8]; |
| 946 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 947 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 948 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 949 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 950 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 951 | /* |
| 952 | * Capture headers from message starting at <som> according to header list |
| 953 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 954 | */ |
| 955 | void capture_headers(char *som, struct hdr_idx *idx, |
| 956 | char **cap, struct cap_hdr *cap_hdr) |
| 957 | { |
| 958 | char *eol, *sol, *col, *sov; |
| 959 | int cur_idx; |
| 960 | struct cap_hdr *h; |
| 961 | int len; |
| 962 | |
| 963 | sol = som + hdr_idx_first_pos(idx); |
| 964 | cur_idx = hdr_idx_first_idx(idx); |
| 965 | |
| 966 | while (cur_idx) { |
| 967 | eol = sol + idx->v[cur_idx].len; |
| 968 | |
| 969 | col = sol; |
| 970 | while (col < eol && *col != ':') |
| 971 | col++; |
| 972 | |
| 973 | sov = col + 1; |
| 974 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 975 | sov++; |
| 976 | |
| 977 | for (h = cap_hdr; h; h = h->next) { |
| 978 | if ((h->namelen == col - sol) && |
| 979 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 980 | if (cap[h->index] == NULL) |
| 981 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 982 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 983 | |
| 984 | if (cap[h->index] == NULL) { |
| 985 | Alert("HTTP capture : out of memory.\n"); |
| 986 | continue; |
| 987 | } |
| 988 | |
| 989 | len = eol - sov; |
| 990 | if (len > h->len) |
| 991 | len = h->len; |
| 992 | |
| 993 | memcpy(cap[h->index], sov, len); |
| 994 | cap[h->index][len]=0; |
| 995 | } |
| 996 | } |
| 997 | sol = eol + idx->v[cur_idx].cr + 1; |
| 998 | cur_idx = idx->v[cur_idx].next; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1003 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1004 | */ |
| 1005 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1006 | |
| 1007 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1008 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1009 | */ |
| 1010 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1011 | ptr++; \ |
| 1012 | if (likely(ptr < end)) \ |
| 1013 | goto good; \ |
| 1014 | else { \ |
| 1015 | state = (st); \ |
| 1016 | goto http_msg_ood; \ |
| 1017 | } \ |
| 1018 | } while (0) |
| 1019 | |
| 1020 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1021 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1022 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1023 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1024 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1025 | * will give undefined results. |
| 1026 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1027 | * and that msg->sol points to the beginning of the response. |
| 1028 | * If a complete line is found (which implies that at least one CR or LF is |
| 1029 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1030 | * returned indicating an incomplete line (which does not mean that parts have |
| 1031 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1032 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1033 | * upon next call. |
| 1034 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1035 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1036 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1037 | * 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] | 1038 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1039 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1040 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1041 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1042 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1043 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1044 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1045 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1046 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1047 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1048 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1049 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1050 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1051 | |
| 1052 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1053 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1054 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1055 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1056 | state = HTTP_MSG_ERROR; |
| 1057 | break; |
| 1058 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1059 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1060 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1061 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1062 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1063 | goto http_msg_rpcode; |
| 1064 | } |
| 1065 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1066 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1067 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1068 | state = HTTP_MSG_ERROR; |
| 1069 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1070 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1071 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1072 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1073 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1074 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1075 | |
| 1076 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1077 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1078 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1079 | } |
| 1080 | |
| 1081 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1082 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1083 | http_msg_rsp_reason: |
| 1084 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1085 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1086 | msg->sl.st.r_l = 0; |
| 1087 | goto http_msg_rpline_eol; |
| 1088 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1089 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1090 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1091 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1092 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1093 | goto http_msg_rpreason; |
| 1094 | } |
| 1095 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1096 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1097 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1098 | goto http_msg_rsp_reason; |
| 1099 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1100 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1101 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1102 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1103 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1104 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1105 | http_msg_rpline_eol: |
| 1106 | /* We have seen the end of line. Note that we do not |
| 1107 | * necessarily have the \n yet, but at least we know that we |
| 1108 | * have EITHER \r OR \n, otherwise the response would not be |
| 1109 | * complete. We can then record the response length and return |
| 1110 | * to the caller which will be able to register it. |
| 1111 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1112 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1113 | return ptr; |
| 1114 | |
| 1115 | #ifdef DEBUG_FULL |
| 1116 | default: |
| 1117 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1118 | exit(1); |
| 1119 | #endif |
| 1120 | } |
| 1121 | |
| 1122 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1123 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1124 | if (ret_state) |
| 1125 | *ret_state = state; |
| 1126 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1127 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1128 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1131 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1132 | * This function parses a request line between <ptr> and <end>, starting with |
| 1133 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1134 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1135 | * will give undefined results. |
| 1136 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1137 | * and that msg->sol points to the beginning of the request. |
| 1138 | * If a complete line is found (which implies that at least one CR or LF is |
| 1139 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1140 | * returned indicating an incomplete line (which does not mean that parts have |
| 1141 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1142 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1143 | * upon next call. |
| 1144 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1145 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1146 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1147 | * 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] | 1148 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1149 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1150 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1151 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1152 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1153 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1154 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1155 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1156 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1157 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1158 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1159 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1160 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1161 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1162 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1163 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1164 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1165 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1166 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1167 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1168 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1169 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1170 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1171 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1172 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1173 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1174 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1175 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1176 | msg->sl.rq.v_l = 0; |
| 1177 | goto http_msg_rqline_eol; |
| 1178 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1179 | state = HTTP_MSG_ERROR; |
| 1180 | break; |
| 1181 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1182 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1183 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1185 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1186 | goto http_msg_rquri; |
| 1187 | } |
| 1188 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1189 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1190 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1191 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1192 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1193 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1194 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1195 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1196 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1197 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1198 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1199 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1200 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1201 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1202 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1203 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1204 | /* non-ASCII chars are forbidden unless option |
| 1205 | * accept-invalid-http-request is enabled in the frontend. |
| 1206 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1207 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1208 | if (msg->err_pos < -1) |
| 1209 | goto invalid_char; |
| 1210 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1211 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1212 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1213 | } |
| 1214 | |
| 1215 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1216 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1217 | goto http_msg_req09_uri_e; |
| 1218 | } |
| 1219 | |
| 1220 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1221 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1222 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1223 | state = HTTP_MSG_ERROR; |
| 1224 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1225 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1226 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1227 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1228 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1229 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1230 | goto http_msg_rqver; |
| 1231 | } |
| 1232 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1233 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1234 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1235 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1236 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1237 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1238 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1239 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1240 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1241 | |
| 1242 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1243 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1244 | http_msg_rqline_eol: |
| 1245 | /* We have seen the end of line. Note that we do not |
| 1246 | * necessarily have the \n yet, but at least we know that we |
| 1247 | * have EITHER \r OR \n, otherwise the request would not be |
| 1248 | * complete. We can then record the request length and return |
| 1249 | * to the caller which will be able to register it. |
| 1250 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1251 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1252 | return ptr; |
| 1253 | } |
| 1254 | |
| 1255 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1256 | state = HTTP_MSG_ERROR; |
| 1257 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1258 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1259 | #ifdef DEBUG_FULL |
| 1260 | default: |
| 1261 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1262 | exit(1); |
| 1263 | #endif |
| 1264 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1266 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1267 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1268 | if (ret_state) |
| 1269 | *ret_state = state; |
| 1270 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1271 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1272 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1273 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1274 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1275 | /* |
| 1276 | * Returns the data from Authorization header. Function may be called more |
| 1277 | * than once so data is stored in txn->auth_data. When no header is found |
| 1278 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1279 | * searching again for something we are unable to find anyway. |
| 1280 | */ |
| 1281 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1282 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1283 | |
| 1284 | int |
| 1285 | get_http_auth(struct session *s) |
| 1286 | { |
| 1287 | |
| 1288 | struct http_txn *txn = &s->txn; |
| 1289 | struct chunk auth_method; |
| 1290 | struct hdr_ctx ctx; |
| 1291 | char *h, *p; |
| 1292 | int len; |
| 1293 | |
| 1294 | #ifdef DEBUG_AUTH |
| 1295 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1296 | #endif |
| 1297 | |
| 1298 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1299 | return 0; |
| 1300 | |
| 1301 | if (txn->auth.method) |
| 1302 | return 1; |
| 1303 | |
| 1304 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1305 | |
| 1306 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1307 | |
| 1308 | if (txn->flags & TX_USE_PX_CONN) { |
| 1309 | h = "Proxy-Authorization"; |
| 1310 | len = strlen(h); |
| 1311 | } else { |
| 1312 | h = "Authorization"; |
| 1313 | len = strlen(h); |
| 1314 | } |
| 1315 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1316 | 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] | 1317 | return 0; |
| 1318 | |
| 1319 | h = ctx.line + ctx.val; |
| 1320 | |
| 1321 | p = memchr(h, ' ', ctx.vlen); |
| 1322 | if (!p || p == h) |
| 1323 | return 0; |
| 1324 | |
| 1325 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1326 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1327 | |
| 1328 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1329 | |
| 1330 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1331 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1332 | |
| 1333 | if (len < 0) |
| 1334 | return 0; |
| 1335 | |
| 1336 | |
| 1337 | get_http_auth_buff[len] = '\0'; |
| 1338 | |
| 1339 | p = strchr(get_http_auth_buff, ':'); |
| 1340 | |
| 1341 | if (!p) |
| 1342 | return 0; |
| 1343 | |
| 1344 | txn->auth.user = get_http_auth_buff; |
| 1345 | *p = '\0'; |
| 1346 | txn->auth.pass = p+1; |
| 1347 | |
| 1348 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1349 | return 1; |
| 1350 | } |
| 1351 | |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1355 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1356 | /* |
| 1357 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1358 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1359 | * ensuring that the message does not wrap. The function can be preempted |
| 1360 | * everywhere when data are missing and recalled at the exact same location |
| 1361 | * with no information loss. The message may even be realigned between two |
| 1362 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1363 | * 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] | 1364 | * fields. Note that msg->sol will be initialized after completing the first |
| 1365 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1366 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1367 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1368 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1369 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1370 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1371 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1372 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1373 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1374 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1375 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1376 | ptr = buf->p + msg->next; |
| 1377 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1378 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1379 | if (unlikely(ptr >= end)) |
| 1380 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1381 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1382 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1383 | /* |
| 1384 | * First, states that are specific to the response only. |
| 1385 | * We check them first so that request and headers are |
| 1386 | * closer to each other (accessed more often). |
| 1387 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1388 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1389 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1390 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1391 | /* we have a start of message, but we have to check |
| 1392 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1393 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1394 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1395 | if (unlikely(ptr != buf->p)) { |
| 1396 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1397 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1398 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1399 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1400 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1401 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1402 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1403 | hdr_idx_init(idx); |
| 1404 | state = HTTP_MSG_RPVER; |
| 1405 | goto http_msg_rpver; |
| 1406 | } |
| 1407 | |
| 1408 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1409 | goto http_msg_invalid; |
| 1410 | |
| 1411 | if (unlikely(*ptr == '\n')) |
| 1412 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1413 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1414 | /* stop here */ |
| 1415 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1416 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1417 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1418 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1419 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1420 | /* stop here */ |
| 1421 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1422 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1423 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1424 | case HTTP_MSG_RPVER_SP: |
| 1425 | case HTTP_MSG_RPCODE: |
| 1426 | case HTTP_MSG_RPCODE_SP: |
| 1427 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1428 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1429 | state, ptr, end, |
| 1430 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1431 | if (unlikely(!ptr)) |
| 1432 | return; |
| 1433 | |
| 1434 | /* we have a full response and we know that we have either a CR |
| 1435 | * or an LF at <ptr>. |
| 1436 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1438 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1439 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1440 | if (likely(*ptr == '\r')) |
| 1441 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1442 | goto http_msg_rpline_end; |
| 1443 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1444 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1445 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1446 | /* msg->sol must point to the first of CR or LF. */ |
| 1447 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1448 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1449 | /* stop here */ |
| 1450 | |
| 1451 | /* |
| 1452 | * Second, states that are specific to the request only |
| 1453 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1455 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1457 | /* we have a start of message, but we have to check |
| 1458 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1459 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1460 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1461 | if (likely(ptr != buf->p)) { |
| 1462 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1463 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1464 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1465 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1466 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1467 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1468 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1469 | state = HTTP_MSG_RQMETH; |
| 1470 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1472 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1474 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | if (unlikely(*ptr == '\n')) |
| 1477 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1478 | 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] | 1479 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1480 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1482 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1484 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1485 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1488 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1489 | case HTTP_MSG_RQMETH_SP: |
| 1490 | case HTTP_MSG_RQURI: |
| 1491 | case HTTP_MSG_RQURI_SP: |
| 1492 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1493 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1494 | state, ptr, end, |
| 1495 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | if (unlikely(!ptr)) |
| 1497 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1498 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1499 | /* we have a full request and we know that we have either a CR |
| 1500 | * or an LF at <ptr>. |
| 1501 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1503 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1504 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | if (likely(*ptr == '\r')) |
| 1506 | 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] | 1507 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1508 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1509 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1510 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1511 | /* check for HTTP/0.9 request : no version information available. |
| 1512 | * msg->sol must point to the first of CR or LF. |
| 1513 | */ |
| 1514 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1515 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1516 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1518 | 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] | 1519 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1521 | /* |
| 1522 | * Common states below |
| 1523 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1525 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1526 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1528 | goto http_msg_hdr_name; |
| 1529 | } |
| 1530 | |
| 1531 | if (likely(*ptr == '\r')) |
| 1532 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1533 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1534 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1536 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | /* assumes msg->sol points to the first char */ |
| 1538 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1539 | 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] | 1540 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1541 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1542 | 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] | 1543 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1544 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1545 | goto http_msg_invalid; |
| 1546 | |
| 1547 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1548 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1549 | |
| 1550 | /* and we still accept this non-token character */ |
| 1551 | 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] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1554 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1555 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1557 | 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] | 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1560 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1561 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1563 | goto http_msg_hdr_val; |
| 1564 | } |
| 1565 | |
| 1566 | if (likely(*ptr == '\r')) |
| 1567 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1568 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1571 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1572 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1573 | 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] | 1574 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1576 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1578 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1579 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1580 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | goto http_msg_hdr_l1_sp; |
| 1582 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1583 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1584 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | goto http_msg_complete_header; |
| 1586 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1587 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1588 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1589 | /* assumes msg->sol points to the first char, and msg->sov |
| 1590 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1591 | */ |
| 1592 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1593 | 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] | 1594 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1595 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1597 | * real header end in case it ends with lots of LWS, but is this |
| 1598 | * really needed ? |
| 1599 | */ |
| 1600 | if (likely(*ptr == '\r')) |
| 1601 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1602 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1605 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1607 | 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] | 1608 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1609 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1610 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1611 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1612 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1613 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1614 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | goto http_msg_hdr_val; |
| 1616 | } |
| 1617 | http_msg_complete_header: |
| 1618 | /* |
| 1619 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1620 | * Assumes msg->sol points to the first char, msg->sov points |
| 1621 | * to the first character of the value and msg->eol to the |
| 1622 | * first CR or LF so we know how the line ends. We insert last |
| 1623 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1624 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1625 | 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] | 1626 | idx, idx->tail) < 0)) |
| 1627 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1628 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1629 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1631 | goto http_msg_hdr_name; |
| 1632 | } |
| 1633 | |
| 1634 | if (likely(*ptr == '\r')) |
| 1635 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1636 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1637 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1639 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1641 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1642 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1643 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1644 | msg->eoh = msg->sol; |
| 1645 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1646 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1648 | |
| 1649 | case HTTP_MSG_ERROR: |
| 1650 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1651 | break; |
| 1652 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | #ifdef DEBUG_FULL |
| 1654 | default: |
| 1655 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1656 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1657 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | } |
| 1659 | http_msg_ood: |
| 1660 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1661 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1662 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1663 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1664 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | http_msg_invalid: |
| 1666 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1667 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1668 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | return; |
| 1670 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1671 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1672 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1673 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1674 | * nothing is done and 1 is returned. |
| 1675 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1676 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1677 | { |
| 1678 | int delta; |
| 1679 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1680 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1681 | |
| 1682 | if (msg->sl.rq.v_l != 0) |
| 1683 | return 1; |
| 1684 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1685 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1686 | delta = 0; |
| 1687 | |
| 1688 | if (msg->sl.rq.u_l == 0) { |
| 1689 | /* if no URI was set, add "/" */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1690 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1691 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1692 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1693 | } |
| 1694 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1695 | 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] | 1696 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1697 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1698 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1699 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1700 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1701 | NULL, NULL); |
| 1702 | if (unlikely(!cur_end)) |
| 1703 | return 0; |
| 1704 | |
| 1705 | /* we have a full HTTP/1.0 request now and we know that |
| 1706 | * we have either a CR or an LF at <ptr>. |
| 1707 | */ |
| 1708 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1709 | return 1; |
| 1710 | } |
| 1711 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1712 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1713 | * and "keep-alive" values. If we already know that some headers may safely |
| 1714 | * 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] | 1715 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1716 | * - 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] | 1717 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1718 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1719 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1720 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1721 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1722 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1723 | 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] | 1724 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1725 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1726 | const char *hdr_val = "Connection"; |
| 1727 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1728 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1729 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1730 | return; |
| 1731 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1732 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1733 | hdr_val = "Proxy-Connection"; |
| 1734 | hdr_len = 16; |
| 1735 | } |
| 1736 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1737 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1738 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1739 | 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] | 1740 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1741 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1742 | if (to_del & 2) |
| 1743 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1744 | else |
| 1745 | txn->flags |= TX_CON_KAL_SET; |
| 1746 | } |
| 1747 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1748 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1749 | if (to_del & 1) |
| 1750 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1751 | else |
| 1752 | txn->flags |= TX_CON_CLO_SET; |
| 1753 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1754 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1755 | txn->flags |= TX_HDR_CONN_UPG; |
| 1756 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1757 | } |
| 1758 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1759 | txn->flags |= TX_HDR_CONN_PRS; |
| 1760 | return; |
| 1761 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1762 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1763 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1764 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1765 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1766 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1767 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1768 | 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] | 1769 | { |
| 1770 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1771 | const char *hdr_val = "Connection"; |
| 1772 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1773 | |
| 1774 | ctx.idx = 0; |
| 1775 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1776 | |
| 1777 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1778 | hdr_val = "Proxy-Connection"; |
| 1779 | hdr_len = 16; |
| 1780 | } |
| 1781 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1782 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1783 | 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] | 1784 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1785 | if (wanted & TX_CON_KAL_SET) |
| 1786 | txn->flags |= TX_CON_KAL_SET; |
| 1787 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1788 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1789 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1790 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1791 | if (wanted & TX_CON_CLO_SET) |
| 1792 | txn->flags |= TX_CON_CLO_SET; |
| 1793 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1794 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1795 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1796 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1797 | |
| 1798 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1799 | return; |
| 1800 | |
| 1801 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1802 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1803 | hdr_val = "Connection: close"; |
| 1804 | hdr_len = 17; |
| 1805 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1806 | hdr_val = "Proxy-Connection: close"; |
| 1807 | hdr_len = 23; |
| 1808 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1809 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1813 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1814 | hdr_val = "Connection: keep-alive"; |
| 1815 | hdr_len = 22; |
| 1816 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1817 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1818 | hdr_len = 28; |
| 1819 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1820 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1821 | } |
| 1822 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1823 | } |
| 1824 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1825 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1826 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1827 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1828 | * 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] | 1829 | * 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] | 1830 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1831 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1832 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1833 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1834 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1835 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1836 | const char *end = buf->data + buf->size; |
| 1837 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1838 | unsigned int chunk = 0; |
| 1839 | |
| 1840 | /* The chunk size is in the following form, though we are only |
| 1841 | * interested in the size and CRLF : |
| 1842 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1843 | */ |
| 1844 | while (1) { |
| 1845 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1846 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1847 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1848 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1849 | if (c < 0) /* not a hex digit anymore */ |
| 1850 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1851 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1852 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1853 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1854 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1855 | chunk = (chunk << 4) + c; |
| 1856 | } |
| 1857 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1858 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1859 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1860 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1861 | |
| 1862 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1863 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1864 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1865 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1866 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1867 | } |
| 1868 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1869 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1870 | * for the end of chunk size. |
| 1871 | */ |
| 1872 | while (1) { |
| 1873 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1874 | /* we now have a CR or an LF at ptr */ |
| 1875 | if (likely(*ptr == '\r')) { |
| 1876 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1877 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1878 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1879 | return 0; |
| 1880 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1881 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1882 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1883 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1884 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1885 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1886 | /* done */ |
| 1887 | break; |
| 1888 | } |
| 1889 | else if (*ptr == ';') { |
| 1890 | /* chunk extension, ends at next CRLF */ |
| 1891 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1892 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1893 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1894 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1895 | |
| 1896 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1897 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1898 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1899 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1900 | return 0; |
| 1901 | } |
| 1902 | /* we have a CRLF now, loop above */ |
| 1903 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1904 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1905 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1906 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1907 | } |
| 1908 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1909 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1910 | * which may or may not be present. We save that into ->next and |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1911 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1912 | */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1913 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1914 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1915 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1916 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1917 | msg->chunk_len = chunk; |
| 1918 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1919 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1920 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1921 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1922 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1923 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1926 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1927 | * 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] | 1928 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1929 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1930 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1931 | * except maybe msg->next and msg->sov if it could parse some lines, and returns |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1932 | * zero. If a parse error is encountered, the function returns < 0 and does not |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1933 | * change anything except maybe msg->next and msg->sov. Note that the message |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1934 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1935 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1936 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1937 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1938 | * important to note that this function is designed to be able to parse wrapped |
| 1939 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1940 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1941 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1942 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1943 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1944 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1945 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1946 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1947 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1948 | const char *ptr = b_ptr(buf, msg->next); |
| 1949 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1950 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* scan current line and stop at LF or CRLF */ |
| 1953 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1954 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1955 | return 0; |
| 1956 | |
| 1957 | if (*ptr == '\n') { |
| 1958 | if (!p1) |
| 1959 | p1 = ptr; |
| 1960 | p2 = ptr; |
| 1961 | break; |
| 1962 | } |
| 1963 | |
| 1964 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1965 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1966 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1967 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1968 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1969 | p1 = ptr; |
| 1970 | } |
| 1971 | |
| 1972 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1973 | if (ptr >= buf->data + buf->size) |
| 1974 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | /* after LF; point to beginning of next line */ |
| 1978 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1979 | if (p2 >= buf->data + buf->size) |
| 1980 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1981 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1982 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1983 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1984 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1985 | |
| 1986 | /* schedule this line for forwarding */ |
| 1987 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1988 | if (msg->sov >= buf->size) |
| 1989 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1990 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1991 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1992 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1993 | * Everything was scheduled for forwarding, there's nothing |
| 1994 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1995 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1996 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1997 | msg->msg_state = HTTP_MSG_DONE; |
| 1998 | return 1; |
| 1999 | } |
| 2000 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2001 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 2005 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2006 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2007 | * ->sol, ->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2008 | * 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] | 2009 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2010 | * not enough data are available, the function does not change anything and |
| 2011 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2012 | * does not change anything. Note: this function is designed to parse wrapped |
| 2013 | * CRLF at the end of the buffer. |
| 2014 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2015 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2016 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2017 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2018 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2019 | int bytes; |
| 2020 | |
| 2021 | /* NB: we'll check data availabilty at the end. It's not a |
| 2022 | * problem because whatever we match first will be checked |
| 2023 | * against the correct length. |
| 2024 | */ |
| 2025 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2026 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2027 | if (*ptr == '\r') { |
| 2028 | bytes++; |
| 2029 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2030 | if (ptr >= buf->data + buf->size) |
| 2031 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2032 | } |
| 2033 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2034 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2035 | return 0; |
| 2036 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2037 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2038 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2039 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2040 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2041 | |
| 2042 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2043 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2044 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2045 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 2046 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2047 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2048 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2049 | return 1; |
| 2050 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2051 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2052 | |
| 2053 | /* |
| 2054 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2055 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2056 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 2057 | { |
| 2058 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2059 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2060 | struct hdr_ctx ctx; |
| 2061 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2062 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2063 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2064 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2065 | * 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] | 2066 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2067 | */ |
| 2068 | ctx.idx = 0; |
| 2069 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2070 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2071 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2072 | (ctx.vlen < 31 || |
| 2073 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2074 | ctx.line[ctx.val + 30] < '6' || |
| 2075 | (ctx.line[ctx.val + 30] == '6' && |
| 2076 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2077 | s->comp_algo = NULL; |
| 2078 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2081 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2082 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2083 | ctx.idx = 0; |
| 2084 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2085 | 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] | 2086 | if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| 2087 | s->comp_algo = comp_algo; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2088 | |
| 2089 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2090 | |
| 2091 | if ((s->be->comp && s->be->comp->offload) || |
| 2092 | (s->fe->comp && s->fe->comp->offload)) { |
| 2093 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2094 | ctx.idx = 0; |
| 2095 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2096 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2097 | } |
| 2098 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2099 | return 1; |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2106 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2107 | 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] | 2108 | if (comp_algo->add_data == identity_add_data) { |
| 2109 | s->comp_algo = comp_algo; |
| 2110 | return 1; |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2116 | return 0; |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Selects a comression algorithm depending of the server response. |
| 2121 | */ |
| 2122 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2123 | { |
| 2124 | struct http_txn *txn = &s->txn; |
| 2125 | struct http_msg *msg = &txn->rsp; |
| 2126 | struct hdr_ctx ctx; |
| 2127 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2128 | |
| 2129 | /* no common compression algorithm was found in request header */ |
| 2130 | if (s->comp_algo == NULL) |
| 2131 | goto fail; |
| 2132 | |
| 2133 | /* HTTP < 1.1 should not be compressed */ |
| 2134 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2135 | goto fail; |
| 2136 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2137 | /* 200 only */ |
| 2138 | if (txn->status != 200) |
| 2139 | goto fail; |
| 2140 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2141 | /* Content-Length is null */ |
| 2142 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2143 | goto fail; |
| 2144 | |
Willy Tarreau | 667c2a3 | 2013-04-09 08:13:58 +0200 | [diff] [blame] | 2145 | /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */ |
| 2146 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 2147 | goto fail; |
| 2148 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2149 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2150 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2151 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2152 | goto fail; |
| 2153 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2154 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2155 | ctx.idx = 0; |
| 2156 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2157 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2158 | goto fail; |
| 2159 | } |
| 2160 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2161 | comp_type = NULL; |
| 2162 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2163 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2164 | * not listed in the "compression type" directive if any. If no content-type was |
| 2165 | * found but configuration requires one, we don't compress either. Backend has |
| 2166 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2167 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2168 | ctx.idx = 0; |
| 2169 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2170 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2171 | goto fail; |
| 2172 | |
| 2173 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2174 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2175 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2176 | if (ctx.vlen >= comp_type->name_len && |
| 2177 | 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] | 2178 | /* this Content-Type should be compressed */ |
| 2179 | break; |
| 2180 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2181 | /* this Content-Type should not be compressed */ |
| 2182 | if (comp_type == NULL) |
| 2183 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2184 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2185 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2186 | else { /* no content-type header */ |
| 2187 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2188 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2191 | /* limit compression rate */ |
| 2192 | if (global.comp_rate_lim > 0) |
| 2193 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2194 | goto fail; |
| 2195 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2196 | /* limit cpu usage */ |
| 2197 | if (idle_pct < compress_min_idle) |
| 2198 | goto fail; |
| 2199 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2200 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2201 | 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] | 2202 | goto fail; |
| 2203 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2204 | s->flags |= SN_COMP_READY; |
| 2205 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2206 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2207 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2208 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2209 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2210 | |
| 2211 | /* add Transfer-Encoding header */ |
| 2212 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2213 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2214 | |
| 2215 | /* |
| 2216 | * Add Content-Encoding header when it's not identity encoding. |
| 2217 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2218 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2219 | * header. |
| 2220 | */ |
| 2221 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2222 | trash.len = 18; |
| 2223 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2224 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2225 | trash.len += s->comp_algo->name_len; |
| 2226 | trash.str[trash.len] = '\0'; |
| 2227 | 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] | 2228 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2229 | return 1; |
| 2230 | |
| 2231 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2232 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2237 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2238 | * processing can continue on next analysers, or zero if it either needs more |
| 2239 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2240 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2241 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2242 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2243 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2244 | 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] | 2245 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2246 | /* |
| 2247 | * We will parse the partial (or complete) lines. |
| 2248 | * We will check the request syntax, and also join multi-line |
| 2249 | * headers. An index of all the lines will be elaborated while |
| 2250 | * parsing. |
| 2251 | * |
| 2252 | * For the parsing, we use a 28 states FSM. |
| 2253 | * |
| 2254 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2255 | * req->buf->p = beginning of request |
| 2256 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2257 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2258 | * msg->eol = end of current header or line (LF or CRLF) |
| 2259 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2260 | * |
| 2261 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2262 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2263 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2264 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2265 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2266 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2267 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2268 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2269 | struct http_txn *txn = &s->txn; |
| 2270 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2271 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2272 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2273 | 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] | 2274 | now_ms, __FUNCTION__, |
| 2275 | s, |
| 2276 | req, |
| 2277 | req->rex, req->wex, |
| 2278 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2279 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2280 | req->analysers); |
| 2281 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2282 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2283 | s->srv_error = http_return_srv_error; |
| 2284 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2285 | /* There's a protected area at the end of the buffer for rewriting |
| 2286 | * purposes. We don't want to start to parse the request if the |
| 2287 | * protected area is affected, because we may have to move processed |
| 2288 | * data later, which is much more complicated. |
| 2289 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2290 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2291 | if (txn->flags & TX_NOT_FIRST) { |
| 2292 | if (unlikely(!channel_reserved(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2293 | 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] | 2294 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2295 | /* 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] | 2296 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2297 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2298 | return 0; |
| 2299 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2300 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2301 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2302 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2305 | /* Note that we have the same problem with the response ; we |
| 2306 | * may want to send a redirect, error or anything which requires |
| 2307 | * some spare space. So we'll ensure that we have at least |
| 2308 | * maxrewrite bytes available in the response buffer before |
| 2309 | * processing that one. This will only affect pipelined |
| 2310 | * keep-alive requests. |
| 2311 | */ |
| 2312 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2313 | unlikely(!channel_reserved(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2314 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2315 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2316 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2317 | 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] | 2318 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2319 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2320 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2321 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2322 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2323 | return 0; |
| 2324 | } |
| 2325 | } |
| 2326 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2327 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2328 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2329 | } |
| 2330 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2331 | /* 1: we might have to print this header in debug mode */ |
| 2332 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2333 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2334 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2335 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2336 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2337 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2338 | /* this is a bit complex : in case of error on the request line, |
| 2339 | * we know that rq.l is still zero, so we display only the part |
| 2340 | * up to the end of the line (truncated by debug_hdr). |
| 2341 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2342 | 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] | 2343 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2344 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2346 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2347 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2348 | while (cur_idx) { |
| 2349 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2350 | debug_hdr("clihdr", s, sol, eol); |
| 2351 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2352 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2353 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2354 | } |
| 2355 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2356 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2357 | /* |
| 2358 | * Now we quickly check if we have found a full valid request. |
| 2359 | * If not so, we check the FD and buffer states before leaving. |
| 2360 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2361 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2362 | * requests are checked first. When waiting for a second request |
| 2363 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2364 | * we note the error in the session flags but don't set any state. |
| 2365 | * Since the error will be noted there, it will not be counted by |
| 2366 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2367 | * Last, we may increase some tracked counters' http request errors on |
| 2368 | * the cases that are deliberately the client's fault. For instance, |
| 2369 | * a timeout or connection reset is not counted as an error. However |
| 2370 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2371 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2372 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2373 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2374 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2375 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2376 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2377 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2378 | session_inc_http_req_ctr(s); |
| 2379 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2380 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2381 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2382 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2383 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2384 | /* 1: Since we are in header mode, if there's no space |
| 2385 | * left for headers, we won't be able to free more |
| 2386 | * later, so the session will never terminate. We |
| 2387 | * must terminate it now. |
| 2388 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2389 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2390 | /* FIXME: check if URI is set and return Status |
| 2391 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2392 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2393 | session_inc_http_req_ctr(s); |
| 2394 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2395 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2396 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2397 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2398 | goto return_bad_req; |
| 2399 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2400 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2401 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2402 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2403 | if (!(s->flags & SN_ERR_MASK)) |
| 2404 | s->flags |= SN_ERR_CLICL; |
| 2405 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2406 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2407 | goto failed_keep_alive; |
| 2408 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2409 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2410 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2411 | 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] | 2412 | session_inc_http_err_ctr(s); |
| 2413 | } |
| 2414 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2415 | txn->status = 400; |
| 2416 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2417 | msg->msg_state = HTTP_MSG_ERROR; |
| 2418 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2419 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2420 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2421 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2422 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2423 | if (s->listener->counters) |
| 2424 | s->listener->counters->failed_req++; |
| 2425 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2426 | if (!(s->flags & SN_FINST_MASK)) |
| 2427 | s->flags |= SN_FINST_R; |
| 2428 | return 0; |
| 2429 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2430 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2432 | 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] | 2433 | if (!(s->flags & SN_ERR_MASK)) |
| 2434 | s->flags |= SN_ERR_CLITO; |
| 2435 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2436 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2437 | goto failed_keep_alive; |
| 2438 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2439 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2440 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2441 | 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] | 2442 | session_inc_http_err_ctr(s); |
| 2443 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2444 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2445 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2446 | msg->msg_state = HTTP_MSG_ERROR; |
| 2447 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2448 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2449 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2450 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2451 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2452 | if (s->listener->counters) |
| 2453 | s->listener->counters->failed_req++; |
| 2454 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2455 | if (!(s->flags & SN_FINST_MASK)) |
| 2456 | s->flags |= SN_FINST_R; |
| 2457 | return 0; |
| 2458 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2459 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2460 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2461 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2462 | if (!(s->flags & SN_ERR_MASK)) |
| 2463 | s->flags |= SN_ERR_CLICL; |
| 2464 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2465 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2466 | goto failed_keep_alive; |
| 2467 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2468 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2469 | 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] | 2470 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2471 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2472 | msg->msg_state = HTTP_MSG_ERROR; |
| 2473 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2474 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2475 | session_inc_http_err_ctr(s); |
| 2476 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2477 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2478 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2479 | if (s->listener->counters) |
| 2480 | s->listener->counters->failed_req++; |
| 2481 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2482 | if (!(s->flags & SN_FINST_MASK)) |
| 2483 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2484 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2485 | } |
| 2486 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2487 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2488 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2489 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2490 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2491 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2492 | /* We need more data, we have to re-enable quick-ack in case we |
| 2493 | * previously disabled it, otherwise we might cause the client |
| 2494 | * to delay next data. |
| 2495 | */ |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 2496 | setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2497 | } |
| 2498 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2499 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2500 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2501 | /* If the client starts to talk, let's fall back to |
| 2502 | * request timeout processing. |
| 2503 | */ |
| 2504 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2505 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2506 | } |
| 2507 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2508 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2509 | if (!tick_isset(req->analyse_exp)) { |
| 2510 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2511 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2512 | tick_isset(s->be->timeout.httpka)) |
| 2513 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2514 | else |
| 2515 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2516 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2517 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2518 | /* we're not ready yet */ |
| 2519 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2520 | |
| 2521 | failed_keep_alive: |
| 2522 | /* Here we process low-level errors for keep-alive requests. In |
| 2523 | * short, if the request is not the first one and it experiences |
| 2524 | * a timeout, read error or shutdown, we just silently close so |
| 2525 | * that the client can try again. |
| 2526 | */ |
| 2527 | txn->status = 0; |
| 2528 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2529 | req->analysers = 0; |
| 2530 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2531 | s->logs.level = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2532 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2533 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2534 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2535 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2536 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2537 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2538 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2539 | * 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] | 2540 | * 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] | 2541 | * 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] | 2542 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2543 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2544 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2545 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2546 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2547 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2548 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2549 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2550 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2551 | /* kill the pending keep-alive timeout */ |
| 2552 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2553 | req->analyse_exp = TICK_ETERNITY; |
| 2554 | } |
| 2555 | |
| 2556 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2557 | /* Maybe we found in invalid header name while we were configured not |
| 2558 | * to block on that, so we have to capture it now. |
| 2559 | */ |
| 2560 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2561 | 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] | 2562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2563 | /* |
| 2564 | * 1: identify the method |
| 2565 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2566 | 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] | 2567 | |
| 2568 | /* we can make use of server redirect on GET and HEAD */ |
| 2569 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2570 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2571 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | /* |
| 2573 | * 2: check if the URI matches the monitor_uri. |
| 2574 | * We have to do this for every request which gets in, because |
| 2575 | * the monitor-uri is defined by the frontend. |
| 2576 | */ |
| 2577 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2578 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2579 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2580 | s->fe->monitor_uri, |
| 2581 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2582 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2583 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2584 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2585 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2586 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2587 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2588 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2589 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2590 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2591 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2592 | 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] | 2593 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2594 | ret = acl_pass(ret); |
| 2595 | if (cond->pol == ACL_COND_UNLESS) |
| 2596 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2597 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2598 | if (ret) { |
| 2599 | /* we fail this request, let's return 503 service unavail */ |
| 2600 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2601 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2602 | if (!(s->flags & SN_ERR_MASK)) |
| 2603 | 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] | 2604 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2605 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2606 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2607 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2608 | /* nothing to fail, let's reply normaly */ |
| 2609 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2610 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 2611 | if (!(s->flags & SN_ERR_MASK)) |
| 2612 | 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] | 2613 | goto return_prx_cond; |
| 2614 | } |
| 2615 | |
| 2616 | /* |
| 2617 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2618 | * Note: we cannot log anymore if the request has been |
| 2619 | * classified as invalid. |
| 2620 | */ |
| 2621 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2622 | /* we have a complete HTTP request that we must log */ |
| 2623 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2624 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2625 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2626 | if (urilen >= REQURI_LEN) |
| 2627 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2628 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2629 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2630 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2631 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2632 | s->do_log(s); |
| 2633 | } else { |
| 2634 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2635 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2636 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2637 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2638 | /* 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] | 2639 | 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] | 2640 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2641 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2642 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2643 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2644 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2645 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2646 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2647 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2648 | |
| 2649 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2650 | 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] | 2651 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2652 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2653 | * we have what looks like a proxied connection instead of a connection, |
| 2654 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2655 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2656 | * happen to do that despite being non-standard :-( |
| 2657 | * We consider that a request not beginning with either '/' or '*' is |
| 2658 | * a proxied connection, which covers both "scheme://location" and |
| 2659 | * CONNECT ip:port. |
| 2660 | */ |
| 2661 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2662 | 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] | 2663 | txn->flags |= TX_USE_PX_CONN; |
| 2664 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2665 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2666 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2667 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2668 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2669 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2670 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2671 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2672 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2673 | /* 6: determine the transfer-length. |
| 2674 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2675 | * the presence of a message-body in a REQUEST and its transfer length |
| 2676 | * must be determined that way (in order of precedence) : |
| 2677 | * 1. The presence of a message-body in a request is signaled by the |
| 2678 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2679 | * in the request's header fields. When a request message contains |
| 2680 | * both a message-body of non-zero length and a method that does |
| 2681 | * not define any semantics for that request message-body, then an |
| 2682 | * origin server SHOULD either ignore the message-body or respond |
| 2683 | * with an appropriate error message (e.g., 413). A proxy or |
| 2684 | * gateway, when presented the same request, SHOULD either forward |
| 2685 | * the request inbound with the message- body or ignore the |
| 2686 | * message-body when determining a response. |
| 2687 | * |
| 2688 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2689 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2690 | * transfer-length is defined by the use of this transfer-coding. |
| 2691 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2692 | * transfer-coding is not present, the transfer-length is defined |
| 2693 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2694 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2695 | * 3. If a Content-Length header field is present, its decimal value in |
| 2696 | * OCTETs represents both the entity-length and the transfer-length. |
| 2697 | * If a message is received with both a Transfer-Encoding header |
| 2698 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2699 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2700 | * 4. By the server closing the connection. (Closing the connection |
| 2701 | * cannot be used to indicate the end of a request body, since that |
| 2702 | * would leave no possibility for the server to send back a response.) |
| 2703 | * |
| 2704 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2705 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2706 | * it is terminated by closing the connection. When the "chunked" |
| 2707 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2708 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2709 | */ |
| 2710 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2711 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2712 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2713 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2714 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2715 | 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] | 2716 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2717 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2718 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2719 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2720 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2721 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2722 | break; |
| 2723 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2724 | } |
| 2725 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2726 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2727 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2728 | 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] | 2729 | signed long long cl; |
| 2730 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2731 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2732 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2733 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2734 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2735 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2736 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2737 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2738 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2739 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2740 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2741 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2742 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2743 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2744 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2745 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2746 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2747 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2748 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2749 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2750 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2751 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2752 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2753 | } |
| 2754 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2755 | /* bodyless requests have a known length */ |
| 2756 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2757 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2758 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2759 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2760 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2761 | req->analyse_exp = TICK_ETERNITY; |
| 2762 | return 1; |
| 2763 | |
| 2764 | return_bad_req: |
| 2765 | /* We centralize bad requests processing here */ |
| 2766 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2767 | /* we detected a parsing error. We want to archive this request |
| 2768 | * in the dedicated proxy area for later troubleshooting. |
| 2769 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2770 | 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] | 2771 | } |
| 2772 | |
| 2773 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2774 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2775 | 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] | 2776 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2777 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2778 | if (s->listener->counters) |
| 2779 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2780 | |
| 2781 | return_prx_cond: |
| 2782 | if (!(s->flags & SN_ERR_MASK)) |
| 2783 | s->flags |= SN_ERR_PRXCOND; |
| 2784 | if (!(s->flags & SN_FINST_MASK)) |
| 2785 | s->flags |= SN_FINST_R; |
| 2786 | |
| 2787 | req->analysers = 0; |
| 2788 | req->analyse_exp = TICK_ETERNITY; |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2792 | /* We reached the stats page through a POST request. |
| 2793 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2794 | * Returns 1 if request was parsed or zero if it needs more data. |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2795 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2796 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2797 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2798 | struct proxy *px = NULL; |
| 2799 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2800 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2801 | char key[LINESIZE]; |
| 2802 | int action = ST_ADM_ACTION_NONE; |
| 2803 | int reprocess = 0; |
| 2804 | |
| 2805 | int total_servers = 0; |
| 2806 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2807 | |
| 2808 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2809 | char *st_cur_param = NULL; |
| 2810 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2811 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2812 | first_param = req->buf->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2813 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2814 | |
| 2815 | cur_param = next_param = end_params; |
| 2816 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2817 | if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2818 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2819 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2820 | return 1; |
| 2821 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2822 | else if (end_params > req->buf->p + req->buf->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2823 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2824 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2825 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | *end_params = '\0'; |
| 2829 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2830 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2831 | |
| 2832 | /* |
| 2833 | * Parse the parameters in reverse order to only store the last value. |
| 2834 | * From the html form, the backend and the action are at the end. |
| 2835 | */ |
| 2836 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2837 | char *value; |
| 2838 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2839 | |
| 2840 | cur_param--; |
| 2841 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2842 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2843 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2844 | poffset = (cur_param != first_param ? 1 : 0); |
| 2845 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2846 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2847 | strncpy(key, cur_param + poffset, plen); |
| 2848 | key[plen - 1] = '\0'; |
| 2849 | } else { |
| 2850 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2851 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | /* Parse the value */ |
| 2855 | value = key; |
| 2856 | while (*value != '\0' && *value != '=') { |
| 2857 | value++; |
| 2858 | } |
| 2859 | if (*value == '=') { |
| 2860 | /* Ok, a value is found, we can mark the end of the key */ |
| 2861 | *value++ = '\0'; |
| 2862 | } |
| 2863 | |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2864 | if (url_decode(key) < 0 || url_decode(value) < 0) |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2865 | break; |
| 2866 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2867 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2868 | if (!px && (strcmp(key, "b") == 0)) { |
| 2869 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2870 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2871 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2872 | goto out; |
| 2873 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2874 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2875 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2876 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2877 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2878 | } |
| 2879 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2880 | action = ST_ADM_ACTION_ENABLE; |
| 2881 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2882 | else if (strcmp(value, "stop") == 0) { |
| 2883 | action = ST_ADM_ACTION_STOP; |
| 2884 | } |
| 2885 | else if (strcmp(value, "start") == 0) { |
| 2886 | action = ST_ADM_ACTION_START; |
| 2887 | } |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2888 | else if (strcmp(value, "shutdown") == 0) { |
| 2889 | action = ST_ADM_ACTION_SHUTDOWN; |
| 2890 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2891 | else { |
| 2892 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2893 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2897 | if (!(px && action)) { |
| 2898 | /* |
| 2899 | * Indicates that we'll need to reprocess the parameters |
| 2900 | * as soon as backend and action are known |
| 2901 | */ |
| 2902 | if (!reprocess) { |
| 2903 | st_cur_param = cur_param; |
| 2904 | st_next_param = next_param; |
| 2905 | } |
| 2906 | reprocess = 1; |
| 2907 | } |
| 2908 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2909 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2910 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2911 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2912 | /* Not already in maintenance, we can change the server state */ |
| 2913 | sv->state |= SRV_MAINTAIN; |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2914 | set_server_down(&sv->check); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2915 | altered_servers++; |
| 2916 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2917 | } |
| 2918 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2919 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2920 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2921 | /* Already in maintenance, we can change the server state */ |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 2922 | set_server_up(&sv->check); |
Simon Horman | 58c3297 | 2013-11-25 10:46:38 +0900 | [diff] [blame] | 2923 | sv->check.health = sv->check.rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2924 | altered_servers++; |
| 2925 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2926 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2927 | break; |
| 2928 | case ST_ADM_ACTION_STOP: |
| 2929 | case ST_ADM_ACTION_START: |
| 2930 | if (action == ST_ADM_ACTION_START) |
| 2931 | sv->uweight = sv->iweight; |
| 2932 | else |
| 2933 | sv->uweight = 0; |
| 2934 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 2935 | server_recalc_eweight(sv); |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2936 | |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2937 | altered_servers++; |
| 2938 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2939 | break; |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2940 | case ST_ADM_ACTION_SHUTDOWN: |
| 2941 | if (px->state != PR_STSTOPPED) { |
| 2942 | struct session *sess, *sess_bck; |
| 2943 | |
| 2944 | list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv) |
| 2945 | if (sess->srv_conn == sv) |
| 2946 | session_shutdown(sess, SN_ERR_KILLED); |
| 2947 | |
| 2948 | altered_servers++; |
| 2949 | total_servers++; |
| 2950 | } |
| 2951 | break; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2952 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2953 | } else { |
| 2954 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2955 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2956 | } |
| 2957 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2958 | if (reprocess && px && action) { |
| 2959 | /* Now, we know the backend and the action chosen by the user. |
| 2960 | * We can safely restart from the first server parameter |
| 2961 | * to reprocess them |
| 2962 | */ |
| 2963 | cur_param = st_cur_param; |
| 2964 | next_param = st_next_param; |
| 2965 | reprocess = 0; |
| 2966 | goto reprocess_servers; |
| 2967 | } |
| 2968 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2969 | next_param = cur_param; |
| 2970 | } |
| 2971 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2972 | |
| 2973 | if (total_servers == 0) { |
| 2974 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2975 | } |
| 2976 | else if (altered_servers == 0) { |
| 2977 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2978 | } |
| 2979 | else if (altered_servers == total_servers) { |
| 2980 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2981 | } |
| 2982 | else { |
| 2983 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2984 | } |
| 2985 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2986 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2989 | /* This function checks whether we need to enable a POST analyser to parse a |
| 2990 | * stats request, and also registers the stats I/O handler. It returns zero |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 2991 | * if it needs to come back again, otherwise non-zero if it finishes. In the |
| 2992 | * latter case, it also clears the request analysers. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2993 | */ |
| 2994 | int http_handle_stats(struct session *s, struct channel *req) |
| 2995 | { |
| 2996 | struct stats_admin_rule *stats_admin_rule; |
| 2997 | struct stream_interface *si = s->rep->prod; |
| 2998 | struct http_txn *txn = &s->txn; |
| 2999 | struct http_msg *msg = &txn->req; |
| 3000 | struct uri_auth *uri = s->be->uri_auth; |
| 3001 | |
| 3002 | /* now check whether we have some admin rules for this request */ |
| 3003 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3004 | int ret = 1; |
| 3005 | |
| 3006 | if (stats_admin_rule->cond) { |
| 3007 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3008 | ret = acl_pass(ret); |
| 3009 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3010 | ret = !ret; |
| 3011 | } |
| 3012 | |
| 3013 | if (ret) { |
| 3014 | /* no rule, or the rule matches */ |
| 3015 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
| 3016 | break; |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | /* Was the status page requested with a POST ? */ |
| 3021 | if (unlikely(txn->meth == HTTP_METH_POST)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3022 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN]; |
| 3023 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3024 | if (si->applet.ctx.stats.flags & STAT_ADMIN) { |
| 3025 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3026 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3027 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3028 | */ |
| 3029 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3030 | struct hdr_ctx ctx; |
| 3031 | ctx.idx = 0; |
| 3032 | /* Expect is allowed in 1.1, look for it */ |
| 3033 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 3034 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3035 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3036 | } |
| 3037 | } |
| 3038 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3039 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3040 | } |
| 3041 | if (!http_process_req_stat_post(si, txn, req)) |
| 3042 | return 0; /* we need more data */ |
| 3043 | } |
| 3044 | else |
| 3045 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3046 | /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 3047 | scope_txt[0] = 0; |
| 3048 | if (si->applet.ctx.stats.scope_len) { |
| 3049 | strcpy(scope_txt, STAT_SCOPE_PATTERN); |
| 3050 | memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len); |
| 3051 | scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0; |
| 3052 | } |
| 3053 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3054 | |
| 3055 | /* We don't want to land on the posted stats page because a refresh will |
| 3056 | * repost the data. We don't want this to happen on accident so we redirect |
| 3057 | * the browse to the stats page with a GET. |
| 3058 | */ |
| 3059 | chunk_printf(&trash, |
Yves Lafon | 4e8ec50 | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3060 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3061 | "Cache-Control: no-cache\r\n" |
| 3062 | "Content-Type: text/plain\r\n" |
| 3063 | "Connection: close\r\n" |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3064 | "Location: %s;st=%s%s%s%s\r\n" |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3065 | "\r\n", |
| 3066 | uri->uri_prefix, |
| 3067 | ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) && |
| 3068 | (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) && |
| 3069 | stat_status_codes[si->applet.ctx.stats.st_code]) ? |
| 3070 | stat_status_codes[si->applet.ctx.stats.st_code] : |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3071 | stat_status_codes[STAT_STATUS_UNKN], |
| 3072 | (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "", |
| 3073 | (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "", |
| 3074 | scope_txt); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3075 | |
| 3076 | s->txn.status = 303; |
| 3077 | s->logs.tv_request = now; |
| 3078 | stream_int_retnclose(req->prod, &trash); |
| 3079 | s->target = &http_stats_applet.obj_type; /* just for logging the applet name */ |
| 3080 | |
| 3081 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3082 | s->fe->fe_counters.intercepted_req++; |
| 3083 | |
| 3084 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3085 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3086 | if (!(s->flags & SN_FINST_MASK)) |
| 3087 | s->flags |= SN_FINST_R; |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 3088 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3089 | return 1; |
| 3090 | } |
| 3091 | |
| 3092 | /* OK, let's go on now */ |
| 3093 | |
| 3094 | chunk_printf(&trash, |
| 3095 | "HTTP/1.0 200 OK\r\n" |
| 3096 | "Cache-Control: no-cache\r\n" |
| 3097 | "Connection: close\r\n" |
| 3098 | "Content-Type: %s\r\n", |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 3099 | (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain"); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3100 | |
| 3101 | if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH)) |
| 3102 | chunk_appendf(&trash, "Refresh: %d\r\n", |
| 3103 | uri->refresh); |
| 3104 | |
| 3105 | chunk_appendf(&trash, "\r\n"); |
| 3106 | |
| 3107 | s->txn.status = 200; |
| 3108 | s->logs.tv_request = now; |
| 3109 | |
| 3110 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3111 | s->fe->fe_counters.intercepted_req++; |
| 3112 | |
| 3113 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3114 | s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3115 | if (!(s->flags & SN_FINST_MASK)) |
| 3116 | s->flags |= SN_FINST_R; |
| 3117 | |
| 3118 | if (s->txn.meth == HTTP_METH_HEAD) { |
| 3119 | /* that's all we return in case of HEAD request, so let's immediately close. */ |
| 3120 | stream_int_retnclose(req->prod, &trash); |
| 3121 | s->target = &http_stats_applet.obj_type; /* just for logging the applet name */ |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 3122 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3123 | return 1; |
| 3124 | } |
| 3125 | |
| 3126 | /* OK, push the response and hand over to the stats I/O handler */ |
| 3127 | bi_putchk(s->rep, &trash); |
| 3128 | |
| 3129 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3130 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
| 3131 | s->target = s->rep->prod->conn->target; // for logging only |
| 3132 | s->rep->prod->conn->xprt_ctx = s; |
| 3133 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
| 3134 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3135 | return 1; |
| 3136 | } |
| 3137 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3138 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3139 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3140 | */ |
| 3141 | static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
| 3142 | { |
| 3143 | #ifdef IP_TOS |
| 3144 | if (from.ss_family == AF_INET) |
| 3145 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3146 | #endif |
| 3147 | #ifdef IPV6_TCLASS |
| 3148 | if (from.ss_family == AF_INET6) { |
| 3149 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3150 | /* v4-mapped addresses need IP_TOS */ |
| 3151 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3152 | else |
| 3153 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3154 | } |
| 3155 | #endif |
| 3156 | } |
| 3157 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3158 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3159 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3160 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3161 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3162 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3163 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3164 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3165 | 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] | 3166 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3167 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3168 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3169 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3170 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3171 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3172 | continue; |
| 3173 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3174 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3175 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3176 | int ret; |
| 3177 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3178 | 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] | 3179 | ret = acl_pass(ret); |
| 3180 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3181 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3182 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3183 | |
| 3184 | if (!ret) /* condition not matched */ |
| 3185 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3186 | } |
| 3187 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3189 | switch (rule->action) { |
| 3190 | case HTTP_REQ_ACT_ALLOW: |
| 3191 | return NULL; /* "allow" rules are OK */ |
| 3192 | |
| 3193 | case HTTP_REQ_ACT_DENY: |
| 3194 | txn->flags |= TX_CLDENY; |
| 3195 | return rule; |
| 3196 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3197 | case HTTP_REQ_ACT_TARPIT: |
| 3198 | txn->flags |= TX_CLTARPIT; |
| 3199 | return rule; |
| 3200 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3201 | case HTTP_REQ_ACT_AUTH: |
| 3202 | return rule; |
| 3203 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3204 | case HTTP_REQ_ACT_REDIR: |
| 3205 | return rule; |
| 3206 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3207 | case HTTP_REQ_ACT_SET_NICE: |
| 3208 | s->task->nice = rule->arg.nice; |
| 3209 | break; |
| 3210 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3211 | case HTTP_REQ_ACT_SET_TOS: |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3212 | inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3213 | break; |
| 3214 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3215 | case HTTP_REQ_ACT_SET_MARK: |
| 3216 | #ifdef SO_MARK |
| 3217 | setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
| 3218 | #endif |
| 3219 | break; |
| 3220 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3221 | case HTTP_REQ_ACT_SET_LOGL: |
| 3222 | s->logs.level = rule->arg.loglevel; |
| 3223 | break; |
| 3224 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3225 | case HTTP_REQ_ACT_SET_HDR: |
| 3226 | ctx.idx = 0; |
| 3227 | /* remove all occurrences of the header */ |
| 3228 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3229 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3230 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3231 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3232 | /* now fall through to header addition */ |
| 3233 | |
| 3234 | case HTTP_REQ_ACT_ADD_HDR: |
| 3235 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3236 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3237 | trash.len = rule->arg.hdr_add.name_len; |
| 3238 | trash.str[trash.len++] = ':'; |
| 3239 | trash.str[trash.len++] = ' '; |
| 3240 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3241 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3242 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3243 | } |
| 3244 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3245 | |
| 3246 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3247 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3248 | } |
| 3249 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3250 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3251 | /* Executes the http-response rules <rules> for session <s>, proxy <px> and |
| 3252 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3253 | * of the response (deny, ...) or NULL if it executed all rules or stopped |
| 3254 | * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny |
| 3255 | * rule. |
| 3256 | */ |
| 3257 | static struct http_res_rule * |
| 3258 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 3259 | { |
| 3260 | struct http_res_rule *rule; |
| 3261 | struct hdr_ctx ctx; |
| 3262 | |
| 3263 | list_for_each_entry(rule, rules, list) { |
| 3264 | if (rule->action >= HTTP_RES_ACT_MAX) |
| 3265 | continue; |
| 3266 | |
| 3267 | /* check optional condition */ |
| 3268 | if (rule->cond) { |
| 3269 | int ret; |
| 3270 | |
| 3271 | ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3272 | ret = acl_pass(ret); |
| 3273 | |
| 3274 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3275 | ret = !ret; |
| 3276 | |
| 3277 | if (!ret) /* condition not matched */ |
| 3278 | continue; |
| 3279 | } |
| 3280 | |
| 3281 | |
| 3282 | switch (rule->action) { |
| 3283 | case HTTP_RES_ACT_ALLOW: |
| 3284 | return NULL; /* "allow" rules are OK */ |
| 3285 | |
| 3286 | case HTTP_RES_ACT_DENY: |
| 3287 | txn->flags |= TX_SVDENY; |
| 3288 | return rule; |
| 3289 | |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3290 | case HTTP_RES_ACT_SET_NICE: |
| 3291 | s->task->nice = rule->arg.nice; |
| 3292 | break; |
| 3293 | |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3294 | case HTTP_RES_ACT_SET_TOS: |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3295 | inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3296 | break; |
| 3297 | |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3298 | case HTTP_RES_ACT_SET_MARK: |
| 3299 | #ifdef SO_MARK |
| 3300 | setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
| 3301 | #endif |
| 3302 | break; |
| 3303 | |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3304 | case HTTP_RES_ACT_SET_LOGL: |
| 3305 | s->logs.level = rule->arg.loglevel; |
| 3306 | break; |
| 3307 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3308 | case HTTP_RES_ACT_SET_HDR: |
| 3309 | ctx.idx = 0; |
| 3310 | /* remove all occurrences of the header */ |
| 3311 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3312 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3313 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3314 | } |
| 3315 | /* now fall through to header addition */ |
| 3316 | |
| 3317 | case HTTP_RES_ACT_ADD_HDR: |
| 3318 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3319 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3320 | trash.len = rule->arg.hdr_add.name_len; |
| 3321 | trash.str[trash.len++] = ':'; |
| 3322 | trash.str[trash.len++] = ' '; |
| 3323 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3324 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3325 | break; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | /* we reached the end of the rules, nothing to report */ |
| 3330 | return NULL; |
| 3331 | } |
| 3332 | |
| 3333 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3334 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3335 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3336 | * as too large a request to build a valid response. |
| 3337 | */ |
| 3338 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3339 | { |
| 3340 | struct http_msg *msg = &txn->req; |
| 3341 | const char *msg_fmt; |
| 3342 | |
| 3343 | /* build redirect message */ |
| 3344 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3345 | case 308: |
| 3346 | msg_fmt = HTTP_308; |
| 3347 | break; |
| 3348 | case 307: |
| 3349 | msg_fmt = HTTP_307; |
| 3350 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3351 | case 303: |
| 3352 | msg_fmt = HTTP_303; |
| 3353 | break; |
| 3354 | case 301: |
| 3355 | msg_fmt = HTTP_301; |
| 3356 | break; |
| 3357 | case 302: |
| 3358 | default: |
| 3359 | msg_fmt = HTTP_302; |
| 3360 | break; |
| 3361 | } |
| 3362 | |
| 3363 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3364 | return 0; |
| 3365 | |
| 3366 | switch(rule->type) { |
| 3367 | case REDIRECT_TYPE_SCHEME: { |
| 3368 | const char *path; |
| 3369 | const char *host; |
| 3370 | struct hdr_ctx ctx; |
| 3371 | int pathlen; |
| 3372 | int hostlen; |
| 3373 | |
| 3374 | host = ""; |
| 3375 | hostlen = 0; |
| 3376 | ctx.idx = 0; |
| 3377 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3378 | host = ctx.line + ctx.val; |
| 3379 | hostlen = ctx.vlen; |
| 3380 | } |
| 3381 | |
| 3382 | path = http_get_path(txn); |
| 3383 | /* build message using path */ |
| 3384 | if (path) { |
| 3385 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3386 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3387 | int qs = 0; |
| 3388 | while (qs < pathlen) { |
| 3389 | if (path[qs] == '?') { |
| 3390 | pathlen = qs; |
| 3391 | break; |
| 3392 | } |
| 3393 | qs++; |
| 3394 | } |
| 3395 | } |
| 3396 | } else { |
| 3397 | path = "/"; |
| 3398 | pathlen = 1; |
| 3399 | } |
| 3400 | |
| 3401 | /* check if we can add scheme + "://" + host + path */ |
| 3402 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3403 | return 0; |
| 3404 | |
| 3405 | /* add scheme */ |
| 3406 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3407 | trash.len += rule->rdr_len; |
| 3408 | |
| 3409 | /* add "://" */ |
| 3410 | memcpy(trash.str + trash.len, "://", 3); |
| 3411 | trash.len += 3; |
| 3412 | |
| 3413 | /* add host */ |
| 3414 | memcpy(trash.str + trash.len, host, hostlen); |
| 3415 | trash.len += hostlen; |
| 3416 | |
| 3417 | /* add path */ |
| 3418 | memcpy(trash.str + trash.len, path, pathlen); |
| 3419 | trash.len += pathlen; |
| 3420 | |
| 3421 | /* append a slash at the end of the location is needed and missing */ |
| 3422 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3423 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3424 | if (trash.len > trash.size - 5) |
| 3425 | return 0; |
| 3426 | trash.str[trash.len] = '/'; |
| 3427 | trash.len++; |
| 3428 | } |
| 3429 | |
| 3430 | break; |
| 3431 | } |
| 3432 | case REDIRECT_TYPE_PREFIX: { |
| 3433 | const char *path; |
| 3434 | int pathlen; |
| 3435 | |
| 3436 | path = http_get_path(txn); |
| 3437 | /* build message using path */ |
| 3438 | if (path) { |
| 3439 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3440 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3441 | int qs = 0; |
| 3442 | while (qs < pathlen) { |
| 3443 | if (path[qs] == '?') { |
| 3444 | pathlen = qs; |
| 3445 | break; |
| 3446 | } |
| 3447 | qs++; |
| 3448 | } |
| 3449 | } |
| 3450 | } else { |
| 3451 | path = "/"; |
| 3452 | pathlen = 1; |
| 3453 | } |
| 3454 | |
| 3455 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3456 | return 0; |
| 3457 | |
| 3458 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3459 | * add anything, otherwise it makes it hard for the user to |
| 3460 | * configure a self-redirection. |
| 3461 | */ |
| 3462 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3463 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3464 | trash.len += rule->rdr_len; |
| 3465 | } |
| 3466 | |
| 3467 | /* add path */ |
| 3468 | memcpy(trash.str + trash.len, path, pathlen); |
| 3469 | trash.len += pathlen; |
| 3470 | |
| 3471 | /* append a slash at the end of the location is needed and missing */ |
| 3472 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3473 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3474 | if (trash.len > trash.size - 5) |
| 3475 | return 0; |
| 3476 | trash.str[trash.len] = '/'; |
| 3477 | trash.len++; |
| 3478 | } |
| 3479 | |
| 3480 | break; |
| 3481 | } |
| 3482 | case REDIRECT_TYPE_LOCATION: |
| 3483 | default: |
| 3484 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3485 | return 0; |
| 3486 | |
| 3487 | /* add location */ |
| 3488 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3489 | trash.len += rule->rdr_len; |
| 3490 | break; |
| 3491 | } |
| 3492 | |
| 3493 | if (rule->cookie_len) { |
| 3494 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3495 | trash.len += 14; |
| 3496 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3497 | trash.len += rule->cookie_len; |
| 3498 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3499 | trash.len += 2; |
| 3500 | } |
| 3501 | |
| 3502 | /* add end of headers and the keep-alive/close status. |
| 3503 | * We may choose to set keep-alive if the Location begins |
| 3504 | * with a slash, because the client will come back to the |
| 3505 | * same server. |
| 3506 | */ |
| 3507 | txn->status = rule->code; |
| 3508 | /* let's log the request time */ |
| 3509 | s->logs.tv_request = now; |
| 3510 | |
| 3511 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3512 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3513 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3514 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3515 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3516 | /* keep-alive possible */ |
| 3517 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3518 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3519 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3520 | trash.len += 30; |
| 3521 | } else { |
| 3522 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3523 | trash.len += 24; |
| 3524 | } |
| 3525 | } |
| 3526 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3527 | trash.len += 4; |
| 3528 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3529 | /* "eat" the request */ |
| 3530 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
| 3531 | msg->sov = 0; |
| 3532 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3533 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3534 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3535 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3536 | } else { |
| 3537 | /* keep-alive not possible */ |
| 3538 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3539 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3540 | trash.len += 29; |
| 3541 | } else { |
| 3542 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3543 | trash.len += 23; |
| 3544 | } |
| 3545 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3546 | txn->req.chn->analysers = 0; |
| 3547 | } |
| 3548 | |
| 3549 | if (!(s->flags & SN_ERR_MASK)) |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 3550 | s->flags |= SN_ERR_LOCAL; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3551 | if (!(s->flags & SN_FINST_MASK)) |
| 3552 | s->flags |= SN_FINST_R; |
| 3553 | |
| 3554 | return 1; |
| 3555 | } |
| 3556 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3557 | /* This stream analyser runs all HTTP request processing which is common to |
| 3558 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3559 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3560 | * 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] | 3561 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3562 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3563 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3564 | 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] | 3565 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3566 | struct http_txn *txn = &s->txn; |
| 3567 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3568 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3569 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3570 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3571 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 3572 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3573 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3574 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3575 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3576 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3577 | return 0; |
| 3578 | } |
| 3579 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3580 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3581 | req->analyse_exp = TICK_ETERNITY; |
| 3582 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3583 | 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] | 3584 | now_ms, __FUNCTION__, |
| 3585 | s, |
| 3586 | req, |
| 3587 | req->rex, req->wex, |
| 3588 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3589 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3590 | req->analysers); |
| 3591 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3592 | /* first check whether we have some ACLs set to block this request */ |
| 3593 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3594 | 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] | 3595 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3596 | ret = acl_pass(ret); |
| 3597 | if (cond->pol == ACL_COND_UNLESS) |
| 3598 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3599 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3600 | if (ret) { |
| 3601 | txn->status = 403; |
| 3602 | /* let's log the request time */ |
| 3603 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3604 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3605 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3606 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3607 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3608 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3609 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3610 | /* just in case we have some per-backend tracking */ |
| 3611 | session_inc_be_http_req_ctr(s); |
| 3612 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3613 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3614 | 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] | 3615 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3616 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3617 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3618 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3619 | if (do_stats) |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3620 | http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3621 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3622 | else |
| 3623 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3625 | /* only apply req{,i}{rep/deny/tarpit} if the request was not yet |
| 3626 | * blocked by an http-request rule. |
| 3627 | */ |
| 3628 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3629 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3630 | goto return_bad_req; |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3631 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3632 | |
Willy Tarreau | 3b44e72 | 2013-11-16 10:28:23 +0100 | [diff] [blame] | 3633 | /* return a 403 if either rule has blocked */ |
| 3634 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3635 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3636 | txn->status = 403; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3637 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3638 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3639 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3640 | s->fe->fe_counters.denied_req++; |
| 3641 | if (s->fe != s->be) |
| 3642 | s->be->be_counters.denied_req++; |
| 3643 | if (s->listener->counters) |
| 3644 | s->listener->counters->denied_req++; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3645 | goto return_prx_cond; |
| 3646 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3647 | |
| 3648 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3649 | * which may be the same as the connect timeout if unspecified. |
| 3650 | * If unset, then set it to zero because we really want it to |
| 3651 | * eventually expire. We build the tarpit as an analyser. |
| 3652 | */ |
| 3653 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3654 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3655 | /* wipe the request out so that we can drop the connection early |
| 3656 | * if the client closes first. |
| 3657 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3658 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3659 | req->analysers = 0; /* remove switching rules etc... */ |
| 3660 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3661 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3662 | if (!req->analyse_exp) |
| 3663 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3664 | session_inc_http_err_ctr(s); |
Willy Tarreau | 687ba13 | 2013-11-16 10:13:35 +0100 | [diff] [blame] | 3665 | s->fe->fe_counters.denied_req++; |
| 3666 | if (s->fe != s->be) |
| 3667 | s->be->be_counters.denied_req++; |
| 3668 | if (s->listener->counters) |
| 3669 | s->listener->counters->denied_req++; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3670 | return 1; |
| 3671 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3672 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3673 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3674 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3675 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3676 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3677 | * in which headers are mangled. However, if another mode is set, it will |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3678 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3679 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3680 | * The method consists in checking if options changed between the two calls |
| 3681 | * (implying that either one is non-null, or one of them is non-null and we |
| 3682 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3683 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3684 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3685 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3686 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3687 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3688 | (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3689 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3690 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3691 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3692 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3693 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3694 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3695 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3696 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3697 | tmp = TX_CON_WANT_CLO; |
| 3698 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3699 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3700 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3701 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3702 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3703 | /* parse the Connection header and possibly clean it */ |
| 3704 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3705 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3706 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3707 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3708 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3709 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3710 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3711 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3712 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3713 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3714 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3715 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3716 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3717 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3718 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3719 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3720 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3721 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3722 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3723 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3724 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3725 | /* we can be blocked here because the request needs to be authenticated, |
| 3726 | * either to pass or to access stats. |
| 3727 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3728 | 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] | 3729 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3730 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3731 | if (!realm) |
| 3732 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3733 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3734 | 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] | 3735 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3736 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3737 | /* on 401 we still count one error, because normal browsing |
| 3738 | * won't significantly increase the counter but brute force |
| 3739 | * attempts will. |
| 3740 | */ |
| 3741 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3742 | goto return_prx_cond; |
| 3743 | } |
| 3744 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3745 | /* add request headers from the rule sets in the same order */ |
| 3746 | list_for_each_entry(wl, &px->req_add, list) { |
| 3747 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3748 | 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] | 3749 | ret = acl_pass(ret); |
| 3750 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3751 | ret = !ret; |
| 3752 | if (!ret) |
| 3753 | continue; |
| 3754 | } |
| 3755 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3756 | 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] | 3757 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3761 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3762 | goto return_bad_req; |
| 3763 | req->analyse_exp = TICK_ETERNITY; |
| 3764 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3765 | } |
| 3766 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3767 | if (unlikely(do_stats)) { |
| 3768 | /* process the stats request now */ |
| 3769 | if (!http_handle_stats(s, req)) { |
| 3770 | /* we need more data, let's come back here later */ |
| 3771 | req->analysers |= an_bit; |
| 3772 | channel_dont_connect(req); |
Willy Tarreau | 7fe3300 | 2013-04-21 08:04:22 +0200 | [diff] [blame] | 3773 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3774 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3775 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3776 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3777 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3778 | /* check whether we have some ACLs set to redirect this request */ |
| 3779 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3780 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3781 | int ret; |
| 3782 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3783 | 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] | 3784 | ret = acl_pass(ret); |
| 3785 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3786 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3787 | if (!ret) |
| 3788 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3789 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3790 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3791 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3792 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3793 | req->analyse_exp = TICK_ETERNITY; |
| 3794 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3795 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3796 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3797 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3798 | * If this happens, then the data will not come immediately, so we must |
| 3799 | * send all what we have without waiting. Note that due to the small gain |
| 3800 | * 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] | 3801 | * 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] | 3802 | * itself once used. |
| 3803 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3804 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3805 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3806 | /* that's OK for us now, let's move on to next analysers */ |
| 3807 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3808 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3809 | return_bad_req: |
| 3810 | /* We centralize bad requests processing here */ |
| 3811 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3812 | /* we detected a parsing error. We want to archive this request |
| 3813 | * in the dedicated proxy area for later troubleshooting. |
| 3814 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3815 | 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] | 3816 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3817 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3818 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3819 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3820 | 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] | 3821 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3822 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3823 | if (s->listener->counters) |
| 3824 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3825 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3826 | return_prx_cond: |
| 3827 | if (!(s->flags & SN_ERR_MASK)) |
| 3828 | s->flags |= SN_ERR_PRXCOND; |
| 3829 | if (!(s->flags & SN_FINST_MASK)) |
| 3830 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3831 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3832 | req->analysers = 0; |
| 3833 | req->analyse_exp = TICK_ETERNITY; |
| 3834 | return 0; |
| 3835 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3836 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3837 | /* This function performs all the processing enabled for the current request. |
| 3838 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3839 | * needs more data, encounters an error, or wants to immediately abort the |
| 3840 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3841 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3842 | 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] | 3843 | { |
| 3844 | struct http_txn *txn = &s->txn; |
| 3845 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3846 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3847 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3848 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3849 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3850 | return 0; |
| 3851 | } |
| 3852 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3853 | 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] | 3854 | now_ms, __FUNCTION__, |
| 3855 | s, |
| 3856 | req, |
| 3857 | req->rex, req->wex, |
| 3858 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3859 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3860 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3861 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3862 | if (s->fe->comp || s->be->comp) |
| 3863 | select_compression_request_header(s, req->buf); |
| 3864 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3865 | /* |
| 3866 | * Right now, we know that we have processed the entire headers |
| 3867 | * and that unwanted requests have been filtered out. We can do |
| 3868 | * whatever we want with the remaining request. Also, now we |
| 3869 | * may have separate values for ->fe, ->be. |
| 3870 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3871 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3872 | /* |
| 3873 | * If HTTP PROXY is set we simply get remote server address |
| 3874 | * parsing incoming request. |
| 3875 | */ |
| 3876 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3877 | url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3878 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3879 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3880 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3881 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3882 | * Note that doing so might move headers in the request, but |
| 3883 | * the fields will stay coherent and the URI will not move. |
| 3884 | * This should only be performed in the backend. |
| 3885 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3886 | 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] | 3887 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3888 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3889 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3890 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3891 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3892 | * so let's do the same now. |
| 3893 | */ |
| 3894 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3895 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3896 | 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] | 3897 | 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] | 3898 | } |
| 3899 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3900 | /* add unique-id if "header-unique-id" is specified */ |
| 3901 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3902 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 3903 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 3904 | goto return_bad_req; |
| 3905 | s->unique_id[0] = '\0'; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3906 | 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] | 3907 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3908 | |
| 3909 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3910 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3911 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3912 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3913 | 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] | 3914 | goto return_bad_req; |
| 3915 | } |
| 3916 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3917 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3918 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3919 | * asks for it. |
| 3920 | */ |
| 3921 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3922 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3923 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3924 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3925 | 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] | 3926 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3927 | /* The header is set to be added only if none is present |
| 3928 | * and we found it, so don't do anything. |
| 3929 | */ |
| 3930 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3931 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3932 | /* Add an X-Forwarded-For header unless the source IP is |
| 3933 | * in the 'except' network range. |
| 3934 | */ |
| 3935 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3936 | (((struct sockaddr_in *)&s->req->prod->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] | 3937 | != s->fe->except_net.s_addr) && |
| 3938 | (!s->be->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3939 | (((struct sockaddr_in *)&s->req->prod->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] | 3940 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3941 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3942 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3943 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3944 | |
| 3945 | /* Note: we rely on the backend to get the header name to be used for |
| 3946 | * x-forwarded-for, because the header is really meant for the backends. |
| 3947 | * However, if the backend did not specify any option, we have to rely |
| 3948 | * on the frontend's header name. |
| 3949 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3950 | if (s->be->fwdfor_hdr_len) { |
| 3951 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3952 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3953 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3954 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3955 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3956 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3957 | len += sprintf(trash.str + 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] | 3958 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3959 | 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] | 3960 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3961 | } |
| 3962 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3963 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3964 | /* FIXME: for the sake of completeness, we should also support |
| 3965 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3966 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3967 | int len; |
| 3968 | char pn[INET6_ADDRSTRLEN]; |
| 3969 | inet_ntop(AF_INET6, |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3970 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3971 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3972 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3973 | /* Note: we rely on the backend to get the header name to be used for |
| 3974 | * x-forwarded-for, because the header is really meant for the backends. |
| 3975 | * However, if the backend did not specify any option, we have to rely |
| 3976 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3977 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3978 | if (s->be->fwdfor_hdr_len) { |
| 3979 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3980 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3981 | } else { |
| 3982 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3983 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3984 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3985 | len += sprintf(trash.str + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3986 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3987 | 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] | 3988 | goto return_bad_req; |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3993 | * 10: add X-Original-To if either the frontend or the backend |
| 3994 | * asks for it. |
| 3995 | */ |
| 3996 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3997 | |
| 3998 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3999 | if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4000 | /* Add an X-Original-To header unless the destination IP is |
| 4001 | * in the 'except' network range. |
| 4002 | */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4003 | conn_get_to_addr(s->req->prod->conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4004 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4005 | if (s->req->prod->conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4006 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4007 | (((struct sockaddr_in *)&s->req->prod->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] | 4008 | != s->fe->except_to.s_addr) && |
| 4009 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4010 | (((struct sockaddr_in *)&s->req->prod->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] | 4011 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4012 | int len; |
| 4013 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4014 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4015 | |
| 4016 | /* Note: we rely on the backend to get the header name to be used for |
| 4017 | * x-original-to, because the header is really meant for the backends. |
| 4018 | * However, if the backend did not specify any option, we have to rely |
| 4019 | * on the frontend's header name. |
| 4020 | */ |
| 4021 | if (s->be->orgto_hdr_len) { |
| 4022 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4023 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4024 | } else { |
| 4025 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4026 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4027 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4028 | len += sprintf(trash.str + 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] | 4029 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4030 | 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] | 4031 | goto return_bad_req; |
| 4032 | } |
| 4033 | } |
| 4034 | } |
| 4035 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4036 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4037 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4038 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4039 | * "Upgrade" is present in the Connection header. |
| 4040 | */ |
| 4041 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4042 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 4043 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4044 | unsigned int want_flags = 0; |
| 4045 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4046 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4047 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 4048 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 4049 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4050 | want_flags |= TX_CON_CLO_SET; |
| 4051 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4052 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 4053 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 4054 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4055 | want_flags |= TX_CON_KAL_SET; |
| 4056 | } |
| 4057 | |
| 4058 | 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] | 4059 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4060 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4061 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4062 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4063 | /* If we have no server assigned yet and we're balancing on url_param |
| 4064 | * with a POST request, we may be interested in checking the body for |
| 4065 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4066 | */ |
| 4067 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 4068 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4069 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4070 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4071 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4072 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4073 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4074 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4075 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4076 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4077 | #ifdef TCP_QUICKACK |
| 4078 | /* We expect some data from the client. Unless we know for sure |
| 4079 | * we already have a full request, we have to re-enable quick-ack |
| 4080 | * in case we previously disabled it, otherwise we might cause |
| 4081 | * the client to delay further data. |
| 4082 | */ |
| 4083 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4084 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4085 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 4086 | setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4087 | #endif |
| 4088 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4089 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4090 | /************************************************************* |
| 4091 | * OK, that's finished for the headers. We have done what we * |
| 4092 | * could. Let's switch to the DATA state. * |
| 4093 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4094 | req->analyse_exp = TICK_ETERNITY; |
| 4095 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4096 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4097 | /* if the server closes the connection, we want to immediately react |
| 4098 | * and close the socket to save packets and syscalls. |
| 4099 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4100 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 4101 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4102 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4103 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4104 | /* OK let's go on with the BODY now */ |
| 4105 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4106 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4107 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4108 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4109 | /* we detected a parsing error. We want to archive this request |
| 4110 | * in the dedicated proxy area for later troubleshooting. |
| 4111 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4112 | 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] | 4113 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4114 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4115 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4116 | txn->status = 400; |
| 4117 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4118 | 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] | 4119 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4120 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4121 | if (s->listener->counters) |
| 4122 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4123 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4124 | if (!(s->flags & SN_ERR_MASK)) |
| 4125 | s->flags |= SN_ERR_PRXCOND; |
| 4126 | if (!(s->flags & SN_FINST_MASK)) |
| 4127 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4128 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4129 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4130 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4131 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4132 | * returns zero, at the beginning because it prevents any other processing |
| 4133 | * from occurring, and at the end because it terminates the request. |
| 4134 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4135 | 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] | 4136 | { |
| 4137 | struct http_txn *txn = &s->txn; |
| 4138 | |
| 4139 | /* This connection is being tarpitted. The CLIENT side has |
| 4140 | * already set the connect expiration date to the right |
| 4141 | * timeout. We just have to check that the client is still |
| 4142 | * there and that the timeout has not expired. |
| 4143 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4144 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4145 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4146 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4147 | return 0; |
| 4148 | |
| 4149 | /* We will set the queue timer to the time spent, just for |
| 4150 | * logging purposes. We fake a 500 server error, so that the |
| 4151 | * attacker will not suspect his connection has been tarpitted. |
| 4152 | * It will not cause trouble to the logs because we can exclude |
| 4153 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4154 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4155 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4156 | |
| 4157 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4158 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4159 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4160 | |
| 4161 | req->analysers = 0; |
| 4162 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4163 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4164 | if (!(s->flags & SN_ERR_MASK)) |
| 4165 | s->flags |= SN_ERR_PRXCOND; |
| 4166 | if (!(s->flags & SN_FINST_MASK)) |
| 4167 | s->flags |= SN_FINST_T; |
| 4168 | return 0; |
| 4169 | } |
| 4170 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4171 | /* This function is an analyser which processes the HTTP request body. It looks |
| 4172 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 4173 | * must only be called after the standard HTTP request processing has occurred, |
| 4174 | * because it expects the request to be parsed. It returns zero if it needs to |
| 4175 | * read more data, or 1 once it has completed its analysis. |
| 4176 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4177 | int http_process_request_body(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4178 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4179 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4180 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4181 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4182 | |
| 4183 | /* We have to parse the HTTP request body to find any required data. |
| 4184 | * "balance url_param check_post" should have been the only way to get |
| 4185 | * into this. We were brought here after HTTP header analysis, so all |
| 4186 | * related structures are ready. |
| 4187 | */ |
| 4188 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4189 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4190 | goto missing_data; |
| 4191 | |
| 4192 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4193 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4194 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4195 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4196 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4197 | struct hdr_ctx ctx; |
| 4198 | ctx.idx = 0; |
| 4199 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4200 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4201 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4202 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4203 | } |
| 4204 | } |
| 4205 | msg->msg_state = HTTP_MSG_100_SENT; |
| 4206 | } |
| 4207 | |
| 4208 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4209 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4210 | * req->buf->p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4211 | * is still null. We must save the body in msg->next because it |
| 4212 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4213 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4214 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4215 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4216 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4217 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4218 | else |
| 4219 | msg->msg_state = HTTP_MSG_DATA; |
| 4220 | } |
| 4221 | |
| 4222 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4223 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4224 | * 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] | 4225 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4226 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4227 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4228 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4229 | if (!ret) |
| 4230 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4231 | else if (ret < 0) { |
| 4232 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4233 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4234 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4235 | } |
| 4236 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4237 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4238 | * We have the first data byte is in msg->sov. We're waiting for at |
| 4239 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4240 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4241 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4242 | if (msg->body_len < limit) |
| 4243 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4244 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4245 | if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4246 | goto http_end; |
| 4247 | |
| 4248 | missing_data: |
| 4249 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4250 | if (buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4251 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4252 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4253 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4254 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4255 | 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] | 4256 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4257 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4258 | |
| 4259 | if (!(s->flags & SN_ERR_MASK)) |
| 4260 | s->flags |= SN_ERR_CLITO; |
| 4261 | if (!(s->flags & SN_FINST_MASK)) |
| 4262 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4263 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4264 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4265 | |
| 4266 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4267 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4268 | /* Not enough data. We'll re-use the http-request |
| 4269 | * timeout here. Ideally, we should set the timeout |
| 4270 | * relative to the accept() date. We just set the |
| 4271 | * request timeout once at the beginning of the |
| 4272 | * request. |
| 4273 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4274 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4275 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4276 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4277 | return 0; |
| 4278 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4279 | |
| 4280 | http_end: |
| 4281 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4282 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4283 | req->analysers &= ~an_bit; |
| 4284 | req->analyse_exp = TICK_ETERNITY; |
| 4285 | return 1; |
| 4286 | |
| 4287 | return_bad_req: /* let's centralize all bad requests */ |
| 4288 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4289 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4290 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4291 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4292 | if (!(s->flags & SN_ERR_MASK)) |
| 4293 | s->flags |= SN_ERR_PRXCOND; |
| 4294 | if (!(s->flags & SN_FINST_MASK)) |
| 4295 | s->flags |= SN_FINST_R; |
| 4296 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4297 | return_err_msg: |
| 4298 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4299 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4300 | if (s->listener->counters) |
| 4301 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4302 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4303 | } |
| 4304 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4305 | /* send a server's name with an outgoing request over an established connection. |
| 4306 | * Note: this function is designed to be called once the request has been scheduled |
| 4307 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4308 | * proceeding. |
| 4309 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4310 | 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] | 4311 | |
| 4312 | struct hdr_ctx ctx; |
| 4313 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4314 | char *hdr_name = be->server_id_hdr_name; |
| 4315 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4316 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4317 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4318 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4319 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4320 | ctx.idx = 0; |
| 4321 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4322 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4323 | if (old_o) { |
| 4324 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4325 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4326 | } |
| 4327 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4328 | old_i = chn->buf->i; |
| 4329 | 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] | 4330 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4331 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4335 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4336 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4337 | hdr_val += hdr_name_len; |
| 4338 | *hdr_val++ = ':'; |
| 4339 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4340 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4341 | 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] | 4342 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4343 | if (old_o) { |
| 4344 | /* If this was a forwarded request, we must readjust the amount of |
| 4345 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4346 | * variations. Note that if the request was already scheduled for |
| 4347 | * forwarding, it had its req->sol pointing to the body, which |
| 4348 | * must then be updated too. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4349 | */ |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4350 | txn->req.sol += chn->buf->i - old_i; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4351 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4352 | } |
| 4353 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4354 | return 0; |
| 4355 | } |
| 4356 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4357 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4358 | * right now but it works. |
| 4359 | */ |
| 4360 | void http_end_txn_clean_session(struct session *s) |
| 4361 | { |
| 4362 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4363 | * server's connections. We need a safer way to reinitialize buffer |
| 4364 | * flags. We also need a more accurate method for computing per-request |
| 4365 | * data. |
| 4366 | */ |
| 4367 | http_silent_debug(__LINE__, s); |
| 4368 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4369 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 4370 | si_shutr(s->req->cons); |
| 4371 | si_shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4372 | |
| 4373 | http_silent_debug(__LINE__, s); |
| 4374 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4375 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4376 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4377 | if (unlikely(s->srv_conn)) |
| 4378 | sess_change_server(s, NULL); |
| 4379 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4380 | |
| 4381 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4382 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 4383 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4384 | |
| 4385 | if (s->txn.status) { |
| 4386 | int n; |
| 4387 | |
| 4388 | n = s->txn.status / 100; |
| 4389 | if (n < 1 || n > 5) |
| 4390 | n = 0; |
| 4391 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4392 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4393 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4394 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4395 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4396 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4397 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4398 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4399 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4400 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4401 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4402 | s->be->be_counters.p.http.comp_rsp++; |
| 4403 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4407 | s->logs.bytes_in -= s->req->buf->i; |
| 4408 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4409 | |
| 4410 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4411 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4412 | !(s->flags & SN_MONITOR) && |
| 4413 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4414 | s->do_log(s); |
| 4415 | } |
| 4416 | |
| 4417 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4418 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4419 | tv_zero(&s->logs.tv_request); |
| 4420 | s->logs.t_queue = -1; |
| 4421 | s->logs.t_connect = -1; |
| 4422 | s->logs.t_data = -1; |
| 4423 | s->logs.t_close = 0; |
| 4424 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4425 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4426 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4427 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4428 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4429 | |
| 4430 | if (s->pend_pos) |
| 4431 | pendconn_free(s->pend_pos); |
| 4432 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4433 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4434 | if (s->flags & SN_CURR_SESS) { |
| 4435 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4436 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4437 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4438 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4439 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4440 | } |
| 4441 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4442 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4443 | |
| 4444 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4445 | s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */ |
| 4446 | s->req->cons->conn->flags = CO_FL_NONE; |
Willy Tarreau | 14cba4b | 2012-11-30 17:33:05 +0100 | [diff] [blame] | 4447 | s->req->cons->conn->err_code = CO_ER_NONE; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4448 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4449 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4450 | s->req->cons->err_loc = NULL; |
| 4451 | s->req->cons->exp = TICK_ETERNITY; |
| 4452 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4453 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4454 | 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] | 4455 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4456 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4457 | |
| 4458 | if (s->flags & SN_COMP_READY) |
| 4459 | s->comp_algo->end(&s->comp_ctx); |
| 4460 | s->comp_algo = NULL; |
| 4461 | s->flags &= ~SN_COMP_READY; |
| 4462 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4463 | s->txn.meth = 0; |
| 4464 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4465 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4466 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4467 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4468 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4469 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4470 | s->req->flags |= CF_NEVER_WAIT; |
| 4471 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4472 | } |
| 4473 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4474 | /* if the request buffer is not empty, it means we're |
| 4475 | * about to process another request, so send pending |
| 4476 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4477 | * Just don't do this if the buffer is close to be full, |
| 4478 | * because the request will wait for it to flush a little |
| 4479 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4480 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4481 | if (s->req->buf->i) { |
| 4482 | if (s->rep->buf->o && |
| 4483 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4484 | 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] | 4485 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4486 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4487 | |
| 4488 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4489 | channel_auto_read(s->req); |
| 4490 | channel_auto_close(s->req); |
| 4491 | channel_auto_read(s->rep); |
| 4492 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4493 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4494 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4495 | s->rep->analysers = 0; |
| 4496 | |
| 4497 | http_silent_debug(__LINE__, s); |
| 4498 | } |
| 4499 | |
| 4500 | |
| 4501 | /* This function updates the request state machine according to the response |
| 4502 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4503 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4504 | * it is only used to find when a request/response couple is complete. Both |
| 4505 | * this function and its equivalent should loop until both return zero. It |
| 4506 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4507 | */ |
| 4508 | int http_sync_req_state(struct session *s) |
| 4509 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4510 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4511 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4512 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4513 | unsigned int old_state = txn->req.msg_state; |
| 4514 | |
| 4515 | http_silent_debug(__LINE__, s); |
| 4516 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4517 | return 0; |
| 4518 | |
| 4519 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4520 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4521 | * We can shut the read side unless we want to abort_on_close, |
| 4522 | * or we have a POST request. The issue with POST requests is |
| 4523 | * that some browsers still send a CRLF after the request, and |
| 4524 | * this CRLF must be read so that it does not remain in the kernel |
| 4525 | * buffers, otherwise a close could cause an RST on some systems |
| 4526 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4527 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4528 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4529 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4530 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4531 | /* if the server closes the connection, we want to immediately react |
| 4532 | * and close the socket to save packets and syscalls. |
| 4533 | */ |
| 4534 | chn->cons->flags |= SI_FL_NOHALF; |
| 4535 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4536 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4537 | goto wait_other_side; |
| 4538 | |
| 4539 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4540 | /* The server has not finished to respond, so we |
| 4541 | * don't want to move in order not to upset it. |
| 4542 | */ |
| 4543 | goto wait_other_side; |
| 4544 | } |
| 4545 | |
| 4546 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4547 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4548 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4549 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4550 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4551 | goto wait_other_side; |
| 4552 | } |
| 4553 | |
| 4554 | /* When we get here, it means that both the request and the |
| 4555 | * response have finished receiving. Depending on the connection |
| 4556 | * mode, we'll have to wait for the last bytes to leave in either |
| 4557 | * direction, and sometimes for a close to be effective. |
| 4558 | */ |
| 4559 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4560 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4561 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4562 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4563 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4564 | } |
| 4565 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4566 | /* Option forceclose is set, or either side wants to close, |
| 4567 | * let's enforce it now that we're not expecting any new |
| 4568 | * data to come. The caller knows the session is complete |
| 4569 | * once both states are CLOSED. |
| 4570 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4571 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4572 | channel_shutr_now(chn); |
| 4573 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4574 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4575 | } |
| 4576 | else { |
| 4577 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4578 | * mode does not set the body analyser, we can't reach this place |
| 4579 | * in tunnel mode, so we're left with keep-alive only. |
| 4580 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4581 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4582 | channel_auto_read(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4583 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4584 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4585 | } |
| 4586 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4587 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4588 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4589 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4590 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4591 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4592 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4593 | goto http_msg_closing; |
| 4594 | } |
| 4595 | else { |
| 4596 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4597 | goto http_msg_closed; |
| 4598 | } |
| 4599 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4600 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4604 | http_msg_closing: |
| 4605 | /* nothing else to forward, just waiting for the output buffer |
| 4606 | * to be empty and for the shutw_now to take effect. |
| 4607 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4608 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4609 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4610 | goto http_msg_closed; |
| 4611 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4612 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4613 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4614 | goto wait_other_side; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4619 | http_msg_closed: |
| 4620 | goto wait_other_side; |
| 4621 | } |
| 4622 | |
| 4623 | wait_other_side: |
| 4624 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4625 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | |
| 4629 | /* This function updates the response state machine according to the request |
| 4630 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4631 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4632 | * it is only used to find when a request/response couple is complete. Both |
| 4633 | * this function and its equivalent should loop until both return zero. It |
| 4634 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4635 | */ |
| 4636 | int http_sync_res_state(struct session *s) |
| 4637 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4638 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4639 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4640 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4641 | unsigned int old_state = txn->rsp.msg_state; |
| 4642 | |
| 4643 | http_silent_debug(__LINE__, s); |
| 4644 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4645 | return 0; |
| 4646 | |
| 4647 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4648 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4649 | * still monitor the server connection for a possible close |
| 4650 | * while the request is being uploaded, so we don't disable |
| 4651 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4652 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4653 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4654 | |
| 4655 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4656 | goto wait_other_side; |
| 4657 | |
| 4658 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4659 | /* The client seems to still be sending data, probably |
| 4660 | * because we got an error response during an upload. |
| 4661 | * We have the choice of either breaking the connection |
| 4662 | * or letting it pass through. Let's do the later. |
| 4663 | */ |
| 4664 | goto wait_other_side; |
| 4665 | } |
| 4666 | |
| 4667 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4668 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4669 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4670 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4671 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4672 | goto wait_other_side; |
| 4673 | } |
| 4674 | |
| 4675 | /* When we get here, it means that both the request and the |
| 4676 | * response have finished receiving. Depending on the connection |
| 4677 | * mode, we'll have to wait for the last bytes to leave in either |
| 4678 | * direction, and sometimes for a close to be effective. |
| 4679 | */ |
| 4680 | |
| 4681 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4682 | /* Server-close mode : shut read and wait for the request |
| 4683 | * side to close its output buffer. The caller will detect |
| 4684 | * when we're in DONE and the other is in CLOSED and will |
| 4685 | * catch that for the final cleanup. |
| 4686 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4687 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4688 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4689 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4690 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4691 | /* Option forceclose is set, or either side wants to close, |
| 4692 | * let's enforce it now that we're not expecting any new |
| 4693 | * data to come. The caller knows the session is complete |
| 4694 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4695 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4696 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4697 | channel_shutr_now(chn); |
| 4698 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4699 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4700 | } |
| 4701 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4702 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4703 | * mode does not set the body analyser, we can't reach this place |
| 4704 | * in tunnel mode, so we're left with keep-alive only. |
| 4705 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4706 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4707 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4708 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4709 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4710 | } |
| 4711 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4712 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4713 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4714 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4715 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4716 | goto http_msg_closing; |
| 4717 | } |
| 4718 | else { |
| 4719 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4720 | goto http_msg_closed; |
| 4721 | } |
| 4722 | } |
| 4723 | goto wait_other_side; |
| 4724 | } |
| 4725 | |
| 4726 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4727 | http_msg_closing: |
| 4728 | /* nothing else to forward, just waiting for the output buffer |
| 4729 | * to be empty and for the shutw_now to take effect. |
| 4730 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4731 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4732 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4733 | goto http_msg_closed; |
| 4734 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4735 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4736 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4737 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4738 | if (objt_server(s->target)) |
| 4739 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4740 | goto wait_other_side; |
| 4741 | } |
| 4742 | } |
| 4743 | |
| 4744 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4745 | http_msg_closed: |
| 4746 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4747 | bi_erase(chn); |
| 4748 | channel_auto_close(chn); |
| 4749 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4750 | goto wait_other_side; |
| 4751 | } |
| 4752 | |
| 4753 | wait_other_side: |
| 4754 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4755 | /* We force the response to leave immediately if we're waiting for the |
| 4756 | * other side, since there is no pending shutdown to push it out. |
| 4757 | */ |
| 4758 | if (!channel_is_empty(chn)) |
| 4759 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4760 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | |
| 4764 | /* Resync the request and response state machines. Return 1 if either state |
| 4765 | * changes. |
| 4766 | */ |
| 4767 | int http_resync_states(struct session *s) |
| 4768 | { |
| 4769 | struct http_txn *txn = &s->txn; |
| 4770 | int old_req_state = txn->req.msg_state; |
| 4771 | int old_res_state = txn->rsp.msg_state; |
| 4772 | |
| 4773 | http_silent_debug(__LINE__, s); |
| 4774 | http_sync_req_state(s); |
| 4775 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4776 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4777 | if (!http_sync_res_state(s)) |
| 4778 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4779 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4780 | if (!http_sync_req_state(s)) |
| 4781 | break; |
| 4782 | } |
| 4783 | http_silent_debug(__LINE__, s); |
| 4784 | /* OK, both state machines agree on a compatible state. |
| 4785 | * There are a few cases we're interested in : |
| 4786 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4787 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4788 | * directions, so let's simply disable both analysers. |
| 4789 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4790 | * request. |
| 4791 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4792 | * with server-close mode means we've completed one request and we |
| 4793 | * must re-initialize the server connection. |
| 4794 | */ |
| 4795 | |
| 4796 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4797 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4798 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4799 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4800 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4801 | channel_auto_close(s->req); |
| 4802 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4803 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4804 | channel_auto_close(s->rep); |
| 4805 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4806 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4807 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4808 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4809 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4810 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4811 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4812 | channel_auto_close(s->rep); |
| 4813 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4814 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4815 | channel_abort(s->req); |
| 4816 | channel_auto_close(s->req); |
| 4817 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4818 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4819 | } |
| 4820 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4821 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4822 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4823 | /* server-close: terminate this server connection and |
| 4824 | * reinitialize a fresh-new transaction. |
| 4825 | */ |
| 4826 | http_end_txn_clean_session(s); |
| 4827 | } |
| 4828 | |
| 4829 | http_silent_debug(__LINE__, s); |
| 4830 | return txn->req.msg_state != old_req_state || |
| 4831 | txn->rsp.msg_state != old_res_state; |
| 4832 | } |
| 4833 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4834 | /* This function is an analyser which forwards request body (including chunk |
| 4835 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4836 | * zero byte. The only situation where it must not be called is when we're in |
| 4837 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4838 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4839 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4840 | * 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] | 4841 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4842 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4843 | * It eventually adjusts sol to match sov after the data in between have been sent. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4844 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4845 | 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] | 4846 | { |
| 4847 | struct http_txn *txn = &s->txn; |
| 4848 | struct http_msg *msg = &s->txn.req; |
| 4849 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4850 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4851 | return 0; |
| 4852 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4853 | 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] | 4854 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4855 | /* Output closed while we were sending data. We must abort and |
| 4856 | * wake the other side up. |
| 4857 | */ |
| 4858 | msg->msg_state = HTTP_MSG_ERROR; |
| 4859 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4860 | return 1; |
| 4861 | } |
| 4862 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4863 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4864 | channel_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4865 | |
| 4866 | /* Note that we don't have to send 100-continue back because we don't |
| 4867 | * need the data to complete our job, and it's up to the server to |
| 4868 | * decide whether to return 100, 417 or anything else in return of |
| 4869 | * an "Expect: 100-continue" header. |
| 4870 | */ |
| 4871 | |
| 4872 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4873 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4874 | * req->buf->p still points to the beginning of the message and msg->sol |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4875 | * is still null. We must save the body in msg->next because it |
| 4876 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4877 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4878 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4879 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4880 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4881 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4882 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4883 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4886 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4887 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4888 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4889 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4890 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4891 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4892 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4893 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4894 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4895 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4896 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4897 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4898 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4899 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4900 | /* must still forward */ |
| 4901 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4902 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4903 | |
| 4904 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4905 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4906 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4907 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4908 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4909 | } |
| 4910 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4911 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4912 | * 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] | 4913 | * TRAILERS state. |
| 4914 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4915 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4916 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4917 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4918 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4919 | else if (ret < 0) { |
| 4920 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4921 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4922 | 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] | 4923 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4924 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4925 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4926 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4927 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4928 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4929 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4930 | |
| 4931 | if (ret == 0) |
| 4932 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4933 | else if (ret < 0) { |
| 4934 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4935 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4936 | 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] | 4937 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4938 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4939 | /* we're in MSG_CHUNK_SIZE now */ |
| 4940 | } |
| 4941 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4942 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4943 | |
| 4944 | if (ret == 0) |
| 4945 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4946 | else if (ret < 0) { |
| 4947 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4948 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4949 | 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] | 4950 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4951 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4952 | /* we're in HTTP_MSG_DONE now */ |
| 4953 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4954 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4955 | int old_state = msg->msg_state; |
| 4956 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4957 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4958 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4959 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4960 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4961 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4962 | if (http_resync_states(s)) { |
| 4963 | /* some state changes occurred, maybe the analyser |
| 4964 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4965 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4966 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4967 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4968 | /* request errors are most likely due to |
| 4969 | * the server aborting the transfer. |
| 4970 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4971 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4972 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4973 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4974 | 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] | 4975 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4976 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4977 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4978 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4979 | |
| 4980 | /* If "option abortonclose" is set on the backend, we |
| 4981 | * want to monitor the client's connection and forward |
| 4982 | * any shutdown notification to the server, which will |
| 4983 | * decide whether to close or to go on processing the |
| 4984 | * request. |
| 4985 | */ |
| 4986 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4987 | channel_auto_read(req); |
| 4988 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4989 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4990 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4991 | /* POST requests may require to read extra CRLF |
| 4992 | * sent by broken browsers and which could cause |
| 4993 | * an RST to be sent upon close on some systems |
| 4994 | * (eg: Linux). |
| 4995 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4996 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4997 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4998 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4999 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5000 | } |
| 5001 | } |
| 5002 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5003 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5004 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5005 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5006 | if (!(s->flags & SN_ERR_MASK)) |
| 5007 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5008 | if (!(s->flags & SN_FINST_MASK)) { |
| 5009 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5010 | s->flags |= SN_FINST_H; |
| 5011 | else |
| 5012 | s->flags |= SN_FINST_D; |
| 5013 | } |
| 5014 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5015 | s->fe->fe_counters.cli_aborts++; |
| 5016 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5017 | if (objt_server(s->target)) |
| 5018 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5019 | |
| 5020 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5021 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5022 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5023 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5024 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5025 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5026 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5027 | /* 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] | 5028 | * 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] | 5029 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5030 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5031 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5032 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5033 | /* 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] | 5034 | * 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] | 5035 | * 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] | 5036 | * modes are already handled by the stream sock layer. We must not do |
| 5037 | * this in content-length mode because it could present the MSG_MORE |
| 5038 | * flag with the last block of forwarded data, which would cause an |
| 5039 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5040 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5041 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5042 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5043 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5044 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5045 | return 0; |
| 5046 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5047 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5048 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5049 | if (s->listener->counters) |
| 5050 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5051 | return_bad_req_stats_ok: |
| 5052 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5053 | if (txn->status) { |
| 5054 | /* Note: we don't send any error if some data were already sent */ |
| 5055 | stream_int_retnclose(req->prod, NULL); |
| 5056 | } else { |
| 5057 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5058 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5059 | } |
| 5060 | req->analysers = 0; |
| 5061 | 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] | 5062 | |
| 5063 | if (!(s->flags & SN_ERR_MASK)) |
| 5064 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5065 | if (!(s->flags & SN_FINST_MASK)) { |
| 5066 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5067 | s->flags |= SN_FINST_H; |
| 5068 | else |
| 5069 | s->flags |= SN_FINST_D; |
| 5070 | } |
| 5071 | return 0; |
| 5072 | |
| 5073 | aborted_xfer: |
| 5074 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5075 | if (txn->status) { |
| 5076 | /* Note: we don't send any error if some data were already sent */ |
| 5077 | stream_int_retnclose(req->prod, NULL); |
| 5078 | } else { |
| 5079 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5080 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5081 | } |
| 5082 | req->analysers = 0; |
| 5083 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5084 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5085 | s->fe->fe_counters.srv_aborts++; |
| 5086 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5087 | if (objt_server(s->target)) |
| 5088 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5089 | |
| 5090 | if (!(s->flags & SN_ERR_MASK)) |
| 5091 | s->flags |= SN_ERR_SRVCL; |
| 5092 | if (!(s->flags & SN_FINST_MASK)) { |
| 5093 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 5094 | s->flags |= SN_FINST_H; |
| 5095 | else |
| 5096 | s->flags |= SN_FINST_D; |
| 5097 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5098 | return 0; |
| 5099 | } |
| 5100 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5101 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5102 | * processing can continue on next analysers, or zero if it either needs more |
| 5103 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 5104 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 5105 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5106 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5107 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5108 | 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] | 5109 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5110 | struct http_txn *txn = &s->txn; |
| 5111 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5112 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5113 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5114 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5115 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5116 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5117 | 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] | 5118 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5119 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5120 | rep, |
| 5121 | rep->rex, rep->wex, |
| 5122 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5123 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5124 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5125 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5126 | /* |
| 5127 | * Now parse the partial (or complete) lines. |
| 5128 | * We will check the response syntax, and also join multi-line |
| 5129 | * headers. An index of all the lines will be elaborated while |
| 5130 | * parsing. |
| 5131 | * |
| 5132 | * For the parsing, we use a 28 states FSM. |
| 5133 | * |
| 5134 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5135 | * rep->buf->p = beginning of response |
| 5136 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5137 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5138 | * msg->eol = end of current header or line (LF or CRLF) |
| 5139 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5140 | */ |
| 5141 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5142 | /* There's a protected area at the end of the buffer for rewriting |
| 5143 | * purposes. We don't want to start to parse the request if the |
| 5144 | * protected area is affected, because we may have to move processed |
| 5145 | * data later, which is much more complicated. |
| 5146 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5147 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5148 | if (unlikely(!channel_reserved(rep))) { |
| 5149 | /* some data has still not left the buffer, wake us once that's done */ |
| 5150 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5151 | goto abort_response; |
| 5152 | channel_dont_close(rep); |
| 5153 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 5154 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5155 | } |
| 5156 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5157 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5158 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5159 | buffer_slow_realign(rep->buf); |
| 5160 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5161 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5162 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5163 | } |
| 5164 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5165 | /* 1: we might have to print this header in debug mode */ |
| 5166 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5167 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5168 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5169 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5170 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5171 | sol = rep->buf->p; |
| 5172 | 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] | 5173 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5174 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5175 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5176 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5177 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5178 | while (cur_idx) { |
| 5179 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5180 | debug_hdr("srvhdr", s, sol, eol); |
| 5181 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5182 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5183 | } |
| 5184 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5185 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5186 | /* |
| 5187 | * Now we quickly check if we have found a full valid response. |
| 5188 | * If not so, we check the FD and buffer states before leaving. |
| 5189 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5190 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5191 | * responses are checked first. |
| 5192 | * |
| 5193 | * Depending on whether the client is still there or not, we |
| 5194 | * may send an error response back or not. Note that normally |
| 5195 | * we should only check for HTTP status there, and check I/O |
| 5196 | * errors somewhere else. |
| 5197 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5198 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5199 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5200 | /* Invalid response */ |
| 5201 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5202 | /* we detected a parsing error. We want to archive this response |
| 5203 | * in the dedicated proxy area for later troubleshooting. |
| 5204 | */ |
| 5205 | hdr_response_bad: |
| 5206 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5207 | 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] | 5208 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5209 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5210 | if (objt_server(s->target)) { |
| 5211 | objt_server(s->target)->counters.failed_resp++; |
| 5212 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5213 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5214 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5215 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5216 | rep->analysers = 0; |
| 5217 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5218 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5219 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5220 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5221 | |
| 5222 | if (!(s->flags & SN_ERR_MASK)) |
| 5223 | s->flags |= SN_ERR_PRXCOND; |
| 5224 | if (!(s->flags & SN_FINST_MASK)) |
| 5225 | s->flags |= SN_FINST_H; |
| 5226 | |
| 5227 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5228 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5229 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5230 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5231 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5232 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5233 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5234 | goto hdr_response_bad; |
| 5235 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5236 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5237 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5238 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5239 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5240 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5241 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5242 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5243 | if (objt_server(s->target)) { |
| 5244 | objt_server(s->target)->counters.failed_resp++; |
| 5245 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5246 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5247 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5248 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5249 | rep->analysers = 0; |
| 5250 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5251 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5252 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5253 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5254 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5255 | if (!(s->flags & SN_ERR_MASK)) |
| 5256 | s->flags |= SN_ERR_SRVCL; |
| 5257 | if (!(s->flags & SN_FINST_MASK)) |
| 5258 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5259 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5260 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5261 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5262 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5263 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5264 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5265 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5266 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5267 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5268 | if (objt_server(s->target)) { |
| 5269 | objt_server(s->target)->counters.failed_resp++; |
| 5270 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5271 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5272 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5273 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5274 | rep->analysers = 0; |
| 5275 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5276 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5277 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5278 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5279 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5280 | if (!(s->flags & SN_ERR_MASK)) |
| 5281 | s->flags |= SN_ERR_SRVTO; |
| 5282 | if (!(s->flags & SN_FINST_MASK)) |
| 5283 | s->flags |= SN_FINST_H; |
| 5284 | return 0; |
| 5285 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5286 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5287 | /* client abort with an abortonclose */ |
| 5288 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5289 | s->fe->fe_counters.cli_aborts++; |
| 5290 | s->be->be_counters.cli_aborts++; |
| 5291 | if (objt_server(s->target)) |
| 5292 | objt_server(s->target)->counters.cli_aborts++; |
| 5293 | |
| 5294 | rep->analysers = 0; |
| 5295 | channel_auto_close(rep); |
| 5296 | |
| 5297 | txn->status = 400; |
| 5298 | bi_erase(rep); |
| 5299 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5300 | |
| 5301 | if (!(s->flags & SN_ERR_MASK)) |
| 5302 | s->flags |= SN_ERR_CLICL; |
| 5303 | if (!(s->flags & SN_FINST_MASK)) |
| 5304 | s->flags |= SN_FINST_H; |
| 5305 | |
| 5306 | /* process_session() will take care of the error */ |
| 5307 | return 0; |
| 5308 | } |
| 5309 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5310 | /* 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] | 5311 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5312 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5313 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5314 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5315 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5316 | if (objt_server(s->target)) { |
| 5317 | objt_server(s->target)->counters.failed_resp++; |
| 5318 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5319 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5320 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5321 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5322 | rep->analysers = 0; |
| 5323 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5324 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5325 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5326 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5327 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5328 | if (!(s->flags & SN_ERR_MASK)) |
| 5329 | s->flags |= SN_ERR_SRVCL; |
| 5330 | if (!(s->flags & SN_FINST_MASK)) |
| 5331 | s->flags |= SN_FINST_H; |
| 5332 | return 0; |
| 5333 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5334 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5335 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5336 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5337 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5338 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5339 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5340 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5341 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5342 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5343 | |
| 5344 | if (!(s->flags & SN_ERR_MASK)) |
| 5345 | s->flags |= SN_ERR_CLICL; |
| 5346 | if (!(s->flags & SN_FINST_MASK)) |
| 5347 | s->flags |= SN_FINST_H; |
| 5348 | |
| 5349 | /* process_session() will take care of the error */ |
| 5350 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5351 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5352 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5353 | channel_dont_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | /* More interesting part now : we know that we have a complete |
| 5358 | * response which at least looks like HTTP. We have an indicator |
| 5359 | * of each header's length, so we can parse them quickly. |
| 5360 | */ |
| 5361 | |
| 5362 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5363 | 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] | 5364 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5365 | /* |
| 5366 | * 1: get the status code |
| 5367 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5368 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5369 | if (n < 1 || n > 5) |
| 5370 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5371 | /* when the client triggers a 4xx from the server, it's most often due |
| 5372 | * to a missing object or permission. These events should be tracked |
| 5373 | * because if they happen often, it may indicate a brute force or a |
| 5374 | * vulnerability scan. |
| 5375 | */ |
| 5376 | if (n == 4) |
| 5377 | session_inc_http_err_ctr(s); |
| 5378 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5379 | if (objt_server(s->target)) |
| 5380 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5381 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5382 | /* check if the response is HTTP/1.1 or above */ |
| 5383 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5384 | ((rep->buf->p[5] > '1') || |
| 5385 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5386 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5387 | |
| 5388 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5389 | 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] | 5390 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5391 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5392 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5393 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5394 | 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] | 5395 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5396 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5397 | * and 505 are triggered on demand by client request, so we must not |
| 5398 | * count them as server failures. |
| 5399 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5400 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5401 | 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] | 5402 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5403 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5404 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5405 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5406 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5407 | /* |
| 5408 | * 2: check for cacheability. |
| 5409 | */ |
| 5410 | |
| 5411 | switch (txn->status) { |
| 5412 | case 200: |
| 5413 | case 203: |
| 5414 | case 206: |
| 5415 | case 300: |
| 5416 | case 301: |
| 5417 | case 410: |
| 5418 | /* RFC2616 @13.4: |
| 5419 | * "A response received with a status code of |
| 5420 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5421 | * by a cache (...) unless a cache-control |
| 5422 | * directive prohibits caching." |
| 5423 | * |
| 5424 | * RFC2616 @9.5: POST method : |
| 5425 | * "Responses to this method are not cacheable, |
| 5426 | * unless the response includes appropriate |
| 5427 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5428 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5429 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5430 | ((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] | 5431 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5432 | break; |
| 5433 | default: |
| 5434 | break; |
| 5435 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5436 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5437 | /* |
| 5438 | * 3: we may need to capture headers |
| 5439 | */ |
| 5440 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5441 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5442 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5443 | txn->rsp.cap, s->fe->rsp_cap); |
| 5444 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5445 | /* 4: determine the transfer-length. |
| 5446 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5447 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5448 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5449 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5450 | * All responses to the HEAD request method MUST NOT include a |
| 5451 | * message-body, even though the presence of entity-header fields |
| 5452 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5453 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5454 | * message-body. All other responses do include a message-body, |
| 5455 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5456 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5457 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5458 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5459 | * always terminated by the first empty line after the header fields, |
| 5460 | * regardless of the entity-header fields present in the message. |
| 5461 | * |
| 5462 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5463 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5464 | * transfer-length is defined by the use of this transfer-coding. |
| 5465 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5466 | * transfer-coding is not present, the transfer-length is defined by |
| 5467 | * the sender closing the connection. |
| 5468 | * |
| 5469 | * 3. If a Content-Length header field is present, its decimal value in |
| 5470 | * OCTETs represents both the entity-length and the transfer-length. |
| 5471 | * If a message is received with both a Transfer-Encoding header |
| 5472 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5473 | * |
| 5474 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5475 | * the transfer-length is not otherwise specified, then this self- |
| 5476 | * delimiting media type defines the transfer-length. This media |
| 5477 | * type MUST NOT be used unless the sender knows that the recipient |
| 5478 | * can parse it; the presence in a request of a Range header with |
| 5479 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5480 | * client can parse multipart/byteranges responses. |
| 5481 | * |
| 5482 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5483 | */ |
| 5484 | |
| 5485 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5486 | * 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] | 5487 | * 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] | 5488 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5489 | */ |
| 5490 | if (txn->meth == HTTP_METH_HEAD || |
| 5491 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5492 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5493 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5494 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5495 | goto skip_content_length; |
| 5496 | } |
| 5497 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5498 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5499 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5500 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5501 | 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] | 5502 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5503 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5504 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5505 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5506 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5507 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5508 | break; |
| 5509 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5513 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5514 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5515 | 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] | 5516 | signed long long cl; |
| 5517 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5518 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5519 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5520 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5521 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5522 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5523 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5524 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5525 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5526 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5527 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5528 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5529 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5530 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5531 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5532 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5533 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5534 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5535 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5536 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5537 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5538 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5539 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5540 | } |
| 5541 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5542 | if (s->fe->comp || s->be->comp) |
| 5543 | select_compression_response_header(s, rep->buf); |
| 5544 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5545 | /* FIXME: we should also implement the multipart/byterange method. |
| 5546 | * For now on, we resort to close mode in this case (unknown length). |
| 5547 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5548 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5549 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5550 | /* end of job, return OK */ |
| 5551 | rep->analysers &= ~an_bit; |
| 5552 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5553 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5554 | return 1; |
| 5555 | } |
| 5556 | |
| 5557 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5558 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5559 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5560 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5561 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5562 | int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5563 | { |
| 5564 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5565 | struct http_msg *msg = &txn->rsp; |
| 5566 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5567 | struct cond_wordlist *wl; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5568 | struct http_res_rule *http_res_last_rule = NULL; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5569 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5570 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5571 | now_ms, __FUNCTION__, |
| 5572 | t, |
| 5573 | rep, |
| 5574 | rep->rex, rep->wex, |
| 5575 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5576 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5577 | rep->analysers); |
| 5578 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5579 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5580 | return 0; |
| 5581 | |
| 5582 | rep->analysers &= ~an_bit; |
| 5583 | rep->analyse_exp = TICK_ETERNITY; |
| 5584 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5585 | /* Now we have to check if we need to modify the Connection header. |
| 5586 | * This is more difficult on the response than it is on the request, |
| 5587 | * because we can have two different HTTP versions and we don't know |
| 5588 | * how the client will interprete a response. For instance, let's say |
| 5589 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5590 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5591 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5592 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5593 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5594 | * indicating how a request MAY be understood by the client. In case |
| 5595 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5596 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5597 | * will fall back to explicit close. Note that we won't take risks with |
| 5598 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5599 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5600 | */ |
| 5601 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5602 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5603 | txn->status == 101)) { |
| 5604 | /* Either we've established an explicit tunnel, or we're |
| 5605 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5606 | * to understand the next protocols. We have to switch to tunnel |
| 5607 | * mode, so that we transfer the request and responses then let |
| 5608 | * this protocol pass unmodified. When we later implement specific |
| 5609 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5610 | * header which contains information about that protocol for |
| 5611 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5612 | */ |
| 5613 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5614 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5615 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5616 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5617 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5618 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5619 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5620 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5621 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5622 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5623 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5624 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5625 | /* now adjust header transformations depending on current state */ |
| 5626 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5627 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5628 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5629 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5630 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5631 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5632 | else { /* SCL / KAL */ |
| 5633 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5634 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5635 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5636 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5637 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5638 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5639 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5640 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5641 | /* Some keep-alive responses are converted to Server-close if |
| 5642 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5643 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5644 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5645 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5646 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5647 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5648 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5649 | } |
| 5650 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5651 | /* we want to have the response time before we start processing it */ |
| 5652 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 5653 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5654 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5655 | /* |
| 5656 | * 3: we will have to evaluate the filters. |
| 5657 | * As opposed to version 1.2, now they will be evaluated in the |
| 5658 | * filters order and not in the header order. This means that |
| 5659 | * each filter has to be validated among all headers. |
| 5660 | * |
| 5661 | * Filters are tried with ->be first, then with ->fe if it is |
| 5662 | * different from ->be. |
| 5663 | */ |
| 5664 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5665 | cur_proxy = t->be; |
| 5666 | while (1) { |
| 5667 | struct proxy *rule_set = cur_proxy; |
| 5668 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5669 | /* evaluate http-response rules */ |
| 5670 | if (!http_res_last_rule) |
| 5671 | http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn); |
| 5672 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5673 | /* try headers filters */ |
| 5674 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5675 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5676 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5677 | if (objt_server(t->target)) { |
| 5678 | objt_server(t->target)->counters.failed_resp++; |
| 5679 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5680 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5681 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5682 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5683 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5684 | txn->status = 502; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5685 | t->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5686 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5687 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5688 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5689 | if (!(t->flags & SN_ERR_MASK)) |
| 5690 | t->flags |= SN_ERR_PRXCOND; |
| 5691 | if (!(t->flags & SN_FINST_MASK)) |
| 5692 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5693 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5694 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5695 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5696 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5697 | /* has the response been denied ? */ |
| 5698 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5699 | if (objt_server(t->target)) |
| 5700 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5701 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5702 | t->be->be_counters.denied_resp++; |
| 5703 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5704 | if (t->listener->counters) |
| 5705 | t->listener->counters->denied_resp++; |
| 5706 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5707 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5708 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5709 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5710 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5711 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5712 | if (txn->status < 200) |
| 5713 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5714 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5715 | int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5716 | ret = acl_pass(ret); |
| 5717 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5718 | ret = !ret; |
| 5719 | if (!ret) |
| 5720 | continue; |
| 5721 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5722 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5723 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5724 | } |
| 5725 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5726 | /* check whether we're already working on the frontend */ |
| 5727 | if (cur_proxy == t->fe) |
| 5728 | break; |
| 5729 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5730 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5731 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5732 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5733 | * We may be facing a 100-continue response, in which case this |
| 5734 | * is not the right response, and we're waiting for the next one. |
| 5735 | * Let's allow this response to go to the client and wait for the |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5736 | * next one. |
| 5737 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5738 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5739 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5740 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5741 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5742 | txn->status = 0; |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5743 | t->logs.t_data = -1; /* was not a response yet */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5744 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5745 | return 1; |
| 5746 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5747 | else if (unlikely(txn->status < 200)) |
| 5748 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5749 | |
| 5750 | /* we don't have any 1xx status code now */ |
| 5751 | |
| 5752 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5753 | * 4: check for server cookie. |
| 5754 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5755 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5756 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5757 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5758 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5759 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5760 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5761 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5762 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5763 | if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5764 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5765 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5766 | /* |
| 5767 | * 6: add server cookie in the response if needed |
| 5768 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5769 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5770 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5771 | (!(t->flags & SN_DIRECT) || |
| 5772 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5773 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5774 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5775 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5776 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5777 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5778 | /* the server is known, it's not the one the client requested, or the |
| 5779 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5780 | * insert a set-cookie here, except if we want to insert only on POST |
| 5781 | * requests and this one isn't. Note that servers which don't have cookies |
| 5782 | * (eg: some backup servers) will return a full cookie removal request. |
| 5783 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5784 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5785 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5786 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5787 | t->be->cookie_name); |
| 5788 | } |
| 5789 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5790 | chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5791 | |
| 5792 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5793 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5794 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5795 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5796 | trash.len += 5; |
| 5797 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5798 | if (t->be->cookie_maxlife) { |
| 5799 | /* emit first_date, which is either the original one or |
| 5800 | * the current date. |
| 5801 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5802 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5803 | s30tob64(txn->cookie_first_date ? |
| 5804 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5805 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5806 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5807 | } |
| 5808 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5809 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5810 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5811 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5812 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5813 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5814 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5815 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5816 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5817 | |
| 5818 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5819 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5820 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5821 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5822 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5823 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5824 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5825 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5826 | /* the server did not change, only the date was updated */ |
| 5827 | txn->flags |= TX_SCK_UPDATED; |
| 5828 | else |
| 5829 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5830 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5831 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5832 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5833 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5834 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5835 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5836 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5837 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5838 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5839 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5840 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5841 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5842 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5843 | } |
| 5844 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5845 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5846 | /* |
| 5847 | * 7: check if result will be cacheable with a cookie. |
| 5848 | * We'll block the response if security checks have caught |
| 5849 | * nasty things such as a cacheable cookie. |
| 5850 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5851 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5852 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5853 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5854 | |
| 5855 | /* we're in presence of a cacheable response containing |
| 5856 | * a set-cookie header. We'll block it as requested by |
| 5857 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5858 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5859 | if (objt_server(t->target)) |
| 5860 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5861 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5862 | t->be->be_counters.denied_resp++; |
| 5863 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5864 | if (t->listener->counters) |
| 5865 | t->listener->counters->denied_resp++; |
| 5866 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5867 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5868 | t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5869 | send_log(t->be, LOG_ALERT, |
| 5870 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5871 | t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5872 | goto return_srv_prx_502; |
| 5873 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5874 | |
| 5875 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5876 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5877 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5878 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 5879 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5880 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5881 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 5882 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5883 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5884 | unsigned int want_flags = 0; |
| 5885 | |
| 5886 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5887 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5888 | /* we want a keep-alive response here. Keep-alive header |
| 5889 | * required if either side is not 1.1. |
| 5890 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5891 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5892 | want_flags |= TX_CON_KAL_SET; |
| 5893 | } |
| 5894 | else { |
| 5895 | /* we want a close response here. Close header required if |
| 5896 | * the server is 1.1, regardless of the client. |
| 5897 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5898 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5899 | want_flags |= TX_CON_CLO_SET; |
| 5900 | } |
| 5901 | |
| 5902 | 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] | 5903 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5904 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5905 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5906 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5907 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5908 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5909 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5910 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5911 | /************************************************************* |
| 5912 | * OK, that's finished for the headers. We have done what we * |
| 5913 | * could. Let's switch to the DATA state. * |
| 5914 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5915 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5916 | /* if the user wants to log as soon as possible, without counting |
| 5917 | * bytes from the server, then this is the right moment. We have |
| 5918 | * to temporarily assign bytes_out to log what we currently have. |
| 5919 | */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 5920 | if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5921 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5922 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5923 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5924 | t->logs.bytes_out = 0; |
| 5925 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5926 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5927 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5928 | * otherwise we would not let the client side wake up. |
| 5929 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5930 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5931 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5932 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5933 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5934 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5935 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5936 | /* This function is an analyser which forwards response body (including chunk |
| 5937 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5938 | * zero byte. The only situation where it must not be called is when we're in |
| 5939 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5940 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5941 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5942 | * 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] | 5943 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5944 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 5945 | * It eventually adjusts sol to match sov after the data in between have been sent. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5946 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5947 | 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] | 5948 | { |
| 5949 | struct http_txn *txn = &s->txn; |
| 5950 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5951 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5952 | static struct buffer *tmpbuf = NULL; |
| 5953 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5954 | int consumed_data = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5955 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5956 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5957 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5958 | return 0; |
| 5959 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5960 | 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] | 5961 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5962 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5963 | /* Output closed while we were sending data. We must abort and |
| 5964 | * wake the other side up. |
| 5965 | */ |
| 5966 | msg->msg_state = HTTP_MSG_ERROR; |
| 5967 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5968 | return 1; |
| 5969 | } |
| 5970 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5971 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5972 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5973 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5974 | /* this is the first time we need the compression buffer */ |
| 5975 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 5976 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 5977 | goto aborted_xfer; /* no memory */ |
| 5978 | } |
| 5979 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5980 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5981 | /* we have msg->sov which points to the first byte of message body. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5982 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 5983 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5984 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5985 | channel_forward(res, msg->sov); |
| 5986 | msg->next = 0; |
| 5987 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5988 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5989 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5990 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5991 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5992 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5995 | if (s->comp_algo != NULL) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5996 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5997 | if (ret < 0) |
| 5998 | goto missing_data; /* not enough spaces in buffers */ |
| 5999 | compressing = 1; |
| 6000 | } |
| 6001 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6002 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6003 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6004 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6005 | if (s->comp_algo == NULL) { |
| 6006 | bytes = msg->sov - msg->sol; |
| 6007 | if (msg->chunk_len || bytes) { |
| 6008 | msg->sol = msg->sov; |
| 6009 | msg->next -= bytes; /* will be forwarded */ |
| 6010 | msg->chunk_len += bytes; |
| 6011 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6012 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 6013 | } |
| 6014 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6015 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 6016 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6017 | if (compressing) { |
| 6018 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 6019 | if (ret < 0) |
| 6020 | goto aborted_xfer; |
| 6021 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6022 | |
| 6023 | if (res->to_forward || msg->chunk_len) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6024 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6025 | |
| 6026 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6027 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6028 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6029 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 6030 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6031 | if (compressing && consumed_data) { |
| 6032 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6033 | compressing = 0; |
| 6034 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6035 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6036 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6037 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6038 | |
| 6039 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 6040 | /* we want the CRLF after the data */ |
| 6041 | |
| 6042 | ret = http_skip_chunk_crlf(msg); |
| 6043 | if (ret == 0) |
| 6044 | goto missing_data; |
| 6045 | else if (ret < 0) { |
| 6046 | if (msg->err_pos >= 0) |
| 6047 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 6048 | goto return_bad_res; |
| 6049 | } |
| 6050 | /* skipping data in buffer for compression */ |
| 6051 | if (compressing) { |
| 6052 | b_adv(res->buf, msg->next); |
| 6053 | msg->next = 0; |
| 6054 | msg->sov = 0; |
| 6055 | msg->sol = 0; |
| 6056 | } |
| 6057 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 6058 | |
| 6059 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6060 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6061 | * set ->sov and ->next to point to the body and switch to DATA or |
| 6062 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6063 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6064 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6065 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 6066 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6067 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6068 | else if (ret < 0) { |
| 6069 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6070 | 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] | 6071 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6072 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6073 | if (compressing) { |
| 6074 | if (likely(msg->chunk_len > 0)) { |
| 6075 | /* skipping data if we are in compression mode */ |
| 6076 | b_adv(res->buf, msg->next); |
| 6077 | msg->next = 0; |
| 6078 | msg->sov = 0; |
| 6079 | msg->sol = 0; |
| 6080 | } else { |
| 6081 | if (consumed_data) { |
| 6082 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 6083 | compressing = 0; |
| 6084 | } |
| 6085 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6086 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 6087 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6088 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6089 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6090 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
| 6091 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6092 | if (ret == 0) |
| 6093 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6094 | else if (ret < 0) { |
| 6095 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 6096 | 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] | 6097 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6098 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 6099 | if (s->comp_algo != NULL) { |
| 6100 | /* forwarding trailers */ |
| 6101 | channel_forward(res, msg->next); |
| 6102 | msg->next = 0; |
| 6103 | } |
Willy Tarreau | 2d43e18 | 2013-04-03 00:22:25 +0200 | [diff] [blame] | 6104 | /* we're in HTTP_MSG_DONE now, but we might still have |
| 6105 | * some data pending, so let's loop over once. |
| 6106 | */ |
| 6107 | break; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6108 | |
| 6109 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6110 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6111 | |
| 6112 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6113 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6114 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6115 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6116 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6117 | if (http_resync_states(s)) { |
| 6118 | http_silent_debug(__LINE__, s); |
| 6119 | /* some state changes occurred, maybe the analyser |
| 6120 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6121 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6122 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6123 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6124 | /* response errors are most likely due to |
| 6125 | * the client aborting the transfer. |
| 6126 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6127 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6128 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 6129 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6130 | 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] | 6131 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 6132 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6133 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6134 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6135 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6136 | } |
| 6137 | } |
| 6138 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6139 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 6140 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6141 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 6142 | compressing = 0; |
| 6143 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6144 | |
| 6145 | if (res->flags & CF_SHUTW) |
| 6146 | goto aborted_xfer; |
| 6147 | |
| 6148 | /* stop waiting for data if the input is closed before the end. If the |
| 6149 | * client side was already closed, it means that the client has aborted, |
| 6150 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6151 | * server abort. |
| 6152 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6153 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6154 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 6155 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6156 | if (!(s->flags & SN_ERR_MASK)) |
| 6157 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6158 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6159 | if (objt_server(s->target)) |
| 6160 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6161 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6162 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6163 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6164 | /* 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] | 6165 | if (!s->req->analysers) |
| 6166 | goto return_bad_res; |
| 6167 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6168 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6169 | if (s->comp_algo == NULL) { |
| 6170 | bytes = msg->sov - msg->sol; |
| 6171 | if (msg->chunk_len || bytes) { |
| 6172 | msg->sol = msg->sov; |
| 6173 | msg->next -= bytes; /* will be forwarded */ |
| 6174 | msg->chunk_len += bytes; |
| 6175 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6176 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6177 | } |
| 6178 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6179 | /* 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] | 6180 | * 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] | 6181 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6182 | * close. |
| 6183 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6184 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6185 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6186 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6187 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6188 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6189 | /* 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] | 6190 | * 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] | 6191 | * 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] | 6192 | * modes are already handled by the stream sock layer. We must not do |
| 6193 | * this in content-length mode because it could present the MSG_MORE |
| 6194 | * flag with the last block of forwarded data, which would cause an |
| 6195 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6196 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6197 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6198 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6199 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6200 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6201 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6202 | return 0; |
| 6203 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6204 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6205 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6206 | if (objt_server(s->target)) |
| 6207 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6208 | |
| 6209 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6210 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6211 | /* don't send any error message as we're in the body */ |
| 6212 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6213 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6214 | 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] | 6215 | if (objt_server(s->target)) |
| 6216 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6217 | |
| 6218 | if (!(s->flags & SN_ERR_MASK)) |
| 6219 | s->flags |= SN_ERR_PRXCOND; |
| 6220 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6221 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6222 | return 0; |
| 6223 | |
| 6224 | aborted_xfer: |
| 6225 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6226 | /* don't send any error message as we're in the body */ |
| 6227 | stream_int_retnclose(res->cons, NULL); |
| 6228 | res->analysers = 0; |
| 6229 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6230 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6231 | s->fe->fe_counters.cli_aborts++; |
| 6232 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6233 | if (objt_server(s->target)) |
| 6234 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6235 | |
| 6236 | if (!(s->flags & SN_ERR_MASK)) |
| 6237 | s->flags |= SN_ERR_CLICL; |
| 6238 | if (!(s->flags & SN_FINST_MASK)) |
| 6239 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6240 | return 0; |
| 6241 | } |
| 6242 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6243 | /* Iterate the same filter through all request headers. |
| 6244 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6245 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6246 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6247 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6248 | int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6249 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6250 | char term; |
| 6251 | char *cur_ptr, *cur_end, *cur_next; |
| 6252 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6253 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6254 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6255 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6256 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6257 | last_hdr = 0; |
| 6258 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6259 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6260 | old_idx = 0; |
| 6261 | |
| 6262 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6263 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6264 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6265 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6266 | (exp->action == ACT_ALLOW || |
| 6267 | exp->action == ACT_DENY || |
| 6268 | exp->action == ACT_TARPIT)) |
| 6269 | return 0; |
| 6270 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6271 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6272 | if (!cur_idx) |
| 6273 | break; |
| 6274 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6275 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6276 | cur_ptr = cur_next; |
| 6277 | cur_end = cur_ptr + cur_hdr->len; |
| 6278 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6279 | |
| 6280 | /* Now we have one header between cur_ptr and cur_end, |
| 6281 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6282 | */ |
| 6283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6284 | /* The annoying part is that pattern matching needs |
| 6285 | * that we modify the contents to null-terminate all |
| 6286 | * strings before testing them. |
| 6287 | */ |
| 6288 | |
| 6289 | term = *cur_end; |
| 6290 | *cur_end = '\0'; |
| 6291 | |
| 6292 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6293 | switch (exp->action) { |
| 6294 | case ACT_SETBE: |
| 6295 | /* It is not possible to jump a second time. |
| 6296 | * FIXME: should we return an HTTP/500 here so that |
| 6297 | * the admin knows there's a problem ? |
| 6298 | */ |
| 6299 | if (t->be != t->fe) |
| 6300 | break; |
| 6301 | |
| 6302 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6303 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6304 | last_hdr = 1; |
| 6305 | break; |
| 6306 | |
| 6307 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6308 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6309 | last_hdr = 1; |
| 6310 | break; |
| 6311 | |
| 6312 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6313 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6314 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6315 | break; |
| 6316 | |
| 6317 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6318 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6319 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6320 | break; |
| 6321 | |
| 6322 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6323 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6324 | 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] | 6325 | /* FIXME: if the user adds a newline in the replacement, the |
| 6326 | * index will not be recalculated for now, and the new line |
| 6327 | * will not be counted as a new header. |
| 6328 | */ |
| 6329 | |
| 6330 | cur_end += delta; |
| 6331 | cur_next += delta; |
| 6332 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6333 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6334 | break; |
| 6335 | |
| 6336 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6337 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6338 | cur_next += delta; |
| 6339 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6340 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6341 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6342 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6343 | cur_hdr->len = 0; |
| 6344 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6345 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6346 | break; |
| 6347 | |
| 6348 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6349 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6350 | if (cur_end) |
| 6351 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6352 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6353 | /* keep the link from this header to next one in case of later |
| 6354 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6355 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6356 | old_idx = cur_idx; |
| 6357 | } |
| 6358 | return 0; |
| 6359 | } |
| 6360 | |
| 6361 | |
| 6362 | /* Apply the filter to the request line. |
| 6363 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6364 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6365 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6366 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6367 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6368 | int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6369 | { |
| 6370 | char term; |
| 6371 | char *cur_ptr, *cur_end; |
| 6372 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6373 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6374 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6375 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6376 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6377 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6378 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6379 | (exp->action == ACT_ALLOW || |
| 6380 | exp->action == ACT_DENY || |
| 6381 | exp->action == ACT_TARPIT)) |
| 6382 | return 0; |
| 6383 | else if (exp->action == ACT_REMOVE) |
| 6384 | return 0; |
| 6385 | |
| 6386 | done = 0; |
| 6387 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6388 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6389 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6390 | |
| 6391 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6392 | |
| 6393 | /* The annoying part is that pattern matching needs |
| 6394 | * that we modify the contents to null-terminate all |
| 6395 | * strings before testing them. |
| 6396 | */ |
| 6397 | |
| 6398 | term = *cur_end; |
| 6399 | *cur_end = '\0'; |
| 6400 | |
| 6401 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6402 | switch (exp->action) { |
| 6403 | case ACT_SETBE: |
| 6404 | /* It is not possible to jump a second time. |
| 6405 | * FIXME: should we return an HTTP/500 here so that |
| 6406 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6407 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6408 | if (t->be != t->fe) |
| 6409 | break; |
| 6410 | |
| 6411 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6412 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6413 | done = 1; |
| 6414 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6416 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6417 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6418 | done = 1; |
| 6419 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6420 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6421 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6422 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6423 | done = 1; |
| 6424 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6425 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6426 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6427 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6428 | done = 1; |
| 6429 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6431 | case ACT_REPLACE: |
| 6432 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6433 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6434 | 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] | 6435 | /* FIXME: if the user adds a newline in the replacement, the |
| 6436 | * index will not be recalculated for now, and the new line |
| 6437 | * will not be counted as a new header. |
| 6438 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6439 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6440 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6441 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6442 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6443 | HTTP_MSG_RQMETH, |
| 6444 | cur_ptr, cur_end + 1, |
| 6445 | NULL, NULL); |
| 6446 | if (unlikely(!cur_end)) |
| 6447 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6448 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6449 | /* we have a full request and we know that we have either a CR |
| 6450 | * or an LF at <ptr>. |
| 6451 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6452 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6453 | 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] | 6454 | /* there is no point trying this regex on headers */ |
| 6455 | return 1; |
| 6456 | } |
| 6457 | } |
| 6458 | *cur_end = term; /* restore the string terminator */ |
| 6459 | return done; |
| 6460 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6461 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6462 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6463 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6464 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6465 | * 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] | 6466 | * 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] | 6467 | * unparsable request. Since it can manage the switch to another backend, it |
| 6468 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6469 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6470 | 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] | 6471 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6472 | struct http_txn *txn = &s->txn; |
| 6473 | struct hdr_exp *exp; |
| 6474 | |
| 6475 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6476 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6477 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6478 | /* |
| 6479 | * The interleaving of transformations and verdicts |
| 6480 | * makes it difficult to decide to continue or stop |
| 6481 | * the evaluation. |
| 6482 | */ |
| 6483 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6484 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6485 | break; |
| 6486 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6487 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6488 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6489 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6490 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6491 | |
| 6492 | /* if this filter had a condition, evaluate it now and skip to |
| 6493 | * next filter if the condition does not match. |
| 6494 | */ |
| 6495 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6496 | 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] | 6497 | ret = acl_pass(ret); |
| 6498 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6499 | ret = !ret; |
| 6500 | |
| 6501 | if (!ret) |
| 6502 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6506 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6507 | if (unlikely(ret < 0)) |
| 6508 | return -1; |
| 6509 | |
| 6510 | if (likely(ret == 0)) { |
| 6511 | /* The filter did not match the request, it can be |
| 6512 | * iterated through all headers. |
| 6513 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6514 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6515 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6516 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6517 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6521 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6522 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6523 | * Try to retrieve the server associated to the appsession. |
| 6524 | * If the server is found, it's assigned to the session. |
| 6525 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6526 | void manage_client_side_appsession(struct session *t, const char *buf, int len) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6527 | struct http_txn *txn = &t->txn; |
| 6528 | appsess *asession = NULL; |
| 6529 | char *sessid_temp = NULL; |
| 6530 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6531 | if (len > t->be->appsession_len) { |
| 6532 | len = t->be->appsession_len; |
| 6533 | } |
| 6534 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6535 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6536 | /* 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] | 6537 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6538 | /* 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] | 6539 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6540 | } |
| 6541 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6542 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6543 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6544 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6545 | return; |
| 6546 | } |
| 6547 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6548 | memcpy(txn->sessid, buf, len); |
| 6549 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6553 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6554 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6555 | return; |
| 6556 | } |
| 6557 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6558 | memcpy(sessid_temp, buf, len); |
| 6559 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6560 | |
| 6561 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6562 | /* free previously allocated memory */ |
| 6563 | pool_free2(apools.sessid, sessid_temp); |
| 6564 | |
| 6565 | if (asession != NULL) { |
| 6566 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6567 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6568 | asession->request_count++; |
| 6569 | |
| 6570 | if (asession->serverid != NULL) { |
| 6571 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6572 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6573 | while (srv) { |
| 6574 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6575 | if ((srv->state & SRV_RUNNING) || |
| 6576 | (t->be->options & PR_O_PERSIST) || |
| 6577 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6578 | /* we found the server and it's usable */ |
| 6579 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6580 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6581 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6582 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6583 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6584 | break; |
| 6585 | } else { |
| 6586 | txn->flags &= ~TX_CK_MASK; |
| 6587 | txn->flags |= TX_CK_DOWN; |
| 6588 | } |
| 6589 | } |
| 6590 | srv = srv->next; |
| 6591 | } |
| 6592 | } |
| 6593 | } |
| 6594 | } |
| 6595 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6596 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6597 | * same way as with headers above except that the semi-colon also ends a token. |
| 6598 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6599 | * return a valid result. |
| 6600 | */ |
| 6601 | char *find_cookie_value_end(char *s, const char *e) |
| 6602 | { |
| 6603 | int quoted, qdpair; |
| 6604 | |
| 6605 | quoted = qdpair = 0; |
| 6606 | for (; s < e; s++) { |
| 6607 | if (qdpair) qdpair = 0; |
| 6608 | else if (quoted) { |
| 6609 | if (*s == '\\') qdpair = 1; |
| 6610 | else if (*s == '"') quoted = 0; |
| 6611 | } |
| 6612 | else if (*s == '"') quoted = 1; |
| 6613 | else if (*s == ',' || *s == ';') return s; |
| 6614 | } |
| 6615 | return s; |
| 6616 | } |
| 6617 | |
| 6618 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6619 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6620 | * the first delimiter is updated if required. The function tries as much as |
| 6621 | * possible to respect the following principles : |
| 6622 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6623 | * colon, in which case <next> is simply removed |
| 6624 | * - set exactly one space character after the new first delimiter, unless |
| 6625 | * there are not enough characters in the block being moved to do so. |
| 6626 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6627 | * one. |
| 6628 | * |
| 6629 | * It is the caller's responsibility to ensure that : |
| 6630 | * - <from> points to a valid delimiter or the colon ; |
| 6631 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6632 | * - there are non-space chars before <from> ; |
| 6633 | * - there is a CR/LF at or after <next>. |
| 6634 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6635 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6636 | { |
| 6637 | char *prev = *from; |
| 6638 | |
| 6639 | if (*prev == ':') { |
| 6640 | /* We're removing the first value, preserve the colon and add a |
| 6641 | * space if possible. |
| 6642 | */ |
| 6643 | if (!http_is_crlf[(unsigned char)*next]) |
| 6644 | next++; |
| 6645 | prev++; |
| 6646 | if (prev < next) |
| 6647 | *prev++ = ' '; |
| 6648 | |
| 6649 | while (http_is_spht[(unsigned char)*next]) |
| 6650 | next++; |
| 6651 | } else { |
| 6652 | /* Remove useless spaces before the old delimiter. */ |
| 6653 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6654 | prev--; |
| 6655 | *from = prev; |
| 6656 | |
| 6657 | /* copy the delimiter and if possible a space if we're |
| 6658 | * not at the end of the line. |
| 6659 | */ |
| 6660 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6661 | *prev++ = *next++; |
| 6662 | if (prev + 1 < next) |
| 6663 | *prev++ = ' '; |
| 6664 | while (http_is_spht[(unsigned char)*next]) |
| 6665 | next++; |
| 6666 | } |
| 6667 | } |
| 6668 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6669 | } |
| 6670 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6671 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6672 | * 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] | 6673 | * desirable to call it only when needed. This code is quite complex because |
| 6674 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6675 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6676 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6677 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6678 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6679 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6680 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6681 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6682 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6683 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6684 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6685 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6686 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6687 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6688 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6689 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6690 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6691 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6692 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6693 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6694 | hdr_beg = hdr_next; |
| 6695 | hdr_end = hdr_beg + cur_hdr->len; |
| 6696 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6697 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6698 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6699 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6700 | * "Cookie:" headers. |
| 6701 | */ |
| 6702 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6703 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6704 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6705 | old_idx = cur_idx; |
| 6706 | continue; |
| 6707 | } |
| 6708 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6709 | del_from = NULL; /* nothing to be deleted */ |
| 6710 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6711 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6712 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6713 | * attributes whose name begin with a '$', and associate them with |
| 6714 | * the right cookie, if we want to delete this cookie. |
| 6715 | * So there are 3 cases for each cookie read : |
| 6716 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6717 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6718 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6719 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6720 | * "special" cookie. |
| 6721 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6722 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6723 | * *MUST* delete it. |
| 6724 | * |
| 6725 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6726 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6727 | * the RFC explicitly allows spaces before it, and not within the |
| 6728 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6729 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6730 | * after the equal sign too, resulting in some (rare) buggy |
| 6731 | * implementations trying to do that. So let's do what servers do. |
| 6732 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6733 | * allowed quoted strings in values, with any possible character |
| 6734 | * after a backslash, including control chars and delimitors, which |
| 6735 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6736 | * within values even without quotes. |
| 6737 | * |
| 6738 | * We have to keep multiple pointers in order to support cookie |
| 6739 | * removal at the beginning, middle or end of header without |
| 6740 | * corrupting the header. All of these headers are valid : |
| 6741 | * |
| 6742 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6743 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6744 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6745 | * | | | | | | | | | |
| 6746 | * | | | | | | | | hdr_end <--+ |
| 6747 | * | | | | | | | +--> next |
| 6748 | * | | | | | | +----> val_end |
| 6749 | * | | | | | +-----------> val_beg |
| 6750 | * | | | | +--------------> equal |
| 6751 | * | | | +----------------> att_end |
| 6752 | * | | +---------------------> att_beg |
| 6753 | * | +--------------------------> prev |
| 6754 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6755 | */ |
| 6756 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6757 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6758 | /* Iterate through all cookies on this line */ |
| 6759 | |
| 6760 | /* find att_beg */ |
| 6761 | att_beg = prev + 1; |
| 6762 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6763 | att_beg++; |
| 6764 | |
| 6765 | /* find att_end : this is the first character after the last non |
| 6766 | * space before the equal. It may be equal to hdr_end. |
| 6767 | */ |
| 6768 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6769 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6770 | while (equal < hdr_end) { |
| 6771 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6772 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6773 | if (http_is_spht[(unsigned char)*equal++]) |
| 6774 | continue; |
| 6775 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6776 | } |
| 6777 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6778 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6779 | * is between <att_beg> and <equal>, both may be identical. |
| 6780 | */ |
| 6781 | |
| 6782 | /* look for end of cookie if there is an equal sign */ |
| 6783 | if (equal < hdr_end && *equal == '=') { |
| 6784 | /* look for the beginning of the value */ |
| 6785 | val_beg = equal + 1; |
| 6786 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6787 | val_beg++; |
| 6788 | |
| 6789 | /* find the end of the value, respecting quotes */ |
| 6790 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6791 | |
| 6792 | /* make val_end point to the first white space or delimitor after the value */ |
| 6793 | val_end = next; |
| 6794 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6795 | val_end--; |
| 6796 | } else { |
| 6797 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6798 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6799 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6800 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6801 | * they will automatically be removed if a header before them is removed, |
| 6802 | * since they're supposed to be linked together. |
| 6803 | */ |
| 6804 | if (*att_beg == '$') |
| 6805 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6806 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6807 | /* Ignore cookies with no equal sign */ |
| 6808 | if (equal == next) { |
| 6809 | /* This is not our cookie, so we must preserve it. But if we already |
| 6810 | * scheduled another cookie for removal, we cannot remove the |
| 6811 | * complete header, but we can remove the previous block itself. |
| 6812 | */ |
| 6813 | preserve_hdr = 1; |
| 6814 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6815 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6816 | val_end += delta; |
| 6817 | next += delta; |
| 6818 | hdr_end += delta; |
| 6819 | hdr_next += delta; |
| 6820 | cur_hdr->len += delta; |
| 6821 | http_msg_move_end(&txn->req, delta); |
| 6822 | prev = del_from; |
| 6823 | del_from = NULL; |
| 6824 | } |
| 6825 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6826 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6827 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6828 | /* if there are spaces around the equal sign, we need to |
| 6829 | * strip them otherwise we'll get trouble for cookie captures, |
| 6830 | * or even for rewrites. Since this happens extremely rarely, |
| 6831 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6832 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6833 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6834 | int stripped_before = 0; |
| 6835 | int stripped_after = 0; |
| 6836 | |
| 6837 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6838 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6839 | equal += stripped_before; |
| 6840 | val_beg += stripped_before; |
| 6841 | } |
| 6842 | |
| 6843 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6844 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6845 | val_beg += stripped_after; |
| 6846 | stripped_before += stripped_after; |
| 6847 | } |
| 6848 | |
| 6849 | val_end += stripped_before; |
| 6850 | next += stripped_before; |
| 6851 | hdr_end += stripped_before; |
| 6852 | hdr_next += stripped_before; |
| 6853 | cur_hdr->len += stripped_before; |
| 6854 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6855 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6856 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6857 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6858 | /* First, let's see if we want to capture this cookie. We check |
| 6859 | * that we don't already have a client side cookie, because we |
| 6860 | * can only capture one. Also as an optimisation, we ignore |
| 6861 | * cookies shorter than the declared name. |
| 6862 | */ |
| 6863 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6864 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6865 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6866 | int log_len = val_end - att_beg; |
| 6867 | |
| 6868 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6869 | Alert("HTTP logging : out of memory.\n"); |
| 6870 | } else { |
| 6871 | if (log_len > t->fe->capture_len) |
| 6872 | log_len = t->fe->capture_len; |
| 6873 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6874 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6875 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6876 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6877 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6878 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6879 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6880 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6881 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6882 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6883 | * For cookies in prefix mode, the form is : |
| 6884 | * |
| 6885 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6886 | */ |
| 6887 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6888 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6889 | struct server *srv = t->be->srv; |
| 6890 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6891 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6892 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6893 | * have the server ID between val_beg and delim, and the original cookie between |
| 6894 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6895 | * |
| 6896 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6897 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6898 | * | || || | |+-> next |
| 6899 | * | || || | +--> val_end |
| 6900 | * | || || +---------> delim |
| 6901 | * | || |+------------> val_beg |
| 6902 | * | || +-------------> att_end = equal |
| 6903 | * | |+-----------------> att_beg |
| 6904 | * | +------------------> prev |
| 6905 | * +-------------------------> hdr_beg |
| 6906 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6907 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6908 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6909 | for (delim = val_beg; delim < val_end; delim++) |
| 6910 | if (*delim == COOKIE_DELIM) |
| 6911 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6912 | } else { |
| 6913 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6914 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6915 | /* Now check if the cookie contains a date field, which would |
| 6916 | * appear after a vertical bar ('|') just after the server name |
| 6917 | * and before the delimiter. |
| 6918 | */ |
| 6919 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6920 | if (vbar1) { |
| 6921 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6922 | * right is the last seen date. It is a base64 encoded |
| 6923 | * 30-bit value representing the UNIX date since the |
| 6924 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6925 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6926 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6927 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6928 | if (val_end - vbar1 >= 5) { |
| 6929 | val = b64tos30(vbar1); |
| 6930 | if (val > 0) |
| 6931 | txn->cookie_last_date = val << 2; |
| 6932 | } |
| 6933 | /* look for a second vertical bar */ |
| 6934 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6935 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6936 | val = b64tos30(vbar1 + 1); |
| 6937 | if (val > 0) |
| 6938 | txn->cookie_first_date = val << 2; |
| 6939 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6940 | } |
| 6941 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6942 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6943 | /* if the cookie has an expiration date and the proxy wants to check |
| 6944 | * it, then we do that now. We first check if the cookie is too old, |
| 6945 | * then only if it has expired. We detect strict overflow because the |
| 6946 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6947 | * in the future are ignored if their offset is beyond one day. This |
| 6948 | * allows an admin to fix timezone issues without expiring everyone |
| 6949 | * and at the same time avoids keeping unwanted side effects for too |
| 6950 | * long. |
| 6951 | */ |
| 6952 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6953 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6954 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6955 | txn->flags &= ~TX_CK_MASK; |
| 6956 | txn->flags |= TX_CK_OLD; |
| 6957 | delim = val_beg; // let's pretend we have not found the cookie |
| 6958 | txn->cookie_first_date = 0; |
| 6959 | txn->cookie_last_date = 0; |
| 6960 | } |
| 6961 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6962 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6963 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6964 | txn->flags &= ~TX_CK_MASK; |
| 6965 | txn->flags |= TX_CK_EXPIRED; |
| 6966 | delim = val_beg; // let's pretend we have not found the cookie |
| 6967 | txn->cookie_first_date = 0; |
| 6968 | txn->cookie_last_date = 0; |
| 6969 | } |
| 6970 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6971 | /* Here, we'll look for the first running server which supports the cookie. |
| 6972 | * This allows to share a same cookie between several servers, for example |
| 6973 | * to dedicate backup servers to specific servers only. |
| 6974 | * However, to prevent clients from sticking to cookie-less backup server |
| 6975 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6976 | * empty cookies and mark them as invalid. |
| 6977 | * The same behaviour is applied when persistence must be ignored. |
| 6978 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6979 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6980 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6981 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6982 | while (srv) { |
| 6983 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6984 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6985 | if ((srv->state & SRV_RUNNING) || |
| 6986 | (t->be->options & PR_O_PERSIST) || |
| 6987 | (t->flags & SN_FORCE_PRST)) { |
| 6988 | /* we found the server and we can use it */ |
| 6989 | txn->flags &= ~TX_CK_MASK; |
| 6990 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6991 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6992 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6993 | break; |
| 6994 | } else { |
| 6995 | /* we found a server, but it's down, |
| 6996 | * mark it as such and go on in case |
| 6997 | * another one is available. |
| 6998 | */ |
| 6999 | txn->flags &= ~TX_CK_MASK; |
| 7000 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7001 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7002 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7003 | srv = srv->next; |
| 7004 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7005 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7006 | 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] | 7007 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7008 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7009 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 7010 | txn->flags |= TX_CK_UNUSED; |
| 7011 | else |
| 7012 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7013 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7014 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7015 | /* depending on the cookie mode, we may have to either : |
| 7016 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7017 | * the server never sees it ; |
| 7018 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7019 | * application cookie so that it does not get accidentely removed later, |
| 7020 | * if we're in cookie prefix mode |
| 7021 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7022 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7023 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7024 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7025 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7026 | val_end += delta; |
| 7027 | next += delta; |
| 7028 | hdr_end += delta; |
| 7029 | hdr_next += delta; |
| 7030 | cur_hdr->len += delta; |
| 7031 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7032 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7033 | del_from = NULL; |
| 7034 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7035 | } |
| 7036 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7037 | (t->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] | 7038 | del_from = prev; |
| 7039 | } |
| 7040 | } else { |
| 7041 | /* This is not our cookie, so we must preserve it. But if we already |
| 7042 | * scheduled another cookie for removal, we cannot remove the |
| 7043 | * complete header, but we can remove the previous block itself. |
| 7044 | */ |
| 7045 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7046 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7047 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7048 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7049 | if (att_beg >= del_from) |
| 7050 | att_beg += delta; |
| 7051 | if (att_end >= del_from) |
| 7052 | att_end += delta; |
| 7053 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7054 | val_end += delta; |
| 7055 | next += delta; |
| 7056 | hdr_end += delta; |
| 7057 | hdr_next += delta; |
| 7058 | cur_hdr->len += delta; |
| 7059 | http_msg_move_end(&txn->req, delta); |
| 7060 | prev = del_from; |
| 7061 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7062 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7063 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7064 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7065 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 7066 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 7067 | int cmp_len, value_len; |
| 7068 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7069 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7070 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 7071 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7072 | value_begin = att_beg + t->be->appsession_name_len; |
| 7073 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 7074 | } else { |
| 7075 | cmp_len = att_end - att_beg; |
| 7076 | value_begin = val_beg; |
| 7077 | value_len = val_end - val_beg; |
| 7078 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7079 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7080 | /* let's see if the cookie is our appcookie */ |
| 7081 | if (cmp_len == t->be->appsession_name_len && |
| 7082 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 7083 | manage_client_side_appsession(t, value_begin, value_len); |
| 7084 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7085 | } |
| 7086 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7087 | /* continue with next cookie on this header line */ |
| 7088 | att_beg = next; |
| 7089 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7090 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7091 | /* There are no more cookies on this line. |
| 7092 | * We may still have one (or several) marked for deletion at the |
| 7093 | * end of the line. We must do this now in two ways : |
| 7094 | * - if some cookies must be preserved, we only delete from the |
| 7095 | * mark to the end of line ; |
| 7096 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7097 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7098 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7099 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7100 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7101 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7102 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7103 | cur_hdr->len += delta; |
| 7104 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7105 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7106 | |
| 7107 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7108 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7109 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7110 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7111 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7112 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7113 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7114 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7115 | } |
| 7116 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7117 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7118 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7119 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7123 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7124 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7125 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7126 | int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7127 | { |
| 7128 | char term; |
| 7129 | char *cur_ptr, *cur_end, *cur_next; |
| 7130 | int cur_idx, old_idx, last_hdr; |
| 7131 | struct http_txn *txn = &t->txn; |
| 7132 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7133 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7134 | |
| 7135 | last_hdr = 0; |
| 7136 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7137 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7138 | old_idx = 0; |
| 7139 | |
| 7140 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7141 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7142 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7143 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7144 | (exp->action == ACT_ALLOW || |
| 7145 | exp->action == ACT_DENY)) |
| 7146 | return 0; |
| 7147 | |
| 7148 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7149 | if (!cur_idx) |
| 7150 | break; |
| 7151 | |
| 7152 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7153 | cur_ptr = cur_next; |
| 7154 | cur_end = cur_ptr + cur_hdr->len; |
| 7155 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7156 | |
| 7157 | /* Now we have one header between cur_ptr and cur_end, |
| 7158 | * and the next header starts at cur_next. |
| 7159 | */ |
| 7160 | |
| 7161 | /* The annoying part is that pattern matching needs |
| 7162 | * that we modify the contents to null-terminate all |
| 7163 | * strings before testing them. |
| 7164 | */ |
| 7165 | |
| 7166 | term = *cur_end; |
| 7167 | *cur_end = '\0'; |
| 7168 | |
| 7169 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7170 | switch (exp->action) { |
| 7171 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7172 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7173 | last_hdr = 1; |
| 7174 | break; |
| 7175 | |
| 7176 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7177 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7178 | last_hdr = 1; |
| 7179 | break; |
| 7180 | |
| 7181 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7182 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7183 | 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] | 7184 | /* FIXME: if the user adds a newline in the replacement, the |
| 7185 | * index will not be recalculated for now, and the new line |
| 7186 | * will not be counted as a new header. |
| 7187 | */ |
| 7188 | |
| 7189 | cur_end += delta; |
| 7190 | cur_next += delta; |
| 7191 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7192 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7193 | break; |
| 7194 | |
| 7195 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7196 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7197 | cur_next += delta; |
| 7198 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7199 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7200 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7201 | txn->hdr_idx.used--; |
| 7202 | cur_hdr->len = 0; |
| 7203 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7204 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7205 | break; |
| 7206 | |
| 7207 | } |
| 7208 | } |
| 7209 | if (cur_end) |
| 7210 | *cur_end = term; /* restore the string terminator */ |
| 7211 | |
| 7212 | /* keep the link from this header to next one in case of later |
| 7213 | * removal of next header. |
| 7214 | */ |
| 7215 | old_idx = cur_idx; |
| 7216 | } |
| 7217 | return 0; |
| 7218 | } |
| 7219 | |
| 7220 | |
| 7221 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7222 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7223 | * or -1 if a replacement resulted in an invalid status line. |
| 7224 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7225 | int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7226 | { |
| 7227 | char term; |
| 7228 | char *cur_ptr, *cur_end; |
| 7229 | int done; |
| 7230 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7231 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7232 | |
| 7233 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7234 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7235 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7236 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7237 | (exp->action == ACT_ALLOW || |
| 7238 | exp->action == ACT_DENY)) |
| 7239 | return 0; |
| 7240 | else if (exp->action == ACT_REMOVE) |
| 7241 | return 0; |
| 7242 | |
| 7243 | done = 0; |
| 7244 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7245 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7246 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7247 | |
| 7248 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7249 | |
| 7250 | /* The annoying part is that pattern matching needs |
| 7251 | * that we modify the contents to null-terminate all |
| 7252 | * strings before testing them. |
| 7253 | */ |
| 7254 | |
| 7255 | term = *cur_end; |
| 7256 | *cur_end = '\0'; |
| 7257 | |
| 7258 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7259 | switch (exp->action) { |
| 7260 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7261 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7262 | done = 1; |
| 7263 | break; |
| 7264 | |
| 7265 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7266 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7267 | done = 1; |
| 7268 | break; |
| 7269 | |
| 7270 | case ACT_REPLACE: |
| 7271 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7272 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7273 | 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] | 7274 | /* FIXME: if the user adds a newline in the replacement, the |
| 7275 | * index will not be recalculated for now, and the new line |
| 7276 | * will not be counted as a new header. |
| 7277 | */ |
| 7278 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7279 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7280 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7281 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7282 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7283 | cur_ptr, cur_end + 1, |
| 7284 | NULL, NULL); |
| 7285 | if (unlikely(!cur_end)) |
| 7286 | return -1; |
| 7287 | |
| 7288 | /* we have a full respnse and we know that we have either a CR |
| 7289 | * or an LF at <ptr>. |
| 7290 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7291 | 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] | 7292 | 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] | 7293 | /* there is no point trying this regex on headers */ |
| 7294 | return 1; |
| 7295 | } |
| 7296 | } |
| 7297 | *cur_end = term; /* restore the string terminator */ |
| 7298 | return done; |
| 7299 | } |
| 7300 | |
| 7301 | |
| 7302 | |
| 7303 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7304 | * 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] | 7305 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7306 | * unparsable response. |
| 7307 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7308 | 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] | 7309 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7310 | struct http_txn *txn = &s->txn; |
| 7311 | struct hdr_exp *exp; |
| 7312 | |
| 7313 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7314 | int ret; |
| 7315 | |
| 7316 | /* |
| 7317 | * The interleaving of transformations and verdicts |
| 7318 | * makes it difficult to decide to continue or stop |
| 7319 | * the evaluation. |
| 7320 | */ |
| 7321 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7322 | if (txn->flags & TX_SVDENY) |
| 7323 | break; |
| 7324 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7325 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7326 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7327 | exp->action == ACT_PASS)) { |
| 7328 | exp = exp->next; |
| 7329 | continue; |
| 7330 | } |
| 7331 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7332 | /* if this filter had a condition, evaluate it now and skip to |
| 7333 | * next filter if the condition does not match. |
| 7334 | */ |
| 7335 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7336 | 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] | 7337 | ret = acl_pass(ret); |
| 7338 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7339 | ret = !ret; |
| 7340 | if (!ret) |
| 7341 | continue; |
| 7342 | } |
| 7343 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7344 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7345 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7346 | if (unlikely(ret < 0)) |
| 7347 | return -1; |
| 7348 | |
| 7349 | if (likely(ret == 0)) { |
| 7350 | /* The filter did not match the response, it can be |
| 7351 | * iterated through all headers. |
| 7352 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7353 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7354 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7355 | } |
| 7356 | return 0; |
| 7357 | } |
| 7358 | |
| 7359 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7360 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7361 | * 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] | 7362 | * desirable to call it only when needed. This function is also used when we |
| 7363 | * 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] | 7364 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7365 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7366 | { |
| 7367 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7368 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7369 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7370 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7371 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7372 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7373 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7374 | /* Iterate through the headers. |
| 7375 | * we start with the start line. |
| 7376 | */ |
| 7377 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7378 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7379 | |
| 7380 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7381 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7382 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7383 | |
| 7384 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7385 | hdr_beg = hdr_next; |
| 7386 | hdr_end = hdr_beg + cur_hdr->len; |
| 7387 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7388 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7389 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7390 | * next header starts at hdr_next. We're only interested in |
| 7391 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7392 | */ |
| 7393 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7394 | is_cookie2 = 0; |
| 7395 | prev = hdr_beg + 10; |
| 7396 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7397 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7398 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7399 | if (!val) { |
| 7400 | old_idx = cur_idx; |
| 7401 | continue; |
| 7402 | } |
| 7403 | is_cookie2 = 1; |
| 7404 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7405 | } |
| 7406 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7407 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7408 | * <prev> points to the colon. |
| 7409 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7410 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7411 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7412 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7413 | * check-cache is enabled) and we are not interested in checking |
| 7414 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7415 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7416 | if (t->be->cookie_name == NULL && |
| 7417 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7418 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7419 | return; |
| 7420 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7421 | /* OK so now we know we have to process this response cookie. |
| 7422 | * The format of the Set-Cookie header is slightly different |
| 7423 | * from the format of the Cookie header in that it does not |
| 7424 | * support the comma as a cookie delimiter (thus the header |
| 7425 | * cannot be folded) because the Expires attribute described in |
| 7426 | * the original Netscape's spec may contain an unquoted date |
| 7427 | * with a comma inside. We have to live with this because |
| 7428 | * many browsers don't support Max-Age and some browsers don't |
| 7429 | * support quoted strings. However the Set-Cookie2 header is |
| 7430 | * clean. |
| 7431 | * |
| 7432 | * We have to keep multiple pointers in order to support cookie |
| 7433 | * removal at the beginning, middle or end of header without |
| 7434 | * corrupting the header (in case of set-cookie2). A special |
| 7435 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7436 | * fields after the first semi-colon. The <next> pointer points |
| 7437 | * either to the end of line (set-cookie) or next unquoted comma |
| 7438 | * (set-cookie2). All of these headers are valid : |
| 7439 | * |
| 7440 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7441 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7442 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7443 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7444 | * | | | | | | | | | | |
| 7445 | * | | | | | | | | +-> next hdr_end <--+ |
| 7446 | * | | | | | | | +------------> scav |
| 7447 | * | | | | | | +--------------> val_end |
| 7448 | * | | | | | +--------------------> val_beg |
| 7449 | * | | | | +----------------------> equal |
| 7450 | * | | | +------------------------> att_end |
| 7451 | * | | +----------------------------> att_beg |
| 7452 | * | +------------------------------> prev |
| 7453 | * +-----------------------------------------> hdr_beg |
| 7454 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7455 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7456 | for (; prev < hdr_end; prev = next) { |
| 7457 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7458 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7459 | /* find att_beg */ |
| 7460 | att_beg = prev + 1; |
| 7461 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7462 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7463 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7464 | /* find att_end : this is the first character after the last non |
| 7465 | * space before the equal. It may be equal to hdr_end. |
| 7466 | */ |
| 7467 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7468 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7469 | while (equal < hdr_end) { |
| 7470 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7471 | break; |
| 7472 | if (http_is_spht[(unsigned char)*equal++]) |
| 7473 | continue; |
| 7474 | att_end = equal; |
| 7475 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7476 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7477 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7478 | * is between <att_beg> and <equal>, both may be identical. |
| 7479 | */ |
| 7480 | |
| 7481 | /* look for end of cookie if there is an equal sign */ |
| 7482 | if (equal < hdr_end && *equal == '=') { |
| 7483 | /* look for the beginning of the value */ |
| 7484 | val_beg = equal + 1; |
| 7485 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7486 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7487 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7488 | /* find the end of the value, respecting quotes */ |
| 7489 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7490 | |
| 7491 | /* make val_end point to the first white space or delimitor after the value */ |
| 7492 | val_end = next; |
| 7493 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7494 | val_end--; |
| 7495 | } else { |
| 7496 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7497 | val_beg = val_end = next = equal; |
| 7498 | } |
| 7499 | |
| 7500 | if (next < hdr_end) { |
| 7501 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7502 | * a colon or semi-colon before the end. So skip all attr-value |
| 7503 | * pairs and look for the next comma. For Set-Cookie, since |
| 7504 | * commas are permitted in values, skip to the end. |
| 7505 | */ |
| 7506 | if (is_cookie2) |
| 7507 | next = find_hdr_value_end(next, hdr_end); |
| 7508 | else |
| 7509 | next = hdr_end; |
| 7510 | } |
| 7511 | |
| 7512 | /* Now everything is as on the diagram above */ |
| 7513 | |
| 7514 | /* Ignore cookies with no equal sign */ |
| 7515 | if (equal == val_end) |
| 7516 | continue; |
| 7517 | |
| 7518 | /* If there are spaces around the equal sign, we need to |
| 7519 | * strip them otherwise we'll get trouble for cookie captures, |
| 7520 | * or even for rewrites. Since this happens extremely rarely, |
| 7521 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7522 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7523 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7524 | int stripped_before = 0; |
| 7525 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7526 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7527 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7528 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7529 | equal += stripped_before; |
| 7530 | val_beg += stripped_before; |
| 7531 | } |
| 7532 | |
| 7533 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7534 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7535 | val_beg += stripped_after; |
| 7536 | stripped_before += stripped_after; |
| 7537 | } |
| 7538 | |
| 7539 | val_end += stripped_before; |
| 7540 | next += stripped_before; |
| 7541 | hdr_end += stripped_before; |
| 7542 | hdr_next += stripped_before; |
| 7543 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7544 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7545 | } |
| 7546 | |
| 7547 | /* First, let's see if we want to capture this cookie. We check |
| 7548 | * that we don't already have a server side cookie, because we |
| 7549 | * can only capture one. Also as an optimisation, we ignore |
| 7550 | * cookies shorter than the declared name. |
| 7551 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7552 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7553 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7554 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7555 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7556 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7557 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7558 | Alert("HTTP logging : out of memory.\n"); |
| 7559 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7560 | else { |
| 7561 | if (log_len > t->fe->capture_len) |
| 7562 | log_len = t->fe->capture_len; |
| 7563 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7564 | txn->srv_cookie[log_len] = 0; |
| 7565 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7566 | } |
| 7567 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7568 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7569 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7570 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7571 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7572 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7573 | /* assume passive cookie by default */ |
| 7574 | txn->flags &= ~TX_SCK_MASK; |
| 7575 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7576 | |
| 7577 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7578 | * this occurrence because we'll insert another one later. |
| 7579 | * 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] | 7580 | * a direct access. |
| 7581 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7582 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7583 | /* The "preserve" flag was set, we don't want to touch the |
| 7584 | * server's cookie. |
| 7585 | */ |
| 7586 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7587 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7588 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7589 | /* this cookie must be deleted */ |
| 7590 | if (*prev == ':' && next == hdr_end) { |
| 7591 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7592 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7593 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7594 | txn->hdr_idx.used--; |
| 7595 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7596 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7597 | hdr_next += delta; |
| 7598 | http_msg_move_end(&txn->rsp, delta); |
| 7599 | /* note: while both invalid now, <next> and <hdr_end> |
| 7600 | * are still equal, so the for() will stop as expected. |
| 7601 | */ |
| 7602 | } else { |
| 7603 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7604 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7605 | next = prev; |
| 7606 | hdr_end += delta; |
| 7607 | hdr_next += delta; |
| 7608 | cur_hdr->len += delta; |
| 7609 | http_msg_move_end(&txn->rsp, delta); |
| 7610 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7611 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7612 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7613 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7614 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7615 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7616 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7617 | * with this server since we know it. |
| 7618 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7619 | 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] | 7620 | next += delta; |
| 7621 | hdr_end += delta; |
| 7622 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7623 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7624 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7625 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7626 | txn->flags &= ~TX_SCK_MASK; |
| 7627 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7628 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7629 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7630 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7631 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7632 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7633 | 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] | 7634 | next += delta; |
| 7635 | hdr_end += delta; |
| 7636 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7637 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7638 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7639 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7640 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7641 | txn->flags &= ~TX_SCK_MASK; |
| 7642 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7643 | } |
| 7644 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7645 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7646 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7647 | int cmp_len, value_len; |
| 7648 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7649 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7650 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7651 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7652 | value_begin = att_beg + t->be->appsession_name_len; |
| 7653 | value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7654 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7655 | cmp_len = att_end - att_beg; |
| 7656 | value_begin = val_beg; |
| 7657 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7658 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7659 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7660 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7661 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7662 | /* free a possibly previously allocated memory */ |
| 7663 | pool_free2(apools.sessid, txn->sessid); |
| 7664 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7665 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7666 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7667 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7668 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7669 | return; |
| 7670 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7671 | memcpy(txn->sessid, value_begin, value_len); |
| 7672 | txn->sessid[value_len] = 0; |
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 | } |
| 7675 | /* that's done for this cookie, check the next one on the same |
| 7676 | * line when next != hdr_end (only if is_cookie2). |
| 7677 | */ |
| 7678 | } |
| 7679 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7680 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7681 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7682 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7683 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7684 | appsess *asession = NULL; |
| 7685 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7686 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7687 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7688 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7689 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7690 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7691 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7692 | return; |
| 7693 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7694 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7695 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7696 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7697 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7698 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 7699 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7700 | return; |
| 7701 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7702 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7703 | asession->sessid[t->be->appsession_len] = 0; |
| 7704 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7705 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7706 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7707 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7708 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 7709 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7710 | return; |
| 7711 | } |
| 7712 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7713 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7714 | |
| 7715 | asession->request_count = 0; |
| 7716 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7717 | } |
| 7718 | |
| 7719 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7720 | asession->request_count++; |
| 7721 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7722 | } |
| 7723 | |
| 7724 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7725 | /* |
| 7726 | * Check if response is cacheable or not. Updates t->flags. |
| 7727 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7728 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7729 | { |
| 7730 | struct http_txn *txn = &t->txn; |
| 7731 | char *p1, *p2; |
| 7732 | |
| 7733 | char *cur_ptr, *cur_end, *cur_next; |
| 7734 | int cur_idx; |
| 7735 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7736 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7737 | return; |
| 7738 | |
| 7739 | /* Iterate through the headers. |
| 7740 | * we start with the start line. |
| 7741 | */ |
| 7742 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7743 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7744 | |
| 7745 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7746 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7747 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7748 | |
| 7749 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7750 | cur_ptr = cur_next; |
| 7751 | cur_end = cur_ptr + cur_hdr->len; |
| 7752 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7753 | |
| 7754 | /* We have one full header between cur_ptr and cur_end, and the |
| 7755 | * next header starts at cur_next. We're only interested in |
| 7756 | * "Cookie:" headers. |
| 7757 | */ |
| 7758 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7759 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7760 | if (val) { |
| 7761 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7762 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7763 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7764 | return; |
| 7765 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7766 | } |
| 7767 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7768 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7769 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7770 | continue; |
| 7771 | |
| 7772 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7773 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7774 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7775 | |
| 7776 | if (p1 >= cur_end) /* no more info */ |
| 7777 | continue; |
| 7778 | |
| 7779 | /* p1 is at the beginning of the value */ |
| 7780 | p2 = p1; |
| 7781 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7782 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7783 | p2++; |
| 7784 | |
| 7785 | /* we have a complete value between p1 and p2 */ |
| 7786 | if (p2 < cur_end && *p2 == '=') { |
| 7787 | /* we have something of the form no-cache="set-cookie" */ |
| 7788 | if ((cur_end - p1 >= 21) && |
| 7789 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7790 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7791 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7792 | continue; |
| 7793 | } |
| 7794 | |
| 7795 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7796 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7797 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7798 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7799 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7800 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7801 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7802 | return; |
| 7803 | } |
| 7804 | |
| 7805 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7806 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7807 | continue; |
| 7808 | } |
| 7809 | } |
| 7810 | } |
| 7811 | |
| 7812 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7813 | /* |
| 7814 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7815 | * If the server is found, it's assigned to the session. |
| 7816 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7817 | void get_srv_from_appsession(struct session *t, const char *begin, int len) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7818 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7819 | char *end_params, *first_param, *cur_param, *next_param; |
| 7820 | char separator; |
| 7821 | int value_len; |
| 7822 | |
| 7823 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7824 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7825 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7826 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7827 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7828 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7829 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7830 | first_param = NULL; |
| 7831 | switch (mode) { |
| 7832 | case PR_O2_AS_M_PP: |
| 7833 | first_param = memchr(begin, ';', len); |
| 7834 | break; |
| 7835 | case PR_O2_AS_M_QS: |
| 7836 | first_param = memchr(begin, '?', len); |
| 7837 | break; |
| 7838 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7839 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7840 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7841 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7842 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7843 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7844 | switch (mode) { |
| 7845 | case PR_O2_AS_M_PP: |
| 7846 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7847 | end_params = (char *) begin + len; |
| 7848 | } |
| 7849 | separator = ';'; |
| 7850 | break; |
| 7851 | case PR_O2_AS_M_QS: |
| 7852 | end_params = (char *) begin + len; |
| 7853 | separator = '&'; |
| 7854 | break; |
| 7855 | default: |
| 7856 | /* unknown mode, shouldn't happen */ |
| 7857 | return; |
| 7858 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7859 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7860 | cur_param = next_param = end_params; |
| 7861 | while (cur_param > first_param) { |
| 7862 | cur_param--; |
| 7863 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7864 | /* let's see if this is the appsession parameter */ |
| 7865 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7866 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7867 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7868 | /* Cool... it's the right one */ |
| 7869 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7870 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7871 | if (value_len > 0) { |
| 7872 | manage_client_side_appsession(t, cur_param, value_len); |
| 7873 | } |
| 7874 | break; |
| 7875 | } |
| 7876 | next_param = cur_param; |
| 7877 | } |
| 7878 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7879 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7880 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7881 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7882 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7883 | } |
| 7884 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7885 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7886 | * 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] | 7887 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7888 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7889 | * 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] | 7890 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7891 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7892 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7893 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7894 | 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] | 7895 | { |
| 7896 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7897 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7898 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7899 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7900 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7901 | if (!uri_auth) |
| 7902 | return 0; |
| 7903 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7904 | 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] | 7905 | return 0; |
| 7906 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7907 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7908 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 7909 | si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7910 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7911 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7912 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7913 | return 0; |
| 7914 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7915 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7916 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7917 | return 0; |
| 7918 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7919 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7920 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7921 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7922 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7923 | break; |
| 7924 | } |
| 7925 | h++; |
| 7926 | } |
| 7927 | |
| 7928 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7929 | h = uri + uri_auth->uri_len; |
| 7930 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7931 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7932 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7933 | break; |
| 7934 | } |
| 7935 | h++; |
| 7936 | } |
| 7937 | } |
| 7938 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7939 | h = uri + uri_auth->uri_len; |
| 7940 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7941 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 7942 | si->applet.ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7943 | break; |
| 7944 | } |
| 7945 | h++; |
| 7946 | } |
| 7947 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7948 | h = uri + uri_auth->uri_len; |
| 7949 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7950 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7951 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7952 | h += 4; |
Cyril Bonté | 20a804a | 2012-05-10 19:42:52 +0200 | [diff] [blame] | 7953 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7954 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7955 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7956 | si->applet.ctx.stats.st_code = i; |
| 7957 | break; |
| 7958 | } |
| 7959 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7960 | break; |
| 7961 | } |
| 7962 | h++; |
| 7963 | } |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 7964 | |
| 7965 | si->applet.ctx.stats.scope_str = 0; |
| 7966 | si->applet.ctx.stats.scope_len = 0; |
| 7967 | h = uri + uri_auth->uri_len; |
| 7968 | while (h <= uri + msg->sl.rq.u_l - 8) { |
| 7969 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 7970 | int itx = 0; |
| 7971 | const char *h2; |
| 7972 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 7973 | const char *err; |
| 7974 | |
| 7975 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 7976 | h2 = h; |
| 7977 | si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p; |
| 7978 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 7979 | itx++; |
| 7980 | h++; |
| 7981 | } |
| 7982 | |
| 7983 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 7984 | itx = STAT_SCOPE_TXT_MAXLEN; |
| 7985 | si->applet.ctx.stats.scope_len = itx; |
| 7986 | |
| 7987 | /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 7988 | memcpy(scope_txt, h2, itx); |
| 7989 | scope_txt[itx] = '\0'; |
| 7990 | err = invalid_char(scope_txt); |
| 7991 | if (err) { |
| 7992 | /* bad char in search text => clear scope */ |
| 7993 | si->applet.ctx.stats.scope_str = 0; |
| 7994 | si->applet.ctx.stats.scope_len = 0; |
| 7995 | } |
| 7996 | break; |
| 7997 | } |
| 7998 | h++; |
| 7999 | } |
| 8000 | |
| 8001 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8002 | return 1; |
| 8003 | } |
| 8004 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8005 | /* |
| 8006 | * 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] | 8007 | * By default it tries to report the error position as msg->err_pos. However if |
| 8008 | * this one is not set, it will then report msg->next, which is the last known |
| 8009 | * 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] | 8010 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8011 | */ |
| 8012 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8013 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8014 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8015 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8016 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8017 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8018 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8019 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8020 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8021 | len1 = MIN(len1, es->len); |
| 8022 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8023 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8024 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8025 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8026 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8027 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8028 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8029 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8030 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8031 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8032 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8033 | es->when = date; // user-visible date |
| 8034 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8035 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8036 | es->oe = other_end; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8037 | es->src = s->req->prod->conn->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8038 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8039 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8040 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8041 | es->s_flags = s->flags; |
| 8042 | es->t_flags = s->txn.flags; |
| 8043 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8044 | es->b_out = chn->buf->o; |
| 8045 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8046 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8047 | es->m_clen = msg->chunk_len; |
| 8048 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8049 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8050 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8051 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8052 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8053 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8054 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8055 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8056 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8057 | * 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] | 8058 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8059 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8060 | * 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] | 8061 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8062 | 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] | 8063 | struct hdr_idx *idx, int occ, |
| 8064 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8065 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8066 | struct hdr_ctx local_ctx; |
| 8067 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8068 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8069 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8070 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8071 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8072 | if (!ctx) { |
| 8073 | local_ctx.idx = 0; |
| 8074 | ctx = &local_ctx; |
| 8075 | } |
| 8076 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8077 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8078 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8079 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8080 | occ--; |
| 8081 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8082 | *vptr = ctx->line + ctx->val; |
| 8083 | *vlen = ctx->vlen; |
| 8084 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8085 | } |
| 8086 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8087 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | /* negative occurrence, we scan all the list then walk back */ |
| 8091 | if (-occ > MAX_HDR_HISTORY) |
| 8092 | return 0; |
| 8093 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8094 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8095 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8096 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8097 | len_hist[hist_ptr] = ctx->vlen; |
| 8098 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8099 | hist_ptr = 0; |
| 8100 | found++; |
| 8101 | } |
| 8102 | if (-occ > found) |
| 8103 | return 0; |
| 8104 | /* 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] | 8105 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8106 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8107 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8108 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8109 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8110 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8111 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8112 | *vptr = ptr_hist[hist_ptr]; |
| 8113 | *vlen = len_hist[hist_ptr]; |
| 8114 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8115 | } |
| 8116 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8117 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8118 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8119 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8120 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8121 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8122 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8123 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8124 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8125 | * line header values and does not stop at commas. |
| 8126 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8127 | */ |
| 8128 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8129 | struct hdr_idx *idx, int occ, |
| 8130 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8131 | { |
| 8132 | struct hdr_ctx local_ctx; |
| 8133 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8134 | int len_hist[MAX_HDR_HISTORY]; |
| 8135 | unsigned int hist_ptr; |
| 8136 | int found; |
| 8137 | |
| 8138 | if (!ctx) { |
| 8139 | local_ctx.idx = 0; |
| 8140 | ctx = &local_ctx; |
| 8141 | } |
| 8142 | |
| 8143 | if (occ >= 0) { |
| 8144 | /* search from the beginning */ |
| 8145 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8146 | occ--; |
| 8147 | if (occ <= 0) { |
| 8148 | *vptr = ctx->line + ctx->val; |
| 8149 | *vlen = ctx->vlen; |
| 8150 | return 1; |
| 8151 | } |
| 8152 | } |
| 8153 | return 0; |
| 8154 | } |
| 8155 | |
| 8156 | /* negative occurrence, we scan all the list then walk back */ |
| 8157 | if (-occ > MAX_HDR_HISTORY) |
| 8158 | return 0; |
| 8159 | |
| 8160 | found = hist_ptr = 0; |
| 8161 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8162 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8163 | len_hist[hist_ptr] = ctx->vlen; |
| 8164 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8165 | hist_ptr = 0; |
| 8166 | found++; |
| 8167 | } |
| 8168 | if (-occ > found) |
| 8169 | return 0; |
| 8170 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8171 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8172 | */ |
| 8173 | hist_ptr += occ; |
| 8174 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8175 | hist_ptr -= MAX_HDR_HISTORY; |
| 8176 | *vptr = ptr_hist[hist_ptr]; |
| 8177 | *vlen = len_hist[hist_ptr]; |
| 8178 | return 1; |
| 8179 | } |
| 8180 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8181 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8182 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8183 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8184 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8185 | */ |
| 8186 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 8187 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8188 | int max; |
| 8189 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 8190 | dir, (unsigned short)t->req->prod->conn->t.sock.fd, |
| 8191 | (unsigned short)t->req->cons->conn->t.sock.fd); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8192 | |
| 8193 | for (max = 0; start + max < end; max++) |
| 8194 | if (start[max] == '\r' || start[max] == '\n') |
| 8195 | break; |
| 8196 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8197 | UBOUND(max, trash.size - trash.len - 3); |
| 8198 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8199 | trash.str[trash.len++] = '\n'; |
| 8200 | if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8201 | } |
| 8202 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8203 | /* |
| 8204 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 8205 | * the required fields are properly allocated and that we only need to (re)init |
| 8206 | * them. This should be used before processing any new request. |
| 8207 | */ |
| 8208 | void http_init_txn(struct session *s) |
| 8209 | { |
| 8210 | struct http_txn *txn = &s->txn; |
| 8211 | struct proxy *fe = s->fe; |
| 8212 | |
| 8213 | txn->flags = 0; |
| 8214 | txn->status = -1; |
| 8215 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8216 | txn->cookie_first_date = 0; |
| 8217 | txn->cookie_last_date = 0; |
| 8218 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8219 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8220 | 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] | 8221 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 8222 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 8223 | 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] | 8224 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 8225 | txn->req.chunk_len = 0LL; |
| 8226 | txn->req.body_len = 0LL; |
| 8227 | txn->rsp.chunk_len = 0LL; |
| 8228 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8229 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8230 | 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] | 8231 | txn->req.chn = s->req; |
| 8232 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8233 | |
| 8234 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8235 | |
| 8236 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8237 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8238 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8239 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8240 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8241 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8242 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8243 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8244 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8245 | |
| 8246 | if (txn->hdr_idx.v) |
| 8247 | hdr_idx_init(&txn->hdr_idx); |
| 8248 | } |
| 8249 | |
| 8250 | /* to be used at the end of a transaction */ |
| 8251 | void http_end_txn(struct session *s) |
| 8252 | { |
| 8253 | struct http_txn *txn = &s->txn; |
| 8254 | |
| 8255 | /* these ones will have been dynamically allocated */ |
| 8256 | pool_free2(pool2_requri, txn->uri); |
| 8257 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8258 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8259 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8260 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8261 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8262 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8263 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8264 | txn->uri = NULL; |
| 8265 | txn->srv_cookie = NULL; |
| 8266 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8267 | |
| 8268 | if (txn->req.cap) { |
| 8269 | struct cap_hdr *h; |
| 8270 | for (h = s->fe->req_cap; h; h = h->next) |
| 8271 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8272 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8273 | } |
| 8274 | |
| 8275 | if (txn->rsp.cap) { |
| 8276 | struct cap_hdr *h; |
| 8277 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8278 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8279 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8280 | } |
| 8281 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8282 | } |
| 8283 | |
| 8284 | /* to be used at the end of a transaction to prepare a new one */ |
| 8285 | void http_reset_txn(struct session *s) |
| 8286 | { |
| 8287 | http_end_txn(s); |
| 8288 | http_init_txn(s); |
| 8289 | |
| 8290 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8291 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8292 | s->logs.level = 0; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8293 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8294 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8295 | /* re-init store persistence */ |
| 8296 | s->store_count = 0; |
| 8297 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8298 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8299 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8300 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8301 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8302 | /* We must trim any excess data from the response buffer, because we |
| 8303 | * may have blocked an invalid response from a server that we don't |
| 8304 | * want to accidentely forward once we disable the analysers, nor do |
| 8305 | * we want those data to come along with next response. A typical |
| 8306 | * example of such data would be from a buggy server responding to |
| 8307 | * a HEAD with some data, or sending more than the advertised |
| 8308 | * content-length. |
| 8309 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8310 | if (unlikely(s->rep->buf->i)) |
| 8311 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8312 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8313 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8314 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8315 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8316 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8317 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8318 | |
| 8319 | s->req->rex = TICK_ETERNITY; |
| 8320 | s->req->wex = TICK_ETERNITY; |
| 8321 | s->req->analyse_exp = TICK_ETERNITY; |
| 8322 | s->rep->rex = TICK_ETERNITY; |
| 8323 | s->rep->wex = TICK_ETERNITY; |
| 8324 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8325 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8326 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8327 | void free_http_req_rules(struct list *r) { |
| 8328 | struct http_req_rule *tr, *pr; |
| 8329 | |
| 8330 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8331 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8332 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8333 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8334 | |
| 8335 | free(pr); |
| 8336 | } |
| 8337 | } |
| 8338 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8339 | /* parse an "http-request" rule */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8340 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8341 | { |
| 8342 | struct http_req_rule *rule; |
| 8343 | int cur_arg; |
| 8344 | |
| 8345 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8346 | if (!rule) { |
| 8347 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8348 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8349 | } |
| 8350 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8351 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8352 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8353 | cur_arg = 1; |
| 8354 | } else if (!strcmp(args[0], "deny")) { |
| 8355 | rule->action = HTTP_REQ_ACT_DENY; |
| 8356 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8357 | } else if (!strcmp(args[0], "tarpit")) { |
| 8358 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8359 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8360 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8361 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8362 | cur_arg = 1; |
| 8363 | |
| 8364 | while(*args[cur_arg]) { |
| 8365 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8366 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8367 | cur_arg+=2; |
| 8368 | continue; |
| 8369 | } else |
| 8370 | break; |
| 8371 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8372 | } else if (!strcmp(args[0], "set-nice")) { |
| 8373 | rule->action = HTTP_REQ_ACT_SET_NICE; |
| 8374 | cur_arg = 1; |
| 8375 | |
| 8376 | if (!*args[cur_arg] || |
| 8377 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8378 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8379 | file, linenum, args[0]); |
| 8380 | goto out_err; |
| 8381 | } |
| 8382 | rule->arg.nice = atoi(args[cur_arg]); |
| 8383 | if (rule->arg.nice < -1024) |
| 8384 | rule->arg.nice = -1024; |
| 8385 | else if (rule->arg.nice > 1024) |
| 8386 | rule->arg.nice = 1024; |
| 8387 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8388 | } else if (!strcmp(args[0], "set-tos")) { |
| 8389 | #ifdef IP_TOS |
| 8390 | char *err; |
| 8391 | rule->action = HTTP_REQ_ACT_SET_TOS; |
| 8392 | cur_arg = 1; |
| 8393 | |
| 8394 | if (!*args[cur_arg] || |
| 8395 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8396 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8397 | file, linenum, args[0]); |
| 8398 | goto out_err; |
| 8399 | } |
| 8400 | |
| 8401 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8402 | if (err && *err != '\0') { |
| 8403 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8404 | file, linenum, err, args[0]); |
| 8405 | goto out_err; |
| 8406 | } |
| 8407 | cur_arg++; |
| 8408 | #else |
| 8409 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8410 | goto out_err; |
| 8411 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8412 | } else if (!strcmp(args[0], "set-mark")) { |
| 8413 | #ifdef SO_MARK |
| 8414 | char *err; |
| 8415 | rule->action = HTTP_REQ_ACT_SET_MARK; |
| 8416 | cur_arg = 1; |
| 8417 | |
| 8418 | if (!*args[cur_arg] || |
| 8419 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8420 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8421 | file, linenum, args[0]); |
| 8422 | goto out_err; |
| 8423 | } |
| 8424 | |
| 8425 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8426 | if (err && *err != '\0') { |
| 8427 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8428 | file, linenum, err, args[0]); |
| 8429 | goto out_err; |
| 8430 | } |
| 8431 | cur_arg++; |
| 8432 | global.last_checks |= LSTCHK_NETADM; |
| 8433 | #else |
| 8434 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8435 | goto out_err; |
| 8436 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8437 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8438 | rule->action = HTTP_REQ_ACT_SET_LOGL; |
| 8439 | cur_arg = 1; |
| 8440 | |
| 8441 | if (!*args[cur_arg] || |
| 8442 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8443 | bad_log_level: |
| 8444 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8445 | file, linenum, args[0]); |
| 8446 | goto out_err; |
| 8447 | } |
| 8448 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8449 | rule->arg.loglevel = -1; |
| 8450 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8451 | goto bad_log_level; |
| 8452 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8453 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8454 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8455 | cur_arg = 1; |
| 8456 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8457 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8458 | (*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] | 8459 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8460 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8461 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8462 | } |
| 8463 | |
| 8464 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8465 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8466 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8467 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8468 | proxy->conf.args.ctx = ARGC_HRQ; |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 8469 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8470 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8471 | cur_arg += 2; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8472 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8473 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8474 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8475 | |
| 8476 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) { |
| 8477 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8478 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8479 | goto out_err; |
| 8480 | } |
| 8481 | |
| 8482 | /* this redirect rule might already contain a parsed condition which |
| 8483 | * we'll pass to the http-request rule. |
| 8484 | */ |
| 8485 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8486 | rule->arg.redir = redir; |
| 8487 | rule->cond = redir->cond; |
| 8488 | redir->cond = NULL; |
| 8489 | cur_arg = 2; |
| 8490 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8491 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8492 | 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] | 8493 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8494 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8495 | } |
| 8496 | |
| 8497 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8498 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8499 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8500 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8501 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8502 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8503 | file, linenum, args[0], errmsg); |
| 8504 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8505 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8506 | } |
| 8507 | rule->cond = cond; |
| 8508 | } |
| 8509 | else if (*args[cur_arg]) { |
| 8510 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8511 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8512 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8513 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8514 | } |
| 8515 | |
| 8516 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8517 | out_err: |
| 8518 | free(rule); |
| 8519 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8520 | } |
| 8521 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8522 | /* parse an "http-respose" rule */ |
| 8523 | struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8524 | { |
| 8525 | struct http_res_rule *rule; |
| 8526 | int cur_arg; |
| 8527 | |
| 8528 | rule = calloc(1, sizeof(*rule)); |
| 8529 | if (!rule) { |
| 8530 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8531 | goto out_err; |
| 8532 | } |
| 8533 | |
| 8534 | if (!strcmp(args[0], "allow")) { |
| 8535 | rule->action = HTTP_RES_ACT_ALLOW; |
| 8536 | cur_arg = 1; |
| 8537 | } else if (!strcmp(args[0], "deny")) { |
| 8538 | rule->action = HTTP_RES_ACT_DENY; |
| 8539 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8540 | } else if (!strcmp(args[0], "set-nice")) { |
| 8541 | rule->action = HTTP_RES_ACT_SET_NICE; |
| 8542 | cur_arg = 1; |
| 8543 | |
| 8544 | if (!*args[cur_arg] || |
| 8545 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8546 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8547 | file, linenum, args[0]); |
| 8548 | goto out_err; |
| 8549 | } |
| 8550 | rule->arg.nice = atoi(args[cur_arg]); |
| 8551 | if (rule->arg.nice < -1024) |
| 8552 | rule->arg.nice = -1024; |
| 8553 | else if (rule->arg.nice > 1024) |
| 8554 | rule->arg.nice = 1024; |
| 8555 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8556 | } else if (!strcmp(args[0], "set-tos")) { |
| 8557 | #ifdef IP_TOS |
| 8558 | char *err; |
| 8559 | rule->action = HTTP_RES_ACT_SET_TOS; |
| 8560 | cur_arg = 1; |
| 8561 | |
| 8562 | if (!*args[cur_arg] || |
| 8563 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8564 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8565 | file, linenum, args[0]); |
| 8566 | goto out_err; |
| 8567 | } |
| 8568 | |
| 8569 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8570 | if (err && *err != '\0') { |
| 8571 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8572 | file, linenum, err, args[0]); |
| 8573 | goto out_err; |
| 8574 | } |
| 8575 | cur_arg++; |
| 8576 | #else |
| 8577 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8578 | goto out_err; |
| 8579 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8580 | } else if (!strcmp(args[0], "set-mark")) { |
| 8581 | #ifdef SO_MARK |
| 8582 | char *err; |
| 8583 | rule->action = HTTP_RES_ACT_SET_MARK; |
| 8584 | cur_arg = 1; |
| 8585 | |
| 8586 | if (!*args[cur_arg] || |
| 8587 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8588 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8589 | file, linenum, args[0]); |
| 8590 | goto out_err; |
| 8591 | } |
| 8592 | |
| 8593 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8594 | if (err && *err != '\0') { |
| 8595 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8596 | file, linenum, err, args[0]); |
| 8597 | goto out_err; |
| 8598 | } |
| 8599 | cur_arg++; |
| 8600 | global.last_checks |= LSTCHK_NETADM; |
| 8601 | #else |
| 8602 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8603 | goto out_err; |
| 8604 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8605 | } else if (!strcmp(args[0], "set-log-level")) { |
| 8606 | rule->action = HTTP_RES_ACT_SET_LOGL; |
| 8607 | cur_arg = 1; |
| 8608 | |
| 8609 | if (!*args[cur_arg] || |
| 8610 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8611 | bad_log_level: |
| 8612 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8613 | file, linenum, args[0]); |
| 8614 | goto out_err; |
| 8615 | } |
| 8616 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8617 | rule->arg.loglevel = -1; |
| 8618 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 8619 | goto bad_log_level; |
| 8620 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8621 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8622 | rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR; |
| 8623 | cur_arg = 1; |
| 8624 | |
| 8625 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8626 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8627 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8628 | file, linenum, args[0]); |
| 8629 | goto out_err; |
| 8630 | } |
| 8631 | |
| 8632 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8633 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8634 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8635 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8636 | proxy->conf.args.ctx = ARGC_HRS; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8637 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8638 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR); |
| 8639 | cur_arg += 2; |
| 8640 | } else { |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8641 | 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] | 8642 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
| 8643 | goto out_err; |
| 8644 | } |
| 8645 | |
| 8646 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8647 | struct acl_cond *cond; |
| 8648 | char *errmsg = NULL; |
| 8649 | |
| 8650 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8651 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8652 | file, linenum, args[0], errmsg); |
| 8653 | free(errmsg); |
| 8654 | goto out_err; |
| 8655 | } |
| 8656 | rule->cond = cond; |
| 8657 | } |
| 8658 | else if (*args[cur_arg]) { |
| 8659 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8660 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8661 | file, linenum, args[0], args[cur_arg]); |
| 8662 | goto out_err; |
| 8663 | } |
| 8664 | |
| 8665 | return rule; |
| 8666 | out_err: |
| 8667 | free(rule); |
| 8668 | return NULL; |
| 8669 | } |
| 8670 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8671 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
| 8672 | * with <err> filled with the error message. |
| 8673 | */ |
| 8674 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
| 8675 | const char **args, char **errmsg) |
| 8676 | { |
| 8677 | struct redirect_rule *rule; |
| 8678 | int cur_arg; |
| 8679 | int type = REDIRECT_TYPE_NONE; |
| 8680 | int code = 302; |
| 8681 | const char *destination = NULL; |
| 8682 | const char *cookie = NULL; |
| 8683 | int cookie_set = 0; |
| 8684 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8685 | struct acl_cond *cond = NULL; |
| 8686 | |
| 8687 | cur_arg = 0; |
| 8688 | while (*(args[cur_arg])) { |
| 8689 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8690 | if (!*args[cur_arg + 1]) |
| 8691 | goto missing_arg; |
| 8692 | |
| 8693 | type = REDIRECT_TYPE_LOCATION; |
| 8694 | cur_arg++; |
| 8695 | destination = args[cur_arg]; |
| 8696 | } |
| 8697 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8698 | if (!*args[cur_arg + 1]) |
| 8699 | goto missing_arg; |
| 8700 | |
| 8701 | type = REDIRECT_TYPE_PREFIX; |
| 8702 | cur_arg++; |
| 8703 | destination = args[cur_arg]; |
| 8704 | } |
| 8705 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8706 | if (!*args[cur_arg + 1]) |
| 8707 | goto missing_arg; |
| 8708 | |
| 8709 | type = REDIRECT_TYPE_SCHEME; |
| 8710 | cur_arg++; |
| 8711 | destination = args[cur_arg]; |
| 8712 | } |
| 8713 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8714 | if (!*args[cur_arg + 1]) |
| 8715 | goto missing_arg; |
| 8716 | |
| 8717 | cur_arg++; |
| 8718 | cookie = args[cur_arg]; |
| 8719 | cookie_set = 1; |
| 8720 | } |
| 8721 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8722 | if (!*args[cur_arg + 1]) |
| 8723 | goto missing_arg; |
| 8724 | |
| 8725 | cur_arg++; |
| 8726 | cookie = args[cur_arg]; |
| 8727 | cookie_set = 0; |
| 8728 | } |
| 8729 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8730 | if (!*args[cur_arg + 1]) |
| 8731 | goto missing_arg; |
| 8732 | |
| 8733 | cur_arg++; |
| 8734 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8735 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8736 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8737 | "'%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] | 8738 | args[cur_arg - 1], args[cur_arg]); |
| 8739 | return NULL; |
| 8740 | } |
| 8741 | } |
| 8742 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8743 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8744 | } |
| 8745 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8746 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8747 | } |
| 8748 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8749 | strcmp(args[cur_arg], "unless") == 0) { |
| 8750 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8751 | if (!cond) { |
| 8752 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8753 | return NULL; |
| 8754 | } |
| 8755 | break; |
| 8756 | } |
| 8757 | else { |
| 8758 | memprintf(errmsg, |
| 8759 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8760 | args[cur_arg]); |
| 8761 | return NULL; |
| 8762 | } |
| 8763 | cur_arg++; |
| 8764 | } |
| 8765 | |
| 8766 | if (type == REDIRECT_TYPE_NONE) { |
| 8767 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8768 | return NULL; |
| 8769 | } |
| 8770 | |
| 8771 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8772 | rule->cond = cond; |
| 8773 | rule->rdr_str = strdup(destination); |
| 8774 | rule->rdr_len = strlen(destination); |
| 8775 | if (cookie) { |
| 8776 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8777 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8778 | */ |
| 8779 | rule->cookie_len = strlen(cookie); |
| 8780 | if (cookie_set) { |
| 8781 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8782 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8783 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8784 | rule->cookie_len += 9; |
| 8785 | } else { |
| 8786 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8787 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8788 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 8789 | rule->cookie_len += 20; |
| 8790 | } |
| 8791 | } |
| 8792 | rule->type = type; |
| 8793 | rule->code = code; |
| 8794 | rule->flags = flags; |
| 8795 | LIST_INIT(&rule->list); |
| 8796 | return rule; |
| 8797 | |
| 8798 | missing_arg: |
| 8799 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 8800 | return NULL; |
| 8801 | } |
| 8802 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8803 | /************************************************************************/ |
| 8804 | /* The code below is dedicated to ACL parsing and matching */ |
| 8805 | /************************************************************************/ |
| 8806 | |
| 8807 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8808 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 8809 | * which means that a request or response is ready. If some data is missing, |
| 8810 | * 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] | 8811 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 8812 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8813 | * |
| 8814 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8815 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 8816 | * decide whether or not an HTTP message is present ; |
| 8817 | * 0 if the requested data cannot be fetched or if it is certain that |
| 8818 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8819 | * 1 if an HTTP message is ready |
| 8820 | */ |
| 8821 | static int |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8822 | 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] | 8823 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8824 | { |
| 8825 | struct http_txn *txn = l7; |
| 8826 | struct http_msg *msg = &txn->req; |
| 8827 | |
| 8828 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8829 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8830 | */ |
| 8831 | |
| 8832 | if (unlikely(!s || !txn)) |
| 8833 | return 0; |
| 8834 | |
| 8835 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8836 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8837 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8838 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8839 | if (unlikely(!s->req)) |
| 8840 | return 0; |
| 8841 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8842 | /* If the buffer does not leave enough free space at the end, |
| 8843 | * we must first realign it. |
| 8844 | */ |
| 8845 | if (s->req->buf->p > s->req->buf->data && |
| 8846 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 8847 | buffer_slow_realign(s->req->buf); |
| 8848 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8849 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 8850 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8851 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8852 | |
| 8853 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8854 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8855 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 8856 | |
| 8857 | /* Still no valid request ? */ |
| 8858 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8859 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8860 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8861 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8862 | } |
| 8863 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8864 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8865 | return 0; |
| 8866 | } |
| 8867 | |
| 8868 | /* OK we just got a valid HTTP request. We have some minor |
| 8869 | * preparation to perform so that further checks can rely |
| 8870 | * on HTTP tests. |
| 8871 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8872 | |
| 8873 | /* If the request was parsed but was too large, we must absolutely |
| 8874 | * return an error so that it is not processed. At the moment this |
| 8875 | * cannot happen, but if the parsers are to change in the future, |
| 8876 | * we want this check to be maintained. |
| 8877 | */ |
| 8878 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 8879 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 8880 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8881 | smp->data.uint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8882 | return 1; |
| 8883 | } |
| 8884 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8885 | 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] | 8886 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8887 | s->flags |= SN_REDIRECTABLE; |
| 8888 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8889 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 8890 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8891 | } |
| 8892 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8893 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8894 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8895 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8896 | |
| 8897 | /* otherwise everything's ready for the request */ |
| 8898 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8899 | else { |
| 8900 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8901 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 8902 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8903 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8904 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8905 | } |
| 8906 | |
| 8907 | /* everything's OK */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8908 | smp->data.uint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8909 | return 1; |
| 8910 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8911 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8912 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8913 | 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] | 8914 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8915 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 8916 | 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] | 8917 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8918 | |
| 8919 | /* 1. Check on METHOD |
| 8920 | * We use the pre-parsed method if it is known, and store its number as an |
| 8921 | * integer. If it is unknown, we use the pointer and the length. |
| 8922 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8923 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8924 | { |
| 8925 | int len, meth; |
| 8926 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8927 | len = strlen(*text); |
| 8928 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8929 | |
| 8930 | pattern->val.i = meth; |
| 8931 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8932 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8933 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8934 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8935 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8936 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8937 | pattern->len = len; |
| 8938 | } |
| 8939 | return 1; |
| 8940 | } |
| 8941 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8942 | /* This function fetches the method of current HTTP request and stores |
| 8943 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 8944 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 8945 | * in <len> and <ptr> is NULL ; |
| 8946 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 8947 | * <len> to its length. |
| 8948 | * This is intended to be used with acl_match_meth() only. |
| 8949 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8950 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 8951 | 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] | 8952 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8953 | { |
| 8954 | int meth; |
| 8955 | struct http_txn *txn = l7; |
| 8956 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8957 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8958 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8959 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8960 | smp->type = SMP_T_UINT; |
| 8961 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8962 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8963 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8964 | /* ensure the indexes are not affected */ |
| 8965 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8966 | smp->type = SMP_T_CSTR; |
| 8967 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8968 | smp->data.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8969 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8970 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8971 | return 1; |
| 8972 | } |
| 8973 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8974 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8975 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8976 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8977 | int icase; |
| 8978 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8979 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8980 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8981 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8982 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8983 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8984 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8985 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8986 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8987 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 8988 | if (pattern->val.i != HTTP_METH_OTHER) |
| 8989 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8990 | |
| 8991 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8992 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8993 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8994 | |
| 8995 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8996 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 8997 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8998 | return ACL_PAT_FAIL; |
| 8999 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9000 | } |
| 9001 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9002 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9003 | 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] | 9004 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9005 | { |
| 9006 | struct http_txn *txn = l7; |
| 9007 | char *ptr; |
| 9008 | int len; |
| 9009 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9010 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9011 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9012 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9013 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9014 | |
| 9015 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9016 | if (len <= 0) |
| 9017 | return 0; |
| 9018 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9019 | smp->type = SMP_T_CSTR; |
| 9020 | smp->data.str.str = ptr; |
| 9021 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9022 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9023 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9024 | return 1; |
| 9025 | } |
| 9026 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9027 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9028 | 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] | 9029 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9030 | { |
| 9031 | struct http_txn *txn = l7; |
| 9032 | char *ptr; |
| 9033 | int len; |
| 9034 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9035 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9036 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9037 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9038 | return 0; |
| 9039 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9040 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9041 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9042 | |
| 9043 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9044 | if (len <= 0) |
| 9045 | return 0; |
| 9046 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9047 | smp->type = SMP_T_CSTR; |
| 9048 | smp->data.str.str = ptr; |
| 9049 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9050 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9051 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9052 | return 1; |
| 9053 | } |
| 9054 | |
| 9055 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9056 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9057 | 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] | 9058 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9059 | { |
| 9060 | struct http_txn *txn = l7; |
| 9061 | char *ptr; |
| 9062 | int len; |
| 9063 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9064 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9065 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9066 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9067 | return 0; |
| 9068 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9069 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9070 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9071 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9072 | smp->type = SMP_T_UINT; |
| 9073 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9074 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9075 | return 1; |
| 9076 | } |
| 9077 | |
| 9078 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9079 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9080 | 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] | 9081 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9082 | { |
| 9083 | struct http_txn *txn = l7; |
| 9084 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9085 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9086 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9087 | smp->type = SMP_T_CSTR; |
| 9088 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9089 | smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9090 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9091 | return 1; |
| 9092 | } |
| 9093 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9094 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9095 | 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] | 9096 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9097 | { |
| 9098 | struct http_txn *txn = l7; |
| 9099 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9100 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9101 | |
| 9102 | /* Parse HTTP request */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 9103 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to); |
| 9104 | if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9105 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9106 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 9107 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9108 | |
| 9109 | /* |
| 9110 | * If we are parsing url in frontend space, we prepare backend stage |
| 9111 | * to not parse again the same url ! optimization lazyness... |
| 9112 | */ |
| 9113 | if (px->options & PR_O_HTTP_PROXY) |
| 9114 | l4->flags |= SN_ADDR_SET; |
| 9115 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9116 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9117 | return 1; |
| 9118 | } |
| 9119 | |
| 9120 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9121 | 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] | 9122 | const struct arg *args, struct sample *smp, const char *kw) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9123 | { |
| 9124 | struct http_txn *txn = l7; |
| 9125 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9126 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9127 | |
| 9128 | /* Same optimization as url_ip */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 9129 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9130 | smp->type = SMP_T_UINT; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 9131 | smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9132 | |
| 9133 | if (px->options & PR_O_HTTP_PROXY) |
| 9134 | l4->flags |= SN_ADDR_SET; |
| 9135 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9136 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9137 | return 1; |
| 9138 | } |
| 9139 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9140 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9141 | * Accepts an optional argument of type string containing the header field name, |
| 9142 | * and an optional argument of type signed or unsigned integer to request an |
| 9143 | * 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] | 9144 | * headers are considered from the first one. It does not stop on commas and |
| 9145 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9146 | */ |
| 9147 | static int |
| 9148 | 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] | 9149 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9150 | { |
| 9151 | struct http_txn *txn = l7; |
| 9152 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9153 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 9154 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9155 | int occ = 0; |
| 9156 | const char *name_str = NULL; |
| 9157 | int name_len = 0; |
| 9158 | |
| 9159 | if (!ctx) { |
| 9160 | /* first call */ |
| 9161 | ctx = &static_hdr_ctx; |
| 9162 | ctx->idx = 0; |
| 9163 | smp->ctx.a[0] = ctx; |
| 9164 | } |
| 9165 | |
| 9166 | if (args) { |
| 9167 | if (args[0].type != ARGT_STR) |
| 9168 | return 0; |
| 9169 | name_str = args[0].data.str.str; |
| 9170 | name_len = args[0].data.str.len; |
| 9171 | |
| 9172 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9173 | occ = args[1].data.uint; |
| 9174 | } |
| 9175 | |
| 9176 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9177 | |
| 9178 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9179 | /* search for header from the beginning */ |
| 9180 | ctx->idx = 0; |
| 9181 | |
| 9182 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9183 | /* no explicit occurrence and single fetch => last header by default */ |
| 9184 | occ = -1; |
| 9185 | |
| 9186 | if (!occ) |
| 9187 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9188 | smp->flags |= SMP_F_NOT_LAST; |
| 9189 | |
| 9190 | smp->type = SMP_T_CSTR; |
| 9191 | smp->flags |= SMP_F_VOL_HDR; |
| 9192 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len)) |
| 9193 | return 1; |
| 9194 | |
| 9195 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9196 | return 0; |
| 9197 | } |
| 9198 | |
| 9199 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9200 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9201 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9202 | */ |
| 9203 | static int |
| 9204 | 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] | 9205 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9206 | { |
| 9207 | struct http_txn *txn = l7; |
| 9208 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9209 | struct hdr_ctx ctx; |
| 9210 | const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp; |
| 9211 | int cnt; |
| 9212 | |
| 9213 | if (!args || args->type != ARGT_STR) |
| 9214 | return 0; |
| 9215 | |
| 9216 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9217 | |
| 9218 | ctx.idx = 0; |
| 9219 | cnt = 0; |
| 9220 | while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx)) |
| 9221 | cnt++; |
| 9222 | |
| 9223 | smp->type = SMP_T_UINT; |
| 9224 | smp->data.uint = cnt; |
| 9225 | smp->flags = SMP_F_VOL_HDR; |
| 9226 | return 1; |
| 9227 | } |
| 9228 | |
| 9229 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9230 | * Accepts an optional argument of type string containing the header field name, |
| 9231 | * and an optional argument of type signed or unsigned integer to request an |
| 9232 | * 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] | 9233 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9234 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9235 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9236 | 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] | 9237 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9238 | { |
| 9239 | struct http_txn *txn = l7; |
| 9240 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9241 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9242 | 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] | 9243 | int occ = 0; |
| 9244 | const char *name_str = NULL; |
| 9245 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9246 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9247 | if (!ctx) { |
| 9248 | /* first call */ |
| 9249 | ctx = &static_hdr_ctx; |
| 9250 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9251 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9252 | } |
| 9253 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9254 | if (args) { |
| 9255 | if (args[0].type != ARGT_STR) |
| 9256 | return 0; |
| 9257 | name_str = args[0].data.str.str; |
| 9258 | name_len = args[0].data.str.len; |
| 9259 | |
| 9260 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 9261 | occ = args[1].data.uint; |
| 9262 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9263 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9264 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9265 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9266 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9267 | /* search for header from the beginning */ |
| 9268 | ctx->idx = 0; |
| 9269 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9270 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9271 | /* no explicit occurrence and single fetch => last header by default */ |
| 9272 | occ = -1; |
| 9273 | |
| 9274 | if (!occ) |
| 9275 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9276 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9277 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9278 | smp->type = SMP_T_CSTR; |
| 9279 | smp->flags |= SMP_F_VOL_HDR; |
| 9280 | 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] | 9281 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9282 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9283 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9284 | return 0; |
| 9285 | } |
| 9286 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9287 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9288 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9289 | */ |
| 9290 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9291 | 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] | 9292 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9293 | { |
| 9294 | struct http_txn *txn = l7; |
| 9295 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9296 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9297 | 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] | 9298 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9299 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9300 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9301 | return 0; |
| 9302 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9303 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9304 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9305 | ctx.idx = 0; |
| 9306 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9307 | 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] | 9308 | cnt++; |
| 9309 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9310 | smp->type = SMP_T_UINT; |
| 9311 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9312 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9313 | return 1; |
| 9314 | } |
| 9315 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9316 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9317 | * takes a mandatory argument of type string and an optional one of type int |
| 9318 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9319 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9320 | */ |
| 9321 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9322 | 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] | 9323 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9324 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9325 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9326 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9327 | if (ret > 0) { |
| 9328 | smp->type = SMP_T_UINT; |
| 9329 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9330 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9331 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9332 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9333 | } |
| 9334 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9335 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9336 | * and an optional one of type int to designate a specific occurrence. |
| 9337 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9338 | */ |
| 9339 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9340 | 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] | 9341 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9342 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9343 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9344 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9345 | 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] | 9346 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 9347 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9348 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9349 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9350 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9351 | if (smp->data.str.len < temp->size - 1) { |
| 9352 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 9353 | temp->str[smp->data.str.len] = '\0'; |
| 9354 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 9355 | smp->type = SMP_T_IPV6; |
| 9356 | break; |
| 9357 | } |
| 9358 | } |
| 9359 | } |
| 9360 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9361 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9362 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9363 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9364 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9365 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9366 | } |
| 9367 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9368 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9369 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9370 | */ |
| 9371 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9372 | 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] | 9373 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9374 | { |
| 9375 | struct http_txn *txn = l7; |
| 9376 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9377 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9378 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9379 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9380 | 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] | 9381 | ptr = http_get_path(txn); |
| 9382 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9383 | return 0; |
| 9384 | |
| 9385 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9386 | smp->type = SMP_T_CSTR; |
| 9387 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9388 | |
| 9389 | while (ptr < end && *ptr != '?') |
| 9390 | ptr++; |
| 9391 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9392 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9393 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9394 | return 1; |
| 9395 | } |
| 9396 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9397 | /* This produces a concatenation of the first occurrence of the Host header |
| 9398 | * followed by the path component if it begins with a slash ('/'). This means |
| 9399 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 9400 | * If no Host header is found, then the path is returned as-is. The returned |
| 9401 | * value is stored in the trash so it does not need to be marked constant. |
| 9402 | */ |
| 9403 | static int |
| 9404 | 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] | 9405 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9406 | { |
| 9407 | struct http_txn *txn = l7; |
| 9408 | char *ptr, *end, *beg; |
| 9409 | struct hdr_ctx ctx; |
| 9410 | |
| 9411 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9412 | |
| 9413 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9414 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) || |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9415 | !ctx.vlen) |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9416 | return smp_fetch_path(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9417 | |
| 9418 | /* 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] | 9419 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9420 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 9421 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9422 | smp->data.str.len = ctx.vlen; |
| 9423 | |
| 9424 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9425 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9426 | beg = http_get_path(txn); |
| 9427 | if (!beg) |
| 9428 | beg = end; |
| 9429 | |
| 9430 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9431 | |
| 9432 | if (beg < ptr && *beg == '/') { |
| 9433 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 9434 | smp->data.str.len += ptr - beg; |
| 9435 | } |
| 9436 | |
| 9437 | smp->flags = SMP_F_VOL_1ST; |
| 9438 | return 1; |
| 9439 | } |
| 9440 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9441 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 9442 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 9443 | * This means that '*' will not be added, resulting in exactly the first Host |
| 9444 | * 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] | 9445 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 9446 | * 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] | 9447 | * high-traffic sites without having to store whole paths. |
| 9448 | */ |
| 9449 | static int |
| 9450 | 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] | 9451 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 9452 | { |
| 9453 | struct http_txn *txn = l7; |
| 9454 | struct hdr_ctx ctx; |
| 9455 | unsigned int hash = 0; |
| 9456 | char *ptr, *beg, *end; |
| 9457 | int len; |
| 9458 | |
| 9459 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9460 | |
| 9461 | ctx.idx = 0; |
| 9462 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 9463 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 9464 | ptr = ctx.line + ctx.val; |
| 9465 | len = ctx.vlen; |
| 9466 | while (len--) |
| 9467 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 9468 | } |
| 9469 | |
| 9470 | /* now retrieve the path */ |
| 9471 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 9472 | beg = http_get_path(txn); |
| 9473 | if (!beg) |
| 9474 | beg = end; |
| 9475 | |
| 9476 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 9477 | |
| 9478 | if (beg < ptr && *beg == '/') { |
| 9479 | while (beg < ptr) |
| 9480 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 9481 | } |
| 9482 | hash = full_hash(hash); |
| 9483 | |
| 9484 | smp->type = SMP_T_UINT; |
| 9485 | smp->data.uint = hash; |
| 9486 | smp->flags = SMP_F_VOL_1ST; |
| 9487 | return 1; |
| 9488 | } |
| 9489 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9490 | /* 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] | 9491 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 9492 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 9493 | * 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] | 9494 | * that in environments where IPv6 is insignificant, truncating the output to |
| 9495 | * 8 bytes would still work. |
| 9496 | */ |
| 9497 | static int |
| 9498 | 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] | 9499 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9500 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9501 | struct chunk *temp; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9502 | |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9503 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9504 | return 0; |
| 9505 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9506 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 9507 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 9508 | temp->len += sizeof(smp->data.uint); |
| 9509 | |
| 9510 | switch (l4->si[0].conn->addr.from.ss_family) { |
| 9511 | case AF_INET: |
| 9512 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4); |
| 9513 | temp->len += 4; |
| 9514 | break; |
| 9515 | case AF_INET6: |
| 9516 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16); |
| 9517 | temp->len += 16; |
| 9518 | break; |
| 9519 | default: |
| 9520 | return 0; |
| 9521 | } |
| 9522 | |
| 9523 | smp->data.str = *temp; |
| 9524 | smp->type = SMP_T_BIN; |
| 9525 | return 1; |
| 9526 | } |
| 9527 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9528 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9529 | 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] | 9530 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9531 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9532 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 9533 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 9534 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9535 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9536 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9537 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9538 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9539 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 9540 | return 1; |
| 9541 | } |
| 9542 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9543 | /* return a valid test if the current request is the first one on the connection */ |
| 9544 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9545 | 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] | 9546 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9547 | { |
| 9548 | if (!s) |
| 9549 | return 0; |
| 9550 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9551 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9552 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9553 | return 1; |
| 9554 | } |
| 9555 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9556 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9557 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9558 | 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] | 9559 | const struct arg *args, struct sample *smp, const char *kw) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9560 | { |
| 9561 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9562 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9563 | return 0; |
| 9564 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9565 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9566 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9567 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9568 | return 0; |
| 9569 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9570 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9571 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9572 | return 1; |
| 9573 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9574 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9575 | /* Accepts exactly 1 argument of type userlist */ |
| 9576 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9577 | 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] | 9578 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9579 | { |
| 9580 | |
| 9581 | if (!args || args->type != ARGT_USR) |
| 9582 | return 0; |
| 9583 | |
| 9584 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9585 | |
| 9586 | if (!get_http_auth(l4)) |
| 9587 | return 0; |
| 9588 | |
| 9589 | /* acl_match_auth() will need several information at once */ |
| 9590 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 9591 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 9592 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 9593 | |
| 9594 | /* if the user does not belong to the userlist or has a wrong password, |
| 9595 | * report that it unconditionally does not match. Otherwise we return |
| 9596 | * a non-zero integer which will be ignored anyway since all the params |
| 9597 | * that acl_match_auth() will use are in test->ctx.a[0,1,2]. |
| 9598 | */ |
| 9599 | smp->type = SMP_T_BOOL; |
| 9600 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 9601 | if (smp->data.uint) |
| 9602 | smp->type = SMP_T_UINT; |
| 9603 | |
| 9604 | return 1; |
| 9605 | } |
| 9606 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9607 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9608 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9609 | * the cookie value is returned into *value and *value_l, and the function |
| 9610 | * returns a pointer to the next pointer to search from if the value was found. |
| 9611 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9612 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9613 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9614 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9615 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9616 | * list is expected. |
| 9617 | */ |
| 9618 | static char * |
| 9619 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9620 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9621 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9622 | { |
| 9623 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9624 | char *next; |
| 9625 | |
| 9626 | /* we search at least a cookie name followed by an equal, and more |
| 9627 | * generally something like this : |
| 9628 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9629 | */ |
| 9630 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9631 | /* Iterate through all cookies on this line */ |
| 9632 | |
| 9633 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9634 | att_beg++; |
| 9635 | |
| 9636 | /* find att_end : this is the first character after the last non |
| 9637 | * space before the equal. It may be equal to hdr_end. |
| 9638 | */ |
| 9639 | equal = att_end = att_beg; |
| 9640 | |
| 9641 | while (equal < hdr_end) { |
| 9642 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9643 | break; |
| 9644 | if (http_is_spht[(unsigned char)*equal++]) |
| 9645 | continue; |
| 9646 | att_end = equal; |
| 9647 | } |
| 9648 | |
| 9649 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9650 | * is between <att_beg> and <equal>, both may be identical. |
| 9651 | */ |
| 9652 | |
| 9653 | /* look for end of cookie if there is an equal sign */ |
| 9654 | if (equal < hdr_end && *equal == '=') { |
| 9655 | /* look for the beginning of the value */ |
| 9656 | val_beg = equal + 1; |
| 9657 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9658 | val_beg++; |
| 9659 | |
| 9660 | /* find the end of the value, respecting quotes */ |
| 9661 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9662 | |
| 9663 | /* make val_end point to the first white space or delimitor after the value */ |
| 9664 | val_end = next; |
| 9665 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9666 | val_end--; |
| 9667 | } else { |
| 9668 | val_beg = val_end = next = equal; |
| 9669 | } |
| 9670 | |
| 9671 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9672 | * they will automatically be removed if a header before them is removed, |
| 9673 | * since they're supposed to be linked together. |
| 9674 | */ |
| 9675 | if (*att_beg == '$') |
| 9676 | continue; |
| 9677 | |
| 9678 | /* Ignore cookies with no equal sign */ |
| 9679 | if (equal == next) |
| 9680 | continue; |
| 9681 | |
| 9682 | /* Now we have the cookie name between att_beg and att_end, and |
| 9683 | * its value between val_beg and val_end. |
| 9684 | */ |
| 9685 | |
| 9686 | if (att_end - att_beg == cookie_name_l && |
| 9687 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9688 | /* let's return this value and indicate where to go on from */ |
| 9689 | *value = val_beg; |
| 9690 | *value_l = val_end - val_beg; |
| 9691 | return next + 1; |
| 9692 | } |
| 9693 | |
| 9694 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9695 | if (!list) |
| 9696 | break; |
| 9697 | } |
| 9698 | |
| 9699 | return NULL; |
| 9700 | } |
| 9701 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9702 | /* 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] | 9703 | * 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] | 9704 | * 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] | 9705 | * 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] | 9706 | * 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] | 9707 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 9708 | * that no iterating is desired, then only last value is fetched if any. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9709 | */ |
| 9710 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9711 | 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] | 9712 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9713 | { |
| 9714 | struct http_txn *txn = l7; |
| 9715 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9716 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9717 | const struct http_msg *msg; |
| 9718 | const char *hdr_name; |
| 9719 | int hdr_name_len; |
| 9720 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9721 | int occ = 0; |
| 9722 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9723 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9724 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9725 | return 0; |
| 9726 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9727 | if (!ctx) { |
| 9728 | /* first call */ |
| 9729 | ctx = &static_hdr_ctx; |
| 9730 | ctx->idx = 0; |
| 9731 | smp->ctx.a[2] = ctx; |
| 9732 | } |
| 9733 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9734 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9735 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9736 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9737 | msg = &txn->req; |
| 9738 | hdr_name = "Cookie"; |
| 9739 | hdr_name_len = 6; |
| 9740 | } else { |
| 9741 | msg = &txn->rsp; |
| 9742 | hdr_name = "Set-Cookie"; |
| 9743 | hdr_name_len = 10; |
| 9744 | } |
| 9745 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9746 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9747 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 9748 | occ = -1; |
| 9749 | |
| 9750 | /* OK so basically here, either we want only one value and it's the |
| 9751 | * last one, or we want to iterate over all of them and we fetch the |
| 9752 | * next one. |
| 9753 | */ |
| 9754 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9755 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9756 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9757 | /* search for the header from the beginning, we must first initialize |
| 9758 | * the search parameters. |
| 9759 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9760 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9761 | ctx->idx = 0; |
| 9762 | } |
| 9763 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9764 | smp->flags |= SMP_F_VOL_HDR; |
| 9765 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9766 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9767 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 9768 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9769 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 9770 | goto out; |
| 9771 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9772 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9773 | continue; |
| 9774 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9775 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 9776 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9777 | } |
| 9778 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9779 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9780 | 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] | 9781 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9782 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9783 | &smp->data.str.str, |
| 9784 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9785 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9786 | found = 1; |
| 9787 | if (occ >= 0) { |
| 9788 | /* one value was returned into smp->data.str.{str,len} */ |
| 9789 | smp->flags |= SMP_F_NOT_LAST; |
| 9790 | return 1; |
| 9791 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9792 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9793 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9794 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9795 | /* all cookie headers and values were scanned. If we're looking for the |
| 9796 | * last occurrence, we may return it now. |
| 9797 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9798 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9799 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9800 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9801 | } |
| 9802 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9803 | /* 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] | 9804 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9805 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9806 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9807 | */ |
| 9808 | static int |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 9809 | 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] | 9810 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9811 | { |
| 9812 | struct http_txn *txn = l7; |
| 9813 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9814 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9815 | const struct http_msg *msg; |
| 9816 | const char *hdr_name; |
| 9817 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9818 | int cnt; |
| 9819 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9820 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9821 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9822 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9823 | return 0; |
| 9824 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9825 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9826 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9827 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9828 | msg = &txn->req; |
| 9829 | hdr_name = "Cookie"; |
| 9830 | hdr_name_len = 6; |
| 9831 | } else { |
| 9832 | msg = &txn->rsp; |
| 9833 | hdr_name = "Set-Cookie"; |
| 9834 | hdr_name_len = 10; |
| 9835 | } |
| 9836 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9837 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 9838 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9839 | ctx.idx = 0; |
| 9840 | cnt = 0; |
| 9841 | |
| 9842 | while (1) { |
| 9843 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 9844 | if (!val_beg) { |
| 9845 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 9846 | break; |
| 9847 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9848 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9849 | continue; |
| 9850 | |
| 9851 | val_beg = ctx.line + ctx.val; |
| 9852 | val_end = val_beg + ctx.vlen; |
| 9853 | } |
| 9854 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9855 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9856 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9857 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9858 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9859 | &smp->data.str.str, |
| 9860 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9861 | cnt++; |
| 9862 | } |
| 9863 | } |
| 9864 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9865 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9866 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9867 | return 1; |
| 9868 | } |
| 9869 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9870 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 9871 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 9872 | */ |
| 9873 | static int |
| 9874 | 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] | 9875 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9876 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 9877 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9878 | |
| 9879 | if (ret > 0) { |
| 9880 | smp->type = SMP_T_UINT; |
| 9881 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9882 | } |
| 9883 | |
| 9884 | return ret; |
| 9885 | } |
| 9886 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9887 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9888 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9889 | /************************************************************************/ |
| 9890 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9891 | /* |
| 9892 | * Given a path string and its length, find the position of beginning of the |
| 9893 | * query string. Returns NULL if no query string is found in the path. |
| 9894 | * |
| 9895 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 9896 | * |
| 9897 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 9898 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9899 | 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] | 9900 | { |
| 9901 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 9902 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9903 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9904 | return p ? p + 1 : NULL; |
| 9905 | } |
| 9906 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9907 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9908 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9909 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9910 | } |
| 9911 | |
| 9912 | /* |
| 9913 | * Given a url parameter, find the starting position of the first occurence, |
| 9914 | * or NULL if the parameter is not found. |
| 9915 | * |
| 9916 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 9917 | * the function will return query_string+8. |
| 9918 | */ |
| 9919 | static char* |
| 9920 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9921 | char* url_param_name, size_t url_param_name_l, |
| 9922 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9923 | { |
| 9924 | char *pos, *last; |
| 9925 | |
| 9926 | pos = query_string; |
| 9927 | last = query_string + query_string_l - url_param_name_l - 1; |
| 9928 | |
| 9929 | while (pos <= last) { |
| 9930 | if (pos[url_param_name_l] == '=') { |
| 9931 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 9932 | return pos; |
| 9933 | pos += url_param_name_l + 1; |
| 9934 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9935 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9936 | pos++; |
| 9937 | pos++; |
| 9938 | } |
| 9939 | return NULL; |
| 9940 | } |
| 9941 | |
| 9942 | /* |
| 9943 | * Given a url parameter name, returns its value and size into *value and |
| 9944 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 9945 | * zero is returned and value/value_l are not touched. |
| 9946 | */ |
| 9947 | static int |
| 9948 | find_url_param_value(char* path, size_t path_l, |
| 9949 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9950 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9951 | { |
| 9952 | char *query_string, *qs_end; |
| 9953 | char *arg_start; |
| 9954 | char *value_start, *value_end; |
| 9955 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9956 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9957 | if (!query_string) |
| 9958 | return 0; |
| 9959 | |
| 9960 | qs_end = path + path_l; |
| 9961 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9962 | url_param_name, url_param_name_l, |
| 9963 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9964 | if (!arg_start) |
| 9965 | return 0; |
| 9966 | |
| 9967 | value_start = arg_start + url_param_name_l + 1; |
| 9968 | value_end = value_start; |
| 9969 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9970 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9971 | value_end++; |
| 9972 | |
| 9973 | *value = value_start; |
| 9974 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 9975 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9976 | } |
| 9977 | |
| 9978 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9979 | 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] | 9980 | const struct arg *args, struct sample *smp, const char *kw) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9981 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9982 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9983 | struct http_txn *txn = l7; |
| 9984 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9985 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9986 | if (!args || args[0].type != ARGT_STR || |
| 9987 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9988 | return 0; |
| 9989 | |
| 9990 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9991 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9992 | if (args[1].type) |
| 9993 | delim = *args[1].data.str.str; |
| 9994 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9995 | 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] | 9996 | args->data.str.str, args->data.str.len, |
| 9997 | &smp->data.str.str, &smp->data.str.len, |
| 9998 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9999 | return 0; |
| 10000 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 10001 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10002 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10003 | return 1; |
| 10004 | } |
| 10005 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10006 | /* Return the signed integer value for the specified url parameter (see url_param |
| 10007 | * above). |
| 10008 | */ |
| 10009 | static int |
| 10010 | 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] | 10011 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 10012 | { |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 10013 | 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] | 10014 | |
| 10015 | if (ret > 0) { |
| 10016 | smp->type = SMP_T_UINT; |
| 10017 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 10018 | } |
| 10019 | |
| 10020 | return ret; |
| 10021 | } |
| 10022 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10023 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10024 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10025 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10026 | * entry. If no Host header is found, then the path is used. The resulting value |
| 10027 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 10028 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 10029 | * high-traffic sites without having to store whole paths. |
| 10030 | * this differs from the base32 functions in that it includes the url parameters |
| 10031 | * as well as the path |
| 10032 | */ |
| 10033 | static int |
| 10034 | 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] | 10035 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10036 | { |
| 10037 | struct http_txn *txn = l7; |
| 10038 | struct hdr_ctx ctx; |
| 10039 | unsigned int hash = 0; |
| 10040 | char *ptr, *beg, *end; |
| 10041 | int len; |
| 10042 | |
| 10043 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10044 | |
| 10045 | ctx.idx = 0; |
| 10046 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 10047 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10048 | ptr = ctx.line + ctx.val; |
| 10049 | len = ctx.vlen; |
| 10050 | while (len--) |
| 10051 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10052 | } |
| 10053 | |
| 10054 | /* now retrieve the path */ |
| 10055 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 10056 | beg = http_get_path(txn); |
| 10057 | if (!beg) |
| 10058 | beg = end; |
| 10059 | |
| 10060 | for (ptr = beg; ptr < end ; ptr++); |
| 10061 | |
| 10062 | if (beg < ptr && *beg == '/') { |
| 10063 | while (beg < ptr) |
| 10064 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10065 | } |
| 10066 | hash = full_hash(hash); |
| 10067 | |
| 10068 | smp->type = SMP_T_UINT; |
| 10069 | smp->data.uint = hash; |
| 10070 | smp->flags = SMP_F_VOL_1ST; |
| 10071 | return 1; |
| 10072 | } |
| 10073 | |
| 10074 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 10075 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10076 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 10077 | * on the source address length. The URL hash is stored before the address so |
| 10078 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10079 | * 8 bytes would still work. |
| 10080 | */ |
| 10081 | static int |
| 10082 | 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] | 10083 | const struct arg *args, struct sample *smp, const char *kw) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10084 | { |
| 10085 | struct chunk *temp; |
| 10086 | |
Willy Tarreau | e155ec2 | 2013-11-18 18:33:22 +0100 | [diff] [blame] | 10087 | if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10088 | return 0; |
| 10089 | |
| 10090 | temp = get_trash_chunk(); |
| 10091 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 10092 | temp->len += sizeof(smp->data.uint); |
| 10093 | |
| 10094 | switch (l4->si[0].conn->addr.from.ss_family) { |
| 10095 | case AF_INET: |
| 10096 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4); |
| 10097 | temp->len += 4; |
| 10098 | break; |
| 10099 | case AF_INET6: |
| 10100 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16); |
| 10101 | temp->len += 16; |
| 10102 | break; |
| 10103 | default: |
| 10104 | return 0; |
| 10105 | } |
| 10106 | |
| 10107 | smp->data.str = *temp; |
| 10108 | smp->type = SMP_T_BIN; |
| 10109 | return 1; |
| 10110 | } |
| 10111 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10112 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 10113 | * keyword. These keywords support an optional positive or negative occurrence |
| 10114 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 10115 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 10116 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 10117 | * error message in case of error, that the caller is responsible for freeing. |
| 10118 | * The initial location must either be freeable or NULL. |
| 10119 | */ |
| 10120 | static int val_hdr(struct arg *arg, char **err_msg) |
| 10121 | { |
| 10122 | 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] | 10123 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10124 | return 0; |
| 10125 | } |
| 10126 | return 1; |
| 10127 | } |
| 10128 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10129 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 10130 | * adds an optional offset found in args[0] and emits a string representing |
| 10131 | * the date in RFC-1123/5322 format. |
| 10132 | */ |
| 10133 | static int sample_conv_http_date(const struct arg *args, struct sample *smp) |
| 10134 | { |
| 10135 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 10136 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 10137 | struct chunk *temp; |
| 10138 | struct tm *tm; |
| 10139 | time_t curr_date = smp->data.uint; |
| 10140 | |
| 10141 | /* add offset */ |
| 10142 | if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) |
| 10143 | curr_date += args[0].data.sint; |
| 10144 | |
| 10145 | tm = gmtime(&curr_date); |
| 10146 | |
| 10147 | temp = get_trash_chunk(); |
| 10148 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 10149 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 10150 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 10151 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 10152 | |
| 10153 | smp->data.str = *temp; |
| 10154 | smp->type = SMP_T_STR; |
| 10155 | return 1; |
| 10156 | } |
| 10157 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10158 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10159 | /* All supported ACL keywords must be declared here. */ |
| 10160 | /************************************************************************/ |
| 10161 | |
| 10162 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10163 | * Please take care of keeping this list alphabetically sorted. |
| 10164 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10165 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10166 | { "base", "base", acl_parse_str, acl_match_str }, |
| 10167 | { "base_beg", "base", acl_parse_str, acl_match_beg }, |
| 10168 | { "base_dir", "base", acl_parse_str, acl_match_dir }, |
| 10169 | { "base_dom", "base", acl_parse_str, acl_match_dom }, |
| 10170 | { "base_end", "base", acl_parse_str, acl_match_end }, |
| 10171 | { "base_len", "base", acl_parse_int, acl_match_len }, |
| 10172 | { "base_reg", "base", acl_parse_reg, acl_match_reg }, |
| 10173 | { "base_sub", "base", acl_parse_str, acl_match_sub }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10174 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10175 | { "cook", "req.cook", acl_parse_str, acl_match_str }, |
| 10176 | { "cook_beg", "req.cook", acl_parse_str, acl_match_beg }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10177 | { "cook_dir", "req.cook", acl_parse_str, acl_match_dir }, |
| 10178 | { "cook_dom", "req.cook", acl_parse_str, acl_match_dom }, |
| 10179 | { "cook_end", "req.cook", acl_parse_str, acl_match_end }, |
| 10180 | { "cook_len", "req.cook", acl_parse_int, acl_match_len }, |
| 10181 | { "cook_reg", "req.cook", acl_parse_reg, acl_match_reg }, |
| 10182 | { "cook_sub", "req.cook", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10183 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10184 | { "hdr", "req.hdr", acl_parse_str, acl_match_str }, |
| 10185 | { "hdr_beg", "req.hdr", acl_parse_str, acl_match_beg }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10186 | { "hdr_dir", "req.hdr", acl_parse_str, acl_match_dir }, |
| 10187 | { "hdr_dom", "req.hdr", acl_parse_str, acl_match_dom }, |
| 10188 | { "hdr_end", "req.hdr", acl_parse_str, acl_match_end }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10189 | { "hdr_len", "req.hdr", acl_parse_int, acl_match_len }, |
| 10190 | { "hdr_reg", "req.hdr", acl_parse_reg, acl_match_reg }, |
| 10191 | { "hdr_sub", "req.hdr", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10192 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10193 | { "http_auth_group", NULL, acl_parse_strcat, acl_match_auth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10194 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10195 | { "method", NULL, acl_parse_meth, acl_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10196 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10197 | { "path", "path", acl_parse_str, acl_match_str }, |
| 10198 | { "path_beg", "path", acl_parse_str, acl_match_beg }, |
| 10199 | { "path_dir", "path", acl_parse_str, acl_match_dir }, |
| 10200 | { "path_dom", "path", acl_parse_str, acl_match_dom }, |
| 10201 | { "path_end", "path", acl_parse_str, acl_match_end }, |
| 10202 | { "path_len", "path", acl_parse_int, acl_match_len }, |
| 10203 | { "path_reg", "path", acl_parse_reg, acl_match_reg }, |
| 10204 | { "path_sub", "path", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10205 | |
Willy Tarreau | ff5afcc | 2013-03-31 18:49:18 +0200 | [diff] [blame] | 10206 | { "req_ver", "req.ver", acl_parse_str, acl_match_str }, |
| 10207 | { "resp_ver", "res.ver", acl_parse_str, acl_match_str }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10208 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10209 | { "scook", "res.cook", acl_parse_str, acl_match_str }, |
| 10210 | { "scook_beg", "res.cook", acl_parse_str, acl_match_beg }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10211 | { "scook_dir", "res.cook", acl_parse_str, acl_match_dir }, |
| 10212 | { "scook_dom", "res.cook", acl_parse_str, acl_match_dom }, |
| 10213 | { "scook_end", "res.cook", acl_parse_str, acl_match_end }, |
| 10214 | { "scook_len", "res.cook", acl_parse_int, acl_match_len }, |
| 10215 | { "scook_reg", "res.cook", acl_parse_reg, acl_match_reg }, |
| 10216 | { "scook_sub", "res.cook", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10217 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10218 | { "shdr", "res.hdr", acl_parse_str, acl_match_str }, |
| 10219 | { "shdr_beg", "res.hdr", acl_parse_str, acl_match_beg }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10220 | { "shdr_dir", "res.hdr", acl_parse_str, acl_match_dir }, |
| 10221 | { "shdr_dom", "res.hdr", acl_parse_str, acl_match_dom }, |
| 10222 | { "shdr_end", "res.hdr", acl_parse_str, acl_match_end }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10223 | { "shdr_len", "res.hdr", acl_parse_int, acl_match_len }, |
| 10224 | { "shdr_reg", "res.hdr", acl_parse_reg, acl_match_reg }, |
| 10225 | { "shdr_sub", "res.hdr", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10226 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10227 | { "url", "url", acl_parse_str, acl_match_str }, |
| 10228 | { "url_beg", "url", acl_parse_str, acl_match_beg }, |
| 10229 | { "url_dir", "url", acl_parse_str, acl_match_dir }, |
| 10230 | { "url_dom", "url", acl_parse_str, acl_match_dom }, |
| 10231 | { "url_end", "url", acl_parse_str, acl_match_end }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10232 | { "url_len", "url", acl_parse_int, acl_match_len }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10233 | { "url_reg", "url", acl_parse_reg, acl_match_reg }, |
| 10234 | { "url_sub", "url", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10235 | |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10236 | { "urlp", "urlp", acl_parse_str, acl_match_str }, |
| 10237 | { "urlp_beg", "urlp", acl_parse_str, acl_match_beg }, |
| 10238 | { "urlp_dir", "urlp", acl_parse_str, acl_match_dir }, |
| 10239 | { "urlp_dom", "urlp", acl_parse_str, acl_match_dom }, |
| 10240 | { "urlp_end", "urlp", acl_parse_str, acl_match_end }, |
Willy Tarreau | d86e29d | 2013-03-25 08:21:05 +0100 | [diff] [blame] | 10241 | { "urlp_len", "urlp", acl_parse_int, acl_match_len }, |
| 10242 | { "urlp_reg", "urlp", acl_parse_reg, acl_match_reg }, |
| 10243 | { "urlp_sub", "urlp", acl_parse_str, acl_match_sub }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10244 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 10245 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 10246 | }}; |
| 10247 | |
| 10248 | /************************************************************************/ |
| 10249 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10250 | /************************************************************************/ |
| 10251 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10252 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10253 | { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10254 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10255 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 10256 | |
| 10257 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 10258 | * are only here to match the ACL's name, are request-only and are used |
| 10259 | * for ACL compatibility only. |
| 10260 | */ |
| 10261 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10262 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 10263 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10264 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10265 | |
| 10266 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 10267 | * only here to match the ACL's name, are request-only and are used for |
| 10268 | * ACL compatibility only. |
| 10269 | */ |
| 10270 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 10271 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10272 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10273 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10274 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 10275 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 10276 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10277 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 10278 | { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP }, |
| 10279 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10280 | |
| 10281 | /* HTTP protocol on the request path */ |
| 10282 | { "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] | 10283 | { "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] | 10284 | |
| 10285 | /* HTTP version on the request path */ |
| 10286 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10287 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10288 | |
| 10289 | /* HTTP version on the response path */ |
| 10290 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10291 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10292 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10293 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 10294 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10295 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10296 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10297 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10298 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10299 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10300 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10301 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10302 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10303 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10304 | |
| 10305 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 10306 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10307 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10308 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10309 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10310 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10311 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 10312 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10313 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10314 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10315 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10316 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 10317 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 10318 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10319 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10320 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10321 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */ |
| 10322 | |
| 10323 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 10324 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV }, |
| 10325 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10326 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 10327 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV }, |
| 10328 | |
| 10329 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP }, |
| 10330 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10331 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10332 | { "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] | 10333 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 10334 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10335 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10336 | { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 10337 | { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 10338 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10339 | }}; |
| 10340 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10341 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10342 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 10343 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 10344 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR }, |
| 10345 | { NULL, NULL, 0, 0, 0 }, |
| 10346 | }}; |
| 10347 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10348 | __attribute__((constructor)) |
| 10349 | static void __http_protocol_init(void) |
| 10350 | { |
| 10351 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10352 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 10353 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10354 | } |
| 10355 | |
| 10356 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 10357 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 10358 | * Local variables: |
| 10359 | * c-indent-level: 8 |
| 10360 | * c-basic-offset: 8 |
| 10361 | * End: |
| 10362 | */ |