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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 44 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 45 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 46 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 49 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 50 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 52 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 54 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 55 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 59 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 62 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 63 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/task.h> |
| 65 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 66 | const char HTTP_100[] = |
| 67 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 68 | |
| 69 | const struct chunk http_100_chunk = { |
| 70 | .str = (char *)&HTTP_100, |
| 71 | .len = sizeof(HTTP_100)-1 |
| 72 | }; |
| 73 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 74 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 75 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 76 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 77 | "Cache-Control: no-cache\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 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 95 | const char *HTTP_401_fmt = |
| 96 | "HTTP/1.0 401 Unauthorized\r\n" |
| 97 | "Cache-Control: no-cache\r\n" |
| 98 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 99 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 101 | "\r\n" |
| 102 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 103 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 104 | const char *HTTP_407_fmt = |
| 105 | "HTTP/1.0 407 Unauthorized\r\n" |
| 106 | "Cache-Control: no-cache\r\n" |
| 107 | "Connection: close\r\n" |
| 108 | "Content-Type: text/html\r\n" |
| 109 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 110 | "\r\n" |
| 111 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 112 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 113 | |
| 114 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 115 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 126 | [HTTP_ERR_200] = |
| 127 | "HTTP/1.0 200 OK\r\n" |
| 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 133 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 134 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_403] = |
| 143 | "HTTP/1.0 403 Forbidden\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_408] = |
| 151 | "HTTP/1.0 408 Request Time-out\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_500] = |
| 159 | "HTTP/1.0 500 Server Error\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_502] = |
| 167 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_503] = |
| 175 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 181 | |
| 182 | [HTTP_ERR_504] = |
| 183 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 184 | "Cache-Control: no-cache\r\n" |
| 185 | "Connection: close\r\n" |
| 186 | "Content-Type: text/html\r\n" |
| 187 | "\r\n" |
| 188 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 189 | |
| 190 | }; |
| 191 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 192 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 193 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 194 | [STAT_STATUS_DENY] = "DENY", |
| 195 | [STAT_STATUS_DONE] = "DONE", |
| 196 | [STAT_STATUS_ERRP] = "ERRP", |
| 197 | [STAT_STATUS_EXCD] = "EXCD", |
| 198 | [STAT_STATUS_NONE] = "NONE", |
| 199 | [STAT_STATUS_PART] = "PART", |
| 200 | [STAT_STATUS_UNKN] = "UNKN", |
| 201 | }; |
| 202 | |
| 203 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 204 | /* We must put the messages here since GCC cannot initialize consts depending |
| 205 | * on strlen(). |
| 206 | */ |
| 207 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 208 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 209 | #define FD_SETS_ARE_BITFIELDS |
| 210 | #ifdef FD_SETS_ARE_BITFIELDS |
| 211 | /* |
| 212 | * This map is used with all the FD_* macros to check whether a particular bit |
| 213 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 214 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 215 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 216 | * exclusively to the macros. |
| 217 | */ |
| 218 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 219 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 220 | |
| 221 | #else |
| 222 | #error "Check if your OS uses bitfields for fd_sets" |
| 223 | #endif |
| 224 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 225 | void init_proto_http() |
| 226 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | int i; |
| 228 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 229 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 231 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 232 | if (!http_err_msgs[msg]) { |
| 233 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 234 | abort(); |
| 235 | } |
| 236 | |
| 237 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 238 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 239 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 240 | |
| 241 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 242 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 243 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 244 | * printable characters above. |
| 245 | */ |
| 246 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 247 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 248 | for (i = 0; i < 32; i++) { |
| 249 | FD_SET(i, hdr_encode_map); |
| 250 | FD_SET(i, url_encode_map); |
| 251 | } |
| 252 | for (i = 127; i < 256; i++) { |
| 253 | FD_SET(i, hdr_encode_map); |
| 254 | FD_SET(i, url_encode_map); |
| 255 | } |
| 256 | |
| 257 | tmp = "\"#{|}"; |
| 258 | while (*tmp) { |
| 259 | FD_SET(*tmp, hdr_encode_map); |
| 260 | tmp++; |
| 261 | } |
| 262 | |
| 263 | tmp = "\"#"; |
| 264 | while (*tmp) { |
| 265 | FD_SET(*tmp, url_encode_map); |
| 266 | tmp++; |
| 267 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 268 | |
| 269 | /* memory allocations */ |
| 270 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 271 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 272 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 273 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 274 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | /* |
| 276 | * We have 26 list of methods (1 per first letter), each of which can have |
| 277 | * up to 3 entries (2 valid, 1 null). |
| 278 | */ |
| 279 | struct http_method_desc { |
| 280 | http_meth_t meth; |
| 281 | int len; |
| 282 | const char text[8]; |
| 283 | }; |
| 284 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 285 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 286 | ['C' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 288 | }, |
| 289 | ['D' - 'A'] = { |
| 290 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 291 | }, |
| 292 | ['G' - 'A'] = { |
| 293 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 294 | }, |
| 295 | ['H' - 'A'] = { |
| 296 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 297 | }, |
| 298 | ['P' - 'A'] = { |
| 299 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 300 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 301 | }, |
| 302 | ['T' - 'A'] = { |
| 303 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 304 | }, |
| 305 | /* rest is empty like this : |
| 306 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 307 | */ |
| 308 | }; |
| 309 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 310 | /* 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] | 311 | * 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] | 312 | * RFC2616 for those chars. |
| 313 | */ |
| 314 | |
| 315 | const char http_is_spht[256] = { |
| 316 | [' '] = 1, ['\t'] = 1, |
| 317 | }; |
| 318 | |
| 319 | const char http_is_crlf[256] = { |
| 320 | ['\r'] = 1, ['\n'] = 1, |
| 321 | }; |
| 322 | |
| 323 | const char http_is_lws[256] = { |
| 324 | [' '] = 1, ['\t'] = 1, |
| 325 | ['\r'] = 1, ['\n'] = 1, |
| 326 | }; |
| 327 | |
| 328 | const char http_is_sep[256] = { |
| 329 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 330 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 331 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 332 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 333 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 334 | }; |
| 335 | |
| 336 | const char http_is_ctl[256] = { |
| 337 | [0 ... 31] = 1, |
| 338 | [127] = 1, |
| 339 | }; |
| 340 | |
| 341 | /* |
| 342 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 343 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 344 | * them correctly. Instead, define every non-CTL char's status. |
| 345 | */ |
| 346 | const char http_is_token[256] = { |
| 347 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 348 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 349 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 350 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 351 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 352 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 353 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 354 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 355 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 356 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 357 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 358 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 359 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 360 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 361 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 362 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 363 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 364 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 365 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 366 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 367 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 368 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 369 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 370 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 371 | }; |
| 372 | |
| 373 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 374 | /* |
| 375 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 376 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 377 | */ |
| 378 | const char http_is_ver_token[256] = { |
| 379 | ['.'] = 1, ['/'] = 1, |
| 380 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 381 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 382 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 383 | }; |
| 384 | |
| 385 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 386 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 387 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 388 | */ |
| 389 | #if defined(DEBUG_FSM) |
| 390 | static void http_silent_debug(int line, struct session *s) |
| 391 | { |
| 392 | int size = 0; |
| 393 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 394 | "[%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", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 395 | line, |
| 396 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 397 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 398 | write(-1, trash, size); |
| 399 | size = 0; |
| 400 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 401 | " %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", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 402 | line, |
| 403 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 404 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 405 | |
| 406 | write(-1, trash, size); |
| 407 | } |
| 408 | #else |
| 409 | #define http_silent_debug(l,s) do { } while (0) |
| 410 | #endif |
| 411 | |
| 412 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 413 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 414 | * 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] | 415 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 416 | * of headers is automatically adjusted. The number of bytes added is returned |
| 417 | * on success, otherwise <0 is returned indicating an error. |
| 418 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 419 | 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] | 420 | { |
| 421 | int bytes, len; |
| 422 | |
| 423 | len = strlen(text); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 424 | bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 425 | if (!bytes) |
| 426 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 427 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 428 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 429 | } |
| 430 | |
| 431 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 432 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 433 | * 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] | 434 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 435 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 436 | * of headers is automatically adjusted. The number of bytes added is returned |
| 437 | * on success, otherwise <0 is returned indicating an error. |
| 438 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 439 | int http_header_add_tail2(struct http_msg *msg, |
| 440 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 441 | { |
| 442 | int bytes; |
| 443 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 444 | bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 445 | if (!bytes) |
| 446 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 447 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 448 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 449 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 450 | |
| 451 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 452 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 453 | * If so, returns the position of the first non-space character relative to |
| 454 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 455 | * to return a pointer to the place after the first space. Returns 0 if the |
| 456 | * header name does not match. Checks are case-insensitive. |
| 457 | */ |
| 458 | int http_header_match2(const char *hdr, const char *end, |
| 459 | const char *name, int len) |
| 460 | { |
| 461 | const char *val; |
| 462 | |
| 463 | if (hdr + len >= end) |
| 464 | return 0; |
| 465 | if (hdr[len] != ':') |
| 466 | return 0; |
| 467 | if (strncasecmp(hdr, name, len) != 0) |
| 468 | return 0; |
| 469 | val = hdr + len + 1; |
| 470 | while (val < end && HTTP_IS_SPHT(*val)) |
| 471 | val++; |
| 472 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 473 | return len + 2; /* we may replace starting from second space */ |
| 474 | return val - hdr; |
| 475 | } |
| 476 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 477 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 478 | * par 2.2 for more information. Note that it requires a valid header to return |
| 479 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 480 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 481 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 482 | { |
| 483 | int quoted, qdpair; |
| 484 | |
| 485 | quoted = qdpair = 0; |
| 486 | for (; s < e; s++) { |
| 487 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 488 | else if (quoted) { |
| 489 | if (*s == '\\') qdpair = 1; |
| 490 | else if (*s == '"') quoted = 0; |
| 491 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 492 | else if (*s == '"') quoted = 1; |
| 493 | else if (*s == ',') return s; |
| 494 | } |
| 495 | return s; |
| 496 | } |
| 497 | |
| 498 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 499 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 500 | * structure holds everything necessary to use the header and find next |
| 501 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 502 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 503 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 504 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 505 | * is NULL when searching for a new values of a header, the current header is |
| 506 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | */ |
| 508 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 509 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | struct hdr_ctx *ctx) |
| 511 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 512 | char *eol, *sov; |
| 513 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 515 | cur_idx = ctx->idx; |
| 516 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 517 | /* We have previously returned a value, let's search |
| 518 | * another one on the same line. |
| 519 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 520 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 521 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 522 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 523 | eol = sol + idx->v[cur_idx].len; |
| 524 | |
| 525 | if (sov >= eol) |
| 526 | /* no more values in this header */ |
| 527 | goto next_hdr; |
| 528 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 529 | /* values remaining for this header, skip the comma but save it |
| 530 | * for later use (eg: for header deletion). |
| 531 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 532 | sov++; |
| 533 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 534 | sov++; |
| 535 | |
| 536 | goto return_hdr; |
| 537 | } |
| 538 | |
| 539 | /* first request for this header */ |
| 540 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 541 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 542 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 543 | while (cur_idx) { |
| 544 | eol = sol + idx->v[cur_idx].len; |
| 545 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 546 | if (len == 0) { |
| 547 | /* No argument was passed, we want any header. |
| 548 | * To achieve this, we simply build a fake request. */ |
| 549 | while (sol + len < eol && sol[len] != ':') |
| 550 | len++; |
| 551 | name = sol; |
| 552 | } |
| 553 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 554 | if ((len < eol - sol) && |
| 555 | (sol[len] == ':') && |
| 556 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 557 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 558 | sov = sol + len + 1; |
| 559 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 560 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 562 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 563 | ctx->prev = old_idx; |
| 564 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | ctx->idx = cur_idx; |
| 566 | ctx->val = sov - sol; |
| 567 | |
| 568 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 569 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 570 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 571 | eol--; |
| 572 | ctx->tws++; |
| 573 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 574 | ctx->vlen = eol - sov; |
| 575 | return 1; |
| 576 | } |
| 577 | next_hdr: |
| 578 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 579 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 580 | cur_idx = idx->v[cur_idx].next; |
| 581 | } |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 586 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 587 | struct hdr_ctx *ctx) |
| 588 | { |
| 589 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 590 | } |
| 591 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 592 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 593 | * the http_find_header functions. The value is removed, as well as surrounding |
| 594 | * 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] | 595 | * 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] | 596 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 597 | * no more header, so any processing may stop. The ctx is always left in a form |
| 598 | * that can be handled by http_find_header2() to find next occurrence. |
| 599 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 600 | 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] | 601 | { |
| 602 | int cur_idx = ctx->idx; |
| 603 | char *sol = ctx->line; |
| 604 | struct hdr_idx_elem *hdr; |
| 605 | int delta, skip_comma; |
| 606 | |
| 607 | if (!cur_idx) |
| 608 | return 0; |
| 609 | |
| 610 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 611 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 613 | delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 614 | http_msg_move_end(msg, delta); |
| 615 | idx->used--; |
| 616 | hdr->len = 0; /* unused entry */ |
| 617 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 618 | if (idx->tail == ctx->idx) |
| 619 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 620 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 621 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 622 | 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] | 623 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | return ctx->idx; |
| 625 | } |
| 626 | |
| 627 | /* 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] | 628 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 629 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 630 | */ |
| 631 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 632 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 633 | delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 634 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 635 | NULL, 0); |
| 636 | hdr->len += delta; |
| 637 | http_msg_move_end(msg, delta); |
| 638 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 639 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 640 | return ctx->idx; |
| 641 | } |
| 642 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 643 | /* This function handles a server error at the stream interface level. The |
| 644 | * stream interface is assumed to be already in a closed state. An optional |
| 645 | * message is copied into the input buffer, and an HTTP status code stored. |
| 646 | * 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] | 647 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 649 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 650 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 652 | buffer_auto_read(si->ob); |
| 653 | buffer_abort(si->ob); |
| 654 | buffer_auto_close(si->ob); |
| 655 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 656 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 657 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 658 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 659 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 660 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | } |
| 662 | if (!(t->flags & SN_ERR_MASK)) |
| 663 | t->flags |= err; |
| 664 | if (!(t->flags & SN_FINST_MASK)) |
| 665 | t->flags |= finst; |
| 666 | } |
| 667 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 668 | /* This function returns the appropriate error location for the given session |
| 669 | * and message. |
| 670 | */ |
| 671 | |
| 672 | struct chunk *error_message(struct session *s, int msgnum) |
| 673 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 674 | if (s->be->errmsg[msgnum].str) |
| 675 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 676 | else if (s->fe->errmsg[msgnum].str) |
| 677 | return &s->fe->errmsg[msgnum]; |
| 678 | else |
| 679 | return &http_err_chunks[msgnum]; |
| 680 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 681 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | /* |
| 683 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 684 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 685 | */ |
| 686 | static http_meth_t find_http_meth(const char *str, const int len) |
| 687 | { |
| 688 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 689 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 690 | |
| 691 | m = ((unsigned)*str - 'A'); |
| 692 | |
| 693 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 694 | for (h = http_methods[m]; h->len > 0; h++) { |
| 695 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 696 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 697 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 698 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 699 | }; |
| 700 | return HTTP_METH_OTHER; |
| 701 | } |
| 702 | return HTTP_METH_NONE; |
| 703 | |
| 704 | } |
| 705 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 706 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 707 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 708 | * It is returned otherwise. |
| 709 | */ |
| 710 | static char * |
| 711 | http_get_path(struct http_txn *txn) |
| 712 | { |
| 713 | char *ptr, *end; |
| 714 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 715 | ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 716 | end = ptr + txn->req.sl.rq.u_l; |
| 717 | |
| 718 | if (ptr >= end) |
| 719 | return NULL; |
| 720 | |
| 721 | /* RFC2616, par. 5.1.2 : |
| 722 | * Request-URI = "*" | absuri | abspath | authority |
| 723 | */ |
| 724 | |
| 725 | if (*ptr == '*') |
| 726 | return NULL; |
| 727 | |
| 728 | if (isalpha((unsigned char)*ptr)) { |
| 729 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 730 | ptr++; |
| 731 | while (ptr < end && |
| 732 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 733 | ptr++; |
| 734 | /* skip '://' */ |
| 735 | if (ptr == end || *ptr++ != ':') |
| 736 | return NULL; |
| 737 | if (ptr == end || *ptr++ != '/') |
| 738 | return NULL; |
| 739 | if (ptr == end || *ptr++ != '/') |
| 740 | return NULL; |
| 741 | } |
| 742 | /* skip [user[:passwd]@]host[:[port]] */ |
| 743 | |
| 744 | while (ptr < end && *ptr != '/') |
| 745 | ptr++; |
| 746 | |
| 747 | if (ptr == end) |
| 748 | return NULL; |
| 749 | |
| 750 | /* OK, we got the '/' ! */ |
| 751 | return ptr; |
| 752 | } |
| 753 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 755 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 756 | * left unchanged and will follow normal proxy processing. |
| 757 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 758 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 759 | { |
| 760 | struct http_txn *txn; |
| 761 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 762 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 763 | char *path; |
| 764 | int len; |
| 765 | |
| 766 | /* 1: create the response header */ |
| 767 | rdr.len = strlen(HTTP_302); |
| 768 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 769 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 770 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 771 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 772 | srv = target_srv(&s->target); |
| 773 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 774 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 775 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 776 | return; |
| 777 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 778 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 779 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 780 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 781 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 782 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 783 | |
| 784 | /* 3: add the request URI */ |
| 785 | txn = &s->txn; |
| 786 | path = http_get_path(txn); |
| 787 | if (!path) |
| 788 | return; |
| 789 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 790 | len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 791 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 792 | return; |
| 793 | |
| 794 | memcpy(rdr.str + rdr.len, path, len); |
| 795 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 796 | |
| 797 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 798 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 799 | rdr.len += 29; |
| 800 | } else { |
| 801 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 802 | rdr.len += 23; |
| 803 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 804 | |
| 805 | /* prepare to return without error. */ |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame] | 806 | si->sock.shutr(si); |
| 807 | si->sock.shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | si->err_type = SI_ET_NONE; |
| 809 | si->err_loc = NULL; |
| 810 | si->state = SI_ST_CLO; |
| 811 | |
| 812 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 813 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 814 | |
| 815 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 816 | if (srv) |
| 817 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 820 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 821 | * that the server side is closed. Note that err_type is actually a |
| 822 | * bitmask, where almost only aborts may be cumulated with other |
| 823 | * values. We consider that aborted operations are more important |
| 824 | * than timeouts or errors due to the fact that nobody else in the |
| 825 | * logs might explain incomplete retries. All others should avoid |
| 826 | * being cumulated. It should normally not be possible to have multiple |
| 827 | * aborts at once, but just in case, the first one in sequence is reported. |
| 828 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 829 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 830 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 831 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | |
| 833 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 834 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 835 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 836 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 837 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 838 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 839 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 840 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 841 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 842 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 843 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 844 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 845 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 847 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 848 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 849 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 850 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 851 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 852 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 853 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 856 | extern const char sess_term_cond[8]; |
| 857 | extern const char sess_fin_state[8]; |
| 858 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 859 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 860 | struct pool_head *pool2_capture; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 861 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 862 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 863 | /* |
| 864 | * Capture headers from message starting at <som> according to header list |
| 865 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 866 | */ |
| 867 | void capture_headers(char *som, struct hdr_idx *idx, |
| 868 | char **cap, struct cap_hdr *cap_hdr) |
| 869 | { |
| 870 | char *eol, *sol, *col, *sov; |
| 871 | int cur_idx; |
| 872 | struct cap_hdr *h; |
| 873 | int len; |
| 874 | |
| 875 | sol = som + hdr_idx_first_pos(idx); |
| 876 | cur_idx = hdr_idx_first_idx(idx); |
| 877 | |
| 878 | while (cur_idx) { |
| 879 | eol = sol + idx->v[cur_idx].len; |
| 880 | |
| 881 | col = sol; |
| 882 | while (col < eol && *col != ':') |
| 883 | col++; |
| 884 | |
| 885 | sov = col + 1; |
| 886 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 887 | sov++; |
| 888 | |
| 889 | for (h = cap_hdr; h; h = h->next) { |
| 890 | if ((h->namelen == col - sol) && |
| 891 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 892 | if (cap[h->index] == NULL) |
| 893 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 894 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 895 | |
| 896 | if (cap[h->index] == NULL) { |
| 897 | Alert("HTTP capture : out of memory.\n"); |
| 898 | continue; |
| 899 | } |
| 900 | |
| 901 | len = eol - sov; |
| 902 | if (len > h->len) |
| 903 | len = h->len; |
| 904 | |
| 905 | memcpy(cap[h->index], sov, len); |
| 906 | cap[h->index][len]=0; |
| 907 | } |
| 908 | } |
| 909 | sol = eol + idx->v[cur_idx].cr + 1; |
| 910 | cur_idx = idx->v[cur_idx].next; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 915 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 916 | */ |
| 917 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 918 | |
| 919 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 920 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 921 | */ |
| 922 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 923 | ptr++; \ |
| 924 | if (likely(ptr < end)) \ |
| 925 | goto good; \ |
| 926 | else { \ |
| 927 | state = (st); \ |
| 928 | goto http_msg_ood; \ |
| 929 | } \ |
| 930 | } while (0) |
| 931 | |
| 932 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 933 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 934 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 935 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 936 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 937 | * will give undefined results. |
| 938 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 939 | * and that msg->sol points to the beginning of the response. |
| 940 | * If a complete line is found (which implies that at least one CR or LF is |
| 941 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 942 | * returned indicating an incomplete line (which does not mean that parts have |
| 943 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 944 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 945 | * upon next call. |
| 946 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 947 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 948 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 949 | * 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] | 950 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 951 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 952 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 953 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 954 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 955 | { |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 956 | const char *msg_start = msg->buf->p; |
| 957 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 959 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 960 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 961 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 962 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 963 | |
| 964 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 965 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 966 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 967 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 968 | state = HTTP_MSG_ERROR; |
| 969 | break; |
| 970 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 971 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 972 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 973 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 974 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 975 | goto http_msg_rpcode; |
| 976 | } |
| 977 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 978 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 979 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 980 | state = HTTP_MSG_ERROR; |
| 981 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 982 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 983 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 984 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 985 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 986 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 987 | |
| 988 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 989 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 990 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 991 | } |
| 992 | |
| 993 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 994 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 995 | http_msg_rsp_reason: |
| 996 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 997 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 998 | msg->sl.st.r_l = 0; |
| 999 | goto http_msg_rpline_eol; |
| 1000 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1001 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1002 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1003 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1004 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1005 | goto http_msg_rpreason; |
| 1006 | } |
| 1007 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1008 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1009 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1010 | goto http_msg_rsp_reason; |
| 1011 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1012 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1013 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1014 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1015 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1016 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1017 | http_msg_rpline_eol: |
| 1018 | /* We have seen the end of line. Note that we do not |
| 1019 | * necessarily have the \n yet, but at least we know that we |
| 1020 | * have EITHER \r OR \n, otherwise the response would not be |
| 1021 | * complete. We can then record the response length and return |
| 1022 | * to the caller which will be able to register it. |
| 1023 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1024 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1025 | return ptr; |
| 1026 | |
| 1027 | #ifdef DEBUG_FULL |
| 1028 | default: |
| 1029 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1030 | exit(1); |
| 1031 | #endif |
| 1032 | } |
| 1033 | |
| 1034 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1035 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1036 | if (ret_state) |
| 1037 | *ret_state = state; |
| 1038 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1039 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1040 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1043 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1044 | * This function parses a request line between <ptr> and <end>, starting with |
| 1045 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1046 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1047 | * will give undefined results. |
| 1048 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1049 | * and that msg->sol points to the beginning of the request. |
| 1050 | * If a complete line is found (which implies that at least one CR or LF is |
| 1051 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1052 | * returned indicating an incomplete line (which does not mean that parts have |
| 1053 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1054 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1055 | * upon next call. |
| 1056 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1057 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1058 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1059 | * 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] | 1060 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1061 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1062 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1063 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1064 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1065 | { |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1066 | const char *msg_start = msg->buf->p; |
| 1067 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1068 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1069 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1070 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1071 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1072 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1073 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1074 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1075 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1076 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1077 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1080 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1081 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1082 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1083 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1084 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1085 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1086 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1087 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1088 | msg->sl.rq.v_l = 0; |
| 1089 | goto http_msg_rqline_eol; |
| 1090 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1091 | state = HTTP_MSG_ERROR; |
| 1092 | break; |
| 1093 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1094 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1095 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1096 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1097 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | goto http_msg_rquri; |
| 1099 | } |
| 1100 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1101 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1102 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1103 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1104 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1106 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1107 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1108 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1109 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1110 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1111 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1112 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1113 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1114 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1115 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1116 | /* non-ASCII chars are forbidden unless option |
| 1117 | * accept-invalid-http-request is enabled in the frontend. |
| 1118 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1119 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1120 | if (msg->err_pos < -1) |
| 1121 | goto invalid_char; |
| 1122 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1123 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1124 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1125 | } |
| 1126 | |
| 1127 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1128 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1129 | goto http_msg_req09_uri_e; |
| 1130 | } |
| 1131 | |
| 1132 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1133 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1134 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1135 | state = HTTP_MSG_ERROR; |
| 1136 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1137 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1138 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1139 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1140 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1141 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1142 | goto http_msg_rqver; |
| 1143 | } |
| 1144 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1145 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1146 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1147 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1148 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1149 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1150 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1151 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1152 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1153 | |
| 1154 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1155 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1156 | http_msg_rqline_eol: |
| 1157 | /* We have seen the end of line. Note that we do not |
| 1158 | * necessarily have the \n yet, but at least we know that we |
| 1159 | * have EITHER \r OR \n, otherwise the request would not be |
| 1160 | * complete. We can then record the request length and return |
| 1161 | * to the caller which will be able to register it. |
| 1162 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1163 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1164 | return ptr; |
| 1165 | } |
| 1166 | |
| 1167 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1168 | state = HTTP_MSG_ERROR; |
| 1169 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | #ifdef DEBUG_FULL |
| 1172 | default: |
| 1173 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1174 | exit(1); |
| 1175 | #endif |
| 1176 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1178 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1179 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1180 | if (ret_state) |
| 1181 | *ret_state = state; |
| 1182 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1183 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1185 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1186 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1187 | /* |
| 1188 | * Returns the data from Authorization header. Function may be called more |
| 1189 | * than once so data is stored in txn->auth_data. When no header is found |
| 1190 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1191 | * searching again for something we are unable to find anyway. |
| 1192 | */ |
| 1193 | |
| 1194 | char get_http_auth_buff[BUFSIZE]; |
| 1195 | |
| 1196 | int |
| 1197 | get_http_auth(struct session *s) |
| 1198 | { |
| 1199 | |
| 1200 | struct http_txn *txn = &s->txn; |
| 1201 | struct chunk auth_method; |
| 1202 | struct hdr_ctx ctx; |
| 1203 | char *h, *p; |
| 1204 | int len; |
| 1205 | |
| 1206 | #ifdef DEBUG_AUTH |
| 1207 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1208 | #endif |
| 1209 | |
| 1210 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1211 | return 0; |
| 1212 | |
| 1213 | if (txn->auth.method) |
| 1214 | return 1; |
| 1215 | |
| 1216 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1217 | |
| 1218 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1219 | |
| 1220 | if (txn->flags & TX_USE_PX_CONN) { |
| 1221 | h = "Proxy-Authorization"; |
| 1222 | len = strlen(h); |
| 1223 | } else { |
| 1224 | h = "Authorization"; |
| 1225 | len = strlen(h); |
| 1226 | } |
| 1227 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1228 | if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1229 | return 0; |
| 1230 | |
| 1231 | h = ctx.line + ctx.val; |
| 1232 | |
| 1233 | p = memchr(h, ' ', ctx.vlen); |
| 1234 | if (!p || p == h) |
| 1235 | return 0; |
| 1236 | |
| 1237 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1238 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1239 | |
| 1240 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1241 | |
| 1242 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1243 | get_http_auth_buff, BUFSIZE - 1); |
| 1244 | |
| 1245 | if (len < 0) |
| 1246 | return 0; |
| 1247 | |
| 1248 | |
| 1249 | get_http_auth_buff[len] = '\0'; |
| 1250 | |
| 1251 | p = strchr(get_http_auth_buff, ':'); |
| 1252 | |
| 1253 | if (!p) |
| 1254 | return 0; |
| 1255 | |
| 1256 | txn->auth.user = get_http_auth_buff; |
| 1257 | *p = '\0'; |
| 1258 | txn->auth.pass = p+1; |
| 1259 | |
| 1260 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1261 | return 1; |
| 1262 | } |
| 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1267 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | /* |
| 1269 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1270 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1271 | * ensuring that the message does not wrap. The function can be preempted |
| 1272 | * everywhere when data are missing and recalled at the exact same location |
| 1273 | * with no information loss. The message may even be realigned between two |
| 1274 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1275 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1276 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1277 | * the first state, so that none of the msg pointers has to be initialized |
| 1278 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1280 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1281 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1282 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1283 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1284 | struct buffer *buf = msg->buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1285 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1286 | state = msg->msg_state; |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1287 | ptr = buf->p + msg->next; |
| 1288 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1289 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1290 | if (unlikely(ptr >= end)) |
| 1291 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1292 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1293 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1294 | /* |
| 1295 | * First, states that are specific to the response only. |
| 1296 | * We check them first so that request and headers are |
| 1297 | * closer to each other (accessed more often). |
| 1298 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1299 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1300 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1301 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1302 | /* we have a start of message, but we have to check |
| 1303 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1304 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1305 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1306 | char *beg = buf->p; |
| 1307 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1308 | if (unlikely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1309 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1310 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1311 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 1312 | bi_fast_delete(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1313 | } |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1314 | msg->sol = msg->som = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1315 | hdr_idx_init(idx); |
| 1316 | state = HTTP_MSG_RPVER; |
| 1317 | goto http_msg_rpver; |
| 1318 | } |
| 1319 | |
| 1320 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1321 | goto http_msg_invalid; |
| 1322 | |
| 1323 | if (unlikely(*ptr == '\n')) |
| 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1325 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1326 | /* stop here */ |
| 1327 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1328 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1329 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1331 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1332 | /* stop here */ |
| 1333 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1335 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | case HTTP_MSG_RPVER_SP: |
| 1337 | case HTTP_MSG_RPCODE: |
| 1338 | case HTTP_MSG_RPCODE_SP: |
| 1339 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1340 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1341 | state, ptr, end, |
| 1342 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1343 | if (unlikely(!ptr)) |
| 1344 | return; |
| 1345 | |
| 1346 | /* we have a full response and we know that we have either a CR |
| 1347 | * or an LF at <ptr>. |
| 1348 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1350 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1351 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | if (likely(*ptr == '\r')) |
| 1353 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1354 | goto http_msg_rpline_end; |
| 1355 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1356 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1357 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1358 | /* msg->sol must point to the first of CR or LF. */ |
| 1359 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1360 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1361 | /* stop here */ |
| 1362 | |
| 1363 | /* |
| 1364 | * Second, states that are specific to the request only |
| 1365 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1366 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1367 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1368 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1369 | /* we have a start of message, but we have to check |
| 1370 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1371 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1372 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1373 | char *beg = buf->p; |
| 1374 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1375 | if (likely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1376 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1377 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1378 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 1379 | bi_fast_delete(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1380 | } |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1381 | msg->sol = msg->som = ptr - buf->p; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1382 | /* we will need this when keep-alive will be supported |
| 1383 | hdr_idx_init(idx); |
| 1384 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1385 | state = HTTP_MSG_RQMETH; |
| 1386 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1390 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1391 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | if (unlikely(*ptr == '\n')) |
| 1393 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1394 | 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] | 1395 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1396 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1398 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1399 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1400 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1401 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1404 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | case HTTP_MSG_RQMETH_SP: |
| 1406 | case HTTP_MSG_RQURI: |
| 1407 | case HTTP_MSG_RQURI_SP: |
| 1408 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1409 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1410 | state, ptr, end, |
| 1411 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | if (unlikely(!ptr)) |
| 1413 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1414 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | /* we have a full request and we know that we have either a CR |
| 1416 | * or an LF at <ptr>. |
| 1417 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1420 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | if (likely(*ptr == '\r')) |
| 1422 | 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] | 1423 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1426 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | /* check for HTTP/0.9 request : no version information available. |
| 1428 | * msg->sol must point to the first of CR or LF. |
| 1429 | */ |
| 1430 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1431 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1434 | 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] | 1435 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | /* |
| 1438 | * Common states below |
| 1439 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1441 | http_msg_hdr_first: |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1442 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1444 | goto http_msg_hdr_name; |
| 1445 | } |
| 1446 | |
| 1447 | if (likely(*ptr == '\r')) |
| 1448 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1449 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1450 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1452 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | /* assumes msg->sol points to the first char */ |
| 1454 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1455 | 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] | 1456 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1457 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1458 | 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] | 1459 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1460 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1461 | goto http_msg_invalid; |
| 1462 | |
| 1463 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1464 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1465 | |
| 1466 | /* and we still accept this non-token character */ |
| 1467 | 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] | 1468 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1470 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1471 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1472 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1473 | 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] | 1474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1476 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1477 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1479 | goto http_msg_hdr_val; |
| 1480 | } |
| 1481 | |
| 1482 | if (likely(*ptr == '\r')) |
| 1483 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1484 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1487 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1489 | 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] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1492 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1494 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1495 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1496 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | goto http_msg_hdr_l1_sp; |
| 1498 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1499 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1500 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | goto http_msg_complete_header; |
| 1502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1504 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1505 | /* assumes msg->sol points to the first char, and msg->sov |
| 1506 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | */ |
| 1508 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1509 | 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] | 1510 | |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1511 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1513 | * real header end in case it ends with lots of LWS, but is this |
| 1514 | * really needed ? |
| 1515 | */ |
| 1516 | if (likely(*ptr == '\r')) |
| 1517 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1518 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1519 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1521 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1523 | 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] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1526 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1528 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1529 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1530 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | goto http_msg_hdr_val; |
| 1532 | } |
| 1533 | http_msg_complete_header: |
| 1534 | /* |
| 1535 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1536 | * Assumes msg->sol points to the first char, msg->sov points |
| 1537 | * to the first character of the value and msg->eol to the |
| 1538 | * first CR or LF so we know how the line ends. We insert last |
| 1539 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1541 | 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] | 1542 | idx, idx->tail) < 0)) |
| 1543 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1544 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1545 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1547 | goto http_msg_hdr_name; |
| 1548 | } |
| 1549 | |
| 1550 | if (likely(*ptr == '\r')) |
| 1551 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1552 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1555 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1557 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1558 | ptr++; |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1559 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1560 | msg->eoh = msg->sol; |
| 1561 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1562 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1564 | |
| 1565 | case HTTP_MSG_ERROR: |
| 1566 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1567 | break; |
| 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | #ifdef DEBUG_FULL |
| 1570 | default: |
| 1571 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1572 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1573 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | } |
| 1575 | http_msg_ood: |
| 1576 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1577 | msg->msg_state = state; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1578 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1580 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | http_msg_invalid: |
| 1582 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1583 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1584 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | return; |
| 1586 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1588 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1589 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1590 | * nothing is done and 1 is returned. |
| 1591 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1592 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1593 | { |
| 1594 | int delta; |
| 1595 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1596 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1597 | |
| 1598 | if (msg->sl.rq.v_l != 0) |
| 1599 | return 1; |
| 1600 | |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1601 | cur_end = msg->buf->p + msg->sol + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1602 | delta = 0; |
| 1603 | |
| 1604 | if (msg->sl.rq.u_l == 0) { |
| 1605 | /* if no URI was set, add "/" */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1606 | delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1607 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1608 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1609 | } |
| 1610 | /* add HTTP version */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1611 | delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1612 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1613 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1614 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1615 | HTTP_MSG_RQMETH, |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1616 | msg->buf->p + msg->sol, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1617 | NULL, NULL); |
| 1618 | if (unlikely(!cur_end)) |
| 1619 | return 0; |
| 1620 | |
| 1621 | /* we have a full HTTP/1.0 request now and we know that |
| 1622 | * we have either a CR or an LF at <ptr>. |
| 1623 | */ |
| 1624 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1628 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1629 | * and "keep-alive" values. If we already know that some headers may safely |
| 1630 | * 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] | 1631 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1632 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1633 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1634 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1635 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1636 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1637 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1638 | 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] | 1639 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1640 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1641 | const char *hdr_val = "Connection"; |
| 1642 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1644 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1645 | return; |
| 1646 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1647 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1648 | hdr_val = "Proxy-Connection"; |
| 1649 | hdr_len = 16; |
| 1650 | } |
| 1651 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1652 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1653 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1654 | while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1655 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1656 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1657 | if (to_del & 2) |
| 1658 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1659 | else |
| 1660 | txn->flags |= TX_CON_KAL_SET; |
| 1661 | } |
| 1662 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1663 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1664 | if (to_del & 1) |
| 1665 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1666 | else |
| 1667 | txn->flags |= TX_CON_CLO_SET; |
| 1668 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1671 | txn->flags |= TX_HDR_CONN_PRS; |
| 1672 | return; |
| 1673 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1674 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1675 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1676 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1677 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1678 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1679 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1680 | 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] | 1681 | { |
| 1682 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1683 | const char *hdr_val = "Connection"; |
| 1684 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1685 | |
| 1686 | ctx.idx = 0; |
| 1687 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1688 | |
| 1689 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1690 | hdr_val = "Proxy-Connection"; |
| 1691 | hdr_len = 16; |
| 1692 | } |
| 1693 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1694 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1695 | while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1696 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1697 | if (wanted & TX_CON_KAL_SET) |
| 1698 | txn->flags |= TX_CON_KAL_SET; |
| 1699 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1700 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1701 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1702 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1703 | if (wanted & TX_CON_CLO_SET) |
| 1704 | txn->flags |= TX_CON_CLO_SET; |
| 1705 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1706 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1707 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1708 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1709 | |
| 1710 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1711 | return; |
| 1712 | |
| 1713 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1714 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1715 | hdr_val = "Connection: close"; |
| 1716 | hdr_len = 17; |
| 1717 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1718 | hdr_val = "Proxy-Connection: close"; |
| 1719 | hdr_len = 23; |
| 1720 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1721 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1725 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1726 | hdr_val = "Connection: keep-alive"; |
| 1727 | hdr_len = 22; |
| 1728 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1729 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1730 | hdr_len = 28; |
| 1731 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1732 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1733 | } |
| 1734 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1737 | /* 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] | 1738 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1739 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1740 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1741 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1742 | * 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] | 1743 | * 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] | 1744 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1745 | int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1746 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1747 | const struct buffer *buf = msg->buf; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1748 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1749 | const char *ptr_old = ptr; |
| 1750 | const char *end = buf->data + buf->size; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1751 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1752 | unsigned int chunk = 0; |
| 1753 | |
| 1754 | /* The chunk size is in the following form, though we are only |
| 1755 | * interested in the size and CRLF : |
| 1756 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1757 | */ |
| 1758 | while (1) { |
| 1759 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1760 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1761 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1762 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1763 | if (c < 0) /* not a hex digit anymore */ |
| 1764 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1765 | if (++ptr >= end) |
| 1766 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1767 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1768 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1769 | chunk = (chunk << 4) + c; |
| 1770 | } |
| 1771 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1772 | /* empty size not allowed */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1773 | if (ptr == ptr_old) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1774 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1775 | |
| 1776 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1777 | if (++ptr >= end) |
| 1778 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1779 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1780 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1783 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1784 | * for the end of chunk size. |
| 1785 | */ |
| 1786 | while (1) { |
| 1787 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1788 | /* we now have a CR or an LF at ptr */ |
| 1789 | if (likely(*ptr == '\r')) { |
| 1790 | if (++ptr >= end) |
| 1791 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1792 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1793 | return 0; |
| 1794 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1796 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1797 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1798 | if (++ptr >= end) |
| 1799 | ptr = buf->data; |
| 1800 | /* done */ |
| 1801 | break; |
| 1802 | } |
| 1803 | else if (*ptr == ';') { |
| 1804 | /* chunk extension, ends at next CRLF */ |
| 1805 | if (++ptr >= end) |
| 1806 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1807 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1808 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1809 | |
| 1810 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1811 | if (++ptr >= end) |
| 1812 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1813 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1814 | return 0; |
| 1815 | } |
| 1816 | /* we have a CRLF now, loop above */ |
| 1817 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1818 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1819 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1820 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1821 | } |
| 1822 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1823 | /* 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] | 1824 | * 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] | 1825 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1826 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1827 | msg->sov += ptr - ptr_old; |
| 1828 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1829 | msg->chunk_len = chunk; |
| 1830 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1831 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1832 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1833 | error: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1834 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1835 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1838 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1839 | * 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] | 1840 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1841 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1842 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1843 | * 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] | 1844 | * 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] | 1845 | * 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] | 1846 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1847 | * which implies that all non-trailers data have already been scheduled for |
| 1848 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1849 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1850 | * important to note that this function is designed to be able to parse wrapped |
| 1851 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1852 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1853 | int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1854 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1855 | const struct buffer *buf = msg->buf; |
| 1856 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1857 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1858 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1859 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1860 | const char *ptr = b_ptr(buf, msg->next); |
| 1861 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1862 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1863 | |
| 1864 | /* scan current line and stop at LF or CRLF */ |
| 1865 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1866 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1867 | return 0; |
| 1868 | |
| 1869 | if (*ptr == '\n') { |
| 1870 | if (!p1) |
| 1871 | p1 = ptr; |
| 1872 | p2 = ptr; |
| 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1877 | if (p1) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1878 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1879 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1880 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1881 | p1 = ptr; |
| 1882 | } |
| 1883 | |
| 1884 | ptr++; |
| 1885 | if (ptr >= buf->data + buf->size) |
| 1886 | ptr = buf->data; |
| 1887 | } |
| 1888 | |
| 1889 | /* after LF; point to beginning of next line */ |
| 1890 | p2++; |
| 1891 | if (p2 >= buf->data + buf->size) |
| 1892 | p2 = buf->data; |
| 1893 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1894 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1895 | if (bytes < 0) |
| 1896 | bytes += buf->size; |
| 1897 | |
| 1898 | /* schedule this line for forwarding */ |
| 1899 | msg->sov += bytes; |
| 1900 | if (msg->sov >= buf->size) |
| 1901 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1902 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1903 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1904 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1905 | * Everything was scheduled for forwarding, there's nothing |
| 1906 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1907 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1908 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1909 | msg->msg_state = HTTP_MSG_DONE; |
| 1910 | return 1; |
| 1911 | } |
| 1912 | /* OK, next line then */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1913 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1918 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1919 | * ->som, ->next in order to include this part into the next forwarding phase. |
| 1920 | * 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] | 1921 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1922 | * not enough data are available, the function does not change anything and |
| 1923 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1924 | * does not change anything. Note: this function is designed to parse wrapped |
| 1925 | * CRLF at the end of the buffer. |
| 1926 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1927 | int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1928 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1929 | const struct buffer *buf = msg->buf; |
| 1930 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1931 | int bytes; |
| 1932 | |
| 1933 | /* NB: we'll check data availabilty at the end. It's not a |
| 1934 | * problem because whatever we match first will be checked |
| 1935 | * against the correct length. |
| 1936 | */ |
| 1937 | bytes = 1; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1938 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1939 | if (*ptr == '\r') { |
| 1940 | bytes++; |
| 1941 | ptr++; |
| 1942 | if (ptr >= buf->data + buf->size) |
| 1943 | ptr = buf->data; |
| 1944 | } |
| 1945 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1946 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | return 0; |
| 1948 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1949 | if (*ptr != '\n') { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1950 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1952 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1953 | |
| 1954 | ptr++; |
| 1955 | if (ptr >= buf->data + buf->size) |
| 1956 | ptr = buf->data; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1957 | /* prepare the CRLF to be forwarded (between ->som and ->sov) */ |
| 1958 | msg->som = 0; |
| 1959 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1960 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1961 | return 1; |
| 1962 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1963 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1964 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1965 | * processing can continue on next analysers, or zero if it either needs more |
| 1966 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 1967 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 1968 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1969 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1970 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1971 | int http_wait_for_request(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1972 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1973 | /* |
| 1974 | * We will parse the partial (or complete) lines. |
| 1975 | * We will check the request syntax, and also join multi-line |
| 1976 | * headers. An index of all the lines will be elaborated while |
| 1977 | * parsing. |
| 1978 | * |
| 1979 | * For the parsing, we use a 28 states FSM. |
| 1980 | * |
| 1981 | * Here is the information we currently have : |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1982 | * req->p + msg->som = beginning of request |
| 1983 | * req->p + msg->eoh = end of processed headers / start of current one |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1984 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1985 | * msg->next = first non-visited byte |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1986 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1987 | * |
| 1988 | * At end of parsing, we may perform a capture of the error (if any), and |
| 1989 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 1990 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1991 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1992 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1993 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1994 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1995 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1996 | struct http_txn *txn = &s->txn; |
| 1997 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1998 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1999 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2000 | 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] | 2001 | now_ms, __FUNCTION__, |
| 2002 | s, |
| 2003 | req, |
| 2004 | req->rex, req->wex, |
| 2005 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2006 | req->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2007 | req->analysers); |
| 2008 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2009 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2010 | s->srv_error = http_return_srv_error; |
| 2011 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2012 | /* There's a protected area at the end of the buffer for rewriting |
| 2013 | * purposes. We don't want to start to parse the request if the |
| 2014 | * protected area is affected, because we may have to move processed |
| 2015 | * data later, which is much more complicated. |
| 2016 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2017 | if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2018 | if ((txn->flags & TX_NOT_FIRST) && |
| 2019 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2020 | bi_end(req) < b_ptr(req, msg->next) || |
| 2021 | bi_end(req) > req->data + req->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2022 | if (req->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2023 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2024 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2025 | /* some data has still not left the buffer, wake us once that's done */ |
| 2026 | buffer_dont_connect(req); |
| 2027 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2028 | return 0; |
| 2029 | } |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2030 | if (bi_end(req) < b_ptr(req, msg->next) || |
| 2031 | bi_end(req) > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | a7fe8e5 | 2012-05-08 20:40:09 +0200 | [diff] [blame] | 2032 | buffer_slow_realign(msg->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2035 | /* Note that we have the same problem with the response ; we |
| 2036 | * may want to send a redirect, error or anything which requires |
| 2037 | * some spare space. So we'll ensure that we have at least |
| 2038 | * maxrewrite bytes available in the response buffer before |
| 2039 | * processing that one. This will only affect pipelined |
| 2040 | * keep-alive requests. |
| 2041 | */ |
| 2042 | if ((txn->flags & TX_NOT_FIRST) && |
| 2043 | unlikely((s->rep->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2044 | bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) || |
| 2045 | bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2046 | if (s->rep->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2047 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2048 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2049 | /* don't let a connection request be initiated */ |
| 2050 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2051 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2052 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2053 | return 0; |
| 2054 | } |
| 2055 | } |
| 2056 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2057 | if (likely(msg->next < req->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2058 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2061 | /* 1: we might have to print this header in debug mode */ |
| 2062 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2063 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2064 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2065 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2066 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2067 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2068 | sol = req->p + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2069 | eol = sol + msg->sl.rq.l; |
| 2070 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2071 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2072 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2073 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2075 | while (cur_idx) { |
| 2076 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2077 | debug_hdr("clihdr", s, sol, eol); |
| 2078 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2079 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2080 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2081 | } |
| 2082 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2083 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2084 | /* |
| 2085 | * Now we quickly check if we have found a full valid request. |
| 2086 | * If not so, we check the FD and buffer states before leaving. |
| 2087 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2088 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2089 | * requests are checked first. When waiting for a second request |
| 2090 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2091 | * we note the error in the session flags but don't set any state. |
| 2092 | * Since the error will be noted there, it will not be counted by |
| 2093 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2094 | * Last, we may increase some tracked counters' http request errors on |
| 2095 | * the cases that are deliberately the client's fault. For instance, |
| 2096 | * a timeout or connection reset is not counted as an error. However |
| 2097 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2098 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2099 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2100 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2101 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2102 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2103 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2104 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2105 | session_inc_http_req_ctr(s); |
| 2106 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2107 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2108 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2109 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2110 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2111 | /* 1: Since we are in header mode, if there's no space |
| 2112 | * left for headers, we won't be able to free more |
| 2113 | * later, so the session will never terminate. We |
| 2114 | * must terminate it now. |
| 2115 | */ |
| 2116 | if (unlikely(req->flags & BF_FULL)) { |
| 2117 | /* FIXME: check if URI is set and return Status |
| 2118 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2119 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2120 | session_inc_http_req_ctr(s); |
| 2121 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2122 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2123 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2124 | msg->err_pos = req->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2125 | goto return_bad_req; |
| 2126 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2127 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2128 | /* 2: have we encountered a read error ? */ |
| 2129 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2130 | if (!(s->flags & SN_ERR_MASK)) |
| 2131 | s->flags |= SN_ERR_CLICL; |
| 2132 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2133 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2134 | goto failed_keep_alive; |
| 2135 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2136 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2137 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2138 | 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] | 2139 | session_inc_http_err_ctr(s); |
| 2140 | } |
| 2141 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2142 | msg->msg_state = HTTP_MSG_ERROR; |
| 2143 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2144 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2145 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2146 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2147 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2148 | if (s->listener->counters) |
| 2149 | s->listener->counters->failed_req++; |
| 2150 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2151 | if (!(s->flags & SN_FINST_MASK)) |
| 2152 | s->flags |= SN_FINST_R; |
| 2153 | return 0; |
| 2154 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2155 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2156 | /* 3: has the read timeout expired ? */ |
| 2157 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2158 | if (!(s->flags & SN_ERR_MASK)) |
| 2159 | s->flags |= SN_ERR_CLITO; |
| 2160 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2161 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2162 | goto failed_keep_alive; |
| 2163 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2164 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2165 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2166 | 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] | 2167 | session_inc_http_err_ctr(s); |
| 2168 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2169 | txn->status = 408; |
| 2170 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2171 | msg->msg_state = HTTP_MSG_ERROR; |
| 2172 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2173 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2174 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2175 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2176 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2177 | if (s->listener->counters) |
| 2178 | s->listener->counters->failed_req++; |
| 2179 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2180 | if (!(s->flags & SN_FINST_MASK)) |
| 2181 | s->flags |= SN_FINST_R; |
| 2182 | return 0; |
| 2183 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2184 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2185 | /* 4: have we encountered a close ? */ |
| 2186 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2187 | if (!(s->flags & SN_ERR_MASK)) |
| 2188 | s->flags |= SN_ERR_CLICL; |
| 2189 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2190 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2191 | goto failed_keep_alive; |
| 2192 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2193 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2194 | 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] | 2195 | txn->status = 400; |
| 2196 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2197 | msg->msg_state = HTTP_MSG_ERROR; |
| 2198 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2199 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2200 | session_inc_http_err_ctr(s); |
| 2201 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2202 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2203 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2204 | if (s->listener->counters) |
| 2205 | s->listener->counters->failed_req++; |
| 2206 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2207 | if (!(s->flags & SN_FINST_MASK)) |
| 2208 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2209 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2210 | } |
| 2211 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2212 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2213 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2214 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2215 | #ifdef TCP_QUICKACK |
| 2216 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2217 | /* We need more data, we have to re-enable quick-ack in case we |
| 2218 | * previously disabled it, otherwise we might cause the client |
| 2219 | * to delay next data. |
| 2220 | */ |
| 2221 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2222 | } |
| 2223 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2224 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2225 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2226 | /* If the client starts to talk, let's fall back to |
| 2227 | * request timeout processing. |
| 2228 | */ |
| 2229 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2230 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2233 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2234 | if (!tick_isset(req->analyse_exp)) { |
| 2235 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2236 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2237 | tick_isset(s->be->timeout.httpka)) |
| 2238 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2239 | else |
| 2240 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2241 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2242 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2243 | /* we're not ready yet */ |
| 2244 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2245 | |
| 2246 | failed_keep_alive: |
| 2247 | /* Here we process low-level errors for keep-alive requests. In |
| 2248 | * short, if the request is not the first one and it experiences |
| 2249 | * a timeout, read error or shutdown, we just silently close so |
| 2250 | * that the client can try again. |
| 2251 | */ |
| 2252 | txn->status = 0; |
| 2253 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2254 | req->analysers = 0; |
| 2255 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2256 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2257 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2258 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2259 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2260 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2261 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2262 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2263 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2264 | * 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] | 2265 | * 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] | 2266 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2267 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2268 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2269 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2270 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2271 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2272 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2273 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2274 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2275 | /* kill the pending keep-alive timeout */ |
| 2276 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2277 | req->analyse_exp = TICK_ETERNITY; |
| 2278 | } |
| 2279 | |
| 2280 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2281 | /* Maybe we found in invalid header name while we were configured not |
| 2282 | * to block on that, so we have to capture it now. |
| 2283 | */ |
| 2284 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2285 | 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] | 2286 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2287 | /* |
| 2288 | * 1: identify the method |
| 2289 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2290 | txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2291 | |
| 2292 | /* we can make use of server redirect on GET and HEAD */ |
| 2293 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2294 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2295 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2296 | /* |
| 2297 | * 2: check if the URI matches the monitor_uri. |
| 2298 | * We have to do this for every request which gets in, because |
| 2299 | * the monitor-uri is defined by the frontend. |
| 2300 | */ |
| 2301 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2302 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2303 | !memcmp(req->p + msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2304 | s->fe->monitor_uri, |
| 2305 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2306 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2307 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2308 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2309 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2310 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2311 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2312 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2313 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2314 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2315 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2316 | 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] | 2317 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2318 | ret = acl_pass(ret); |
| 2319 | if (cond->pol == ACL_COND_UNLESS) |
| 2320 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2321 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2322 | if (ret) { |
| 2323 | /* we fail this request, let's return 503 service unavail */ |
| 2324 | txn->status = 503; |
| 2325 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2326 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2327 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2328 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2329 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2330 | /* nothing to fail, let's reply normaly */ |
| 2331 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2332 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2333 | goto return_prx_cond; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2338 | * Note: we cannot log anymore if the request has been |
| 2339 | * classified as invalid. |
| 2340 | */ |
| 2341 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2342 | /* we have a complete HTTP request that we must log */ |
| 2343 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2344 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2345 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | if (urilen >= REQURI_LEN) |
| 2347 | urilen = REQURI_LEN - 1; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2348 | memcpy(txn->uri, &req->p[msg->som], urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2349 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2350 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2351 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2352 | s->do_log(s); |
| 2353 | } else { |
| 2354 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2355 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2357 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2358 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2359 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2360 | } |
| 2361 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2362 | /* 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] | 2363 | 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] | 2364 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2365 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2366 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2367 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2368 | ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') || |
| 2369 | ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') && |
| 2370 | (req->p[msg->sol + msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2371 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2372 | |
| 2373 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2374 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2375 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2376 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2377 | * we have what looks like a proxied connection instead of a connection, |
| 2378 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2379 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2380 | * happen to do that despite being non-standard :-( |
| 2381 | * We consider that a request not beginning with either '/' or '*' is |
| 2382 | * a proxied connection, which covers both "scheme://location" and |
| 2383 | * CONNECT ip:port. |
| 2384 | */ |
| 2385 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2386 | req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2387 | txn->flags |= TX_USE_PX_CONN; |
| 2388 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2389 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2390 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2391 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2392 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2393 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2394 | capture_headers(req->p + msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2395 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2396 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2397 | /* 6: determine the transfer-length. |
| 2398 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2399 | * the presence of a message-body in a REQUEST and its transfer length |
| 2400 | * must be determined that way (in order of precedence) : |
| 2401 | * 1. The presence of a message-body in a request is signaled by the |
| 2402 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2403 | * in the request's header fields. When a request message contains |
| 2404 | * both a message-body of non-zero length and a method that does |
| 2405 | * not define any semantics for that request message-body, then an |
| 2406 | * origin server SHOULD either ignore the message-body or respond |
| 2407 | * with an appropriate error message (e.g., 413). A proxy or |
| 2408 | * gateway, when presented the same request, SHOULD either forward |
| 2409 | * the request inbound with the message- body or ignore the |
| 2410 | * message-body when determining a response. |
| 2411 | * |
| 2412 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2413 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2414 | * transfer-length is defined by the use of this transfer-coding. |
| 2415 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2416 | * transfer-coding is not present, the transfer-length is defined |
| 2417 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2418 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2419 | * 3. If a Content-Length header field is present, its decimal value in |
| 2420 | * OCTETs represents both the entity-length and the transfer-length. |
| 2421 | * If a message is received with both a Transfer-Encoding header |
| 2422 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2423 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2424 | * 4. By the server closing the connection. (Closing the connection |
| 2425 | * cannot be used to indicate the end of a request body, since that |
| 2426 | * would leave no possibility for the server to send back a response.) |
| 2427 | * |
| 2428 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2429 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2430 | * it is terminated by closing the connection. When the "chunked" |
| 2431 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2432 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2433 | */ |
| 2434 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2435 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2436 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2437 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2438 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2439 | http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2440 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2441 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2442 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2443 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2444 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2445 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2446 | break; |
| 2447 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2448 | } |
| 2449 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2450 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2451 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2452 | http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2453 | signed long long cl; |
| 2454 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2455 | if (!ctx.vlen) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2456 | msg->err_pos = ctx.line + ctx.val - req->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2457 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2458 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2459 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2460 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2461 | msg->err_pos = ctx.line + ctx.val - req->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2462 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2463 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2464 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2465 | if (cl < 0) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2466 | msg->err_pos = ctx.line + ctx.val - req->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2467 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2468 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2469 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2470 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 2471 | msg->err_pos = ctx.line + ctx.val - req->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2472 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2473 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2474 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2475 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2476 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2479 | /* bodyless requests have a known length */ |
| 2480 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2481 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2482 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2483 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2484 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2485 | req->analyse_exp = TICK_ETERNITY; |
| 2486 | return 1; |
| 2487 | |
| 2488 | return_bad_req: |
| 2489 | /* We centralize bad requests processing here */ |
| 2490 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2491 | /* we detected a parsing error. We want to archive this request |
| 2492 | * in the dedicated proxy area for later troubleshooting. |
| 2493 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2494 | 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] | 2495 | } |
| 2496 | |
| 2497 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2498 | txn->status = 400; |
| 2499 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2500 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2501 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2502 | if (s->listener->counters) |
| 2503 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2504 | |
| 2505 | return_prx_cond: |
| 2506 | if (!(s->flags & SN_ERR_MASK)) |
| 2507 | s->flags |= SN_ERR_PRXCOND; |
| 2508 | if (!(s->flags & SN_FINST_MASK)) |
| 2509 | s->flags |= SN_FINST_R; |
| 2510 | |
| 2511 | req->analysers = 0; |
| 2512 | req->analyse_exp = TICK_ETERNITY; |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2516 | /* We reached the stats page through a POST request. |
| 2517 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2518 | * 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] | 2519 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2520 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2521 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2522 | struct proxy *px = NULL; |
| 2523 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2524 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2525 | char key[LINESIZE]; |
| 2526 | int action = ST_ADM_ACTION_NONE; |
| 2527 | int reprocess = 0; |
| 2528 | |
| 2529 | int total_servers = 0; |
| 2530 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2531 | |
| 2532 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2533 | char *st_cur_param = NULL; |
| 2534 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2535 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2536 | first_param = req->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2537 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2538 | |
| 2539 | cur_param = next_param = end_params; |
| 2540 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2541 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2542 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2543 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2544 | return 1; |
| 2545 | } |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2546 | else if (end_params > req->p + req->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2547 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2548 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2549 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | *end_params = '\0'; |
| 2553 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2554 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2555 | |
| 2556 | /* |
| 2557 | * Parse the parameters in reverse order to only store the last value. |
| 2558 | * From the html form, the backend and the action are at the end. |
| 2559 | */ |
| 2560 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2561 | char *value; |
| 2562 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2563 | |
| 2564 | cur_param--; |
| 2565 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2566 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2567 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2568 | poffset = (cur_param != first_param ? 1 : 0); |
| 2569 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2570 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2571 | strncpy(key, cur_param + poffset, plen); |
| 2572 | key[plen - 1] = '\0'; |
| 2573 | } else { |
| 2574 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2575 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | /* Parse the value */ |
| 2579 | value = key; |
| 2580 | while (*value != '\0' && *value != '=') { |
| 2581 | value++; |
| 2582 | } |
| 2583 | if (*value == '=') { |
| 2584 | /* Ok, a value is found, we can mark the end of the key */ |
| 2585 | *value++ = '\0'; |
| 2586 | } |
| 2587 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2588 | if (!url_decode(key) || !url_decode(value)) |
| 2589 | break; |
| 2590 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2591 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2592 | if (!px && (strcmp(key, "b") == 0)) { |
| 2593 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2594 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2595 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2596 | goto out; |
| 2597 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2598 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2599 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2600 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2601 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2602 | } |
| 2603 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2604 | action = ST_ADM_ACTION_ENABLE; |
| 2605 | } |
| 2606 | else { |
| 2607 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2608 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2612 | if (!(px && action)) { |
| 2613 | /* |
| 2614 | * Indicates that we'll need to reprocess the parameters |
| 2615 | * as soon as backend and action are known |
| 2616 | */ |
| 2617 | if (!reprocess) { |
| 2618 | st_cur_param = cur_param; |
| 2619 | st_next_param = next_param; |
| 2620 | } |
| 2621 | reprocess = 1; |
| 2622 | } |
| 2623 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2624 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2625 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2626 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2627 | /* Not already in maintenance, we can change the server state */ |
| 2628 | sv->state |= SRV_MAINTAIN; |
| 2629 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2630 | altered_servers++; |
| 2631 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2632 | } |
| 2633 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2634 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2635 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2636 | /* Already in maintenance, we can change the server state */ |
| 2637 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2638 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2639 | altered_servers++; |
| 2640 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2641 | } |
| 2642 | break; |
| 2643 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2644 | } else { |
| 2645 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2646 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2647 | } |
| 2648 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2649 | if (reprocess && px && action) { |
| 2650 | /* Now, we know the backend and the action chosen by the user. |
| 2651 | * We can safely restart from the first server parameter |
| 2652 | * to reprocess them |
| 2653 | */ |
| 2654 | cur_param = st_cur_param; |
| 2655 | next_param = st_next_param; |
| 2656 | reprocess = 0; |
| 2657 | goto reprocess_servers; |
| 2658 | } |
| 2659 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2660 | next_param = cur_param; |
| 2661 | } |
| 2662 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2663 | |
| 2664 | if (total_servers == 0) { |
| 2665 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2666 | } |
| 2667 | else if (altered_servers == 0) { |
| 2668 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2669 | } |
| 2670 | else if (altered_servers == total_servers) { |
| 2671 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2672 | } |
| 2673 | else { |
| 2674 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2675 | } |
| 2676 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2677 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2678 | } |
| 2679 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2680 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2681 | * or NULL if everything's OK. |
| 2682 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2683 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2684 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2685 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2686 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2687 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2688 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2689 | int ret = 1; |
| 2690 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2691 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2692 | continue; |
| 2693 | |
| 2694 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2695 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2696 | 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] | 2697 | ret = acl_pass(ret); |
| 2698 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2699 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2700 | ret = !ret; |
| 2701 | } |
| 2702 | |
| 2703 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2704 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2705 | return NULL; /* no problem */ |
| 2706 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2707 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | return NULL; |
| 2711 | } |
| 2712 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2713 | /* This stream analyser runs all HTTP request processing which is common to |
| 2714 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2715 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2716 | * 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] | 2717 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2718 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2719 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2720 | int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2721 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2722 | struct http_txn *txn = &s->txn; |
| 2723 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2724 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2725 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2726 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2727 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2728 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2729 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2730 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2731 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2732 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2733 | return 0; |
| 2734 | } |
| 2735 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2736 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2737 | req->analyse_exp = TICK_ETERNITY; |
| 2738 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2739 | 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] | 2740 | now_ms, __FUNCTION__, |
| 2741 | s, |
| 2742 | req, |
| 2743 | req->rex, req->wex, |
| 2744 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2745 | req->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2746 | req->analysers); |
| 2747 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2748 | /* first check whether we have some ACLs set to block this request */ |
| 2749 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2750 | 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] | 2751 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2752 | ret = acl_pass(ret); |
| 2753 | if (cond->pol == ACL_COND_UNLESS) |
| 2754 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2755 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2756 | if (ret) { |
| 2757 | txn->status = 403; |
| 2758 | /* let's log the request time */ |
| 2759 | s->logs.tv_request = now; |
| 2760 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2761 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2762 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2763 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2764 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2765 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2766 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2767 | http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2768 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2769 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2770 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2771 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2772 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2773 | http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2774 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2775 | else |
| 2776 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2777 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2778 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2779 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2780 | txn->status = 403; |
| 2781 | s->logs.tv_request = now; |
| 2782 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2783 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2784 | s->fe->fe_counters.denied_req++; |
| 2785 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2786 | s->be->be_counters.denied_req++; |
| 2787 | if (s->listener->counters) |
| 2788 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2789 | goto return_prx_cond; |
| 2790 | } |
| 2791 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2792 | /* try headers filters */ |
| 2793 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2794 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2795 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2796 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2797 | /* has the request been denied ? */ |
| 2798 | if (txn->flags & TX_CLDENY) { |
| 2799 | /* no need to go further */ |
| 2800 | txn->status = 403; |
| 2801 | /* let's log the request time */ |
| 2802 | s->logs.tv_request = now; |
| 2803 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2804 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2805 | goto return_prx_cond; |
| 2806 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2807 | |
| 2808 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2809 | * which may be the same as the connect timeout if unspecified. |
| 2810 | * If unset, then set it to zero because we really want it to |
| 2811 | * eventually expire. We build the tarpit as an analyser. |
| 2812 | */ |
| 2813 | if (txn->flags & TX_CLTARPIT) { |
| 2814 | buffer_erase(s->req); |
| 2815 | /* wipe the request out so that we can drop the connection early |
| 2816 | * if the client closes first. |
| 2817 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2818 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2819 | req->analysers = 0; /* remove switching rules etc... */ |
| 2820 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2821 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2822 | if (!req->analyse_exp) |
| 2823 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2824 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2825 | return 1; |
| 2826 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2827 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2828 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2829 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2830 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2831 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2832 | * 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] | 2833 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2834 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2835 | * The method consists in checking if options changed between the two calls |
| 2836 | * (implying that either one is non-null, or one of them is non-null and we |
| 2837 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2838 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2839 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2840 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2841 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2842 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2843 | (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] | 2844 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2845 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2846 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2847 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2848 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2849 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2850 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2851 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2852 | tmp = TX_CON_WANT_CLO; |
| 2853 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2854 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2855 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2856 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2857 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2858 | /* parse the Connection header and possibly clean it */ |
| 2859 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2860 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2861 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2862 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2863 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2864 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2865 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2866 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2867 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2868 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2869 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2870 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2871 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2872 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2873 | (!(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] | 2874 | ((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] | 2875 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2876 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2877 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2878 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2879 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2880 | /* we can be blocked here because the request needs to be authenticated, |
| 2881 | * either to pass or to access stats. |
| 2882 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2883 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2884 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2885 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2886 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2887 | if (!realm) |
| 2888 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2889 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2890 | sprintf(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] | 2891 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2892 | txn->status = 401; |
| 2893 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2894 | /* on 401 we still count one error, because normal browsing |
| 2895 | * won't significantly increase the counter but brute force |
| 2896 | * attempts will. |
| 2897 | */ |
| 2898 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2899 | goto return_prx_cond; |
| 2900 | } |
| 2901 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2902 | /* add request headers from the rule sets in the same order */ |
| 2903 | list_for_each_entry(wl, &px->req_add, list) { |
| 2904 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2905 | 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] | 2906 | ret = acl_pass(ret); |
| 2907 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2908 | ret = !ret; |
| 2909 | if (!ret) |
| 2910 | continue; |
| 2911 | } |
| 2912 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2913 | 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] | 2914 | goto return_bad_req; |
| 2915 | } |
| 2916 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2917 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2918 | struct stats_admin_rule *stats_admin_rule; |
| 2919 | |
| 2920 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2921 | * FIXME!!! that one is rather dangerous, we want to |
| 2922 | * make it follow standard rules (eg: clear req->analysers). |
| 2923 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2924 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2925 | /* now check whether we have some admin rules for this request */ |
| 2926 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2927 | int ret = 1; |
| 2928 | |
| 2929 | if (stats_admin_rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2930 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2931 | ret = acl_pass(ret); |
| 2932 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2933 | ret = !ret; |
| 2934 | } |
| 2935 | |
| 2936 | if (ret) { |
| 2937 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2938 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2939 | break; |
| 2940 | } |
| 2941 | } |
| 2942 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2943 | /* Was the status page requested with a POST ? */ |
| 2944 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2945 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2946 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2947 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2948 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2949 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2950 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2951 | struct hdr_ctx ctx; |
| 2952 | ctx.idx = 0; |
| 2953 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2954 | if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) && |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2955 | 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] | 2956 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2957 | } |
| 2958 | } |
| 2959 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2960 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2961 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2962 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2963 | /* we need more data */ |
| 2964 | req->analysers |= an_bit; |
| 2965 | buffer_dont_connect(req); |
| 2966 | return 0; |
| 2967 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2968 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2969 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2970 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2971 | } |
| 2972 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2973 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2974 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 2975 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 2976 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 2977 | s->rep->prod->applet.private = s; |
| 2978 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2979 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2980 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 2981 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2982 | |
| 2983 | return 0; |
| 2984 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2985 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2986 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2987 | /* check whether we have some ACLs set to redirect this request */ |
| 2988 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2989 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2990 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2991 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2992 | 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] | 2993 | ret = acl_pass(ret); |
| 2994 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2995 | ret = !ret; |
| 2996 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2997 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2998 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2999 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3000 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3001 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3002 | /* build redirect message */ |
| 3003 | switch(rule->code) { |
| 3004 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3005 | msg_fmt = HTTP_303; |
| 3006 | break; |
| 3007 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3008 | msg_fmt = HTTP_301; |
| 3009 | break; |
| 3010 | case 302: |
| 3011 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3012 | msg_fmt = HTTP_302; |
| 3013 | break; |
| 3014 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3015 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3016 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3017 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3018 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3019 | switch(rule->type) { |
| 3020 | case REDIRECT_TYPE_PREFIX: { |
| 3021 | const char *path; |
| 3022 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3024 | path = http_get_path(txn); |
| 3025 | /* build message using path */ |
| 3026 | if (path) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3027 | pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3028 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3029 | int qs = 0; |
| 3030 | while (qs < pathlen) { |
| 3031 | if (path[qs] == '?') { |
| 3032 | pathlen = qs; |
| 3033 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3034 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3035 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3036 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3037 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3038 | } else { |
| 3039 | path = "/"; |
| 3040 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3041 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3042 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3043 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3044 | goto return_bad_req; |
| 3045 | |
| 3046 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3047 | * add anything, otherwise it makes it hard for the user to |
| 3048 | * configure a self-redirection. |
| 3049 | */ |
| 3050 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3051 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3052 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3053 | } |
| 3054 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3055 | /* add path */ |
| 3056 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3057 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3058 | |
| 3059 | /* append a slash at the end of the location is needed and missing */ |
| 3060 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3061 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3062 | if (rdr.len > rdr.size - 5) |
| 3063 | goto return_bad_req; |
| 3064 | rdr.str[rdr.len] = '/'; |
| 3065 | rdr.len++; |
| 3066 | } |
| 3067 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3068 | break; |
| 3069 | } |
| 3070 | case REDIRECT_TYPE_LOCATION: |
| 3071 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3072 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3073 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3074 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3075 | /* add location */ |
| 3076 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3077 | rdr.len += rule->rdr_len; |
| 3078 | break; |
| 3079 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3080 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3081 | if (rule->cookie_len) { |
| 3082 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3083 | rdr.len += 14; |
| 3084 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3085 | rdr.len += rule->cookie_len; |
| 3086 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3087 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3088 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3089 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3090 | /* add end of headers and the keep-alive/close status. |
| 3091 | * We may choose to set keep-alive if the Location begins |
| 3092 | * with a slash, because the client will come back to the |
| 3093 | * same server. |
| 3094 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3095 | txn->status = rule->code; |
| 3096 | /* let's log the request time */ |
| 3097 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3098 | |
| 3099 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3100 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3101 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3102 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3103 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3104 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3105 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3106 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3107 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3108 | rdr.len += 30; |
| 3109 | } else { |
| 3110 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3111 | rdr.len += 24; |
| 3112 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3113 | } |
| 3114 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3115 | rdr.len += 4; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3116 | bo_inject(req->prod->ob, rdr.str, rdr.len); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3117 | /* "eat" the request */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3118 | bi_fast_delete(req, msg->sov - msg->som); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3119 | msg->som = msg->sov; |
| 3120 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3121 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3122 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3123 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3124 | break; |
| 3125 | } else { |
| 3126 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3127 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3128 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3129 | rdr.len += 29; |
| 3130 | } else { |
| 3131 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3132 | rdr.len += 23; |
| 3133 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3134 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3135 | goto return_prx_cond; |
| 3136 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3137 | } |
| 3138 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3139 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3140 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3141 | * If this happens, then the data will not come immediately, so we must |
| 3142 | * send all what we have without waiting. Note that due to the small gain |
| 3143 | * in waiting for the body of the request, it's easier to simply put the |
| 3144 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3145 | * itself once used. |
| 3146 | */ |
| 3147 | req->flags |= BF_SEND_DONTWAIT; |
| 3148 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3149 | /* that's OK for us now, let's move on to next analysers */ |
| 3150 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3151 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3152 | return_bad_req: |
| 3153 | /* We centralize bad requests processing here */ |
| 3154 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3155 | /* we detected a parsing error. We want to archive this request |
| 3156 | * in the dedicated proxy area for later troubleshooting. |
| 3157 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3158 | 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] | 3159 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3160 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3161 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3162 | txn->status = 400; |
| 3163 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3164 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3165 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3166 | if (s->listener->counters) |
| 3167 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3168 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3169 | return_prx_cond: |
| 3170 | if (!(s->flags & SN_ERR_MASK)) |
| 3171 | s->flags |= SN_ERR_PRXCOND; |
| 3172 | if (!(s->flags & SN_FINST_MASK)) |
| 3173 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3174 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3175 | req->analysers = 0; |
| 3176 | req->analyse_exp = TICK_ETERNITY; |
| 3177 | return 0; |
| 3178 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3179 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3180 | /* This function performs all the processing enabled for the current request. |
| 3181 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3182 | * needs more data, encounters an error, or wants to immediately abort the |
| 3183 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3184 | */ |
| 3185 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3186 | { |
| 3187 | struct http_txn *txn = &s->txn; |
| 3188 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3189 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3190 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3191 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3192 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3193 | return 0; |
| 3194 | } |
| 3195 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3196 | 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] | 3197 | now_ms, __FUNCTION__, |
| 3198 | s, |
| 3199 | req, |
| 3200 | req->rex, req->wex, |
| 3201 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3202 | req->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3203 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3204 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3205 | /* |
| 3206 | * Right now, we know that we have processed the entire headers |
| 3207 | * and that unwanted requests have been filtered out. We can do |
| 3208 | * whatever we want with the remaining request. Also, now we |
| 3209 | * may have separate values for ->fe, ->be. |
| 3210 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3211 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3212 | /* |
| 3213 | * If HTTP PROXY is set we simply get remote server address |
| 3214 | * parsing incoming request. |
| 3215 | */ |
| 3216 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3217 | url2sa(req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3218 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3219 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3220 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3221 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3222 | * Note that doing so might move headers in the request, but |
| 3223 | * the fields will stay coherent and the URI will not move. |
| 3224 | * This should only be performed in the backend. |
| 3225 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3226 | 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] | 3227 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3228 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3229 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3230 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3231 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3232 | * so let's do the same now. |
| 3233 | */ |
| 3234 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3235 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3236 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3237 | get_srv_from_appsession(s, req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3238 | } |
| 3239 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3240 | /* add unique-id if "header-unique-id" is specified */ |
| 3241 | |
| 3242 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3243 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3244 | |
| 3245 | if (s->fe->header_unique_id && s->unique_id) { |
| 3246 | int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3247 | if (ret < 0 || ret > global.tune.bufsize) |
| 3248 | goto return_bad_req; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3249 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3250 | goto return_bad_req; |
| 3251 | } |
| 3252 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3253 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3254 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3255 | * asks for it. |
| 3256 | */ |
| 3257 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3258 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3259 | |
| 3260 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3261 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3262 | /* The header is set to be added only if none is present |
| 3263 | * and we found it, so don't do anything. |
| 3264 | */ |
| 3265 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3266 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3267 | /* Add an X-Forwarded-For header unless the source IP is |
| 3268 | * in the 'except' network range. |
| 3269 | */ |
| 3270 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3271 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3272 | != s->fe->except_net.s_addr) && |
| 3273 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3274 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3275 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3276 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3277 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3278 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3279 | |
| 3280 | /* Note: we rely on the backend to get the header name to be used for |
| 3281 | * x-forwarded-for, because the header is really meant for the backends. |
| 3282 | * However, if the backend did not specify any option, we have to rely |
| 3283 | * on the frontend's header name. |
| 3284 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3285 | if (s->be->fwdfor_hdr_len) { |
| 3286 | len = s->be->fwdfor_hdr_len; |
| 3287 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3288 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3289 | len = s->fe->fwdfor_hdr_len; |
| 3290 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3291 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3292 | len += sprintf(trash + 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] | 3293 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3294 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3295 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3296 | } |
| 3297 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3298 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3299 | /* FIXME: for the sake of completeness, we should also support |
| 3300 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3301 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3302 | int len; |
| 3303 | char pn[INET6_ADDRSTRLEN]; |
| 3304 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3305 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3306 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3307 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3308 | /* Note: we rely on the backend to get the header name to be used for |
| 3309 | * x-forwarded-for, because the header is really meant for the backends. |
| 3310 | * However, if the backend did not specify any option, we have to rely |
| 3311 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3312 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3313 | if (s->be->fwdfor_hdr_len) { |
| 3314 | len = s->be->fwdfor_hdr_len; |
| 3315 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3316 | } else { |
| 3317 | len = s->fe->fwdfor_hdr_len; |
| 3318 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3319 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3320 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3321 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3322 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3323 | goto return_bad_req; |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3328 | * 10: add X-Original-To if either the frontend or the backend |
| 3329 | * asks for it. |
| 3330 | */ |
| 3331 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3332 | |
| 3333 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3334 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3335 | /* Add an X-Original-To header unless the destination IP is |
| 3336 | * in the 'except' network range. |
| 3337 | */ |
Willy Tarreau | 9b061e3 | 2012-04-07 18:03:52 +0200 | [diff] [blame] | 3338 | stream_sock_get_to_addr(s->req->prod); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3339 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3340 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3341 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3342 | (((struct sockaddr_in *)&s->req->prod->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] | 3343 | != s->fe->except_to.s_addr) && |
| 3344 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3345 | (((struct sockaddr_in *)&s->req->prod->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] | 3346 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3347 | int len; |
| 3348 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3349 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3350 | |
| 3351 | /* Note: we rely on the backend to get the header name to be used for |
| 3352 | * x-original-to, because the header is really meant for the backends. |
| 3353 | * However, if the backend did not specify any option, we have to rely |
| 3354 | * on the frontend's header name. |
| 3355 | */ |
| 3356 | if (s->be->orgto_hdr_len) { |
| 3357 | len = s->be->orgto_hdr_len; |
| 3358 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3359 | } else { |
| 3360 | len = s->fe->orgto_hdr_len; |
| 3361 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3362 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3363 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3364 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3365 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3366 | goto return_bad_req; |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3371 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3372 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3373 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3374 | unsigned int want_flags = 0; |
| 3375 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3376 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3377 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3378 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3379 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3380 | want_flags |= TX_CON_CLO_SET; |
| 3381 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3382 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3383 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3384 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3385 | want_flags |= TX_CON_KAL_SET; |
| 3386 | } |
| 3387 | |
| 3388 | 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] | 3389 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3390 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3391 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3392 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3393 | /* If we have no server assigned yet and we're balancing on url_param |
| 3394 | * with a POST request, we may be interested in checking the body for |
| 3395 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3396 | */ |
| 3397 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3398 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3399 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3400 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3401 | buffer_dont_connect(req); |
| 3402 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3403 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3404 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3405 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3406 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3407 | #ifdef TCP_QUICKACK |
| 3408 | /* We expect some data from the client. Unless we know for sure |
| 3409 | * we already have a full request, we have to re-enable quick-ack |
| 3410 | * in case we previously disabled it, otherwise we might cause |
| 3411 | * the client to delay further data. |
| 3412 | */ |
| 3413 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3414 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3415 | (msg->body_len > req->i - txn->req.eoh - 2))) |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3416 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3417 | #endif |
| 3418 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3419 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3420 | /************************************************************* |
| 3421 | * OK, that's finished for the headers. We have done what we * |
| 3422 | * could. Let's switch to the DATA state. * |
| 3423 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3424 | req->analyse_exp = TICK_ETERNITY; |
| 3425 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3426 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3427 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3428 | /* OK let's go on with the BODY now */ |
| 3429 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3430 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3431 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3432 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3433 | /* we detected a parsing error. We want to archive this request |
| 3434 | * in the dedicated proxy area for later troubleshooting. |
| 3435 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3436 | 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] | 3437 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3438 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3439 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3440 | txn->status = 400; |
| 3441 | req->analysers = 0; |
| 3442 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3443 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3444 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3445 | if (s->listener->counters) |
| 3446 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3447 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3448 | if (!(s->flags & SN_ERR_MASK)) |
| 3449 | s->flags |= SN_ERR_PRXCOND; |
| 3450 | if (!(s->flags & SN_FINST_MASK)) |
| 3451 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3452 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3453 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3454 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3455 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3456 | * returns zero, at the beginning because it prevents any other processing |
| 3457 | * from occurring, and at the end because it terminates the request. |
| 3458 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3459 | int http_process_tarpit(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3460 | { |
| 3461 | struct http_txn *txn = &s->txn; |
| 3462 | |
| 3463 | /* This connection is being tarpitted. The CLIENT side has |
| 3464 | * already set the connect expiration date to the right |
| 3465 | * timeout. We just have to check that the client is still |
| 3466 | * there and that the timeout has not expired. |
| 3467 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3468 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3469 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3470 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3471 | return 0; |
| 3472 | |
| 3473 | /* We will set the queue timer to the time spent, just for |
| 3474 | * logging purposes. We fake a 500 server error, so that the |
| 3475 | * attacker will not suspect his connection has been tarpitted. |
| 3476 | * It will not cause trouble to the logs because we can exclude |
| 3477 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3478 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3479 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3480 | |
| 3481 | txn->status = 500; |
| 3482 | if (req->flags != BF_READ_ERROR) |
| 3483 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3484 | |
| 3485 | req->analysers = 0; |
| 3486 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3487 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3488 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3489 | if (s->listener->counters) |
| 3490 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3491 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3492 | if (!(s->flags & SN_ERR_MASK)) |
| 3493 | s->flags |= SN_ERR_PRXCOND; |
| 3494 | if (!(s->flags & SN_FINST_MASK)) |
| 3495 | s->flags |= SN_FINST_T; |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3499 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3500 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3501 | * must only be called after the standard HTTP request processing has occurred, |
| 3502 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3503 | * read more data, or 1 once it has completed its analysis. |
| 3504 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3505 | int http_process_request_body(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3506 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3507 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3508 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3509 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3510 | |
| 3511 | /* We have to parse the HTTP request body to find any required data. |
| 3512 | * "balance url_param check_post" should have been the only way to get |
| 3513 | * into this. We were brought here after HTTP header analysis, so all |
| 3514 | * related structures are ready. |
| 3515 | */ |
| 3516 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3517 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3518 | goto missing_data; |
| 3519 | |
| 3520 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3521 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3522 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3523 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3524 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3525 | struct hdr_ctx ctx; |
| 3526 | ctx.idx = 0; |
| 3527 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3528 | if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3529 | 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] | 3530 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3531 | } |
| 3532 | } |
| 3533 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3534 | } |
| 3535 | |
| 3536 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3537 | /* we have msg->sov which points to the first byte of message body. |
| 3538 | * msg->som still points to the beginning of the message. We must |
| 3539 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3540 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3541 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3542 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3543 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3544 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3545 | else |
| 3546 | msg->msg_state = HTTP_MSG_DATA; |
| 3547 | } |
| 3548 | |
| 3549 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3550 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3551 | * 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] | 3552 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3553 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 3554 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3555 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3556 | if (!ret) |
| 3557 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3558 | else if (ret < 0) { |
| 3559 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3560 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3561 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3562 | } |
| 3563 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3564 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3565 | * We have the first data byte is in msg->sov. We're waiting for at |
| 3566 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3567 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3568 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3569 | if (msg->body_len < limit) |
| 3570 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3571 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3572 | if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3573 | goto http_end; |
| 3574 | |
| 3575 | missing_data: |
| 3576 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3577 | if (req->flags & BF_FULL) { |
| 3578 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3579 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3580 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3581 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3582 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3583 | txn->status = 408; |
| 3584 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3585 | |
| 3586 | if (!(s->flags & SN_ERR_MASK)) |
| 3587 | s->flags |= SN_ERR_CLITO; |
| 3588 | if (!(s->flags & SN_FINST_MASK)) |
| 3589 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3590 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3591 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3592 | |
| 3593 | /* we get here if we need to wait for more data */ |
| 3594 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3595 | /* Not enough data. We'll re-use the http-request |
| 3596 | * timeout here. Ideally, we should set the timeout |
| 3597 | * relative to the accept() date. We just set the |
| 3598 | * request timeout once at the beginning of the |
| 3599 | * request. |
| 3600 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3601 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3602 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3603 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3604 | return 0; |
| 3605 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3606 | |
| 3607 | http_end: |
| 3608 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3609 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3610 | req->analysers &= ~an_bit; |
| 3611 | req->analyse_exp = TICK_ETERNITY; |
| 3612 | return 1; |
| 3613 | |
| 3614 | return_bad_req: /* let's centralize all bad requests */ |
| 3615 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3616 | txn->status = 400; |
| 3617 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3618 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3619 | if (!(s->flags & SN_ERR_MASK)) |
| 3620 | s->flags |= SN_ERR_PRXCOND; |
| 3621 | if (!(s->flags & SN_FINST_MASK)) |
| 3622 | s->flags |= SN_FINST_R; |
| 3623 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3624 | return_err_msg: |
| 3625 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3626 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3627 | if (s->listener->counters) |
| 3628 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3629 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3630 | } |
| 3631 | |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3632 | /* send a server's name with an outgoing request over an established connection */ |
| 3633 | 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] | 3634 | |
| 3635 | struct hdr_ctx ctx; |
| 3636 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3637 | char *hdr_name = be->server_id_hdr_name; |
| 3638 | int hdr_name_len = be->server_id_hdr_len; |
| 3639 | |
| 3640 | char *hdr_val; |
| 3641 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3642 | ctx.idx = 0; |
| 3643 | |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3644 | while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3645 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3646 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | /* Add the new header requested with the server value */ |
| 3650 | hdr_val = trash; |
| 3651 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3652 | hdr_val += hdr_name_len; |
| 3653 | *hdr_val++ = ':'; |
| 3654 | *hdr_val++ = ' '; |
| 3655 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3656 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3657 | |
| 3658 | return 0; |
| 3659 | } |
| 3660 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3661 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3662 | * right now but it works. |
| 3663 | */ |
| 3664 | void http_end_txn_clean_session(struct session *s) |
| 3665 | { |
| 3666 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3667 | * server's connections. We need a safer way to reinitialize buffer |
| 3668 | * flags. We also need a more accurate method for computing per-request |
| 3669 | * data. |
| 3670 | */ |
| 3671 | http_silent_debug(__LINE__, s); |
| 3672 | |
| 3673 | s->req->cons->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame] | 3674 | s->req->cons->sock.shutr(s->req->cons); |
| 3675 | s->req->cons->sock.shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3676 | |
| 3677 | http_silent_debug(__LINE__, s); |
| 3678 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3679 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3680 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3681 | if (unlikely(s->srv_conn)) |
| 3682 | sess_change_server(s, NULL); |
| 3683 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3684 | |
| 3685 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3686 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3687 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3688 | |
| 3689 | if (s->txn.status) { |
| 3690 | int n; |
| 3691 | |
| 3692 | n = s->txn.status / 100; |
| 3693 | if (n < 1 || n > 5) |
| 3694 | n = 0; |
| 3695 | |
| 3696 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3697 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3698 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3699 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3700 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3701 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /* don't count other requests' data */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3705 | s->logs.bytes_in -= s->req->i; |
| 3706 | s->logs.bytes_out -= s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3707 | |
| 3708 | /* let's do a final log if we need it */ |
| 3709 | if (s->logs.logwait && |
| 3710 | !(s->flags & SN_MONITOR) && |
| 3711 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3712 | s->do_log(s); |
| 3713 | } |
| 3714 | |
| 3715 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3716 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3717 | tv_zero(&s->logs.tv_request); |
| 3718 | s->logs.t_queue = -1; |
| 3719 | s->logs.t_connect = -1; |
| 3720 | s->logs.t_data = -1; |
| 3721 | s->logs.t_close = 0; |
| 3722 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3723 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3724 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3725 | s->logs.bytes_in = s->req->total = s->req->i; |
| 3726 | s->logs.bytes_out = s->rep->total = s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3727 | |
| 3728 | if (s->pend_pos) |
| 3729 | pendconn_free(s->pend_pos); |
| 3730 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3731 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3732 | if (s->flags & SN_CURR_SESS) { |
| 3733 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3734 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3735 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3736 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3737 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3738 | } |
| 3739 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3740 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3741 | |
| 3742 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3743 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3744 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3745 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3746 | s->req->cons->err_loc = NULL; |
| 3747 | s->req->cons->exp = TICK_ETERNITY; |
| 3748 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3749 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 3750 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL|BF_NEVER_WAIT); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3751 | 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] | 3752 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3753 | s->txn.meth = 0; |
| 3754 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3755 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3756 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3757 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3758 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3759 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 3760 | s->req->flags |= BF_NEVER_WAIT; |
| 3761 | s->rep->flags |= BF_NEVER_WAIT; |
| 3762 | } |
| 3763 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3764 | /* if the request buffer is not empty, it means we're |
| 3765 | * about to process another request, so send pending |
| 3766 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3767 | * Just don't do this if the buffer is close to be full, |
| 3768 | * because the request will wait for it to flush a little |
| 3769 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3770 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3771 | if (s->req->i) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 3772 | if (s->rep->o && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3773 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 3774 | bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3775 | s->rep->flags |= BF_EXPECT_MORE; |
| 3776 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3777 | |
| 3778 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3779 | buffer_auto_read(s->req); |
| 3780 | buffer_auto_close(s->req); |
| 3781 | buffer_auto_read(s->rep); |
| 3782 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3783 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3784 | s->req->analysers = s->listener->analysers; |
| 3785 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3786 | s->rep->analysers = 0; |
| 3787 | |
| 3788 | http_silent_debug(__LINE__, s); |
| 3789 | } |
| 3790 | |
| 3791 | |
| 3792 | /* This function updates the request state machine according to the response |
| 3793 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3794 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3795 | * it is only used to find when a request/response couple is complete. Both |
| 3796 | * this function and its equivalent should loop until both return zero. It |
| 3797 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3798 | */ |
| 3799 | int http_sync_req_state(struct session *s) |
| 3800 | { |
| 3801 | struct buffer *buf = s->req; |
| 3802 | struct http_txn *txn = &s->txn; |
| 3803 | unsigned int old_flags = buf->flags; |
| 3804 | unsigned int old_state = txn->req.msg_state; |
| 3805 | |
| 3806 | http_silent_debug(__LINE__, s); |
| 3807 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3808 | return 0; |
| 3809 | |
| 3810 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3811 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3812 | * We can shut the read side unless we want to abort_on_close, |
| 3813 | * or we have a POST request. The issue with POST requests is |
| 3814 | * that some browsers still send a CRLF after the request, and |
| 3815 | * this CRLF must be read so that it does not remain in the kernel |
| 3816 | * buffers, otherwise a close could cause an RST on some systems |
| 3817 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3818 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3819 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3820 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3821 | |
| 3822 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3823 | goto wait_other_side; |
| 3824 | |
| 3825 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3826 | /* The server has not finished to respond, so we |
| 3827 | * don't want to move in order not to upset it. |
| 3828 | */ |
| 3829 | goto wait_other_side; |
| 3830 | } |
| 3831 | |
| 3832 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3833 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3834 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3835 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3836 | goto wait_other_side; |
| 3837 | } |
| 3838 | |
| 3839 | /* When we get here, it means that both the request and the |
| 3840 | * response have finished receiving. Depending on the connection |
| 3841 | * mode, we'll have to wait for the last bytes to leave in either |
| 3842 | * direction, and sometimes for a close to be effective. |
| 3843 | */ |
| 3844 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3845 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3846 | /* Server-close mode : queue a connection close to the server */ |
| 3847 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3848 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3849 | } |
| 3850 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3851 | /* Option forceclose is set, or either side wants to close, |
| 3852 | * let's enforce it now that we're not expecting any new |
| 3853 | * data to come. The caller knows the session is complete |
| 3854 | * once both states are CLOSED. |
| 3855 | */ |
| 3856 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3857 | buffer_shutr_now(buf); |
| 3858 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3859 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3860 | } |
| 3861 | else { |
| 3862 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3863 | * mode does not set the body analyser, we can't reach this place |
| 3864 | * in tunnel mode, so we're left with keep-alive only. |
| 3865 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3866 | */ |
| 3867 | buffer_auto_read(buf); |
| 3868 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3872 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3873 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3874 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3875 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3876 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3877 | goto http_msg_closing; |
| 3878 | } |
| 3879 | else { |
| 3880 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3881 | goto http_msg_closed; |
| 3882 | } |
| 3883 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3884 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3888 | http_msg_closing: |
| 3889 | /* nothing else to forward, just waiting for the output buffer |
| 3890 | * to be empty and for the shutw_now to take effect. |
| 3891 | */ |
| 3892 | if (buf->flags & BF_OUT_EMPTY) { |
| 3893 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3894 | goto http_msg_closed; |
| 3895 | } |
| 3896 | else if (buf->flags & BF_SHUTW) { |
| 3897 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3898 | goto wait_other_side; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3903 | http_msg_closed: |
| 3904 | goto wait_other_side; |
| 3905 | } |
| 3906 | |
| 3907 | wait_other_side: |
| 3908 | http_silent_debug(__LINE__, s); |
| 3909 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3910 | } |
| 3911 | |
| 3912 | |
| 3913 | /* This function updates the response state machine according to the request |
| 3914 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3915 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3916 | * it is only used to find when a request/response couple is complete. Both |
| 3917 | * this function and its equivalent should loop until both return zero. It |
| 3918 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3919 | */ |
| 3920 | int http_sync_res_state(struct session *s) |
| 3921 | { |
| 3922 | struct buffer *buf = s->rep; |
| 3923 | struct http_txn *txn = &s->txn; |
| 3924 | unsigned int old_flags = buf->flags; |
| 3925 | unsigned int old_state = txn->rsp.msg_state; |
| 3926 | |
| 3927 | http_silent_debug(__LINE__, s); |
| 3928 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3929 | return 0; |
| 3930 | |
| 3931 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3932 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3933 | * still monitor the server connection for a possible close |
| 3934 | * while the request is being uploaded, so we don't disable |
| 3935 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3937 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3938 | |
| 3939 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3940 | goto wait_other_side; |
| 3941 | |
| 3942 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3943 | /* The client seems to still be sending data, probably |
| 3944 | * because we got an error response during an upload. |
| 3945 | * We have the choice of either breaking the connection |
| 3946 | * or letting it pass through. Let's do the later. |
| 3947 | */ |
| 3948 | goto wait_other_side; |
| 3949 | } |
| 3950 | |
| 3951 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3952 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3953 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3954 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3955 | goto wait_other_side; |
| 3956 | } |
| 3957 | |
| 3958 | /* When we get here, it means that both the request and the |
| 3959 | * response have finished receiving. Depending on the connection |
| 3960 | * mode, we'll have to wait for the last bytes to leave in either |
| 3961 | * direction, and sometimes for a close to be effective. |
| 3962 | */ |
| 3963 | |
| 3964 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3965 | /* Server-close mode : shut read and wait for the request |
| 3966 | * side to close its output buffer. The caller will detect |
| 3967 | * when we're in DONE and the other is in CLOSED and will |
| 3968 | * catch that for the final cleanup. |
| 3969 | */ |
| 3970 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3971 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3972 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3973 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3974 | /* Option forceclose is set, or either side wants to close, |
| 3975 | * let's enforce it now that we're not expecting any new |
| 3976 | * data to come. The caller knows the session is complete |
| 3977 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3978 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3979 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3980 | buffer_shutr_now(buf); |
| 3981 | buffer_shutw_now(buf); |
| 3982 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3983 | } |
| 3984 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3985 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3986 | * mode does not set the body analyser, we can't reach this place |
| 3987 | * in tunnel mode, so we're left with keep-alive only. |
| 3988 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3989 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3990 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3991 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3995 | /* if we've just closed an output, let's switch */ |
| 3996 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3997 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3998 | goto http_msg_closing; |
| 3999 | } |
| 4000 | else { |
| 4001 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4002 | goto http_msg_closed; |
| 4003 | } |
| 4004 | } |
| 4005 | goto wait_other_side; |
| 4006 | } |
| 4007 | |
| 4008 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4009 | http_msg_closing: |
| 4010 | /* nothing else to forward, just waiting for the output buffer |
| 4011 | * to be empty and for the shutw_now to take effect. |
| 4012 | */ |
| 4013 | if (buf->flags & BF_OUT_EMPTY) { |
| 4014 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4015 | goto http_msg_closed; |
| 4016 | } |
| 4017 | else if (buf->flags & BF_SHUTW) { |
| 4018 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4019 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4020 | if (target_srv(&s->target)) |
| 4021 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4022 | goto wait_other_side; |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4027 | http_msg_closed: |
| 4028 | /* drop any pending data */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4029 | bi_erase(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4030 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4031 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4032 | goto wait_other_side; |
| 4033 | } |
| 4034 | |
| 4035 | wait_other_side: |
| 4036 | http_silent_debug(__LINE__, s); |
| 4037 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4038 | } |
| 4039 | |
| 4040 | |
| 4041 | /* Resync the request and response state machines. Return 1 if either state |
| 4042 | * changes. |
| 4043 | */ |
| 4044 | int http_resync_states(struct session *s) |
| 4045 | { |
| 4046 | struct http_txn *txn = &s->txn; |
| 4047 | int old_req_state = txn->req.msg_state; |
| 4048 | int old_res_state = txn->rsp.msg_state; |
| 4049 | |
| 4050 | http_silent_debug(__LINE__, s); |
| 4051 | http_sync_req_state(s); |
| 4052 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4053 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4054 | if (!http_sync_res_state(s)) |
| 4055 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4056 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4057 | if (!http_sync_req_state(s)) |
| 4058 | break; |
| 4059 | } |
| 4060 | http_silent_debug(__LINE__, s); |
| 4061 | /* OK, both state machines agree on a compatible state. |
| 4062 | * There are a few cases we're interested in : |
| 4063 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4064 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4065 | * directions, so let's simply disable both analysers. |
| 4066 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4067 | * request. |
| 4068 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4069 | * with server-close mode means we've completed one request and we |
| 4070 | * must re-initialize the server connection. |
| 4071 | */ |
| 4072 | |
| 4073 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4074 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4075 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4076 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4077 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4078 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4079 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4080 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4081 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4082 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4083 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4084 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4085 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4086 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4087 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4088 | s->rep->analysers = 0; |
| 4089 | buffer_auto_close(s->rep); |
| 4090 | buffer_auto_read(s->rep); |
| 4091 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4092 | buffer_abort(s->req); |
| 4093 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4094 | buffer_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4095 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4096 | } |
| 4097 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4098 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4099 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4100 | /* server-close: terminate this server connection and |
| 4101 | * reinitialize a fresh-new transaction. |
| 4102 | */ |
| 4103 | http_end_txn_clean_session(s); |
| 4104 | } |
| 4105 | |
| 4106 | http_silent_debug(__LINE__, s); |
| 4107 | return txn->req.msg_state != old_req_state || |
| 4108 | txn->rsp.msg_state != old_res_state; |
| 4109 | } |
| 4110 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4111 | /* This function is an analyser which forwards request body (including chunk |
| 4112 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4113 | * zero byte. The only situation where it must not be called is when we're in |
| 4114 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4115 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4116 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4117 | * 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] | 4118 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4119 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4120 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4121 | */ |
| 4122 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4123 | { |
| 4124 | struct http_txn *txn = &s->txn; |
| 4125 | struct http_msg *msg = &s->txn.req; |
| 4126 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4127 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4128 | return 0; |
| 4129 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4130 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4131 | ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4132 | /* Output closed while we were sending data. We must abort and |
| 4133 | * wake the other side up. |
| 4134 | */ |
| 4135 | msg->msg_state = HTTP_MSG_ERROR; |
| 4136 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4137 | return 1; |
| 4138 | } |
| 4139 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4140 | /* in most states, we should abort in case of early close */ |
| 4141 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4142 | |
| 4143 | /* Note that we don't have to send 100-continue back because we don't |
| 4144 | * need the data to complete our job, and it's up to the server to |
| 4145 | * decide whether to return 100, 417 or anything else in return of |
| 4146 | * an "Expect: 100-continue" header. |
| 4147 | */ |
| 4148 | |
| 4149 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4150 | /* we have msg->sov which points to the first byte of message body. |
| 4151 | * msg->som still points to the beginning of the message. We must |
| 4152 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4153 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4154 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4155 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4156 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4157 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4158 | else { |
| 4159 | msg->msg_state = HTTP_MSG_DATA; |
| 4160 | } |
| 4161 | } |
| 4162 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4163 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4164 | int bytes; |
| 4165 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4166 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4167 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4168 | bytes = msg->sov - msg->som; |
| 4169 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4170 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4171 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4172 | bytes += req->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4173 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4174 | msg->chunk_len += (unsigned int)bytes; |
| 4175 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4176 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4177 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4178 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4179 | /* must still forward */ |
| 4180 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4181 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4182 | |
| 4183 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4184 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4185 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4186 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4187 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4188 | } |
| 4189 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4190 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4191 | * 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] | 4192 | * TRAILERS state. |
| 4193 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4194 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4195 | |
| 4196 | if (!ret) |
| 4197 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4198 | else if (ret < 0) { |
| 4199 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4200 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4201 | 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] | 4202 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4203 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4204 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4205 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4206 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4207 | /* we want the CRLF after the data */ |
| 4208 | int ret; |
| 4209 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4210 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4211 | |
| 4212 | if (ret == 0) |
| 4213 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4214 | else if (ret < 0) { |
| 4215 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4216 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4217 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4218 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4219 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4220 | /* we're in MSG_CHUNK_SIZE now */ |
| 4221 | } |
| 4222 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4223 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4224 | |
| 4225 | if (ret == 0) |
| 4226 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4227 | else if (ret < 0) { |
| 4228 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4229 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4230 | 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] | 4231 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4232 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4233 | /* we're in HTTP_MSG_DONE now */ |
| 4234 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4235 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4236 | int old_state = msg->msg_state; |
| 4237 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4238 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4239 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4240 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4241 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4242 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4243 | if (http_resync_states(s)) { |
| 4244 | /* some state changes occurred, maybe the analyser |
| 4245 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4246 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4247 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4248 | if (req->flags & BF_SHUTW) { |
| 4249 | /* request errors are most likely due to |
| 4250 | * the server aborting the transfer. |
| 4251 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4252 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4253 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4254 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4255 | 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] | 4256 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4257 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4258 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4259 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4260 | |
| 4261 | /* If "option abortonclose" is set on the backend, we |
| 4262 | * want to monitor the client's connection and forward |
| 4263 | * any shutdown notification to the server, which will |
| 4264 | * decide whether to close or to go on processing the |
| 4265 | * request. |
| 4266 | */ |
| 4267 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4268 | buffer_auto_read(req); |
| 4269 | buffer_auto_close(req); |
| 4270 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4271 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4272 | /* POST requests may require to read extra CRLF |
| 4273 | * sent by broken browsers and which could cause |
| 4274 | * an RST to be sent upon close on some systems |
| 4275 | * (eg: Linux). |
| 4276 | */ |
| 4277 | buffer_auto_read(req); |
| 4278 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4279 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4280 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4281 | } |
| 4282 | } |
| 4283 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4284 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4285 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4286 | if (req->flags & BF_SHUTR) { |
| 4287 | if (!(s->flags & SN_ERR_MASK)) |
| 4288 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4289 | if (!(s->flags & SN_FINST_MASK)) { |
| 4290 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4291 | s->flags |= SN_FINST_H; |
| 4292 | else |
| 4293 | s->flags |= SN_FINST_D; |
| 4294 | } |
| 4295 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4296 | s->fe->fe_counters.cli_aborts++; |
| 4297 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4298 | if (target_srv(&s->target)) |
| 4299 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4300 | |
| 4301 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4302 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4303 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4304 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4305 | if (req->flags & BF_SHUTW) |
| 4306 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4308 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4309 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4310 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4311 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4312 | buffer_dont_close(req); |
| 4313 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4314 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4315 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4316 | * 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] | 4317 | * modes are already handled by the stream sock layer. We must not do |
| 4318 | * this in content-length mode because it could present the MSG_MORE |
| 4319 | * flag with the last block of forwarded data, which would cause an |
| 4320 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4321 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4322 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4323 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4324 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4325 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4326 | return 0; |
| 4327 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4328 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4329 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4330 | if (s->listener->counters) |
| 4331 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4332 | return_bad_req_stats_ok: |
| 4333 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4334 | if (txn->status) { |
| 4335 | /* Note: we don't send any error if some data were already sent */ |
| 4336 | stream_int_retnclose(req->prod, NULL); |
| 4337 | } else { |
| 4338 | txn->status = 400; |
| 4339 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4340 | } |
| 4341 | req->analysers = 0; |
| 4342 | 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] | 4343 | |
| 4344 | if (!(s->flags & SN_ERR_MASK)) |
| 4345 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4346 | if (!(s->flags & SN_FINST_MASK)) { |
| 4347 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4348 | s->flags |= SN_FINST_H; |
| 4349 | else |
| 4350 | s->flags |= SN_FINST_D; |
| 4351 | } |
| 4352 | return 0; |
| 4353 | |
| 4354 | aborted_xfer: |
| 4355 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4356 | if (txn->status) { |
| 4357 | /* Note: we don't send any error if some data were already sent */ |
| 4358 | stream_int_retnclose(req->prod, NULL); |
| 4359 | } else { |
| 4360 | txn->status = 502; |
| 4361 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4362 | } |
| 4363 | req->analysers = 0; |
| 4364 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4365 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4366 | s->fe->fe_counters.srv_aborts++; |
| 4367 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4368 | if (target_srv(&s->target)) |
| 4369 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4370 | |
| 4371 | if (!(s->flags & SN_ERR_MASK)) |
| 4372 | s->flags |= SN_ERR_SRVCL; |
| 4373 | if (!(s->flags & SN_FINST_MASK)) { |
| 4374 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4375 | s->flags |= SN_FINST_H; |
| 4376 | else |
| 4377 | s->flags |= SN_FINST_D; |
| 4378 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4379 | return 0; |
| 4380 | } |
| 4381 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4382 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4383 | * processing can continue on next analysers, or zero if it either needs more |
| 4384 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4385 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4386 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4387 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4388 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4389 | int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4390 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4391 | struct http_txn *txn = &s->txn; |
| 4392 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4393 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4394 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4395 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4396 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4397 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4398 | 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] | 4399 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4400 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4401 | rep, |
| 4402 | rep->rex, rep->wex, |
| 4403 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4404 | rep->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4405 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4406 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4407 | /* |
| 4408 | * Now parse the partial (or complete) lines. |
| 4409 | * We will check the response syntax, and also join multi-line |
| 4410 | * headers. An index of all the lines will be elaborated while |
| 4411 | * parsing. |
| 4412 | * |
| 4413 | * For the parsing, we use a 28 states FSM. |
| 4414 | * |
| 4415 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4416 | * rep->data + msg->som = beginning of response |
| 4417 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4418 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4419 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4420 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4421 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4422 | */ |
| 4423 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4424 | /* There's a protected area at the end of the buffer for rewriting |
| 4425 | * purposes. We don't want to start to parse the request if the |
| 4426 | * protected area is affected, because we may have to move processed |
| 4427 | * data later, which is much more complicated. |
| 4428 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4429 | if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4430 | if (unlikely((rep->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 4431 | bi_end(rep) < b_ptr(rep, msg->next) || |
| 4432 | bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4433 | if (rep->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4434 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4435 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4436 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4437 | buffer_dont_close(rep); |
| 4438 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4439 | return 0; |
| 4440 | } |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4441 | if (rep->i <= rep->size - global.tune.maxrewrite) |
Willy Tarreau | a7fe8e5 | 2012-05-08 20:40:09 +0200 | [diff] [blame] | 4442 | buffer_slow_realign(msg->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4443 | } |
| 4444 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4445 | if (likely(msg->next < rep->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4446 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4447 | } |
| 4448 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4449 | /* 1: we might have to print this header in debug mode */ |
| 4450 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4451 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4452 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4453 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4454 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4455 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4456 | sol = rep->p + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4457 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4458 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4459 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4460 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4461 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4462 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4463 | while (cur_idx) { |
| 4464 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4465 | debug_hdr("srvhdr", s, sol, eol); |
| 4466 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4467 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4468 | } |
| 4469 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4470 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4471 | /* |
| 4472 | * Now we quickly check if we have found a full valid response. |
| 4473 | * If not so, we check the FD and buffer states before leaving. |
| 4474 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4475 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4476 | * responses are checked first. |
| 4477 | * |
| 4478 | * Depending on whether the client is still there or not, we |
| 4479 | * may send an error response back or not. Note that normally |
| 4480 | * we should only check for HTTP status there, and check I/O |
| 4481 | * errors somewhere else. |
| 4482 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4483 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4484 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4485 | /* Invalid response */ |
| 4486 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4487 | /* we detected a parsing error. We want to archive this response |
| 4488 | * in the dedicated proxy area for later troubleshooting. |
| 4489 | */ |
| 4490 | hdr_response_bad: |
| 4491 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4492 | 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] | 4493 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4494 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4495 | if (target_srv(&s->target)) { |
| 4496 | target_srv(&s->target)->counters.failed_resp++; |
| 4497 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4498 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4499 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4500 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4501 | rep->analysers = 0; |
| 4502 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4503 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4504 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4505 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4506 | |
| 4507 | if (!(s->flags & SN_ERR_MASK)) |
| 4508 | s->flags |= SN_ERR_PRXCOND; |
| 4509 | if (!(s->flags & SN_FINST_MASK)) |
| 4510 | s->flags |= SN_FINST_H; |
| 4511 | |
| 4512 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4513 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4514 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4515 | /* too large response does not fit in buffer. */ |
| 4516 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4517 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4518 | msg->err_pos = rep->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4519 | goto hdr_response_bad; |
| 4520 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4521 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4522 | /* read error */ |
| 4523 | else if (rep->flags & BF_READ_ERROR) { |
| 4524 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4525 | 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] | 4526 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4527 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4528 | if (target_srv(&s->target)) { |
| 4529 | target_srv(&s->target)->counters.failed_resp++; |
| 4530 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4531 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4532 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4533 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4534 | rep->analysers = 0; |
| 4535 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4536 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4537 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4538 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4539 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4540 | if (!(s->flags & SN_ERR_MASK)) |
| 4541 | s->flags |= SN_ERR_SRVCL; |
| 4542 | if (!(s->flags & SN_FINST_MASK)) |
| 4543 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4544 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4545 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4546 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4547 | /* read timeout : return a 504 to the client. */ |
| 4548 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4549 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4550 | 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] | 4551 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4552 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4553 | if (target_srv(&s->target)) { |
| 4554 | target_srv(&s->target)->counters.failed_resp++; |
| 4555 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4556 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4557 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4558 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4559 | rep->analysers = 0; |
| 4560 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4561 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4562 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4563 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4564 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4565 | if (!(s->flags & SN_ERR_MASK)) |
| 4566 | s->flags |= SN_ERR_SRVTO; |
| 4567 | if (!(s->flags & SN_FINST_MASK)) |
| 4568 | s->flags |= SN_FINST_H; |
| 4569 | return 0; |
| 4570 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4571 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4572 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4573 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4574 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4575 | 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] | 4576 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4577 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4578 | if (target_srv(&s->target)) { |
| 4579 | target_srv(&s->target)->counters.failed_resp++; |
| 4580 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4581 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4582 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4583 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4584 | rep->analysers = 0; |
| 4585 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4586 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4587 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4588 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4589 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4590 | if (!(s->flags & SN_ERR_MASK)) |
| 4591 | s->flags |= SN_ERR_SRVCL; |
| 4592 | if (!(s->flags & SN_FINST_MASK)) |
| 4593 | s->flags |= SN_FINST_H; |
| 4594 | return 0; |
| 4595 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4596 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4597 | /* write error to client (we don't send any message then) */ |
| 4598 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4599 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4600 | 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] | 4601 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4602 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4603 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4604 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4605 | |
| 4606 | if (!(s->flags & SN_ERR_MASK)) |
| 4607 | s->flags |= SN_ERR_CLICL; |
| 4608 | if (!(s->flags & SN_FINST_MASK)) |
| 4609 | s->flags |= SN_FINST_H; |
| 4610 | |
| 4611 | /* process_session() will take care of the error */ |
| 4612 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4613 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4614 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4615 | buffer_dont_close(rep); |
| 4616 | return 0; |
| 4617 | } |
| 4618 | |
| 4619 | /* More interesting part now : we know that we have a complete |
| 4620 | * response which at least looks like HTTP. We have an indicator |
| 4621 | * of each header's length, so we can parse them quickly. |
| 4622 | */ |
| 4623 | |
| 4624 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4625 | 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] | 4626 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4627 | /* |
| 4628 | * 1: get the status code |
| 4629 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4630 | n = rep->p[msg->sol + msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4631 | if (n < 1 || n > 5) |
| 4632 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4633 | /* when the client triggers a 4xx from the server, it's most often due |
| 4634 | * to a missing object or permission. These events should be tracked |
| 4635 | * because if they happen often, it may indicate a brute force or a |
| 4636 | * vulnerability scan. |
| 4637 | */ |
| 4638 | if (n == 4) |
| 4639 | session_inc_http_err_ctr(s); |
| 4640 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4641 | if (target_srv(&s->target)) |
| 4642 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4643 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4644 | /* check if the response is HTTP/1.1 or above */ |
| 4645 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4646 | ((rep->p[msg->sol + 5] > '1') || |
| 4647 | ((rep->p[msg->sol + 5] == '1') && |
| 4648 | (rep->p[msg->sol + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4649 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4650 | |
| 4651 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4652 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4653 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4654 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4655 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4656 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4657 | txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4658 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4659 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4660 | * and 505 are triggered on demand by client request, so we must not |
| 4661 | * count them as server failures. |
| 4662 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4663 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4664 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4665 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4666 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4667 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4668 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4669 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4670 | /* |
| 4671 | * 2: check for cacheability. |
| 4672 | */ |
| 4673 | |
| 4674 | switch (txn->status) { |
| 4675 | case 200: |
| 4676 | case 203: |
| 4677 | case 206: |
| 4678 | case 300: |
| 4679 | case 301: |
| 4680 | case 410: |
| 4681 | /* RFC2616 @13.4: |
| 4682 | * "A response received with a status code of |
| 4683 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4684 | * by a cache (...) unless a cache-control |
| 4685 | * directive prohibits caching." |
| 4686 | * |
| 4687 | * RFC2616 @9.5: POST method : |
| 4688 | * "Responses to this method are not cacheable, |
| 4689 | * unless the response includes appropriate |
| 4690 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4691 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4692 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4693 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4694 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4695 | break; |
| 4696 | default: |
| 4697 | break; |
| 4698 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4699 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4700 | /* |
| 4701 | * 3: we may need to capture headers |
| 4702 | */ |
| 4703 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4704 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4705 | capture_headers(rep->p + msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4706 | txn->rsp.cap, s->fe->rsp_cap); |
| 4707 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4708 | /* 4: determine the transfer-length. |
| 4709 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4710 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4711 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4712 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4713 | * All responses to the HEAD request method MUST NOT include a |
| 4714 | * message-body, even though the presence of entity-header fields |
| 4715 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4716 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4717 | * message-body. All other responses do include a message-body, |
| 4718 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4719 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4720 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4721 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4722 | * always terminated by the first empty line after the header fields, |
| 4723 | * regardless of the entity-header fields present in the message. |
| 4724 | * |
| 4725 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4726 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4727 | * transfer-length is defined by the use of this transfer-coding. |
| 4728 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4729 | * transfer-coding is not present, the transfer-length is defined by |
| 4730 | * the sender closing the connection. |
| 4731 | * |
| 4732 | * 3. If a Content-Length header field is present, its decimal value in |
| 4733 | * OCTETs represents both the entity-length and the transfer-length. |
| 4734 | * If a message is received with both a Transfer-Encoding header |
| 4735 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4736 | * |
| 4737 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4738 | * the transfer-length is not otherwise specified, then this self- |
| 4739 | * delimiting media type defines the transfer-length. This media |
| 4740 | * type MUST NOT be used unless the sender knows that the recipient |
| 4741 | * can parse it; the presence in a request of a Range header with |
| 4742 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4743 | * client can parse multipart/byteranges responses. |
| 4744 | * |
| 4745 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4746 | */ |
| 4747 | |
| 4748 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4749 | * 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] | 4750 | * 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] | 4751 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4752 | */ |
| 4753 | if (txn->meth == HTTP_METH_HEAD || |
| 4754 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4755 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4756 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4757 | goto skip_content_length; |
| 4758 | } |
| 4759 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4760 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4761 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4762 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4763 | http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4764 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4765 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4766 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4767 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4768 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4769 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4770 | break; |
| 4771 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4775 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4776 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4777 | http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4778 | signed long long cl; |
| 4779 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4780 | if (!ctx.vlen) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 4781 | msg->err_pos = ctx.line + ctx.val - rep->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4782 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4783 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4784 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4785 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 4786 | msg->err_pos = ctx.line + ctx.val - rep->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4787 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4788 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4789 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4790 | if (cl < 0) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 4791 | msg->err_pos = ctx.line + ctx.val - rep->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4792 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4793 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4794 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4795 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 4796 | msg->err_pos = ctx.line + ctx.val - rep->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4797 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4798 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4799 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4800 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4801 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4802 | } |
| 4803 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4804 | /* FIXME: we should also implement the multipart/byterange method. |
| 4805 | * For now on, we resort to close mode in this case (unknown length). |
| 4806 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4807 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4808 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4809 | /* end of job, return OK */ |
| 4810 | rep->analysers &= ~an_bit; |
| 4811 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4812 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4813 | return 1; |
| 4814 | } |
| 4815 | |
| 4816 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4817 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4818 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4819 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4820 | */ |
| 4821 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4822 | { |
| 4823 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4824 | struct http_msg *msg = &txn->rsp; |
| 4825 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4826 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4827 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4828 | 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] | 4829 | now_ms, __FUNCTION__, |
| 4830 | t, |
| 4831 | rep, |
| 4832 | rep->rex, rep->wex, |
| 4833 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4834 | rep->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4835 | rep->analysers); |
| 4836 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4837 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4838 | return 0; |
| 4839 | |
| 4840 | rep->analysers &= ~an_bit; |
| 4841 | rep->analyse_exp = TICK_ETERNITY; |
| 4842 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4843 | /* Now we have to check if we need to modify the Connection header. |
| 4844 | * This is more difficult on the response than it is on the request, |
| 4845 | * because we can have two different HTTP versions and we don't know |
| 4846 | * how the client will interprete a response. For instance, let's say |
| 4847 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4848 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4849 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4850 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4851 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4852 | * indicating how a request MAY be understood by the client. In case |
| 4853 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4854 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4855 | * will fall back to explicit close. Note that we won't take risks with |
| 4856 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4857 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4858 | */ |
| 4859 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4860 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4861 | txn->status == 101)) { |
| 4862 | /* Either we've established an explicit tunnel, or we're |
| 4863 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4864 | * to understand the next protocols. We have to switch to tunnel |
| 4865 | * mode, so that we transfer the request and responses then let |
| 4866 | * this protocol pass unmodified. When we later implement specific |
| 4867 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4868 | * header which contains information about that protocol for |
| 4869 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4870 | */ |
| 4871 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4872 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4873 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4874 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4875 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4876 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4877 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4878 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4879 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4880 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4881 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4882 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4883 | /* now adjust header transformations depending on current state */ |
| 4884 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4885 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4886 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4887 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4888 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4889 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4890 | else { /* SCL / KAL */ |
| 4891 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4892 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4893 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4894 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4895 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4896 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4897 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4898 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4899 | /* Some keep-alive responses are converted to Server-close if |
| 4900 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4901 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4902 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4903 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4904 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4905 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4906 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4907 | } |
| 4908 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4909 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4910 | /* |
| 4911 | * 3: we will have to evaluate the filters. |
| 4912 | * As opposed to version 1.2, now they will be evaluated in the |
| 4913 | * filters order and not in the header order. This means that |
| 4914 | * each filter has to be validated among all headers. |
| 4915 | * |
| 4916 | * Filters are tried with ->be first, then with ->fe if it is |
| 4917 | * different from ->be. |
| 4918 | */ |
| 4919 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4920 | cur_proxy = t->be; |
| 4921 | while (1) { |
| 4922 | struct proxy *rule_set = cur_proxy; |
| 4923 | |
| 4924 | /* try headers filters */ |
| 4925 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4926 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4927 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4928 | if (target_srv(&t->target)) { |
| 4929 | target_srv(&t->target)->counters.failed_resp++; |
| 4930 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4931 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4932 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4933 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4934 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4935 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4936 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4937 | bi_erase(rep); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4938 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4939 | if (!(t->flags & SN_ERR_MASK)) |
| 4940 | t->flags |= SN_ERR_PRXCOND; |
| 4941 | if (!(t->flags & SN_FINST_MASK)) |
| 4942 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4943 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4944 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4945 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4946 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4947 | /* has the response been denied ? */ |
| 4948 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4949 | if (target_srv(&t->target)) |
| 4950 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4951 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4952 | t->be->be_counters.denied_resp++; |
| 4953 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4954 | if (t->listener->counters) |
| 4955 | t->listener->counters->denied_resp++; |
| 4956 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4957 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4958 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4959 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4960 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4961 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4962 | if (txn->status < 200) |
| 4963 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4964 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 4965 | 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] | 4966 | ret = acl_pass(ret); |
| 4967 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4968 | ret = !ret; |
| 4969 | if (!ret) |
| 4970 | continue; |
| 4971 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4972 | 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] | 4973 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4974 | } |
| 4975 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4976 | /* check whether we're already working on the frontend */ |
| 4977 | if (cur_proxy == t->fe) |
| 4978 | break; |
| 4979 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4980 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4981 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4982 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4983 | * We may be facing a 100-continue response, in which case this |
| 4984 | * is not the right response, and we're waiting for the next one. |
| 4985 | * 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] | 4986 | * next one. |
| 4987 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4988 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4989 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4990 | msg->next -= buffer_forward(rep, msg->next - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4991 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4992 | txn->status = 0; |
| 4993 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4994 | return 1; |
| 4995 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4996 | else if (unlikely(txn->status < 200)) |
| 4997 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4998 | |
| 4999 | /* we don't have any 1xx status code now */ |
| 5000 | |
| 5001 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5002 | * 4: check for server cookie. |
| 5003 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5004 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5005 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5006 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5007 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5008 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5009 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5010 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5011 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5012 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5013 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5014 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5015 | /* |
| 5016 | * 6: add server cookie in the response if needed |
| 5017 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5018 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5019 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5020 | (!(t->flags & SN_DIRECT) || |
| 5021 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5022 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5023 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5024 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5025 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5026 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5027 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5028 | /* the server is known, it's not the one the client requested, or the |
| 5029 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5030 | * insert a set-cookie here, except if we want to insert only on POST |
| 5031 | * requests and this one isn't. Note that servers which don't have cookies |
| 5032 | * (eg: some backup servers) will return a full cookie removal request. |
| 5033 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5034 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5035 | len = sprintf(trash, |
| 5036 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5037 | t->be->cookie_name); |
| 5038 | } |
| 5039 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5040 | len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5041 | |
| 5042 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5043 | /* emit last_date, which is mandatory */ |
| 5044 | trash[len++] = COOKIE_DELIM_DATE; |
| 5045 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5046 | if (t->be->cookie_maxlife) { |
| 5047 | /* emit first_date, which is either the original one or |
| 5048 | * the current date. |
| 5049 | */ |
| 5050 | trash[len++] = COOKIE_DELIM_DATE; |
| 5051 | s30tob64(txn->cookie_first_date ? |
| 5052 | txn->cookie_first_date >> 2 : |
| 5053 | (date.tv_sec+3) >> 2, trash + len); |
| 5054 | len += 5; |
| 5055 | } |
| 5056 | } |
| 5057 | len += sprintf(trash + len, "; path=/"); |
| 5058 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5059 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5060 | if (t->be->cookie_domain) |
| 5061 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5062 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5063 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5064 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5065 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5066 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5067 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5068 | /* the server did not change, only the date was updated */ |
| 5069 | txn->flags |= TX_SCK_UPDATED; |
| 5070 | else |
| 5071 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5072 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5073 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5074 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5075 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5076 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5077 | */ |
| 5078 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5079 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5080 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5081 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5082 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5083 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5084 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5085 | } |
| 5086 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5087 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5088 | /* |
| 5089 | * 7: check if result will be cacheable with a cookie. |
| 5090 | * We'll block the response if security checks have caught |
| 5091 | * nasty things such as a cacheable cookie. |
| 5092 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5093 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5094 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5095 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5096 | |
| 5097 | /* we're in presence of a cacheable response containing |
| 5098 | * a set-cookie header. We'll block it as requested by |
| 5099 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5100 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5101 | if (target_srv(&t->target)) |
| 5102 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5103 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5104 | t->be->be_counters.denied_resp++; |
| 5105 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5106 | if (t->listener->counters) |
| 5107 | t->listener->counters->denied_resp++; |
| 5108 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5109 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5110 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5111 | send_log(t->be, LOG_ALERT, |
| 5112 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5113 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5114 | goto return_srv_prx_502; |
| 5115 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5116 | |
| 5117 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5118 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5119 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5120 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5121 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5122 | unsigned int want_flags = 0; |
| 5123 | |
| 5124 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5125 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5126 | /* we want a keep-alive response here. Keep-alive header |
| 5127 | * required if either side is not 1.1. |
| 5128 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5129 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5130 | want_flags |= TX_CON_KAL_SET; |
| 5131 | } |
| 5132 | else { |
| 5133 | /* we want a close response here. Close header required if |
| 5134 | * the server is 1.1, regardless of the client. |
| 5135 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5136 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5137 | want_flags |= TX_CON_CLO_SET; |
| 5138 | } |
| 5139 | |
| 5140 | 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] | 5141 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5142 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5143 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5144 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5145 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5146 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5147 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5148 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5149 | /************************************************************* |
| 5150 | * OK, that's finished for the headers. We have done what we * |
| 5151 | * could. Let's switch to the DATA state. * |
| 5152 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5153 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5154 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5155 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5156 | /* if the user wants to log as soon as possible, without counting |
| 5157 | * bytes from the server, then this is the right moment. We have |
| 5158 | * to temporarily assign bytes_out to log what we currently have. |
| 5159 | */ |
| 5160 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5161 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5162 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5163 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5164 | t->logs.bytes_out = 0; |
| 5165 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5166 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5167 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5168 | * otherwise we would not let the client side wake up. |
| 5169 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5170 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5171 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5172 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5173 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5174 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5175 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5176 | /* This function is an analyser which forwards response body (including chunk |
| 5177 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5178 | * zero byte. The only situation where it must not be called is when we're in |
| 5179 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5180 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5181 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5182 | * 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] | 5183 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5184 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5185 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5186 | */ |
| 5187 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5188 | { |
| 5189 | struct http_txn *txn = &s->txn; |
| 5190 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5191 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5192 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5193 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5194 | return 0; |
| 5195 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5196 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 5197 | ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5198 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5199 | /* Output closed while we were sending data. We must abort and |
| 5200 | * wake the other side up. |
| 5201 | */ |
| 5202 | msg->msg_state = HTTP_MSG_ERROR; |
| 5203 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5204 | return 1; |
| 5205 | } |
| 5206 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5207 | /* in most states, we should abort in case of early close */ |
| 5208 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5209 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5210 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5211 | /* we have msg->sov which points to the first byte of message body. |
| 5212 | * msg->som still points to the beginning of the message. We must |
| 5213 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5214 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 5215 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5216 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5217 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5218 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5219 | else { |
| 5220 | msg->msg_state = HTTP_MSG_DATA; |
| 5221 | } |
| 5222 | } |
| 5223 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5224 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5225 | int bytes; |
| 5226 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5227 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5228 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5229 | bytes = msg->sov - msg->som; |
| 5230 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5231 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5232 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5233 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5234 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5235 | msg->chunk_len += (unsigned int)bytes; |
| 5236 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5237 | } |
| 5238 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5239 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5240 | /* must still forward */ |
| 5241 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5242 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5243 | |
| 5244 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5245 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5246 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5247 | else |
| 5248 | msg->msg_state = HTTP_MSG_DONE; |
| 5249 | } |
| 5250 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5251 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5252 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5253 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5254 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5255 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5256 | |
| 5257 | if (!ret) |
| 5258 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5259 | else if (ret < 0) { |
| 5260 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5261 | 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] | 5262 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5263 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5264 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5265 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5266 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5267 | /* we want the CRLF after the data */ |
| 5268 | int ret; |
| 5269 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5270 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5271 | |
| 5272 | if (!ret) |
| 5273 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5274 | else if (ret < 0) { |
| 5275 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5276 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5277 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5278 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5279 | /* we're in MSG_CHUNK_SIZE now */ |
| 5280 | } |
| 5281 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5282 | int ret = http_forward_trailers(msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5283 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5284 | if (ret == 0) |
| 5285 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5286 | else if (ret < 0) { |
| 5287 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5288 | 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] | 5289 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5290 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5291 | /* we're in HTTP_MSG_DONE now */ |
| 5292 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5293 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5294 | int old_state = msg->msg_state; |
| 5295 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5296 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5297 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5298 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5299 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5300 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5301 | if (http_resync_states(s)) { |
| 5302 | http_silent_debug(__LINE__, s); |
| 5303 | /* some state changes occurred, maybe the analyser |
| 5304 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5305 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5306 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5307 | if (res->flags & BF_SHUTW) { |
| 5308 | /* response errors are most likely due to |
| 5309 | * the client aborting the transfer. |
| 5310 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5311 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5312 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5313 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5314 | http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5315 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5316 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5317 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5318 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5319 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5320 | } |
| 5321 | } |
| 5322 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5323 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5324 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5325 | if (res->flags & BF_SHUTR) { |
| 5326 | if (!(s->flags & SN_ERR_MASK)) |
| 5327 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5328 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5329 | if (target_srv(&s->target)) |
| 5330 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5331 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5332 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5333 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5334 | if (res->flags & BF_SHUTW) |
| 5335 | goto aborted_xfer; |
| 5336 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5337 | /* 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] | 5338 | if (!s->req->analysers) |
| 5339 | goto return_bad_res; |
| 5340 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5341 | /* forward any pending data */ |
| 5342 | bytes = msg->sov - msg->som; |
| 5343 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5344 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5345 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5346 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5347 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5348 | msg->chunk_len += (unsigned int)bytes; |
| 5349 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5350 | } |
| 5351 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5352 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5353 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5354 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5355 | * close. |
| 5356 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5357 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5358 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5359 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5360 | buffer_dont_close(res); |
| 5361 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5362 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5363 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5364 | * 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] | 5365 | * modes are already handled by the stream sock layer. We must not do |
| 5366 | * this in content-length mode because it could present the MSG_MORE |
| 5367 | * flag with the last block of forwarded data, which would cause an |
| 5368 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5369 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5370 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5371 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5372 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5373 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5374 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5375 | return 0; |
| 5376 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5377 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5378 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5379 | if (target_srv(&s->target)) |
| 5380 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5381 | |
| 5382 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5383 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5384 | /* don't send any error message as we're in the body */ |
| 5385 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5386 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5387 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5388 | if (target_srv(&s->target)) |
| 5389 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5390 | |
| 5391 | if (!(s->flags & SN_ERR_MASK)) |
| 5392 | s->flags |= SN_ERR_PRXCOND; |
| 5393 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5394 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5395 | return 0; |
| 5396 | |
| 5397 | aborted_xfer: |
| 5398 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5399 | /* don't send any error message as we're in the body */ |
| 5400 | stream_int_retnclose(res->cons, NULL); |
| 5401 | res->analysers = 0; |
| 5402 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5403 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5404 | s->fe->fe_counters.cli_aborts++; |
| 5405 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5406 | if (target_srv(&s->target)) |
| 5407 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5408 | |
| 5409 | if (!(s->flags & SN_ERR_MASK)) |
| 5410 | s->flags |= SN_ERR_CLICL; |
| 5411 | if (!(s->flags & SN_FINST_MASK)) |
| 5412 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5413 | return 0; |
| 5414 | } |
| 5415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5416 | /* Iterate the same filter through all request headers. |
| 5417 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5418 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5419 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5420 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5421 | int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5422 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5423 | char term; |
| 5424 | char *cur_ptr, *cur_end, *cur_next; |
| 5425 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5426 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5427 | struct hdr_idx_elem *cur_hdr; |
| 5428 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5430 | last_hdr = 0; |
| 5431 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5432 | cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5433 | old_idx = 0; |
| 5434 | |
| 5435 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5436 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5437 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5438 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5439 | (exp->action == ACT_ALLOW || |
| 5440 | exp->action == ACT_DENY || |
| 5441 | exp->action == ACT_TARPIT)) |
| 5442 | return 0; |
| 5443 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5444 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5445 | if (!cur_idx) |
| 5446 | break; |
| 5447 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5448 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5449 | cur_ptr = cur_next; |
| 5450 | cur_end = cur_ptr + cur_hdr->len; |
| 5451 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5452 | |
| 5453 | /* Now we have one header between cur_ptr and cur_end, |
| 5454 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5455 | */ |
| 5456 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5457 | /* The annoying part is that pattern matching needs |
| 5458 | * that we modify the contents to null-terminate all |
| 5459 | * strings before testing them. |
| 5460 | */ |
| 5461 | |
| 5462 | term = *cur_end; |
| 5463 | *cur_end = '\0'; |
| 5464 | |
| 5465 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5466 | switch (exp->action) { |
| 5467 | case ACT_SETBE: |
| 5468 | /* It is not possible to jump a second time. |
| 5469 | * FIXME: should we return an HTTP/500 here so that |
| 5470 | * the admin knows there's a problem ? |
| 5471 | */ |
| 5472 | if (t->be != t->fe) |
| 5473 | break; |
| 5474 | |
| 5475 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5476 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5477 | last_hdr = 1; |
| 5478 | break; |
| 5479 | |
| 5480 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5481 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5482 | last_hdr = 1; |
| 5483 | break; |
| 5484 | |
| 5485 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5486 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5487 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5488 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5489 | t->fe->fe_counters.denied_req++; |
| 5490 | if (t->fe != t->be) |
| 5491 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5492 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5493 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5494 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5495 | break; |
| 5496 | |
| 5497 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5498 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5499 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5500 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5501 | t->fe->fe_counters.denied_req++; |
| 5502 | if (t->fe != t->be) |
| 5503 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5504 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5505 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5506 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5507 | break; |
| 5508 | |
| 5509 | case ACT_REPLACE: |
| 5510 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5511 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5512 | /* FIXME: if the user adds a newline in the replacement, the |
| 5513 | * index will not be recalculated for now, and the new line |
| 5514 | * will not be counted as a new header. |
| 5515 | */ |
| 5516 | |
| 5517 | cur_end += delta; |
| 5518 | cur_next += delta; |
| 5519 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5520 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5521 | break; |
| 5522 | |
| 5523 | case ACT_REMOVE: |
| 5524 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5525 | cur_next += delta; |
| 5526 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5527 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5528 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5529 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5530 | cur_hdr->len = 0; |
| 5531 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5532 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5533 | break; |
| 5534 | |
| 5535 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5536 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5537 | if (cur_end) |
| 5538 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5539 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5540 | /* keep the link from this header to next one in case of later |
| 5541 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5542 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5543 | old_idx = cur_idx; |
| 5544 | } |
| 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | |
| 5549 | /* Apply the filter to the request line. |
| 5550 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5551 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5552 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5553 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5554 | */ |
| 5555 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5556 | { |
| 5557 | char term; |
| 5558 | char *cur_ptr, *cur_end; |
| 5559 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5560 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5561 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5562 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5563 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5564 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5565 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5566 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5567 | (exp->action == ACT_ALLOW || |
| 5568 | exp->action == ACT_DENY || |
| 5569 | exp->action == ACT_TARPIT)) |
| 5570 | return 0; |
| 5571 | else if (exp->action == ACT_REMOVE) |
| 5572 | return 0; |
| 5573 | |
| 5574 | done = 0; |
| 5575 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5576 | cur_ptr = req->p + txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5577 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5578 | |
| 5579 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5580 | |
| 5581 | /* The annoying part is that pattern matching needs |
| 5582 | * that we modify the contents to null-terminate all |
| 5583 | * strings before testing them. |
| 5584 | */ |
| 5585 | |
| 5586 | term = *cur_end; |
| 5587 | *cur_end = '\0'; |
| 5588 | |
| 5589 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5590 | switch (exp->action) { |
| 5591 | case ACT_SETBE: |
| 5592 | /* It is not possible to jump a second time. |
| 5593 | * FIXME: should we return an HTTP/500 here so that |
| 5594 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5595 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5596 | if (t->be != t->fe) |
| 5597 | break; |
| 5598 | |
| 5599 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5600 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5601 | done = 1; |
| 5602 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5604 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5605 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5606 | done = 1; |
| 5607 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5608 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5609 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5610 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5611 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5612 | t->fe->fe_counters.denied_req++; |
| 5613 | if (t->fe != t->be) |
| 5614 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5615 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5616 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5618 | done = 1; |
| 5619 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5620 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5621 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5622 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5623 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5624 | t->fe->fe_counters.denied_req++; |
| 5625 | if (t->fe != t->be) |
| 5626 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5627 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5628 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5629 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5630 | done = 1; |
| 5631 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5632 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5633 | case ACT_REPLACE: |
| 5634 | *cur_end = term; /* restore the string terminator */ |
| 5635 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5636 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5637 | /* FIXME: if the user adds a newline in the replacement, the |
| 5638 | * index will not be recalculated for now, and the new line |
| 5639 | * will not be counted as a new header. |
| 5640 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5641 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5642 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5643 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 5644 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5645 | HTTP_MSG_RQMETH, |
| 5646 | cur_ptr, cur_end + 1, |
| 5647 | NULL, NULL); |
| 5648 | if (unlikely(!cur_end)) |
| 5649 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5650 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5651 | /* we have a full request and we know that we have either a CR |
| 5652 | * or an LF at <ptr>. |
| 5653 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5654 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5655 | 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] | 5656 | /* there is no point trying this regex on headers */ |
| 5657 | return 1; |
| 5658 | } |
| 5659 | } |
| 5660 | *cur_end = term; /* restore the string terminator */ |
| 5661 | return done; |
| 5662 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5663 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5664 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5665 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5666 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5667 | * 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] | 5668 | * 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] | 5669 | * unparsable request. Since it can manage the switch to another backend, it |
| 5670 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5671 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5672 | int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5673 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5674 | struct http_txn *txn = &s->txn; |
| 5675 | struct hdr_exp *exp; |
| 5676 | |
| 5677 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5678 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5679 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5680 | /* |
| 5681 | * The interleaving of transformations and verdicts |
| 5682 | * makes it difficult to decide to continue or stop |
| 5683 | * the evaluation. |
| 5684 | */ |
| 5685 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5686 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5687 | break; |
| 5688 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5689 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5690 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5691 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5692 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5693 | |
| 5694 | /* if this filter had a condition, evaluate it now and skip to |
| 5695 | * next filter if the condition does not match. |
| 5696 | */ |
| 5697 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5698 | 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] | 5699 | ret = acl_pass(ret); |
| 5700 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5701 | ret = !ret; |
| 5702 | |
| 5703 | if (!ret) |
| 5704 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5708 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5709 | if (unlikely(ret < 0)) |
| 5710 | return -1; |
| 5711 | |
| 5712 | if (likely(ret == 0)) { |
| 5713 | /* The filter did not match the request, it can be |
| 5714 | * iterated through all headers. |
| 5715 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5716 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5717 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5718 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5719 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5723 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5724 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5725 | * Try to retrieve the server associated to the appsession. |
| 5726 | * If the server is found, it's assigned to the session. |
| 5727 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5728 | 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] | 5729 | struct http_txn *txn = &t->txn; |
| 5730 | appsess *asession = NULL; |
| 5731 | char *sessid_temp = NULL; |
| 5732 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5733 | if (len > t->be->appsession_len) { |
| 5734 | len = t->be->appsession_len; |
| 5735 | } |
| 5736 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5737 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5738 | /* 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] | 5739 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5740 | /* 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] | 5741 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5742 | } |
| 5743 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5744 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5745 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5746 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5747 | return; |
| 5748 | } |
| 5749 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5750 | memcpy(txn->sessid, buf, len); |
| 5751 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5755 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5756 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5757 | return; |
| 5758 | } |
| 5759 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5760 | memcpy(sessid_temp, buf, len); |
| 5761 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5762 | |
| 5763 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5764 | /* free previously allocated memory */ |
| 5765 | pool_free2(apools.sessid, sessid_temp); |
| 5766 | |
| 5767 | if (asession != NULL) { |
| 5768 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5769 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5770 | asession->request_count++; |
| 5771 | |
| 5772 | if (asession->serverid != NULL) { |
| 5773 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5774 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5775 | while (srv) { |
| 5776 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5777 | if ((srv->state & SRV_RUNNING) || |
| 5778 | (t->be->options & PR_O_PERSIST) || |
| 5779 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5780 | /* we found the server and it's usable */ |
| 5781 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5782 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5783 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5784 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5785 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5786 | break; |
| 5787 | } else { |
| 5788 | txn->flags &= ~TX_CK_MASK; |
| 5789 | txn->flags |= TX_CK_DOWN; |
| 5790 | } |
| 5791 | } |
| 5792 | srv = srv->next; |
| 5793 | } |
| 5794 | } |
| 5795 | } |
| 5796 | } |
| 5797 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5798 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5799 | * same way as with headers above except that the semi-colon also ends a token. |
| 5800 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5801 | * return a valid result. |
| 5802 | */ |
| 5803 | char *find_cookie_value_end(char *s, const char *e) |
| 5804 | { |
| 5805 | int quoted, qdpair; |
| 5806 | |
| 5807 | quoted = qdpair = 0; |
| 5808 | for (; s < e; s++) { |
| 5809 | if (qdpair) qdpair = 0; |
| 5810 | else if (quoted) { |
| 5811 | if (*s == '\\') qdpair = 1; |
| 5812 | else if (*s == '"') quoted = 0; |
| 5813 | } |
| 5814 | else if (*s == '"') quoted = 1; |
| 5815 | else if (*s == ',' || *s == ';') return s; |
| 5816 | } |
| 5817 | return s; |
| 5818 | } |
| 5819 | |
| 5820 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5821 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5822 | * the first delimiter is updated if required. The function tries as much as |
| 5823 | * possible to respect the following principles : |
| 5824 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5825 | * colon, in which case <next> is simply removed |
| 5826 | * - set exactly one space character after the new first delimiter, unless |
| 5827 | * there are not enough characters in the block being moved to do so. |
| 5828 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5829 | * one. |
| 5830 | * |
| 5831 | * It is the caller's responsibility to ensure that : |
| 5832 | * - <from> points to a valid delimiter or the colon ; |
| 5833 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5834 | * - there are non-space chars before <from> ; |
| 5835 | * - there is a CR/LF at or after <next>. |
| 5836 | */ |
| 5837 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5838 | { |
| 5839 | char *prev = *from; |
| 5840 | |
| 5841 | if (*prev == ':') { |
| 5842 | /* We're removing the first value, preserve the colon and add a |
| 5843 | * space if possible. |
| 5844 | */ |
| 5845 | if (!http_is_crlf[(unsigned char)*next]) |
| 5846 | next++; |
| 5847 | prev++; |
| 5848 | if (prev < next) |
| 5849 | *prev++ = ' '; |
| 5850 | |
| 5851 | while (http_is_spht[(unsigned char)*next]) |
| 5852 | next++; |
| 5853 | } else { |
| 5854 | /* Remove useless spaces before the old delimiter. */ |
| 5855 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5856 | prev--; |
| 5857 | *from = prev; |
| 5858 | |
| 5859 | /* copy the delimiter and if possible a space if we're |
| 5860 | * not at the end of the line. |
| 5861 | */ |
| 5862 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5863 | *prev++ = *next++; |
| 5864 | if (prev + 1 < next) |
| 5865 | *prev++ = ' '; |
| 5866 | while (http_is_spht[(unsigned char)*next]) |
| 5867 | next++; |
| 5868 | } |
| 5869 | } |
| 5870 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5871 | } |
| 5872 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5873 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5874 | * 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] | 5875 | * desirable to call it only when needed. This code is quite complex because |
| 5876 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5877 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5878 | */ |
| 5879 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5880 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5881 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5882 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5883 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5884 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5885 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5886 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5887 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5888 | old_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5889 | hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5890 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5891 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5892 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5893 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5894 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5895 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5896 | hdr_beg = hdr_next; |
| 5897 | hdr_end = hdr_beg + cur_hdr->len; |
| 5898 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5899 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5900 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5901 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5902 | * "Cookie:" headers. |
| 5903 | */ |
| 5904 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5905 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5906 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5907 | old_idx = cur_idx; |
| 5908 | continue; |
| 5909 | } |
| 5910 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5911 | del_from = NULL; /* nothing to be deleted */ |
| 5912 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5913 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5914 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5915 | * attributes whose name begin with a '$', and associate them with |
| 5916 | * the right cookie, if we want to delete this cookie. |
| 5917 | * So there are 3 cases for each cookie read : |
| 5918 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5919 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5920 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5921 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5922 | * "special" cookie. |
| 5923 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5924 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5925 | * *MUST* delete it. |
| 5926 | * |
| 5927 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5928 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5929 | * the RFC explicitly allows spaces before it, and not within the |
| 5930 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5931 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5932 | * after the equal sign too, resulting in some (rare) buggy |
| 5933 | * implementations trying to do that. So let's do what servers do. |
| 5934 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5935 | * allowed quoted strings in values, with any possible character |
| 5936 | * after a backslash, including control chars and delimitors, which |
| 5937 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5938 | * within values even without quotes. |
| 5939 | * |
| 5940 | * We have to keep multiple pointers in order to support cookie |
| 5941 | * removal at the beginning, middle or end of header without |
| 5942 | * corrupting the header. All of these headers are valid : |
| 5943 | * |
| 5944 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5945 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5946 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5947 | * | | | | | | | | | |
| 5948 | * | | | | | | | | hdr_end <--+ |
| 5949 | * | | | | | | | +--> next |
| 5950 | * | | | | | | +----> val_end |
| 5951 | * | | | | | +-----------> val_beg |
| 5952 | * | | | | +--------------> equal |
| 5953 | * | | | +----------------> att_end |
| 5954 | * | | +---------------------> att_beg |
| 5955 | * | +--------------------------> prev |
| 5956 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5957 | */ |
| 5958 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5959 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 5960 | /* Iterate through all cookies on this line */ |
| 5961 | |
| 5962 | /* find att_beg */ |
| 5963 | att_beg = prev + 1; |
| 5964 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 5965 | att_beg++; |
| 5966 | |
| 5967 | /* find att_end : this is the first character after the last non |
| 5968 | * space before the equal. It may be equal to hdr_end. |
| 5969 | */ |
| 5970 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5971 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5972 | while (equal < hdr_end) { |
| 5973 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5974 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5975 | if (http_is_spht[(unsigned char)*equal++]) |
| 5976 | continue; |
| 5977 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5978 | } |
| 5979 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5980 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 5981 | * is between <att_beg> and <equal>, both may be identical. |
| 5982 | */ |
| 5983 | |
| 5984 | /* look for end of cookie if there is an equal sign */ |
| 5985 | if (equal < hdr_end && *equal == '=') { |
| 5986 | /* look for the beginning of the value */ |
| 5987 | val_beg = equal + 1; |
| 5988 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 5989 | val_beg++; |
| 5990 | |
| 5991 | /* find the end of the value, respecting quotes */ |
| 5992 | next = find_cookie_value_end(val_beg, hdr_end); |
| 5993 | |
| 5994 | /* make val_end point to the first white space or delimitor after the value */ |
| 5995 | val_end = next; |
| 5996 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 5997 | val_end--; |
| 5998 | } else { |
| 5999 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6000 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6001 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6002 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6003 | * they will automatically be removed if a header before them is removed, |
| 6004 | * since they're supposed to be linked together. |
| 6005 | */ |
| 6006 | if (*att_beg == '$') |
| 6007 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6008 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6009 | /* Ignore cookies with no equal sign */ |
| 6010 | if (equal == next) { |
| 6011 | /* This is not our cookie, so we must preserve it. But if we already |
| 6012 | * scheduled another cookie for removal, we cannot remove the |
| 6013 | * complete header, but we can remove the previous block itself. |
| 6014 | */ |
| 6015 | preserve_hdr = 1; |
| 6016 | if (del_from != NULL) { |
| 6017 | int delta = del_hdr_value(req, &del_from, prev); |
| 6018 | val_end += delta; |
| 6019 | next += delta; |
| 6020 | hdr_end += delta; |
| 6021 | hdr_next += delta; |
| 6022 | cur_hdr->len += delta; |
| 6023 | http_msg_move_end(&txn->req, delta); |
| 6024 | prev = del_from; |
| 6025 | del_from = NULL; |
| 6026 | } |
| 6027 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6028 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6029 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6030 | /* if there are spaces around the equal sign, we need to |
| 6031 | * strip them otherwise we'll get trouble for cookie captures, |
| 6032 | * or even for rewrites. Since this happens extremely rarely, |
| 6033 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6034 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6035 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6036 | int stripped_before = 0; |
| 6037 | int stripped_after = 0; |
| 6038 | |
| 6039 | if (att_end != equal) { |
| 6040 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6041 | equal += stripped_before; |
| 6042 | val_beg += stripped_before; |
| 6043 | } |
| 6044 | |
| 6045 | if (val_beg > equal + 1) { |
| 6046 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6047 | val_beg += stripped_after; |
| 6048 | stripped_before += stripped_after; |
| 6049 | } |
| 6050 | |
| 6051 | val_end += stripped_before; |
| 6052 | next += stripped_before; |
| 6053 | hdr_end += stripped_before; |
| 6054 | hdr_next += stripped_before; |
| 6055 | cur_hdr->len += stripped_before; |
| 6056 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6057 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6058 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6059 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6060 | /* First, let's see if we want to capture this cookie. We check |
| 6061 | * that we don't already have a client side cookie, because we |
| 6062 | * can only capture one. Also as an optimisation, we ignore |
| 6063 | * cookies shorter than the declared name. |
| 6064 | */ |
| 6065 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6066 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6067 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6068 | int log_len = val_end - att_beg; |
| 6069 | |
| 6070 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6071 | Alert("HTTP logging : out of memory.\n"); |
| 6072 | } else { |
| 6073 | if (log_len > t->fe->capture_len) |
| 6074 | log_len = t->fe->capture_len; |
| 6075 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6076 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6077 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6078 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6079 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6080 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6081 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6082 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6083 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6084 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6085 | * For cookies in prefix mode, the form is : |
| 6086 | * |
| 6087 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6088 | */ |
| 6089 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6090 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6091 | struct server *srv = t->be->srv; |
| 6092 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6093 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6094 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6095 | * have the server ID between val_beg and delim, and the original cookie between |
| 6096 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6097 | * |
| 6098 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6099 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6100 | * | || || | |+-> next |
| 6101 | * | || || | +--> val_end |
| 6102 | * | || || +---------> delim |
| 6103 | * | || |+------------> val_beg |
| 6104 | * | || +-------------> att_end = equal |
| 6105 | * | |+-----------------> att_beg |
| 6106 | * | +------------------> prev |
| 6107 | * +-------------------------> hdr_beg |
| 6108 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6109 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6110 | if (t->be->options & PR_O_COOK_PFX) { |
| 6111 | for (delim = val_beg; delim < val_end; delim++) |
| 6112 | if (*delim == COOKIE_DELIM) |
| 6113 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6114 | } else { |
| 6115 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6116 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6117 | /* Now check if the cookie contains a date field, which would |
| 6118 | * appear after a vertical bar ('|') just after the server name |
| 6119 | * and before the delimiter. |
| 6120 | */ |
| 6121 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6122 | if (vbar1) { |
| 6123 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6124 | * right is the last seen date. It is a base64 encoded |
| 6125 | * 30-bit value representing the UNIX date since the |
| 6126 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6127 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6128 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6129 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6130 | if (val_end - vbar1 >= 5) { |
| 6131 | val = b64tos30(vbar1); |
| 6132 | if (val > 0) |
| 6133 | txn->cookie_last_date = val << 2; |
| 6134 | } |
| 6135 | /* look for a second vertical bar */ |
| 6136 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6137 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6138 | val = b64tos30(vbar1 + 1); |
| 6139 | if (val > 0) |
| 6140 | txn->cookie_first_date = val << 2; |
| 6141 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6142 | } |
| 6143 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6144 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6145 | /* if the cookie has an expiration date and the proxy wants to check |
| 6146 | * it, then we do that now. We first check if the cookie is too old, |
| 6147 | * then only if it has expired. We detect strict overflow because the |
| 6148 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6149 | * in the future are ignored if their offset is beyond one day. This |
| 6150 | * allows an admin to fix timezone issues without expiring everyone |
| 6151 | * and at the same time avoids keeping unwanted side effects for too |
| 6152 | * long. |
| 6153 | */ |
| 6154 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6155 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6156 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6157 | txn->flags &= ~TX_CK_MASK; |
| 6158 | txn->flags |= TX_CK_OLD; |
| 6159 | delim = val_beg; // let's pretend we have not found the cookie |
| 6160 | txn->cookie_first_date = 0; |
| 6161 | txn->cookie_last_date = 0; |
| 6162 | } |
| 6163 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6164 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6165 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6166 | txn->flags &= ~TX_CK_MASK; |
| 6167 | txn->flags |= TX_CK_EXPIRED; |
| 6168 | delim = val_beg; // let's pretend we have not found the cookie |
| 6169 | txn->cookie_first_date = 0; |
| 6170 | txn->cookie_last_date = 0; |
| 6171 | } |
| 6172 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6173 | /* Here, we'll look for the first running server which supports the cookie. |
| 6174 | * This allows to share a same cookie between several servers, for example |
| 6175 | * to dedicate backup servers to specific servers only. |
| 6176 | * However, to prevent clients from sticking to cookie-less backup server |
| 6177 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6178 | * empty cookies and mark them as invalid. |
| 6179 | * The same behaviour is applied when persistence must be ignored. |
| 6180 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6181 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6182 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6183 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6184 | while (srv) { |
| 6185 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6186 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6187 | if ((srv->state & SRV_RUNNING) || |
| 6188 | (t->be->options & PR_O_PERSIST) || |
| 6189 | (t->flags & SN_FORCE_PRST)) { |
| 6190 | /* we found the server and we can use it */ |
| 6191 | txn->flags &= ~TX_CK_MASK; |
| 6192 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6193 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6194 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6195 | break; |
| 6196 | } else { |
| 6197 | /* we found a server, but it's down, |
| 6198 | * mark it as such and go on in case |
| 6199 | * another one is available. |
| 6200 | */ |
| 6201 | txn->flags &= ~TX_CK_MASK; |
| 6202 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6203 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6204 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6205 | srv = srv->next; |
| 6206 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6207 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6208 | 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] | 6209 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6210 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6211 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6212 | txn->flags |= TX_CK_UNUSED; |
| 6213 | else |
| 6214 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6215 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6216 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6217 | /* depending on the cookie mode, we may have to either : |
| 6218 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6219 | * the server never sees it ; |
| 6220 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6221 | * application cookie so that it does not get accidentely removed later, |
| 6222 | * if we're in cookie prefix mode |
| 6223 | */ |
| 6224 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6225 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6227 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6228 | val_end += delta; |
| 6229 | next += delta; |
| 6230 | hdr_end += delta; |
| 6231 | hdr_next += delta; |
| 6232 | cur_hdr->len += delta; |
| 6233 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6234 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6235 | del_from = NULL; |
| 6236 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6237 | } |
| 6238 | else if (del_from == NULL && |
| 6239 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6240 | del_from = prev; |
| 6241 | } |
| 6242 | } else { |
| 6243 | /* This is not our cookie, so we must preserve it. But if we already |
| 6244 | * scheduled another cookie for removal, we cannot remove the |
| 6245 | * complete header, but we can remove the previous block itself. |
| 6246 | */ |
| 6247 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6248 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6249 | if (del_from != NULL) { |
| 6250 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6251 | if (att_beg >= del_from) |
| 6252 | att_beg += delta; |
| 6253 | if (att_end >= del_from) |
| 6254 | att_end += delta; |
| 6255 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6256 | val_end += delta; |
| 6257 | next += delta; |
| 6258 | hdr_end += delta; |
| 6259 | hdr_next += delta; |
| 6260 | cur_hdr->len += delta; |
| 6261 | http_msg_move_end(&txn->req, delta); |
| 6262 | prev = del_from; |
| 6263 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6264 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6265 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6266 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6267 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6268 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6269 | int cmp_len, value_len; |
| 6270 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6271 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6272 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6273 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6274 | value_begin = att_beg + t->be->appsession_name_len; |
| 6275 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6276 | } else { |
| 6277 | cmp_len = att_end - att_beg; |
| 6278 | value_begin = val_beg; |
| 6279 | value_len = val_end - val_beg; |
| 6280 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6281 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6282 | /* let's see if the cookie is our appcookie */ |
| 6283 | if (cmp_len == t->be->appsession_name_len && |
| 6284 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6285 | manage_client_side_appsession(t, value_begin, value_len); |
| 6286 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6287 | } |
| 6288 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6289 | /* continue with next cookie on this header line */ |
| 6290 | att_beg = next; |
| 6291 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6292 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6293 | /* There are no more cookies on this line. |
| 6294 | * We may still have one (or several) marked for deletion at the |
| 6295 | * end of the line. We must do this now in two ways : |
| 6296 | * - if some cookies must be preserved, we only delete from the |
| 6297 | * mark to the end of line ; |
| 6298 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6299 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6300 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6301 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6302 | if (preserve_hdr) { |
| 6303 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6304 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6305 | cur_hdr->len += delta; |
| 6306 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6307 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6308 | |
| 6309 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6310 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6311 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6312 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6313 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6314 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6315 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6316 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6317 | } |
| 6318 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6319 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6320 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6321 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6322 | } |
| 6323 | |
| 6324 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6325 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6326 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6327 | */ |
| 6328 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6329 | { |
| 6330 | char term; |
| 6331 | char *cur_ptr, *cur_end, *cur_next; |
| 6332 | int cur_idx, old_idx, last_hdr; |
| 6333 | struct http_txn *txn = &t->txn; |
| 6334 | struct hdr_idx_elem *cur_hdr; |
| 6335 | int len, delta; |
| 6336 | |
| 6337 | last_hdr = 0; |
| 6338 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6339 | cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6340 | old_idx = 0; |
| 6341 | |
| 6342 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6343 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6344 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6345 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6346 | (exp->action == ACT_ALLOW || |
| 6347 | exp->action == ACT_DENY)) |
| 6348 | return 0; |
| 6349 | |
| 6350 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6351 | if (!cur_idx) |
| 6352 | break; |
| 6353 | |
| 6354 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6355 | cur_ptr = cur_next; |
| 6356 | cur_end = cur_ptr + cur_hdr->len; |
| 6357 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6358 | |
| 6359 | /* Now we have one header between cur_ptr and cur_end, |
| 6360 | * and the next header starts at cur_next. |
| 6361 | */ |
| 6362 | |
| 6363 | /* The annoying part is that pattern matching needs |
| 6364 | * that we modify the contents to null-terminate all |
| 6365 | * strings before testing them. |
| 6366 | */ |
| 6367 | |
| 6368 | term = *cur_end; |
| 6369 | *cur_end = '\0'; |
| 6370 | |
| 6371 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6372 | switch (exp->action) { |
| 6373 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6374 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6375 | last_hdr = 1; |
| 6376 | break; |
| 6377 | |
| 6378 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6379 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6380 | last_hdr = 1; |
| 6381 | break; |
| 6382 | |
| 6383 | case ACT_REPLACE: |
| 6384 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6385 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6386 | /* FIXME: if the user adds a newline in the replacement, the |
| 6387 | * index will not be recalculated for now, and the new line |
| 6388 | * will not be counted as a new header. |
| 6389 | */ |
| 6390 | |
| 6391 | cur_end += delta; |
| 6392 | cur_next += delta; |
| 6393 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6394 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6395 | break; |
| 6396 | |
| 6397 | case ACT_REMOVE: |
| 6398 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6399 | cur_next += delta; |
| 6400 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6401 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6402 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6403 | txn->hdr_idx.used--; |
| 6404 | cur_hdr->len = 0; |
| 6405 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6406 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6407 | break; |
| 6408 | |
| 6409 | } |
| 6410 | } |
| 6411 | if (cur_end) |
| 6412 | *cur_end = term; /* restore the string terminator */ |
| 6413 | |
| 6414 | /* keep the link from this header to next one in case of later |
| 6415 | * removal of next header. |
| 6416 | */ |
| 6417 | old_idx = cur_idx; |
| 6418 | } |
| 6419 | return 0; |
| 6420 | } |
| 6421 | |
| 6422 | |
| 6423 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6424 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6425 | * or -1 if a replacement resulted in an invalid status line. |
| 6426 | */ |
| 6427 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6428 | { |
| 6429 | char term; |
| 6430 | char *cur_ptr, *cur_end; |
| 6431 | int done; |
| 6432 | struct http_txn *txn = &t->txn; |
| 6433 | int len, delta; |
| 6434 | |
| 6435 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6436 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6437 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6438 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6439 | (exp->action == ACT_ALLOW || |
| 6440 | exp->action == ACT_DENY)) |
| 6441 | return 0; |
| 6442 | else if (exp->action == ACT_REMOVE) |
| 6443 | return 0; |
| 6444 | |
| 6445 | done = 0; |
| 6446 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6447 | cur_ptr = rtr->p + txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6448 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6449 | |
| 6450 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6451 | |
| 6452 | /* The annoying part is that pattern matching needs |
| 6453 | * that we modify the contents to null-terminate all |
| 6454 | * strings before testing them. |
| 6455 | */ |
| 6456 | |
| 6457 | term = *cur_end; |
| 6458 | *cur_end = '\0'; |
| 6459 | |
| 6460 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6461 | switch (exp->action) { |
| 6462 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6463 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6464 | done = 1; |
| 6465 | break; |
| 6466 | |
| 6467 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6468 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6469 | done = 1; |
| 6470 | break; |
| 6471 | |
| 6472 | case ACT_REPLACE: |
| 6473 | *cur_end = term; /* restore the string terminator */ |
| 6474 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6475 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6476 | /* FIXME: if the user adds a newline in the replacement, the |
| 6477 | * index will not be recalculated for now, and the new line |
| 6478 | * will not be counted as a new header. |
| 6479 | */ |
| 6480 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6481 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6482 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6483 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6484 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6485 | cur_ptr, cur_end + 1, |
| 6486 | NULL, NULL); |
| 6487 | if (unlikely(!cur_end)) |
| 6488 | return -1; |
| 6489 | |
| 6490 | /* we have a full respnse and we know that we have either a CR |
| 6491 | * or an LF at <ptr>. |
| 6492 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6493 | txn->status = strl2ui(rtr->p + txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6494 | 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] | 6495 | /* there is no point trying this regex on headers */ |
| 6496 | return 1; |
| 6497 | } |
| 6498 | } |
| 6499 | *cur_end = term; /* restore the string terminator */ |
| 6500 | return done; |
| 6501 | } |
| 6502 | |
| 6503 | |
| 6504 | |
| 6505 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6506 | * 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] | 6507 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6508 | * unparsable response. |
| 6509 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6510 | int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6511 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6512 | struct http_txn *txn = &s->txn; |
| 6513 | struct hdr_exp *exp; |
| 6514 | |
| 6515 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6516 | int ret; |
| 6517 | |
| 6518 | /* |
| 6519 | * The interleaving of transformations and verdicts |
| 6520 | * makes it difficult to decide to continue or stop |
| 6521 | * the evaluation. |
| 6522 | */ |
| 6523 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6524 | if (txn->flags & TX_SVDENY) |
| 6525 | break; |
| 6526 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6527 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6528 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6529 | exp->action == ACT_PASS)) { |
| 6530 | exp = exp->next; |
| 6531 | continue; |
| 6532 | } |
| 6533 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6534 | /* if this filter had a condition, evaluate it now and skip to |
| 6535 | * next filter if the condition does not match. |
| 6536 | */ |
| 6537 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6538 | 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] | 6539 | ret = acl_pass(ret); |
| 6540 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6541 | ret = !ret; |
| 6542 | if (!ret) |
| 6543 | continue; |
| 6544 | } |
| 6545 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6546 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6547 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6548 | if (unlikely(ret < 0)) |
| 6549 | return -1; |
| 6550 | |
| 6551 | if (likely(ret == 0)) { |
| 6552 | /* The filter did not match the response, it can be |
| 6553 | * iterated through all headers. |
| 6554 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6555 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6556 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6557 | } |
| 6558 | return 0; |
| 6559 | } |
| 6560 | |
| 6561 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6562 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6563 | * 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] | 6564 | * desirable to call it only when needed. This function is also used when we |
| 6565 | * 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] | 6566 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6567 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6568 | { |
| 6569 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6570 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6571 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6572 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6573 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6574 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6575 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6576 | /* Iterate through the headers. |
| 6577 | * we start with the start line. |
| 6578 | */ |
| 6579 | old_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6580 | hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6581 | |
| 6582 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6583 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6584 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6585 | |
| 6586 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6587 | hdr_beg = hdr_next; |
| 6588 | hdr_end = hdr_beg + cur_hdr->len; |
| 6589 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6590 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6591 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6592 | * next header starts at hdr_next. We're only interested in |
| 6593 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6594 | */ |
| 6595 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6596 | is_cookie2 = 0; |
| 6597 | prev = hdr_beg + 10; |
| 6598 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6599 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6600 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6601 | if (!val) { |
| 6602 | old_idx = cur_idx; |
| 6603 | continue; |
| 6604 | } |
| 6605 | is_cookie2 = 1; |
| 6606 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6607 | } |
| 6608 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6609 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6610 | * <prev> points to the colon. |
| 6611 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6612 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6613 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6614 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6615 | * check-cache is enabled) and we are not interested in checking |
| 6616 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6617 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6618 | if (t->be->cookie_name == NULL && |
| 6619 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6620 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6621 | return; |
| 6622 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6623 | /* OK so now we know we have to process this response cookie. |
| 6624 | * The format of the Set-Cookie header is slightly different |
| 6625 | * from the format of the Cookie header in that it does not |
| 6626 | * support the comma as a cookie delimiter (thus the header |
| 6627 | * cannot be folded) because the Expires attribute described in |
| 6628 | * the original Netscape's spec may contain an unquoted date |
| 6629 | * with a comma inside. We have to live with this because |
| 6630 | * many browsers don't support Max-Age and some browsers don't |
| 6631 | * support quoted strings. However the Set-Cookie2 header is |
| 6632 | * clean. |
| 6633 | * |
| 6634 | * We have to keep multiple pointers in order to support cookie |
| 6635 | * removal at the beginning, middle or end of header without |
| 6636 | * corrupting the header (in case of set-cookie2). A special |
| 6637 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6638 | * fields after the first semi-colon. The <next> pointer points |
| 6639 | * either to the end of line (set-cookie) or next unquoted comma |
| 6640 | * (set-cookie2). All of these headers are valid : |
| 6641 | * |
| 6642 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6643 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6644 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6645 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6646 | * | | | | | | | | | | |
| 6647 | * | | | | | | | | +-> next hdr_end <--+ |
| 6648 | * | | | | | | | +------------> scav |
| 6649 | * | | | | | | +--------------> val_end |
| 6650 | * | | | | | +--------------------> val_beg |
| 6651 | * | | | | +----------------------> equal |
| 6652 | * | | | +------------------------> att_end |
| 6653 | * | | +----------------------------> att_beg |
| 6654 | * | +------------------------------> prev |
| 6655 | * +-----------------------------------------> hdr_beg |
| 6656 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6657 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6658 | for (; prev < hdr_end; prev = next) { |
| 6659 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6660 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6661 | /* find att_beg */ |
| 6662 | att_beg = prev + 1; |
| 6663 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6664 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6665 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6666 | /* find att_end : this is the first character after the last non |
| 6667 | * space before the equal. It may be equal to hdr_end. |
| 6668 | */ |
| 6669 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6670 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6671 | while (equal < hdr_end) { |
| 6672 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6673 | break; |
| 6674 | if (http_is_spht[(unsigned char)*equal++]) |
| 6675 | continue; |
| 6676 | att_end = equal; |
| 6677 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6678 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6679 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6680 | * is between <att_beg> and <equal>, both may be identical. |
| 6681 | */ |
| 6682 | |
| 6683 | /* look for end of cookie if there is an equal sign */ |
| 6684 | if (equal < hdr_end && *equal == '=') { |
| 6685 | /* look for the beginning of the value */ |
| 6686 | val_beg = equal + 1; |
| 6687 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6688 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6689 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6690 | /* find the end of the value, respecting quotes */ |
| 6691 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6692 | |
| 6693 | /* make val_end point to the first white space or delimitor after the value */ |
| 6694 | val_end = next; |
| 6695 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6696 | val_end--; |
| 6697 | } else { |
| 6698 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6699 | val_beg = val_end = next = equal; |
| 6700 | } |
| 6701 | |
| 6702 | if (next < hdr_end) { |
| 6703 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6704 | * a colon or semi-colon before the end. So skip all attr-value |
| 6705 | * pairs and look for the next comma. For Set-Cookie, since |
| 6706 | * commas are permitted in values, skip to the end. |
| 6707 | */ |
| 6708 | if (is_cookie2) |
| 6709 | next = find_hdr_value_end(next, hdr_end); |
| 6710 | else |
| 6711 | next = hdr_end; |
| 6712 | } |
| 6713 | |
| 6714 | /* Now everything is as on the diagram above */ |
| 6715 | |
| 6716 | /* Ignore cookies with no equal sign */ |
| 6717 | if (equal == val_end) |
| 6718 | continue; |
| 6719 | |
| 6720 | /* If there are spaces around the equal sign, we need to |
| 6721 | * strip them otherwise we'll get trouble for cookie captures, |
| 6722 | * or even for rewrites. Since this happens extremely rarely, |
| 6723 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6724 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6725 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6726 | int stripped_before = 0; |
| 6727 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6728 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6729 | if (att_end != equal) { |
| 6730 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6731 | equal += stripped_before; |
| 6732 | val_beg += stripped_before; |
| 6733 | } |
| 6734 | |
| 6735 | if (val_beg > equal + 1) { |
| 6736 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6737 | val_beg += stripped_after; |
| 6738 | stripped_before += stripped_after; |
| 6739 | } |
| 6740 | |
| 6741 | val_end += stripped_before; |
| 6742 | next += stripped_before; |
| 6743 | hdr_end += stripped_before; |
| 6744 | hdr_next += stripped_before; |
| 6745 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6746 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | /* First, let's see if we want to capture this cookie. We check |
| 6750 | * that we don't already have a server side cookie, because we |
| 6751 | * can only capture one. Also as an optimisation, we ignore |
| 6752 | * cookies shorter than the declared name. |
| 6753 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6754 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6755 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6756 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6757 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6758 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6759 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6760 | Alert("HTTP logging : out of memory.\n"); |
| 6761 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6762 | else { |
| 6763 | if (log_len > t->fe->capture_len) |
| 6764 | log_len = t->fe->capture_len; |
| 6765 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6766 | txn->srv_cookie[log_len] = 0; |
| 6767 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6768 | } |
| 6769 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6770 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6771 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6772 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6773 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6774 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6775 | /* assume passive cookie by default */ |
| 6776 | txn->flags &= ~TX_SCK_MASK; |
| 6777 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6778 | |
| 6779 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6780 | * this occurrence because we'll insert another one later. |
| 6781 | * 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] | 6782 | * a direct access. |
| 6783 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6784 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 6785 | /* The "preserve" flag was set, we don't want to touch the |
| 6786 | * server's cookie. |
| 6787 | */ |
| 6788 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6789 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6790 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6791 | /* this cookie must be deleted */ |
| 6792 | if (*prev == ':' && next == hdr_end) { |
| 6793 | /* whole header */ |
| 6794 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6795 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6796 | txn->hdr_idx.used--; |
| 6797 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6798 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6799 | hdr_next += delta; |
| 6800 | http_msg_move_end(&txn->rsp, delta); |
| 6801 | /* note: while both invalid now, <next> and <hdr_end> |
| 6802 | * are still equal, so the for() will stop as expected. |
| 6803 | */ |
| 6804 | } else { |
| 6805 | /* just remove the value */ |
| 6806 | int delta = del_hdr_value(res, &prev, next); |
| 6807 | next = prev; |
| 6808 | hdr_end += delta; |
| 6809 | hdr_next += delta; |
| 6810 | cur_hdr->len += delta; |
| 6811 | http_msg_move_end(&txn->rsp, delta); |
| 6812 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6813 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6814 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6815 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6816 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6817 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6818 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6819 | * with this server since we know it. |
| 6820 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6821 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6822 | next += delta; |
| 6823 | hdr_end += delta; |
| 6824 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6825 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6826 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6827 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6828 | txn->flags &= ~TX_SCK_MASK; |
| 6829 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6830 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6831 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6832 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6833 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6834 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6835 | delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6836 | next += delta; |
| 6837 | hdr_end += delta; |
| 6838 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6839 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6840 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6841 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6842 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6843 | txn->flags &= ~TX_SCK_MASK; |
| 6844 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6845 | } |
| 6846 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6847 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6848 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6849 | int cmp_len, value_len; |
| 6850 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6851 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6852 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6853 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6854 | value_begin = att_beg + t->be->appsession_name_len; |
| 6855 | 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] | 6856 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6857 | cmp_len = att_end - att_beg; |
| 6858 | value_begin = val_beg; |
| 6859 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6860 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6861 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6862 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6863 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6864 | /* free a possibly previously allocated memory */ |
| 6865 | pool_free2(apools.sessid, txn->sessid); |
| 6866 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6867 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6868 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6869 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6870 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6871 | return; |
| 6872 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6873 | memcpy(txn->sessid, value_begin, value_len); |
| 6874 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6875 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6876 | } |
| 6877 | /* that's done for this cookie, check the next one on the same |
| 6878 | * line when next != hdr_end (only if is_cookie2). |
| 6879 | */ |
| 6880 | } |
| 6881 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6882 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6883 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6884 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6885 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6886 | appsess *asession = NULL; |
| 6887 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6888 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6889 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6890 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6891 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6892 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6893 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6894 | return; |
| 6895 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6896 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 6897 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6898 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6899 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6900 | 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] | 6901 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6902 | return; |
| 6903 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6904 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6905 | asession->sessid[t->be->appsession_len] = 0; |
| 6906 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6907 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6908 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6909 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6910 | 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] | 6911 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6912 | return; |
| 6913 | } |
| 6914 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6915 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6916 | |
| 6917 | asession->request_count = 0; |
| 6918 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6919 | } |
| 6920 | |
| 6921 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6922 | asession->request_count++; |
| 6923 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6927 | /* |
| 6928 | * Check if response is cacheable or not. Updates t->flags. |
| 6929 | */ |
| 6930 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6931 | { |
| 6932 | struct http_txn *txn = &t->txn; |
| 6933 | char *p1, *p2; |
| 6934 | |
| 6935 | char *cur_ptr, *cur_end, *cur_next; |
| 6936 | int cur_idx; |
| 6937 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6938 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6939 | return; |
| 6940 | |
| 6941 | /* Iterate through the headers. |
| 6942 | * we start with the start line. |
| 6943 | */ |
| 6944 | cur_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6945 | cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6946 | |
| 6947 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6948 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6949 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6950 | |
| 6951 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6952 | cur_ptr = cur_next; |
| 6953 | cur_end = cur_ptr + cur_hdr->len; |
| 6954 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6955 | |
| 6956 | /* We have one full header between cur_ptr and cur_end, and the |
| 6957 | * next header starts at cur_next. We're only interested in |
| 6958 | * "Cookie:" headers. |
| 6959 | */ |
| 6960 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6961 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6962 | if (val) { |
| 6963 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6964 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6965 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6966 | return; |
| 6967 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6968 | } |
| 6969 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6970 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6971 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6972 | continue; |
| 6973 | |
| 6974 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6975 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6976 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6977 | |
| 6978 | if (p1 >= cur_end) /* no more info */ |
| 6979 | continue; |
| 6980 | |
| 6981 | /* p1 is at the beginning of the value */ |
| 6982 | p2 = p1; |
| 6983 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6984 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6985 | p2++; |
| 6986 | |
| 6987 | /* we have a complete value between p1 and p2 */ |
| 6988 | if (p2 < cur_end && *p2 == '=') { |
| 6989 | /* we have something of the form no-cache="set-cookie" */ |
| 6990 | if ((cur_end - p1 >= 21) && |
| 6991 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6992 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6993 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6994 | continue; |
| 6995 | } |
| 6996 | |
| 6997 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6998 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6999 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7000 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7001 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7002 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7003 | return; |
| 7004 | } |
| 7005 | |
| 7006 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7007 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7008 | continue; |
| 7009 | } |
| 7010 | } |
| 7011 | } |
| 7012 | |
| 7013 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7014 | /* |
| 7015 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7016 | * If the server is found, it's assigned to the session. |
| 7017 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7018 | 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] | 7019 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7020 | char *end_params, *first_param, *cur_param, *next_param; |
| 7021 | char separator; |
| 7022 | int value_len; |
| 7023 | |
| 7024 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7025 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7026 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7027 | (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] | 7028 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7029 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7030 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7031 | first_param = NULL; |
| 7032 | switch (mode) { |
| 7033 | case PR_O2_AS_M_PP: |
| 7034 | first_param = memchr(begin, ';', len); |
| 7035 | break; |
| 7036 | case PR_O2_AS_M_QS: |
| 7037 | first_param = memchr(begin, '?', len); |
| 7038 | break; |
| 7039 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7040 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7041 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7042 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7043 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7044 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7045 | switch (mode) { |
| 7046 | case PR_O2_AS_M_PP: |
| 7047 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7048 | end_params = (char *) begin + len; |
| 7049 | } |
| 7050 | separator = ';'; |
| 7051 | break; |
| 7052 | case PR_O2_AS_M_QS: |
| 7053 | end_params = (char *) begin + len; |
| 7054 | separator = '&'; |
| 7055 | break; |
| 7056 | default: |
| 7057 | /* unknown mode, shouldn't happen */ |
| 7058 | return; |
| 7059 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7060 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7061 | cur_param = next_param = end_params; |
| 7062 | while (cur_param > first_param) { |
| 7063 | cur_param--; |
| 7064 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7065 | /* let's see if this is the appsession parameter */ |
| 7066 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7067 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7068 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7069 | /* Cool... it's the right one */ |
| 7070 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7071 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7072 | if (value_len > 0) { |
| 7073 | manage_client_side_appsession(t, cur_param, value_len); |
| 7074 | } |
| 7075 | break; |
| 7076 | } |
| 7077 | next_param = cur_param; |
| 7078 | } |
| 7079 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7080 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7081 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7082 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7083 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7084 | } |
| 7085 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7086 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7087 | * 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] | 7088 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7089 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7090 | * 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] | 7091 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7092 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7093 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7094 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7095 | 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] | 7096 | { |
| 7097 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7098 | struct http_msg *msg = &txn->req; |
| 7099 | const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u; |
| 7100 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7101 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7102 | if (!uri_auth) |
| 7103 | return 0; |
| 7104 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7105 | 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] | 7106 | return 0; |
| 7107 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7108 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7109 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7110 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7111 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7112 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7113 | return 0; |
| 7114 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7115 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7116 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7117 | return 0; |
| 7118 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7119 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7120 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7121 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7122 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7123 | break; |
| 7124 | } |
| 7125 | h++; |
| 7126 | } |
| 7127 | |
| 7128 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7129 | h = uri + uri_auth->uri_len; |
| 7130 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7131 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7132 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7133 | break; |
| 7134 | } |
| 7135 | h++; |
| 7136 | } |
| 7137 | } |
| 7138 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7139 | h = uri + uri_auth->uri_len; |
| 7140 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7141 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7142 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7143 | break; |
| 7144 | } |
| 7145 | h++; |
| 7146 | } |
| 7147 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7148 | h = uri + uri_auth->uri_len; |
| 7149 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7150 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7151 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7152 | h += 4; |
Cyril Bonté | 20a804a | 2012-05-10 19:42:52 +0200 | [diff] [blame] | 7153 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7154 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7155 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7156 | si->applet.ctx.stats.st_code = i; |
| 7157 | break; |
| 7158 | } |
| 7159 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7160 | break; |
| 7161 | } |
| 7162 | h++; |
| 7163 | } |
| 7164 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7165 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7166 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7167 | return 1; |
| 7168 | } |
| 7169 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7170 | /* |
| 7171 | * 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] | 7172 | * By default it tries to report the error position as msg->err_pos. However if |
| 7173 | * this one is not set, it will then report msg->next, which is the last known |
| 7174 | * parsing point. The function is able to deal with wrapping buffers. It always |
| 7175 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7176 | */ |
| 7177 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7178 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7179 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7180 | { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7181 | struct buffer *buf = msg->buf; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7182 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7183 | |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7184 | es->len = MIN(buf->i, sizeof(es->buf)); |
| 7185 | len1 = buf->data + buf->size - buf->p; |
| 7186 | len1 = MIN(len1, es->len); |
| 7187 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7188 | |
| 7189 | memcpy(es->buf, buf->p, len1); |
| 7190 | if (len2) |
| 7191 | memcpy(es->buf + len1, buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7192 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7193 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7194 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7195 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7196 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7197 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7198 | es->when = date; // user-visible date |
| 7199 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7200 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7201 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7202 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7203 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7204 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7205 | es->b_flags = buf->flags; |
| 7206 | es->s_flags = s->flags; |
| 7207 | es->t_flags = s->txn.flags; |
| 7208 | es->m_flags = msg->flags; |
| 7209 | es->b_out = buf->o; |
| 7210 | es->b_wrap = buf->data + buf->size - buf->p; |
| 7211 | es->b_tot = buf->total; |
| 7212 | es->m_clen = msg->chunk_len; |
| 7213 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7214 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7215 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7216 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7217 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7218 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7219 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7220 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7221 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7222 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7223 | * -1. |
| 7224 | * 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] | 7225 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7226 | 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] | 7227 | struct hdr_idx *idx, int occ, |
| 7228 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7229 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7230 | struct hdr_ctx local_ctx; |
| 7231 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7232 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7233 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7234 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7235 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7236 | if (!ctx) { |
| 7237 | local_ctx.idx = 0; |
| 7238 | ctx = &local_ctx; |
| 7239 | } |
| 7240 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7241 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7242 | /* search from the beginning */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7243 | while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7244 | occ--; |
| 7245 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7246 | *vptr = ctx->line + ctx->val; |
| 7247 | *vlen = ctx->vlen; |
| 7248 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7249 | } |
| 7250 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7251 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7252 | } |
| 7253 | |
| 7254 | /* negative occurrence, we scan all the list then walk back */ |
| 7255 | if (-occ > MAX_HDR_HISTORY) |
| 7256 | return 0; |
| 7257 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7258 | found = hist_ptr = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7259 | while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7260 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7261 | len_hist[hist_ptr] = ctx->vlen; |
| 7262 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7263 | hist_ptr = 0; |
| 7264 | found++; |
| 7265 | } |
| 7266 | if (-occ > found) |
| 7267 | return 0; |
| 7268 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7269 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7270 | */ |
| 7271 | hist_ptr += occ; |
| 7272 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7273 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7274 | *vptr = ptr_hist[hist_ptr]; |
| 7275 | *vlen = len_hist[hist_ptr]; |
| 7276 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7277 | } |
| 7278 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7279 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7280 | * Print a debug line with a header |
| 7281 | */ |
| 7282 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7283 | { |
| 7284 | int len, max; |
| 7285 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 7286 | dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7287 | max = end - start; |
| 7288 | UBOUND(max, sizeof(trash) - len - 1); |
| 7289 | len += strlcpy2(trash + len, start, max + 1); |
| 7290 | trash[len++] = '\n'; |
Willy Tarreau | 2133782 | 2012-04-29 14:11:38 +0200 | [diff] [blame] | 7291 | if (write(1, trash, len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7292 | } |
| 7293 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7294 | /* |
| 7295 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7296 | * the required fields are properly allocated and that we only need to (re)init |
| 7297 | * them. This should be used before processing any new request. |
| 7298 | */ |
| 7299 | void http_init_txn(struct session *s) |
| 7300 | { |
| 7301 | struct http_txn *txn = &s->txn; |
| 7302 | struct proxy *fe = s->fe; |
| 7303 | |
| 7304 | txn->flags = 0; |
| 7305 | txn->status = -1; |
| 7306 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7307 | global.req_count++; |
| 7308 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7309 | txn->cookie_first_date = 0; |
| 7310 | txn->cookie_last_date = 0; |
| 7311 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7312 | txn->req.flags = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7313 | txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7314 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7315 | txn->rsp.flags = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7316 | txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7317 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7318 | txn->req.chunk_len = 0LL; |
| 7319 | txn->req.body_len = 0LL; |
| 7320 | txn->rsp.chunk_len = 0LL; |
| 7321 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7322 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7323 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 7324 | txn->req.buf = s->req; |
| 7325 | txn->rsp.buf = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7326 | |
| 7327 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7328 | |
| 7329 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7330 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7331 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7332 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7333 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7334 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7335 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7336 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7337 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7338 | |
| 7339 | if (txn->hdr_idx.v) |
| 7340 | hdr_idx_init(&txn->hdr_idx); |
| 7341 | } |
| 7342 | |
| 7343 | /* to be used at the end of a transaction */ |
| 7344 | void http_end_txn(struct session *s) |
| 7345 | { |
| 7346 | struct http_txn *txn = &s->txn; |
| 7347 | |
| 7348 | /* these ones will have been dynamically allocated */ |
| 7349 | pool_free2(pool2_requri, txn->uri); |
| 7350 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7351 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7352 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7353 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7354 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7355 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7356 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7357 | txn->uri = NULL; |
| 7358 | txn->srv_cookie = NULL; |
| 7359 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7360 | |
| 7361 | if (txn->req.cap) { |
| 7362 | struct cap_hdr *h; |
| 7363 | for (h = s->fe->req_cap; h; h = h->next) |
| 7364 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7365 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7366 | } |
| 7367 | |
| 7368 | if (txn->rsp.cap) { |
| 7369 | struct cap_hdr *h; |
| 7370 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7371 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7372 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7373 | } |
| 7374 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7375 | } |
| 7376 | |
| 7377 | /* to be used at the end of a transaction to prepare a new one */ |
| 7378 | void http_reset_txn(struct session *s) |
| 7379 | { |
| 7380 | http_end_txn(s); |
| 7381 | http_init_txn(s); |
| 7382 | |
| 7383 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7384 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7385 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7386 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7387 | /* re-init store persistence */ |
| 7388 | s->store_count = 0; |
| 7389 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7390 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7391 | |
| 7392 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7393 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7394 | /* We must trim any excess data from the response buffer, because we |
| 7395 | * may have blocked an invalid response from a server that we don't |
| 7396 | * want to accidentely forward once we disable the analysers, nor do |
| 7397 | * we want those data to come along with next response. A typical |
| 7398 | * example of such data would be from a buggy server responding to |
| 7399 | * a HEAD with some data, or sending more than the advertised |
| 7400 | * content-length. |
| 7401 | */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 7402 | if (unlikely(s->rep->i)) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 7403 | s->rep->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7404 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7405 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7406 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7407 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7408 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7409 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7410 | |
| 7411 | s->req->rex = TICK_ETERNITY; |
| 7412 | s->req->wex = TICK_ETERNITY; |
| 7413 | s->req->analyse_exp = TICK_ETERNITY; |
| 7414 | s->rep->rex = TICK_ETERNITY; |
| 7415 | s->rep->wex = TICK_ETERNITY; |
| 7416 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7417 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7418 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7419 | void free_http_req_rules(struct list *r) { |
| 7420 | struct http_req_rule *tr, *pr; |
| 7421 | |
| 7422 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7423 | LIST_DEL(&pr->list); |
| 7424 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7425 | free(pr->http_auth.realm); |
| 7426 | |
| 7427 | free(pr); |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7432 | { |
| 7433 | struct http_req_rule *rule; |
| 7434 | int cur_arg; |
| 7435 | |
| 7436 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7437 | if (!rule) { |
| 7438 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7439 | return NULL; |
| 7440 | } |
| 7441 | |
| 7442 | if (!*args[0]) { |
| 7443 | goto req_error_parsing; |
| 7444 | } else if (!strcmp(args[0], "allow")) { |
| 7445 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7446 | cur_arg = 1; |
| 7447 | } else if (!strcmp(args[0], "deny")) { |
| 7448 | rule->action = HTTP_REQ_ACT_DENY; |
| 7449 | cur_arg = 1; |
| 7450 | } else if (!strcmp(args[0], "auth")) { |
| 7451 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7452 | cur_arg = 1; |
| 7453 | |
| 7454 | while(*args[cur_arg]) { |
| 7455 | if (!strcmp(args[cur_arg], "realm")) { |
| 7456 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7457 | cur_arg+=2; |
| 7458 | continue; |
| 7459 | } else |
| 7460 | break; |
| 7461 | } |
| 7462 | } else { |
| 7463 | req_error_parsing: |
| 7464 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7465 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7466 | return NULL; |
| 7467 | } |
| 7468 | |
| 7469 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7470 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7471 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7472 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7473 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 7474 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 7475 | file, linenum, args[0], errmsg); |
| 7476 | free(errmsg); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7477 | return NULL; |
| 7478 | } |
| 7479 | rule->cond = cond; |
| 7480 | } |
| 7481 | else if (*args[cur_arg]) { |
| 7482 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7483 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7484 | file, linenum, args[0], args[cur_arg]); |
| 7485 | return NULL; |
| 7486 | } |
| 7487 | |
| 7488 | return rule; |
| 7489 | } |
| 7490 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7491 | /************************************************************************/ |
| 7492 | /* The code below is dedicated to ACL parsing and matching */ |
| 7493 | /************************************************************************/ |
| 7494 | |
| 7495 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7496 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 7497 | * which means that a request or response is ready. If some data is missing, |
| 7498 | * 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] | 7499 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 7500 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7501 | * |
| 7502 | * The function returns : |
| 7503 | * 0 if some data is missing or if the requested data cannot be fetched |
| 7504 | * -1 if it is certain that we'll never have any HTTP message there |
| 7505 | * 1 if an HTTP message is ready |
| 7506 | */ |
| 7507 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7508 | acl_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] | 7509 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7510 | { |
| 7511 | struct http_txn *txn = l7; |
| 7512 | struct http_msg *msg = &txn->req; |
| 7513 | |
| 7514 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7515 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7516 | */ |
| 7517 | |
| 7518 | if (unlikely(!s || !txn)) |
| 7519 | return 0; |
| 7520 | |
| 7521 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7522 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7523 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7524 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7525 | if (unlikely(!s->req)) |
| 7526 | return 0; |
| 7527 | |
| 7528 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
| 7529 | if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7530 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7531 | return -1; |
| 7532 | } |
| 7533 | |
| 7534 | /* Try to decode HTTP request */ |
| 7535 | if (likely(msg->next < s->req->i)) |
| 7536 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 7537 | |
| 7538 | /* Still no valid request ? */ |
| 7539 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 7540 | if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7541 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7542 | return -1; |
| 7543 | } |
| 7544 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7545 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7546 | return 0; |
| 7547 | } |
| 7548 | |
| 7549 | /* OK we just got a valid HTTP request. We have some minor |
| 7550 | * preparation to perform so that further checks can rely |
| 7551 | * on HTTP tests. |
| 7552 | */ |
| 7553 | txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l); |
| 7554 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7555 | s->flags |= SN_REDIRECTABLE; |
| 7556 | |
| 7557 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7558 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7559 | return -1; |
| 7560 | } |
| 7561 | } |
| 7562 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7563 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7564 | return 0; /* data might have moved and indexes changed */ |
| 7565 | |
| 7566 | /* otherwise everything's ready for the request */ |
| 7567 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7568 | else { |
| 7569 | /* Check for a dependency on a response */ |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7570 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 7571 | return 0; |
| 7572 | } |
| 7573 | |
| 7574 | /* everything's OK */ |
| 7575 | return 1; |
| 7576 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7577 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7578 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7579 | do { int r = acl_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] | 7580 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7581 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7582 | do { int r = acl_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] | 7583 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7584 | |
| 7585 | /* 1. Check on METHOD |
| 7586 | * We use the pre-parsed method if it is known, and store its number as an |
| 7587 | * integer. If it is unknown, we use the pointer and the length. |
| 7588 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7589 | 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] | 7590 | { |
| 7591 | int len, meth; |
| 7592 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7593 | len = strlen(*text); |
| 7594 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7595 | |
| 7596 | pattern->val.i = meth; |
| 7597 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7598 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7599 | if (!pattern->ptr.str) { |
| 7600 | if (err) |
| 7601 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7602 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7603 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7604 | pattern->len = len; |
| 7605 | } |
| 7606 | return 1; |
| 7607 | } |
| 7608 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7609 | /* This function fetches the method of current HTTP request and stores |
| 7610 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7611 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7612 | * in <len> and <ptr> is NULL ; |
| 7613 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7614 | * <len> to its length. |
| 7615 | * This is intended to be used with acl_match_meth() only. |
| 7616 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7617 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7618 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7619 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7620 | { |
| 7621 | int meth; |
| 7622 | struct http_txn *txn = l7; |
| 7623 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7624 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7625 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7626 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7627 | smp->type = SMP_T_UINT; |
| 7628 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7629 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7630 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7631 | /* ensure the indexes are not affected */ |
| 7632 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7633 | smp->type = SMP_T_CSTR; |
| 7634 | smp->data.str.len = txn->req.sl.rq.m_l; |
| 7635 | smp->data.str.str = txn->req.buf->p + txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7636 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7637 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7638 | return 1; |
| 7639 | } |
| 7640 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7641 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7642 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7643 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7644 | int icase; |
| 7645 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7646 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7647 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7648 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7649 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7650 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7651 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7652 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7653 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7654 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7655 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7656 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7657 | |
| 7658 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7659 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7660 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7661 | |
| 7662 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7663 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 7664 | (!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] | 7665 | return ACL_PAT_FAIL; |
| 7666 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | /* 2. Check on Request/Status Version |
| 7670 | * We simply compare strings here. |
| 7671 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7672 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7673 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7674 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7675 | if (!pattern->ptr.str) { |
| 7676 | if (err) |
| 7677 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7678 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7679 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7680 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7681 | return 1; |
| 7682 | } |
| 7683 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7684 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7685 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7686 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7687 | { |
| 7688 | struct http_txn *txn = l7; |
| 7689 | char *ptr; |
| 7690 | int len; |
| 7691 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7692 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7693 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7694 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7695 | ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7696 | |
| 7697 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7698 | if (len <= 0) |
| 7699 | return 0; |
| 7700 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7701 | smp->type = SMP_T_CSTR; |
| 7702 | smp->data.str.str = ptr; |
| 7703 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7704 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7705 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7706 | return 1; |
| 7707 | } |
| 7708 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7709 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7710 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7711 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7712 | { |
| 7713 | struct http_txn *txn = l7; |
| 7714 | char *ptr; |
| 7715 | int len; |
| 7716 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7717 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7718 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7719 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7720 | ptr = txn->rsp.buf->p + txn->rsp.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7721 | |
| 7722 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7723 | if (len <= 0) |
| 7724 | return 0; |
| 7725 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7726 | smp->type = SMP_T_CSTR; |
| 7727 | smp->data.str.str = ptr; |
| 7728 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7729 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7730 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7731 | return 1; |
| 7732 | } |
| 7733 | |
| 7734 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7735 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7736 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7737 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7738 | { |
| 7739 | struct http_txn *txn = l7; |
| 7740 | char *ptr; |
| 7741 | int len; |
| 7742 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7743 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7744 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7745 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7746 | ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7747 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7748 | smp->type = SMP_T_UINT; |
| 7749 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7750 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7751 | return 1; |
| 7752 | } |
| 7753 | |
| 7754 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7755 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7756 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7757 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7758 | { |
| 7759 | struct http_txn *txn = l7; |
| 7760 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7761 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7762 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7763 | smp->type = SMP_T_CSTR; |
| 7764 | smp->data.str.len = txn->req.sl.rq.u_l; |
| 7765 | smp->data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7766 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7767 | return 1; |
| 7768 | } |
| 7769 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7770 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7771 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7772 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7773 | { |
| 7774 | struct http_txn *txn = l7; |
| 7775 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7776 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7777 | |
| 7778 | /* Parse HTTP request */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7779 | url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7780 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 7781 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7782 | smp->type = SMP_T_IPV4; |
| 7783 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7784 | |
| 7785 | /* |
| 7786 | * If we are parsing url in frontend space, we prepare backend stage |
| 7787 | * to not parse again the same url ! optimization lazyness... |
| 7788 | */ |
| 7789 | if (px->options & PR_O_HTTP_PROXY) |
| 7790 | l4->flags |= SN_ADDR_SET; |
| 7791 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7792 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7793 | return 1; |
| 7794 | } |
| 7795 | |
| 7796 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7797 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7798 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7799 | { |
| 7800 | struct http_txn *txn = l7; |
| 7801 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7802 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7803 | |
| 7804 | /* Same optimization as url_ip */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7805 | url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7806 | smp->type = SMP_T_UINT; |
| 7807 | smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7808 | |
| 7809 | if (px->options & PR_O_HTTP_PROXY) |
| 7810 | l4->flags |= SN_ADDR_SET; |
| 7811 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7812 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7813 | return 1; |
| 7814 | } |
| 7815 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7816 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 7817 | * Accepts an optional argument of type string containing the header field name, |
| 7818 | * and an optional argument of type signed or unsigned integer to request an |
| 7819 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 7820 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7821 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7822 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7823 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7824 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7825 | { |
| 7826 | struct http_txn *txn = l7; |
| 7827 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7828 | struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7829 | 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] | 7830 | int occ = 0; |
| 7831 | const char *name_str = NULL; |
| 7832 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7833 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7834 | if (args) { |
| 7835 | if (args[0].type != ARGT_STR) |
| 7836 | return 0; |
| 7837 | name_str = args[0].data.str.str; |
| 7838 | name_len = args[0].data.str.len; |
| 7839 | |
| 7840 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 7841 | occ = args[1].data.uint; |
| 7842 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7843 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7844 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7845 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7846 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7847 | /* search for header from the beginning */ |
| 7848 | ctx->idx = 0; |
| 7849 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7850 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 7851 | /* no explicit occurrence and single fetch => last header by default */ |
| 7852 | occ = -1; |
| 7853 | |
| 7854 | if (!occ) |
| 7855 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7856 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7857 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7858 | smp->type = SMP_T_CSTR; |
| 7859 | smp->flags |= SMP_F_VOL_HDR; |
| 7860 | 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] | 7861 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7862 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7863 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7864 | return 0; |
| 7865 | } |
| 7866 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7867 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7868 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7869 | */ |
| 7870 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7871 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7872 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7873 | { |
| 7874 | struct http_txn *txn = l7; |
| 7875 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7876 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7877 | 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] | 7878 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7879 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7880 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7881 | return 0; |
| 7882 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7883 | CHECK_HTTP_MESSAGE_FIRST(); |
| 7884 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7885 | ctx.idx = 0; |
| 7886 | cnt = 0; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7887 | while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->p + msg->sol, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7888 | cnt++; |
| 7889 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7890 | smp->type = SMP_T_UINT; |
| 7891 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7892 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7893 | return 1; |
| 7894 | } |
| 7895 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7896 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 7897 | * takes a mandatory argument of type string and an optional one of type int |
| 7898 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 7899 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7900 | */ |
| 7901 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7902 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7903 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7904 | { |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7905 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7906 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7907 | if (ret > 0) { |
| 7908 | smp->type = SMP_T_UINT; |
| 7909 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 7910 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7911 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7912 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7913 | } |
| 7914 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7915 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 7916 | * takes a mandatory argument of type string and an optional one of type int |
| 7917 | * to designate a specific occurrence. It returns an IPv4 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7918 | */ |
| 7919 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7920 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7921 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7922 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7923 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7924 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7925 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7926 | smp->type = SMP_T_IPV4; |
| 7927 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7928 | break; |
| 7929 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7930 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 7931 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7932 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7933 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7934 | } |
| 7935 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7936 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7937 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7938 | */ |
| 7939 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7940 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7941 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7942 | { |
| 7943 | struct http_txn *txn = l7; |
| 7944 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7945 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7946 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7947 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7948 | end = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 7949 | ptr = http_get_path(txn); |
| 7950 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7951 | return 0; |
| 7952 | |
| 7953 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7954 | smp->type = SMP_T_CSTR; |
| 7955 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7956 | |
| 7957 | while (ptr < end && *ptr != '?') |
| 7958 | ptr++; |
| 7959 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7960 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7961 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7962 | return 1; |
| 7963 | } |
| 7964 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7965 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7966 | acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7967 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7968 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7969 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7970 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7971 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7972 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7973 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7974 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7975 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7976 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7977 | return 1; |
| 7978 | } |
| 7979 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 7980 | /* return a valid test if the current request is the first one on the connection */ |
| 7981 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7982 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7983 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 7984 | { |
| 7985 | if (!s) |
| 7986 | return 0; |
| 7987 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7988 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7989 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 7990 | return 1; |
| 7991 | } |
| 7992 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7993 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7994 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7995 | acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7996 | const struct arg *args, struct sample *smp) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7997 | { |
| 7998 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7999 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8000 | return 0; |
| 8001 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8002 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8003 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8004 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8005 | return 0; |
| 8006 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8007 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8008 | 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] | 8009 | return 1; |
| 8010 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8011 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 8012 | /* Accepts exactly 1 argument of type userlist */ |
| 8013 | static int |
| 8014 | acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8015 | const struct arg *args, struct sample *smp) |
| 8016 | { |
| 8017 | |
| 8018 | if (!args || args->type != ARGT_USR) |
| 8019 | return 0; |
| 8020 | |
| 8021 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8022 | |
| 8023 | if (!get_http_auth(l4)) |
| 8024 | return 0; |
| 8025 | |
| 8026 | /* acl_match_auth() will need several information at once */ |
| 8027 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 8028 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 8029 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 8030 | |
| 8031 | /* if the user does not belong to the userlist or has a wrong password, |
| 8032 | * report that it unconditionally does not match. Otherwise we return |
| 8033 | * a non-zero integer which will be ignored anyway since all the params |
| 8034 | * that acl_match_auth() will use are in test->ctx.a[0,1,2]. |
| 8035 | */ |
| 8036 | smp->type = SMP_T_BOOL; |
| 8037 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 8038 | if (smp->data.uint) |
| 8039 | smp->type = SMP_T_UINT; |
| 8040 | |
| 8041 | return 1; |
| 8042 | } |
| 8043 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8044 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8045 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8046 | * the cookie value is returned into *value and *value_l, and the function |
| 8047 | * returns a pointer to the next pointer to search from if the value was found. |
| 8048 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8049 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8050 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8051 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8052 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8053 | * list is expected. |
| 8054 | */ |
| 8055 | static char * |
| 8056 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8057 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8058 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8059 | { |
| 8060 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8061 | char *next; |
| 8062 | |
| 8063 | /* we search at least a cookie name followed by an equal, and more |
| 8064 | * generally something like this : |
| 8065 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8066 | */ |
| 8067 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8068 | /* Iterate through all cookies on this line */ |
| 8069 | |
| 8070 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8071 | att_beg++; |
| 8072 | |
| 8073 | /* find att_end : this is the first character after the last non |
| 8074 | * space before the equal. It may be equal to hdr_end. |
| 8075 | */ |
| 8076 | equal = att_end = att_beg; |
| 8077 | |
| 8078 | while (equal < hdr_end) { |
| 8079 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8080 | break; |
| 8081 | if (http_is_spht[(unsigned char)*equal++]) |
| 8082 | continue; |
| 8083 | att_end = equal; |
| 8084 | } |
| 8085 | |
| 8086 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8087 | * is between <att_beg> and <equal>, both may be identical. |
| 8088 | */ |
| 8089 | |
| 8090 | /* look for end of cookie if there is an equal sign */ |
| 8091 | if (equal < hdr_end && *equal == '=') { |
| 8092 | /* look for the beginning of the value */ |
| 8093 | val_beg = equal + 1; |
| 8094 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8095 | val_beg++; |
| 8096 | |
| 8097 | /* find the end of the value, respecting quotes */ |
| 8098 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8099 | |
| 8100 | /* make val_end point to the first white space or delimitor after the value */ |
| 8101 | val_end = next; |
| 8102 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8103 | val_end--; |
| 8104 | } else { |
| 8105 | val_beg = val_end = next = equal; |
| 8106 | } |
| 8107 | |
| 8108 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8109 | * they will automatically be removed if a header before them is removed, |
| 8110 | * since they're supposed to be linked together. |
| 8111 | */ |
| 8112 | if (*att_beg == '$') |
| 8113 | continue; |
| 8114 | |
| 8115 | /* Ignore cookies with no equal sign */ |
| 8116 | if (equal == next) |
| 8117 | continue; |
| 8118 | |
| 8119 | /* Now we have the cookie name between att_beg and att_end, and |
| 8120 | * its value between val_beg and val_end. |
| 8121 | */ |
| 8122 | |
| 8123 | if (att_end - att_beg == cookie_name_l && |
| 8124 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8125 | /* let's return this value and indicate where to go on from */ |
| 8126 | *value = val_beg; |
| 8127 | *value_l = val_end - val_beg; |
| 8128 | return next + 1; |
| 8129 | } |
| 8130 | |
| 8131 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8132 | if (!list) |
| 8133 | break; |
| 8134 | } |
| 8135 | |
| 8136 | return NULL; |
| 8137 | } |
| 8138 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8139 | /* 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] | 8140 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 8141 | * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8142 | * 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] | 8143 | * 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] | 8144 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 8145 | * 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] | 8146 | */ |
| 8147 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8148 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8149 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8150 | { |
| 8151 | struct http_txn *txn = l7; |
| 8152 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8153 | struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8154 | const struct http_msg *msg; |
| 8155 | const char *hdr_name; |
| 8156 | int hdr_name_len; |
| 8157 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8158 | int occ = 0; |
| 8159 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8160 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8161 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8162 | return 0; |
| 8163 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8164 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8165 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8166 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8167 | msg = &txn->req; |
| 8168 | hdr_name = "Cookie"; |
| 8169 | hdr_name_len = 6; |
| 8170 | } else { |
| 8171 | msg = &txn->rsp; |
| 8172 | hdr_name = "Set-Cookie"; |
| 8173 | hdr_name_len = 10; |
| 8174 | } |
| 8175 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8176 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8177 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 8178 | occ = -1; |
| 8179 | |
| 8180 | /* OK so basically here, either we want only one value and it's the |
| 8181 | * last one, or we want to iterate over all of them and we fetch the |
| 8182 | * next one. |
| 8183 | */ |
| 8184 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8185 | sol = msg->buf->p + msg->sol; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8186 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8187 | /* search for the header from the beginning, we must first initialize |
| 8188 | * the search parameters. |
| 8189 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8190 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8191 | ctx->idx = 0; |
| 8192 | } |
| 8193 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8194 | smp->flags |= SMP_F_VOL_HDR; |
| 8195 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8196 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8197 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8198 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8199 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8200 | goto out; |
| 8201 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8202 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8203 | continue; |
| 8204 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8205 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 8206 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8207 | } |
| 8208 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8209 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8210 | 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] | 8211 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8212 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8213 | &smp->data.str.str, |
| 8214 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8215 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8216 | found = 1; |
| 8217 | if (occ >= 0) { |
| 8218 | /* one value was returned into smp->data.str.{str,len} */ |
| 8219 | smp->flags |= SMP_F_NOT_LAST; |
| 8220 | return 1; |
| 8221 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8222 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8223 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8224 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8225 | /* all cookie headers and values were scanned. If we're looking for the |
| 8226 | * last occurrence, we may return it now. |
| 8227 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8228 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8229 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8230 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8231 | } |
| 8232 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8233 | /* 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] | 8234 | * 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] | 8235 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8236 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8237 | */ |
| 8238 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8239 | acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8240 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8241 | { |
| 8242 | struct http_txn *txn = l7; |
| 8243 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8244 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8245 | const struct http_msg *msg; |
| 8246 | const char *hdr_name; |
| 8247 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8248 | int cnt; |
| 8249 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8250 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8251 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8252 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8253 | return 0; |
| 8254 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8255 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8256 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8257 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8258 | msg = &txn->req; |
| 8259 | hdr_name = "Cookie"; |
| 8260 | hdr_name_len = 6; |
| 8261 | } else { |
| 8262 | msg = &txn->rsp; |
| 8263 | hdr_name = "Set-Cookie"; |
| 8264 | hdr_name_len = 10; |
| 8265 | } |
| 8266 | |
| 8267 | sol = msg->buf->p + msg->sol; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8268 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8269 | ctx.idx = 0; |
| 8270 | cnt = 0; |
| 8271 | |
| 8272 | while (1) { |
| 8273 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8274 | if (!val_beg) { |
| 8275 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8276 | break; |
| 8277 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8278 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8279 | continue; |
| 8280 | |
| 8281 | val_beg = ctx.line + ctx.val; |
| 8282 | val_end = val_beg + ctx.vlen; |
| 8283 | } |
| 8284 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8285 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8286 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8287 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8288 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8289 | &smp->data.str.str, |
| 8290 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8291 | cnt++; |
| 8292 | } |
| 8293 | } |
| 8294 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8295 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8296 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8297 | return 1; |
| 8298 | } |
| 8299 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8300 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 8301 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 8302 | */ |
| 8303 | static int |
| 8304 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8305 | const struct arg *args, struct sample *smp) |
| 8306 | { |
| 8307 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp); |
| 8308 | |
| 8309 | if (ret > 0) { |
| 8310 | smp->type = SMP_T_UINT; |
| 8311 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8312 | } |
| 8313 | |
| 8314 | return ret; |
| 8315 | } |
| 8316 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8317 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8318 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8319 | /************************************************************************/ |
| 8320 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8321 | /* |
| 8322 | * Given a path string and its length, find the position of beginning of the |
| 8323 | * query string. Returns NULL if no query string is found in the path. |
| 8324 | * |
| 8325 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8326 | * |
| 8327 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8328 | */ |
| 8329 | static inline char *find_query_string(char *path, size_t path_l) |
| 8330 | { |
| 8331 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8332 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8333 | p = memchr(path, '?', path_l); |
| 8334 | return p ? p + 1 : NULL; |
| 8335 | } |
| 8336 | |
| 8337 | static inline int is_param_delimiter(char c) |
| 8338 | { |
| 8339 | return c == '&' || c == ';'; |
| 8340 | } |
| 8341 | |
| 8342 | /* |
| 8343 | * Given a url parameter, find the starting position of the first occurence, |
| 8344 | * or NULL if the parameter is not found. |
| 8345 | * |
| 8346 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8347 | * the function will return query_string+8. |
| 8348 | */ |
| 8349 | static char* |
| 8350 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8351 | char* url_param_name, size_t url_param_name_l) |
| 8352 | { |
| 8353 | char *pos, *last; |
| 8354 | |
| 8355 | pos = query_string; |
| 8356 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8357 | |
| 8358 | while (pos <= last) { |
| 8359 | if (pos[url_param_name_l] == '=') { |
| 8360 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8361 | return pos; |
| 8362 | pos += url_param_name_l + 1; |
| 8363 | } |
| 8364 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8365 | pos++; |
| 8366 | pos++; |
| 8367 | } |
| 8368 | return NULL; |
| 8369 | } |
| 8370 | |
| 8371 | /* |
| 8372 | * Given a url parameter name, returns its value and size into *value and |
| 8373 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8374 | * zero is returned and value/value_l are not touched. |
| 8375 | */ |
| 8376 | static int |
| 8377 | find_url_param_value(char* path, size_t path_l, |
| 8378 | char* url_param_name, size_t url_param_name_l, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8379 | char** value, int* value_l) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8380 | { |
| 8381 | char *query_string, *qs_end; |
| 8382 | char *arg_start; |
| 8383 | char *value_start, *value_end; |
| 8384 | |
| 8385 | query_string = find_query_string(path, path_l); |
| 8386 | if (!query_string) |
| 8387 | return 0; |
| 8388 | |
| 8389 | qs_end = path + path_l; |
| 8390 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8391 | url_param_name, url_param_name_l); |
| 8392 | if (!arg_start) |
| 8393 | return 0; |
| 8394 | |
| 8395 | value_start = arg_start + url_param_name_l + 1; |
| 8396 | value_end = value_start; |
| 8397 | |
| 8398 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8399 | value_end++; |
| 8400 | |
| 8401 | *value = value_start; |
| 8402 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8403 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8404 | } |
| 8405 | |
| 8406 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8407 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8408 | const struct arg *args, struct sample *smp) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8409 | { |
| 8410 | struct http_txn *txn = l7; |
| 8411 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8412 | |
| 8413 | if (!args || args->type != ARGT_STR) |
| 8414 | return 0; |
| 8415 | |
| 8416 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8417 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8418 | if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8419 | args->data.str.str, args->data.str.len, |
| 8420 | &smp->data.str.str, &smp->data.str.len)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8421 | return 0; |
| 8422 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 8423 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8424 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8425 | return 1; |
| 8426 | } |
| 8427 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8428 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 8429 | * keyword. These keywords support an optional positive or negative occurrence |
| 8430 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 8431 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 8432 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 8433 | * error message in case of error, that the caller is responsible for freeing. |
| 8434 | * The initial location must either be freeable or NULL. |
| 8435 | */ |
| 8436 | static int val_hdr(struct arg *arg, char **err_msg) |
| 8437 | { |
| 8438 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 8439 | if (err_msg) |
| 8440 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 8441 | return 0; |
| 8442 | } |
| 8443 | return 1; |
| 8444 | } |
| 8445 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8446 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8447 | /* All supported ACL keywords must be declared here. */ |
| 8448 | /************************************************************************/ |
| 8449 | |
| 8450 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8451 | * Please take care of keeping this list alphabetically sorted. |
| 8452 | */ |
| 8453 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8454 | { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 8455 | { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8456 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8457 | { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8458 | { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8459 | { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8460 | { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8461 | { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8462 | { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8463 | { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8464 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8465 | { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8466 | { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8467 | { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8468 | { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8469 | { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8470 | { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8471 | { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8472 | { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8473 | { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8474 | { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8475 | { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8476 | |
| 8477 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 8478 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8479 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8480 | |
| 8481 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8482 | |
| 8483 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8484 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8485 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8486 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8487 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8488 | { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8489 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8490 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8491 | |
| 8492 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8493 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8494 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8495 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8496 | { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 8497 | { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8498 | { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8499 | { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8500 | { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8501 | { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8502 | { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8503 | { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8504 | { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8505 | { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8506 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8507 | { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8508 | { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8509 | { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8510 | { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8511 | { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8512 | { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8513 | { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8514 | { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8515 | { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8516 | { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8517 | { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8518 | |
| 8519 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 }, |
| 8520 | |
| 8521 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8522 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8523 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8524 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8525 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8526 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8527 | { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8528 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8529 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8530 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8531 | |
| 8532 | { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 8533 | { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8534 | { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8535 | { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8536 | { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8537 | { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 8538 | { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8539 | { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8540 | { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8541 | |
| 8542 | { NULL, NULL, NULL, NULL }, |
| 8543 | }}; |
| 8544 | |
| 8545 | /************************************************************************/ |
| 8546 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8547 | /************************************************************************/ |
| 8548 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8549 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8550 | { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8551 | { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8552 | { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES }, |
| 8553 | { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */ |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 8554 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8555 | }}; |
| 8556 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8557 | |
| 8558 | __attribute__((constructor)) |
| 8559 | static void __http_protocol_init(void) |
| 8560 | { |
| 8561 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8562 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8563 | } |
| 8564 | |
| 8565 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8566 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8567 | * Local variables: |
| 8568 | * c-indent-level: 8 |
| 8569 | * c-basic-offset: 8 |
| 8570 | * End: |
| 8571 | */ |