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> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 45 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #include <proto/backend.h> |
| 47 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 48 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 51 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 53 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 54 | #include <proto/pattern.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 | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 62 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/task.h> |
| 64 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 65 | const char HTTP_100[] = |
| 66 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 67 | |
| 68 | const struct chunk http_100_chunk = { |
| 69 | .str = (char *)&HTTP_100, |
| 70 | .len = sizeof(HTTP_100)-1 |
| 71 | }; |
| 72 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 73 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 74 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 75 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 85 | |
| 86 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 87 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 92 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 94 | const char *HTTP_401_fmt = |
| 95 | "HTTP/1.0 401 Unauthorized\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 98 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 100 | "\r\n" |
| 101 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 102 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 103 | const char *HTTP_407_fmt = |
| 104 | "HTTP/1.0 407 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
| 107 | "Content-Type: text/html\r\n" |
| 108 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 114 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 115 | [HTTP_ERR_400] = 400, |
| 116 | [HTTP_ERR_403] = 403, |
| 117 | [HTTP_ERR_408] = 408, |
| 118 | [HTTP_ERR_500] = 500, |
| 119 | [HTTP_ERR_502] = 502, |
| 120 | [HTTP_ERR_503] = 503, |
| 121 | [HTTP_ERR_504] = 504, |
| 122 | }; |
| 123 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 124 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 125 | [HTTP_ERR_200] = |
| 126 | "HTTP/1.0 200 OK\r\n" |
| 127 | "Cache-Control: no-cache\r\n" |
| 128 | "Connection: close\r\n" |
| 129 | "Content-Type: text/html\r\n" |
| 130 | "\r\n" |
| 131 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 132 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 134 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | "Cache-Control: no-cache\r\n" |
| 136 | "Connection: close\r\n" |
| 137 | "Content-Type: text/html\r\n" |
| 138 | "\r\n" |
| 139 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 140 | |
| 141 | [HTTP_ERR_403] = |
| 142 | "HTTP/1.0 403 Forbidden\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 148 | |
| 149 | [HTTP_ERR_408] = |
| 150 | "HTTP/1.0 408 Request Time-out\r\n" |
| 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_500] = |
| 158 | "HTTP/1.0 500 Server Error\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_502] = |
| 166 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_503] = |
| 174 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_504] = |
| 182 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 188 | |
| 189 | }; |
| 190 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 191 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 192 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 193 | [STAT_STATUS_DENY] = "DENY", |
| 194 | [STAT_STATUS_DONE] = "DONE", |
| 195 | [STAT_STATUS_ERRP] = "ERRP", |
| 196 | [STAT_STATUS_EXCD] = "EXCD", |
| 197 | [STAT_STATUS_NONE] = "NONE", |
| 198 | [STAT_STATUS_PART] = "PART", |
| 199 | [STAT_STATUS_UNKN] = "UNKN", |
| 200 | }; |
| 201 | |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | /* We must put the messages here since GCC cannot initialize consts depending |
| 204 | * on strlen(). |
| 205 | */ |
| 206 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 207 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | #define FD_SETS_ARE_BITFIELDS |
| 209 | #ifdef FD_SETS_ARE_BITFIELDS |
| 210 | /* |
| 211 | * This map is used with all the FD_* macros to check whether a particular bit |
| 212 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 213 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 214 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 215 | * exclusively to the macros. |
| 216 | */ |
| 217 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 218 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 219 | |
| 220 | #else |
| 221 | #error "Check if your OS uses bitfields for fd_sets" |
| 222 | #endif |
| 223 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 224 | void init_proto_http() |
| 225 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 226 | int i; |
| 227 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 228 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 230 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 231 | if (!http_err_msgs[msg]) { |
| 232 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 233 | abort(); |
| 234 | } |
| 235 | |
| 236 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 237 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 238 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 239 | |
| 240 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 241 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 242 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 243 | * printable characters above. |
| 244 | */ |
| 245 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 246 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 247 | for (i = 0; i < 32; i++) { |
| 248 | FD_SET(i, hdr_encode_map); |
| 249 | FD_SET(i, url_encode_map); |
| 250 | } |
| 251 | for (i = 127; i < 256; i++) { |
| 252 | FD_SET(i, hdr_encode_map); |
| 253 | FD_SET(i, url_encode_map); |
| 254 | } |
| 255 | |
| 256 | tmp = "\"#{|}"; |
| 257 | while (*tmp) { |
| 258 | FD_SET(*tmp, hdr_encode_map); |
| 259 | tmp++; |
| 260 | } |
| 261 | |
| 262 | tmp = "\"#"; |
| 263 | while (*tmp) { |
| 264 | FD_SET(*tmp, url_encode_map); |
| 265 | tmp++; |
| 266 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 267 | |
| 268 | /* memory allocations */ |
| 269 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 270 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 271 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 272 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 274 | /* |
| 275 | * We have 26 list of methods (1 per first letter), each of which can have |
| 276 | * up to 3 entries (2 valid, 1 null). |
| 277 | */ |
| 278 | struct http_method_desc { |
| 279 | http_meth_t meth; |
| 280 | int len; |
| 281 | const char text[8]; |
| 282 | }; |
| 283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 284 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 285 | ['C' - 'A'] = { |
| 286 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 287 | }, |
| 288 | ['D' - 'A'] = { |
| 289 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 290 | }, |
| 291 | ['G' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 293 | }, |
| 294 | ['H' - 'A'] = { |
| 295 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 296 | }, |
| 297 | ['P' - 'A'] = { |
| 298 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 299 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 300 | }, |
| 301 | ['T' - 'A'] = { |
| 302 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 303 | }, |
| 304 | /* rest is empty like this : |
| 305 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 306 | */ |
| 307 | }; |
| 308 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 309 | /* 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] | 310 | * 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] | 311 | * RFC2616 for those chars. |
| 312 | */ |
| 313 | |
| 314 | const char http_is_spht[256] = { |
| 315 | [' '] = 1, ['\t'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_crlf[256] = { |
| 319 | ['\r'] = 1, ['\n'] = 1, |
| 320 | }; |
| 321 | |
| 322 | const char http_is_lws[256] = { |
| 323 | [' '] = 1, ['\t'] = 1, |
| 324 | ['\r'] = 1, ['\n'] = 1, |
| 325 | }; |
| 326 | |
| 327 | const char http_is_sep[256] = { |
| 328 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 329 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 330 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 331 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 332 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 333 | }; |
| 334 | |
| 335 | const char http_is_ctl[256] = { |
| 336 | [0 ... 31] = 1, |
| 337 | [127] = 1, |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 342 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 343 | * them correctly. Instead, define every non-CTL char's status. |
| 344 | */ |
| 345 | const char http_is_token[256] = { |
| 346 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 347 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 348 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 349 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 350 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 351 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 352 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 353 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 354 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 355 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 356 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 357 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 358 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 359 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 360 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 361 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 362 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 363 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 364 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 365 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 366 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 367 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 368 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 369 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 370 | }; |
| 371 | |
| 372 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 373 | /* |
| 374 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 375 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 376 | */ |
| 377 | const char http_is_ver_token[256] = { |
| 378 | ['.'] = 1, ['/'] = 1, |
| 379 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 380 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 381 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 382 | }; |
| 383 | |
| 384 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 385 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 386 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 387 | */ |
| 388 | #if defined(DEBUG_FSM) |
| 389 | static void http_silent_debug(int line, struct session *s) |
| 390 | { |
| 391 | int size = 0; |
| 392 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 393 | "[%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] | 394 | line, |
| 395 | 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] | 396 | 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] | 397 | write(-1, trash, size); |
| 398 | size = 0; |
| 399 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 400 | " %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] | 401 | line, |
| 402 | 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] | 403 | 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] | 404 | |
| 405 | write(-1, trash, size); |
| 406 | } |
| 407 | #else |
| 408 | #define http_silent_debug(l,s) do { } while (0) |
| 409 | #endif |
| 410 | |
| 411 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 412 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 413 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 414 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 415 | * of headers is automatically adjusted. The number of bytes added is returned |
| 416 | * on success, otherwise <0 is returned indicating an error. |
| 417 | */ |
| 418 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 419 | struct hdr_idx *hdr_idx, const char *text) |
| 420 | { |
| 421 | int bytes, len; |
| 422 | |
| 423 | len = strlen(text); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 424 | bytes = buffer_insert_line2(b, b->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 | /* |
| 432 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 433 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 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 | */ |
| 439 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 440 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 441 | { |
| 442 | int bytes; |
| 443 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 444 | bytes = buffer_insert_line2(b, b->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. |
| 595 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 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 | */ |
| 600 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 601 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 602 | { |
| 603 | int cur_idx = ctx->idx; |
| 604 | char *sol = ctx->line; |
| 605 | struct hdr_idx_elem *hdr; |
| 606 | int delta, skip_comma; |
| 607 | |
| 608 | if (!cur_idx) |
| 609 | return 0; |
| 610 | |
| 611 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 612 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 613 | /* This was the only value of the header, we must now remove it entirely. */ |
| 614 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 615 | http_msg_move_end(msg, delta); |
| 616 | idx->used--; |
| 617 | hdr->len = 0; /* unused entry */ |
| 618 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 619 | if (idx->tail == ctx->idx) |
| 620 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 621 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 622 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 623 | 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] | 624 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 625 | return ctx->idx; |
| 626 | } |
| 627 | |
| 628 | /* 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] | 629 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 630 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 631 | */ |
| 632 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 633 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 634 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 635 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 636 | NULL, 0); |
| 637 | hdr->len += delta; |
| 638 | http_msg_move_end(msg, delta); |
| 639 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 640 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 641 | return ctx->idx; |
| 642 | } |
| 643 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 644 | /* This function handles a server error at the stream interface level. The |
| 645 | * stream interface is assumed to be already in a closed state. An optional |
| 646 | * message is copied into the input buffer, and an HTTP status code stored. |
| 647 | * 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] | 648 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 649 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 650 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 651 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 652 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 653 | buffer_auto_read(si->ob); |
| 654 | buffer_abort(si->ob); |
| 655 | buffer_auto_close(si->ob); |
| 656 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 657 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 658 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 659 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 660 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 661 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | } |
| 663 | if (!(t->flags & SN_ERR_MASK)) |
| 664 | t->flags |= err; |
| 665 | if (!(t->flags & SN_FINST_MASK)) |
| 666 | t->flags |= finst; |
| 667 | } |
| 668 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 669 | /* This function returns the appropriate error location for the given session |
| 670 | * and message. |
| 671 | */ |
| 672 | |
| 673 | struct chunk *error_message(struct session *s, int msgnum) |
| 674 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 675 | if (s->be->errmsg[msgnum].str) |
| 676 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 677 | else if (s->fe->errmsg[msgnum].str) |
| 678 | return &s->fe->errmsg[msgnum]; |
| 679 | else |
| 680 | return &http_err_chunks[msgnum]; |
| 681 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 682 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 683 | /* |
| 684 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 685 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 686 | */ |
| 687 | static http_meth_t find_http_meth(const char *str, const int len) |
| 688 | { |
| 689 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 690 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 691 | |
| 692 | m = ((unsigned)*str - 'A'); |
| 693 | |
| 694 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 695 | for (h = http_methods[m]; h->len > 0; h++) { |
| 696 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 697 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 698 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 699 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 700 | }; |
| 701 | return HTTP_METH_OTHER; |
| 702 | } |
| 703 | return HTTP_METH_NONE; |
| 704 | |
| 705 | } |
| 706 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 707 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 708 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 709 | * It is returned otherwise. |
| 710 | */ |
| 711 | static char * |
| 712 | http_get_path(struct http_txn *txn) |
| 713 | { |
| 714 | char *ptr, *end; |
| 715 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 716 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 717 | end = ptr + txn->req.sl.rq.u_l; |
| 718 | |
| 719 | if (ptr >= end) |
| 720 | return NULL; |
| 721 | |
| 722 | /* RFC2616, par. 5.1.2 : |
| 723 | * Request-URI = "*" | absuri | abspath | authority |
| 724 | */ |
| 725 | |
| 726 | if (*ptr == '*') |
| 727 | return NULL; |
| 728 | |
| 729 | if (isalpha((unsigned char)*ptr)) { |
| 730 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 731 | ptr++; |
| 732 | while (ptr < end && |
| 733 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 734 | ptr++; |
| 735 | /* skip '://' */ |
| 736 | if (ptr == end || *ptr++ != ':') |
| 737 | return NULL; |
| 738 | if (ptr == end || *ptr++ != '/') |
| 739 | return NULL; |
| 740 | if (ptr == end || *ptr++ != '/') |
| 741 | return NULL; |
| 742 | } |
| 743 | /* skip [user[:passwd]@]host[:[port]] */ |
| 744 | |
| 745 | while (ptr < end && *ptr != '/') |
| 746 | ptr++; |
| 747 | |
| 748 | if (ptr == end) |
| 749 | return NULL; |
| 750 | |
| 751 | /* OK, we got the '/' ! */ |
| 752 | return ptr; |
| 753 | } |
| 754 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 755 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 756 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 757 | * left unchanged and will follow normal proxy processing. |
| 758 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 759 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 760 | { |
| 761 | struct http_txn *txn; |
| 762 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 763 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | char *path; |
| 765 | int len; |
| 766 | |
| 767 | /* 1: create the response header */ |
| 768 | rdr.len = strlen(HTTP_302); |
| 769 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 770 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 771 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 772 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 773 | srv = target_srv(&s->target); |
| 774 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 775 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 776 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 777 | return; |
| 778 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 779 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 780 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 781 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 782 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 783 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 784 | |
| 785 | /* 3: add the request URI */ |
| 786 | txn = &s->txn; |
| 787 | path = http_get_path(txn); |
| 788 | if (!path) |
| 789 | return; |
| 790 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 791 | len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 792 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 793 | return; |
| 794 | |
| 795 | memcpy(rdr.str + rdr.len, path, len); |
| 796 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 797 | |
| 798 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 799 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 800 | rdr.len += 29; |
| 801 | } else { |
| 802 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 803 | rdr.len += 23; |
| 804 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 805 | |
| 806 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 807 | si->shutr(si); |
| 808 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 809 | si->err_type = SI_ET_NONE; |
| 810 | si->err_loc = NULL; |
| 811 | si->state = SI_ST_CLO; |
| 812 | |
| 813 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 814 | 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] | 815 | |
| 816 | /* 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] | 817 | if (srv) |
| 818 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 821 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 822 | * that the server side is closed. Note that err_type is actually a |
| 823 | * bitmask, where almost only aborts may be cumulated with other |
| 824 | * values. We consider that aborted operations are more important |
| 825 | * than timeouts or errors due to the fact that nobody else in the |
| 826 | * logs might explain incomplete retries. All others should avoid |
| 827 | * being cumulated. It should normally not be possible to have multiple |
| 828 | * aborts at once, but just in case, the first one in sequence is reported. |
| 829 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 830 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 831 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 832 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 833 | |
| 834 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 835 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 836 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 837 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 838 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 839 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 840 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 841 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 842 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 843 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 844 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 845 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 846 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 847 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 848 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 849 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 850 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 851 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 852 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 853 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 854 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 857 | extern const char sess_term_cond[8]; |
| 858 | extern const char sess_fin_state[8]; |
| 859 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 860 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 861 | struct pool_head *pool2_capture; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 862 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 863 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 864 | /* |
| 865 | * Capture headers from message starting at <som> according to header list |
| 866 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 867 | */ |
| 868 | void capture_headers(char *som, struct hdr_idx *idx, |
| 869 | char **cap, struct cap_hdr *cap_hdr) |
| 870 | { |
| 871 | char *eol, *sol, *col, *sov; |
| 872 | int cur_idx; |
| 873 | struct cap_hdr *h; |
| 874 | int len; |
| 875 | |
| 876 | sol = som + hdr_idx_first_pos(idx); |
| 877 | cur_idx = hdr_idx_first_idx(idx); |
| 878 | |
| 879 | while (cur_idx) { |
| 880 | eol = sol + idx->v[cur_idx].len; |
| 881 | |
| 882 | col = sol; |
| 883 | while (col < eol && *col != ':') |
| 884 | col++; |
| 885 | |
| 886 | sov = col + 1; |
| 887 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 888 | sov++; |
| 889 | |
| 890 | for (h = cap_hdr; h; h = h->next) { |
| 891 | if ((h->namelen == col - sol) && |
| 892 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 893 | if (cap[h->index] == NULL) |
| 894 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 895 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 896 | |
| 897 | if (cap[h->index] == NULL) { |
| 898 | Alert("HTTP capture : out of memory.\n"); |
| 899 | continue; |
| 900 | } |
| 901 | |
| 902 | len = eol - sov; |
| 903 | if (len > h->len) |
| 904 | len = h->len; |
| 905 | |
| 906 | memcpy(cap[h->index], sov, len); |
| 907 | cap[h->index][len]=0; |
| 908 | } |
| 909 | } |
| 910 | sol = eol + idx->v[cur_idx].cr + 1; |
| 911 | cur_idx = idx->v[cur_idx].next; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 916 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 917 | */ |
| 918 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 919 | |
| 920 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 921 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 922 | */ |
| 923 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 924 | ptr++; \ |
| 925 | if (likely(ptr < end)) \ |
| 926 | goto good; \ |
| 927 | else { \ |
| 928 | state = (st); \ |
| 929 | goto http_msg_ood; \ |
| 930 | } \ |
| 931 | } while (0) |
| 932 | |
| 933 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 935 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 936 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 937 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 938 | * will give undefined results. |
| 939 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 940 | * and that msg->sol points to the beginning of the response. |
| 941 | * If a complete line is found (which implies that at least one CR or LF is |
| 942 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 943 | * returned indicating an incomplete line (which does not mean that parts have |
| 944 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 945 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 946 | * upon next call. |
| 947 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 948 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 949 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 950 | * 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] | 951 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 952 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 953 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 954 | const char *msg_start, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 955 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 956 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 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 | */ |
| 1024 | msg->sl.st.l = ptr - msg->sol; |
| 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 | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1062 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1063 | const char *msg_start, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1064 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1065 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1066 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1067 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1068 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1069 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1070 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1071 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1074 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1075 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1076 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1078 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1079 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1080 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1082 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1084 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1086 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | msg->sl.rq.v_l = 0; |
| 1088 | goto http_msg_rqline_eol; |
| 1089 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1090 | state = HTTP_MSG_ERROR; |
| 1091 | break; |
| 1092 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1094 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1096 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | goto http_msg_rquri; |
| 1098 | } |
| 1099 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1100 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1101 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1102 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1103 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1104 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1105 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1106 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1108 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1109 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1110 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1111 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1112 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1114 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1115 | /* non-ASCII chars are forbidden unless option |
| 1116 | * accept-invalid-http-request is enabled in the frontend. |
| 1117 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1118 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1119 | if (msg->err_pos < -1) |
| 1120 | goto invalid_char; |
| 1121 | if (msg->err_pos == -1) |
| 1122 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1123 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1124 | } |
| 1125 | |
| 1126 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1127 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1128 | goto http_msg_req09_uri_e; |
| 1129 | } |
| 1130 | |
| 1131 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1132 | invalid_char: |
| 1133 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1134 | state = HTTP_MSG_ERROR; |
| 1135 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1136 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1137 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1138 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1139 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1140 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1141 | goto http_msg_rqver; |
| 1142 | } |
| 1143 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1144 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1145 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1146 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1147 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1148 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1149 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1150 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1151 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1152 | |
| 1153 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1154 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1155 | http_msg_rqline_eol: |
| 1156 | /* We have seen the end of line. Note that we do not |
| 1157 | * necessarily have the \n yet, but at least we know that we |
| 1158 | * have EITHER \r OR \n, otherwise the request would not be |
| 1159 | * complete. We can then record the request length and return |
| 1160 | * to the caller which will be able to register it. |
| 1161 | */ |
| 1162 | msg->sl.rq.l = ptr - msg->sol; |
| 1163 | return ptr; |
| 1164 | } |
| 1165 | |
| 1166 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1167 | state = HTTP_MSG_ERROR; |
| 1168 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1169 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1170 | #ifdef DEBUG_FULL |
| 1171 | default: |
| 1172 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1173 | exit(1); |
| 1174 | #endif |
| 1175 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1176 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1177 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1178 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1179 | if (ret_state) |
| 1180 | *ret_state = state; |
| 1181 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1182 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1183 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1185 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1186 | /* |
| 1187 | * Returns the data from Authorization header. Function may be called more |
| 1188 | * than once so data is stored in txn->auth_data. When no header is found |
| 1189 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1190 | * searching again for something we are unable to find anyway. |
| 1191 | */ |
| 1192 | |
| 1193 | char get_http_auth_buff[BUFSIZE]; |
| 1194 | |
| 1195 | int |
| 1196 | get_http_auth(struct session *s) |
| 1197 | { |
| 1198 | |
| 1199 | struct http_txn *txn = &s->txn; |
| 1200 | struct chunk auth_method; |
| 1201 | struct hdr_ctx ctx; |
| 1202 | char *h, *p; |
| 1203 | int len; |
| 1204 | |
| 1205 | #ifdef DEBUG_AUTH |
| 1206 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1207 | #endif |
| 1208 | |
| 1209 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1210 | return 0; |
| 1211 | |
| 1212 | if (txn->auth.method) |
| 1213 | return 1; |
| 1214 | |
| 1215 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1216 | |
| 1217 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (txn->flags & TX_USE_PX_CONN) { |
| 1220 | h = "Proxy-Authorization"; |
| 1221 | len = strlen(h); |
| 1222 | } else { |
| 1223 | h = "Authorization"; |
| 1224 | len = strlen(h); |
| 1225 | } |
| 1226 | |
| 1227 | if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1228 | return 0; |
| 1229 | |
| 1230 | h = ctx.line + ctx.val; |
| 1231 | |
| 1232 | p = memchr(h, ' ', ctx.vlen); |
| 1233 | if (!p || p == h) |
| 1234 | return 0; |
| 1235 | |
| 1236 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1237 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1238 | |
| 1239 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1240 | |
| 1241 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1242 | get_http_auth_buff, BUFSIZE - 1); |
| 1243 | |
| 1244 | if (len < 0) |
| 1245 | return 0; |
| 1246 | |
| 1247 | |
| 1248 | get_http_auth_buff[len] = '\0'; |
| 1249 | |
| 1250 | p = strchr(get_http_auth_buff, ':'); |
| 1251 | |
| 1252 | if (!p) |
| 1253 | return 0; |
| 1254 | |
| 1255 | txn->auth.user = get_http_auth_buff; |
| 1256 | *p = '\0'; |
| 1257 | txn->auth.pass = p+1; |
| 1258 | |
| 1259 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1266 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1267 | /* |
| 1268 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1269 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1270 | * when data are missing and recalled at the exact same location with no |
| 1271 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1272 | * 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] | 1273 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1274 | * the first state, so that none of the msg pointers has to be initialized |
| 1275 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1276 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1278 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1279 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1280 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1281 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1282 | state = msg->msg_state; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1283 | ptr = buffer_wrap_add(buf, buf->p + msg->next); |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1284 | end = buffer_wrap_add(buf, buf->p + buf->i); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1285 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1286 | if (unlikely(ptr >= end)) |
| 1287 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1290 | /* |
| 1291 | * First, states that are specific to the response only. |
| 1292 | * We check them first so that request and headers are |
| 1293 | * closer to each other (accessed more often). |
| 1294 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1296 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1297 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1298 | /* we have a start of message, but we have to check |
| 1299 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1300 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1301 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1302 | char *beg = buf->p; |
| 1303 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1304 | if (unlikely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1305 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1306 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1307 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1308 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | } |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1310 | msg->som = ptr - buf->p; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1311 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1312 | hdr_idx_init(idx); |
| 1313 | state = HTTP_MSG_RPVER; |
| 1314 | goto http_msg_rpver; |
| 1315 | } |
| 1316 | |
| 1317 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1318 | goto http_msg_invalid; |
| 1319 | |
| 1320 | if (unlikely(*ptr == '\n')) |
| 1321 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1322 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1323 | /* stop here */ |
| 1324 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1325 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1326 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1327 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1328 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1329 | /* stop here */ |
| 1330 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1331 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1332 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | case HTTP_MSG_RPVER_SP: |
| 1334 | case HTTP_MSG_RPCODE: |
| 1335 | case HTTP_MSG_RPCODE_SP: |
| 1336 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1337 | ptr = (char *)http_parse_stsline(msg, buf->data, buf->p, |
| 1338 | state, ptr, end, |
| 1339 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1340 | if (unlikely(!ptr)) |
| 1341 | return; |
| 1342 | |
| 1343 | /* we have a full response and we know that we have either a CR |
| 1344 | * or an LF at <ptr>. |
| 1345 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1347 | |
| 1348 | msg->sol = ptr; |
| 1349 | if (likely(*ptr == '\r')) |
| 1350 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1351 | goto http_msg_rpline_end; |
| 1352 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1354 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | /* msg->sol must point to the first of CR or LF. */ |
| 1356 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1357 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1358 | /* stop here */ |
| 1359 | |
| 1360 | /* |
| 1361 | * Second, states that are specific to the request only |
| 1362 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1363 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1364 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1366 | /* we have a start of message, but we have to check |
| 1367 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1368 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1369 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1370 | char *beg = buf->p; |
| 1371 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1372 | if (likely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1373 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1374 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1375 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1376 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | } |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1378 | msg->som = ptr - buf->p; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1379 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1380 | /* we will need this when keep-alive will be supported |
| 1381 | hdr_idx_init(idx); |
| 1382 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1383 | state = HTTP_MSG_RQMETH; |
| 1384 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1385 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1388 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | if (unlikely(*ptr == '\n')) |
| 1391 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1392 | 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] | 1393 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1396 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1398 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1399 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1400 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1402 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | case HTTP_MSG_RQMETH_SP: |
| 1404 | case HTTP_MSG_RQURI: |
| 1405 | case HTTP_MSG_RQURI_SP: |
| 1406 | case HTTP_MSG_RQVER: |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1407 | ptr = (char *)http_parse_reqline(msg, buf->data, buf->p, |
| 1408 | state, ptr, end, |
| 1409 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1410 | if (unlikely(!ptr)) |
| 1411 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | /* we have a full request and we know that we have either a CR |
| 1414 | * or an LF at <ptr>. |
| 1415 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | msg->sol = ptr; |
| 1419 | if (likely(*ptr == '\r')) |
| 1420 | 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] | 1421 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1424 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | /* check for HTTP/0.9 request : no version information available. |
| 1426 | * msg->sol must point to the first of CR or LF. |
| 1427 | */ |
| 1428 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1429 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1432 | 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] | 1433 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1435 | /* |
| 1436 | * Common states below |
| 1437 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1439 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | msg->sol = ptr; |
| 1441 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1442 | goto http_msg_hdr_name; |
| 1443 | } |
| 1444 | |
| 1445 | if (likely(*ptr == '\r')) |
| 1446 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1447 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1448 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1450 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | /* assumes msg->sol points to the first char */ |
| 1452 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1453 | 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] | 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | if (likely(*ptr == ':')) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1456 | msg->col = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1458 | } |
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 */ |
| 1464 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 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 | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 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 | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1500 | msg->eol = buf->p + 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 | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | /* assumes msg->sol points to the first char, msg->col to the |
| 1506 | * colon, and msg->sov points to the first character of the |
| 1507 | * value. |
| 1508 | */ |
| 1509 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1510 | 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] | 1511 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | msg->eol = ptr; |
| 1513 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1514 | * real header end in case it ends with lots of LWS, but is this |
| 1515 | * really needed ? |
| 1516 | */ |
| 1517 | if (likely(*ptr == '\r')) |
| 1518 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1519 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1522 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1524 | 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] | 1525 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1527 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1529 | /* LWS: replace HT,CR,LF with spaces */ |
| 1530 | for (; msg->eol < ptr; msg->eol++) |
| 1531 | *msg->eol = ' '; |
| 1532 | goto http_msg_hdr_val; |
| 1533 | } |
| 1534 | http_msg_complete_header: |
| 1535 | /* |
| 1536 | * It was a new header, so the last one is finished. |
| 1537 | * Assumes msg->sol points to the first char, msg->col to the |
| 1538 | * colon, msg->sov points to the first character of the value |
| 1539 | * and msg->eol to the first CR or LF so we know how the line |
| 1540 | * ends. We insert last header into the index. |
| 1541 | */ |
| 1542 | /* |
| 1543 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1544 | write(2, msg->sol, msg->eol-msg->sol); |
| 1545 | fprintf(stderr,"\n"); |
| 1546 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1547 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1549 | idx, idx->tail) < 0)) |
| 1550 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1551 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | msg->sol = ptr; |
| 1553 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1554 | goto http_msg_hdr_name; |
| 1555 | } |
| 1556 | |
| 1557 | if (likely(*ptr == '\r')) |
| 1558 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1559 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1560 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1562 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1564 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1565 | ptr++; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1566 | msg->col = msg->sov = msg->next = ptr - buf->p; |
| 1567 | msg->eoh = msg->sol - buf->p; |
| 1568 | msg->sol = buf->p; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1569 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1571 | |
| 1572 | case HTTP_MSG_ERROR: |
| 1573 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1574 | break; |
| 1575 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1576 | #ifdef DEBUG_FULL |
| 1577 | default: |
| 1578 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1579 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1580 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | } |
| 1582 | http_msg_ood: |
| 1583 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1584 | msg->msg_state = state; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1585 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | http_msg_invalid: |
| 1589 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1590 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1591 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | return; |
| 1593 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1594 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1595 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1596 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1597 | * nothing is done and 1 is returned. |
| 1598 | */ |
| 1599 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1600 | { |
| 1601 | int delta; |
| 1602 | char *cur_end; |
| 1603 | |
| 1604 | if (msg->sl.rq.v_l != 0) |
| 1605 | return 1; |
| 1606 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1607 | cur_end = msg->sol + msg->sl.rq.l; |
| 1608 | delta = 0; |
| 1609 | |
| 1610 | if (msg->sl.rq.u_l == 0) { |
| 1611 | /* if no URI was set, add "/" */ |
| 1612 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1613 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1614 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1615 | } |
| 1616 | /* add HTTP version */ |
| 1617 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1618 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1619 | cur_end += delta; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1620 | cur_end = (char *)http_parse_reqline(msg, req->data, req->p, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1621 | HTTP_MSG_RQMETH, |
| 1622 | msg->sol, cur_end + 1, |
| 1623 | NULL, NULL); |
| 1624 | if (unlikely(!cur_end)) |
| 1625 | return 0; |
| 1626 | |
| 1627 | /* we have a full HTTP/1.0 request now and we know that |
| 1628 | * we have either a CR or an LF at <ptr>. |
| 1629 | */ |
| 1630 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1631 | return 1; |
| 1632 | } |
| 1633 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1634 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1635 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1636 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1637 | * are used for that : |
| 1638 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1639 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1640 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1641 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1642 | * harmless combinations may be removed. Do not call that after changes have |
| 1643 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1644 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1645 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1646 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1647 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1648 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1649 | const char *hdr_val = "Connection"; |
| 1650 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1651 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1652 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1653 | return; |
| 1654 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1655 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1656 | hdr_val = "Proxy-Connection"; |
| 1657 | hdr_len = 16; |
| 1658 | } |
| 1659 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1660 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1661 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1662 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1663 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1664 | txn->flags |= TX_HDR_CONN_KAL; |
| 1665 | if ((to_del & 2) && buf) |
| 1666 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1667 | else |
| 1668 | txn->flags |= TX_CON_KAL_SET; |
| 1669 | } |
| 1670 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1671 | txn->flags |= TX_HDR_CONN_CLO; |
| 1672 | if ((to_del & 1) && buf) |
| 1673 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1674 | else |
| 1675 | txn->flags |= TX_CON_CLO_SET; |
| 1676 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1677 | } |
| 1678 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1679 | txn->flags |= TX_HDR_CONN_PRS; |
| 1680 | return; |
| 1681 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1682 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1683 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1684 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1685 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1686 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1687 | */ |
| 1688 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1689 | { |
| 1690 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1691 | const char *hdr_val = "Connection"; |
| 1692 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1693 | |
| 1694 | ctx.idx = 0; |
| 1695 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1696 | |
| 1697 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1698 | hdr_val = "Proxy-Connection"; |
| 1699 | hdr_len = 16; |
| 1700 | } |
| 1701 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1702 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1703 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1704 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1705 | if (wanted & TX_CON_KAL_SET) |
| 1706 | txn->flags |= TX_CON_KAL_SET; |
| 1707 | else |
| 1708 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1709 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1710 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1711 | if (wanted & TX_CON_CLO_SET) |
| 1712 | txn->flags |= TX_CON_CLO_SET; |
| 1713 | else |
| 1714 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1715 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1716 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1717 | |
| 1718 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1719 | return; |
| 1720 | |
| 1721 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1722 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1723 | hdr_val = "Connection: close"; |
| 1724 | hdr_len = 17; |
| 1725 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1726 | hdr_val = "Proxy-Connection: close"; |
| 1727 | hdr_len = 23; |
| 1728 | } |
| 1729 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1733 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1734 | hdr_val = "Connection: keep-alive"; |
| 1735 | hdr_len = 22; |
| 1736 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1737 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1738 | hdr_len = 28; |
| 1739 | } |
| 1740 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1741 | } |
| 1742 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1743 | } |
| 1744 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1745 | /* 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] | 1746 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1747 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1748 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1749 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1750 | * 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] | 1751 | * 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] | 1752 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1753 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1754 | { |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1755 | char *ptr = buffer_wrap_add(buf, buf->p + msg->next); |
| 1756 | char *ptr_old = ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1757 | char *end = buf->data + buf->size; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1758 | char *stop = buffer_wrap_add(buf, buf->p + buf->i); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1759 | unsigned int chunk = 0; |
| 1760 | |
| 1761 | /* The chunk size is in the following form, though we are only |
| 1762 | * interested in the size and CRLF : |
| 1763 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1764 | */ |
| 1765 | while (1) { |
| 1766 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1767 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1768 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1769 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1770 | if (c < 0) /* not a hex digit anymore */ |
| 1771 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1772 | if (++ptr >= end) |
| 1773 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1774 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1775 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1776 | chunk = (chunk << 4) + c; |
| 1777 | } |
| 1778 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1779 | /* empty size not allowed */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1780 | if (ptr == ptr_old) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1781 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1782 | |
| 1783 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1784 | if (++ptr >= end) |
| 1785 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1786 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1787 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1788 | } |
| 1789 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1790 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1791 | * for the end of chunk size. |
| 1792 | */ |
| 1793 | while (1) { |
| 1794 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1795 | /* we now have a CR or an LF at ptr */ |
| 1796 | if (likely(*ptr == '\r')) { |
| 1797 | if (++ptr >= end) |
| 1798 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1799 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1802 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1803 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1804 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1805 | if (++ptr >= end) |
| 1806 | ptr = buf->data; |
| 1807 | /* done */ |
| 1808 | break; |
| 1809 | } |
| 1810 | else if (*ptr == ';') { |
| 1811 | /* chunk extension, ends at next CRLF */ |
| 1812 | if (++ptr >= end) |
| 1813 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1814 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1815 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1816 | |
| 1817 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1818 | if (++ptr >= end) |
| 1819 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1820 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1821 | return 0; |
| 1822 | } |
| 1823 | /* we have a CRLF now, loop above */ |
| 1824 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1825 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1826 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1827 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1830 | /* 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] | 1831 | * 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] | 1832 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1833 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1834 | msg->sov += ptr - ptr_old; |
| 1835 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1836 | msg->chunk_len = chunk; |
| 1837 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1838 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1839 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1840 | error: |
| 1841 | msg->err_pos = ptr - buf->data; |
| 1842 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1845 | /* This function skips trailers in the buffer <buf> associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1846 | * 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] | 1847 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1848 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1849 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1850 | * 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] | 1851 | * 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] | 1852 | * 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] | 1853 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1854 | * which implies that all non-trailers data have already been scheduled for |
| 1855 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1856 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1857 | * important to note that this function is designed to be able to parse wrapped |
| 1858 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1859 | */ |
| 1860 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1861 | { |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1862 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1863 | while (1) { |
| 1864 | char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1865 | char *ptr = buffer_wrap_add(buf, buf->p + msg->next); |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1866 | char *stop = buffer_wrap_add(buf, buf->p + buf->i); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1867 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* scan current line and stop at LF or CRLF */ |
| 1870 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1871 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1872 | return 0; |
| 1873 | |
| 1874 | if (*ptr == '\n') { |
| 1875 | if (!p1) |
| 1876 | p1 = ptr; |
| 1877 | p2 = ptr; |
| 1878 | break; |
| 1879 | } |
| 1880 | |
| 1881 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1882 | if (p1) { |
| 1883 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1884 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1885 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1886 | p1 = ptr; |
| 1887 | } |
| 1888 | |
| 1889 | ptr++; |
| 1890 | if (ptr >= buf->data + buf->size) |
| 1891 | ptr = buf->data; |
| 1892 | } |
| 1893 | |
| 1894 | /* after LF; point to beginning of next line */ |
| 1895 | p2++; |
| 1896 | if (p2 >= buf->data + buf->size) |
| 1897 | p2 = buf->data; |
| 1898 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1899 | bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1900 | if (bytes < 0) |
| 1901 | bytes += buf->size; |
| 1902 | |
| 1903 | /* schedule this line for forwarding */ |
| 1904 | msg->sov += bytes; |
| 1905 | if (msg->sov >= buf->size) |
| 1906 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1907 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1908 | if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1909 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1910 | * Everything was scheduled for forwarding, there's nothing |
| 1911 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1912 | */ |
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 | msg->msg_state = HTTP_MSG_DONE; |
| 1915 | return 1; |
| 1916 | } |
| 1917 | /* OK, next line then */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1918 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1923 | * 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] | 1924 | * ->som, ->next in order to include this part into the next forwarding phase. |
| 1925 | * 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] | 1926 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1927 | * not enough data are available, the function does not change anything and |
| 1928 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1929 | * does not change anything. Note: this function is designed to parse wrapped |
| 1930 | * CRLF at the end of the buffer. |
| 1931 | */ |
| 1932 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 1933 | { |
| 1934 | char *ptr; |
| 1935 | int bytes; |
| 1936 | |
| 1937 | /* NB: we'll check data availabilty at the end. It's not a |
| 1938 | * problem because whatever we match first will be checked |
| 1939 | * against the correct length. |
| 1940 | */ |
| 1941 | bytes = 1; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1942 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1943 | if (*ptr == '\r') { |
| 1944 | bytes++; |
| 1945 | ptr++; |
| 1946 | if (ptr >= buf->data + buf->size) |
| 1947 | ptr = buf->data; |
| 1948 | } |
| 1949 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1950 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | return 0; |
| 1952 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1953 | if (*ptr != '\n') { |
| 1954 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1955 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1956 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1957 | |
| 1958 | ptr++; |
| 1959 | if (ptr >= buf->data + buf->size) |
| 1960 | ptr = buf->data; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 1961 | /* prepare the CRLF to be forwarded (between ->som and ->sov) */ |
| 1962 | msg->som = 0; |
| 1963 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1964 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1965 | return 1; |
| 1966 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1967 | |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1968 | /* This function may only be used when the buffer's o is empty */ |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1969 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 1970 | { |
| 1971 | char *end = buf->data + buf->size; |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1972 | int off = buf->data + buf->size - buf->p; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* two possible cases : |
| 1975 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1976 | * - the buffer is in two blocks, we move it via the swap_buffer |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1977 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1978 | if (buf->i) { |
| 1979 | int block1 = buf->i; |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1980 | int block2 = 0; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1981 | if (buf->p + buf->i > buf->data + buf->size) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1982 | /* non-contiguous block */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1983 | block1 = buf->data + buf->size - buf->p; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1984 | block2 = buf->p + buf->i - (buf->data + buf->size); |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1985 | } |
| 1986 | if (block2) |
| 1987 | memcpy(swap_buffer, buf->data, block2); |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1988 | memmove(buf->data, buf->p, block1); |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1989 | if (block2) |
| 1990 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* adjust all known pointers */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1994 | buf->p = buf->data; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1995 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 1996 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 1997 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1998 | if (msg->err_pos >= 0) { |
| 1999 | msg->err_pos += off; |
| 2000 | if (msg->err_pos >= buf->size) |
| 2001 | msg->err_pos -= buf->size; |
| 2002 | } |
| 2003 | |
| 2004 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2005 | if (buffer_len(buf) >= buffer_max_len(buf)) |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2006 | buf->flags |= BF_FULL; |
| 2007 | } |
| 2008 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2009 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2010 | * processing can continue on next analysers, or zero if it either needs more |
| 2011 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2012 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2013 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2014 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2015 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2016 | 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] | 2017 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2018 | /* |
| 2019 | * We will parse the partial (or complete) lines. |
| 2020 | * We will check the request syntax, and also join multi-line |
| 2021 | * headers. An index of all the lines will be elaborated while |
| 2022 | * parsing. |
| 2023 | * |
| 2024 | * For the parsing, we use a 28 states FSM. |
| 2025 | * |
| 2026 | * Here is the information we currently have : |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 2027 | * req->p + msg->som = beginning of request |
| 2028 | * req->p + msg->eoh = end of processed headers / start of current one |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2029 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2030 | * msg->next = first non-visited byte |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2031 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2032 | * |
| 2033 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2034 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2035 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2036 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2037 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2038 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2039 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2040 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2041 | struct http_txn *txn = &s->txn; |
| 2042 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2043 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2044 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2045 | 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] | 2046 | now_ms, __FUNCTION__, |
| 2047 | s, |
| 2048 | req, |
| 2049 | req->rex, req->wex, |
| 2050 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2051 | req->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2052 | req->analysers); |
| 2053 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2054 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2055 | s->srv_error = http_return_srv_error; |
| 2056 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2057 | /* There's a protected area at the end of the buffer for rewriting |
| 2058 | * purposes. We don't want to start to parse the request if the |
| 2059 | * protected area is affected, because we may have to move processed |
| 2060 | * data later, which is much more complicated. |
| 2061 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2062 | if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2063 | if ((txn->flags & TX_NOT_FIRST) && |
| 2064 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2065 | buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) || |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2066 | buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2067 | if (req->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2068 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2069 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2070 | /* some data has still not left the buffer, wake us once that's done */ |
| 2071 | buffer_dont_connect(req); |
| 2072 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2073 | return 0; |
| 2074 | } |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2075 | if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) || |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2076 | buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2077 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2080 | /* Note that we have the same problem with the response ; we |
| 2081 | * may want to send a redirect, error or anything which requires |
| 2082 | * some spare space. So we'll ensure that we have at least |
| 2083 | * maxrewrite bytes available in the response buffer before |
| 2084 | * processing that one. This will only affect pipelined |
| 2085 | * keep-alive requests. |
| 2086 | */ |
| 2087 | if ((txn->flags & TX_NOT_FIRST) && |
| 2088 | unlikely((s->rep->flags & BF_FULL) || |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2089 | buffer_wrap_add(s->rep, s->rep->p + s->rep->i) < buffer_wrap_add(s->rep, s->rep->p + txn->rsp.next) || |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2090 | buffer_wrap_add(s->rep, s->rep->p + s->rep->i) > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2091 | if (s->rep->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2092 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2093 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2094 | /* don't let a connection request be initiated */ |
| 2095 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2096 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2097 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2098 | return 0; |
| 2099 | } |
| 2100 | } |
| 2101 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2102 | if (likely(msg->next < req->i)) /* some unparsed data are available */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2103 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2106 | /* 1: we might have to print this header in debug mode */ |
| 2107 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2108 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2109 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2110 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2111 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2112 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 2113 | sol = req->p + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2114 | eol = sol + msg->sl.rq.l; |
| 2115 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2116 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2117 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2118 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2119 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2120 | while (cur_idx) { |
| 2121 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2122 | debug_hdr("clihdr", s, sol, eol); |
| 2123 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2124 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2125 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2128 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2129 | /* |
| 2130 | * Now we quickly check if we have found a full valid request. |
| 2131 | * If not so, we check the FD and buffer states before leaving. |
| 2132 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2133 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2134 | * requests are checked first. When waiting for a second request |
| 2135 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2136 | * we note the error in the session flags but don't set any state. |
| 2137 | * Since the error will be noted there, it will not be counted by |
| 2138 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2139 | * Last, we may increase some tracked counters' http request errors on |
| 2140 | * the cases that are deliberately the client's fault. For instance, |
| 2141 | * a timeout or connection reset is not counted as an error. However |
| 2142 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2143 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2144 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2145 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2146 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2147 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2148 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2149 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2150 | session_inc_http_req_ctr(s); |
| 2151 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2152 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2153 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2154 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2155 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2156 | /* 1: Since we are in header mode, if there's no space |
| 2157 | * left for headers, we won't be able to free more |
| 2158 | * later, so the session will never terminate. We |
| 2159 | * must terminate it now. |
| 2160 | */ |
| 2161 | if (unlikely(req->flags & BF_FULL)) { |
| 2162 | /* FIXME: check if URI is set and return Status |
| 2163 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2164 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2165 | session_inc_http_req_ctr(s); |
| 2166 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2167 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2168 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2169 | msg->err_pos = req->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2170 | goto return_bad_req; |
| 2171 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2172 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2173 | /* 2: have we encountered a read error ? */ |
| 2174 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2175 | if (!(s->flags & SN_ERR_MASK)) |
| 2176 | s->flags |= SN_ERR_CLICL; |
| 2177 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2178 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2179 | goto failed_keep_alive; |
| 2180 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2181 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2182 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2183 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2184 | session_inc_http_err_ctr(s); |
| 2185 | } |
| 2186 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2187 | msg->msg_state = HTTP_MSG_ERROR; |
| 2188 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2189 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2190 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2191 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2192 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2193 | if (s->listener->counters) |
| 2194 | s->listener->counters->failed_req++; |
| 2195 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2196 | if (!(s->flags & SN_FINST_MASK)) |
| 2197 | s->flags |= SN_FINST_R; |
| 2198 | return 0; |
| 2199 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2200 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2201 | /* 3: has the read timeout expired ? */ |
| 2202 | 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] | 2203 | if (!(s->flags & SN_ERR_MASK)) |
| 2204 | s->flags |= SN_ERR_CLITO; |
| 2205 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2206 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2207 | goto failed_keep_alive; |
| 2208 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2209 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2210 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2211 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2212 | session_inc_http_err_ctr(s); |
| 2213 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2214 | txn->status = 408; |
| 2215 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2216 | msg->msg_state = HTTP_MSG_ERROR; |
| 2217 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2218 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2219 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2220 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2221 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2222 | if (s->listener->counters) |
| 2223 | s->listener->counters->failed_req++; |
| 2224 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2225 | if (!(s->flags & SN_FINST_MASK)) |
| 2226 | s->flags |= SN_FINST_R; |
| 2227 | return 0; |
| 2228 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2229 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2230 | /* 4: have we encountered a close ? */ |
| 2231 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2232 | if (!(s->flags & SN_ERR_MASK)) |
| 2233 | s->flags |= SN_ERR_CLICL; |
| 2234 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2235 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2236 | goto failed_keep_alive; |
| 2237 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2238 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2239 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2240 | txn->status = 400; |
| 2241 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2242 | msg->msg_state = HTTP_MSG_ERROR; |
| 2243 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2244 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2245 | session_inc_http_err_ctr(s); |
| 2246 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2247 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2248 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2249 | if (s->listener->counters) |
| 2250 | s->listener->counters->failed_req++; |
| 2251 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2252 | if (!(s->flags & SN_FINST_MASK)) |
| 2253 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2254 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2257 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2258 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2259 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2260 | #ifdef TCP_QUICKACK |
| 2261 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2262 | /* We need more data, we have to re-enable quick-ack in case we |
| 2263 | * previously disabled it, otherwise we might cause the client |
| 2264 | * to delay next data. |
| 2265 | */ |
| 2266 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2267 | } |
| 2268 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2269 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2270 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2271 | /* If the client starts to talk, let's fall back to |
| 2272 | * request timeout processing. |
| 2273 | */ |
| 2274 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2275 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2276 | } |
| 2277 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2278 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2279 | if (!tick_isset(req->analyse_exp)) { |
| 2280 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2281 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2282 | tick_isset(s->be->timeout.httpka)) |
| 2283 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2284 | else |
| 2285 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2286 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2287 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2288 | /* we're not ready yet */ |
| 2289 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2290 | |
| 2291 | failed_keep_alive: |
| 2292 | /* Here we process low-level errors for keep-alive requests. In |
| 2293 | * short, if the request is not the first one and it experiences |
| 2294 | * a timeout, read error or shutdown, we just silently close so |
| 2295 | * that the client can try again. |
| 2296 | */ |
| 2297 | txn->status = 0; |
| 2298 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2299 | req->analysers = 0; |
| 2300 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2301 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2302 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2303 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2304 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2305 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2306 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2307 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2308 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2309 | * 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] | 2310 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2311 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2312 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2313 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2314 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2315 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2316 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2317 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2318 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2319 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2320 | /* kill the pending keep-alive timeout */ |
| 2321 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2322 | req->analyse_exp = TICK_ETERNITY; |
| 2323 | } |
| 2324 | |
| 2325 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2326 | /* Maybe we found in invalid header name while we were configured not |
| 2327 | * to block on that, so we have to capture it now. |
| 2328 | */ |
| 2329 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2330 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2331 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2332 | /* |
| 2333 | * 1: identify the method |
| 2334 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2335 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2336 | |
| 2337 | /* we can make use of server redirect on GET and HEAD */ |
| 2338 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2339 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2340 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2341 | /* |
| 2342 | * 2: check if the URI matches the monitor_uri. |
| 2343 | * We have to do this for every request which gets in, because |
| 2344 | * the monitor-uri is defined by the frontend. |
| 2345 | */ |
| 2346 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2347 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2348 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2349 | s->fe->monitor_uri, |
| 2350 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2351 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2352 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2353 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2354 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2355 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2357 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2358 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2359 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2360 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2361 | int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2362 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2363 | ret = acl_pass(ret); |
| 2364 | if (cond->pol == ACL_COND_UNLESS) |
| 2365 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2366 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2367 | if (ret) { |
| 2368 | /* we fail this request, let's return 503 service unavail */ |
| 2369 | txn->status = 503; |
| 2370 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2371 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2372 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2373 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2374 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2375 | /* nothing to fail, let's reply normaly */ |
| 2376 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2377 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2378 | goto return_prx_cond; |
| 2379 | } |
| 2380 | |
| 2381 | /* |
| 2382 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2383 | * Note: we cannot log anymore if the request has been |
| 2384 | * classified as invalid. |
| 2385 | */ |
| 2386 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2387 | /* we have a complete HTTP request that we must log */ |
| 2388 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2389 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2390 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2391 | if (urilen >= REQURI_LEN) |
| 2392 | urilen = REQURI_LEN - 1; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 2393 | memcpy(txn->uri, &req->p[msg->som], urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2394 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2395 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2396 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2397 | s->do_log(s); |
| 2398 | } else { |
| 2399 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2400 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2401 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2402 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2403 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2404 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2405 | } |
| 2406 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2407 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2408 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2409 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2410 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2411 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2412 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2413 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2414 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2415 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2416 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2417 | |
| 2418 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2419 | 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] | 2420 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2421 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2422 | * we have what looks like a proxied connection instead of a connection, |
| 2423 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2424 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2425 | * happen to do that despite being non-standard :-( |
| 2426 | * We consider that a request not beginning with either '/' or '*' is |
| 2427 | * a proxied connection, which covers both "scheme://location" and |
| 2428 | * CONNECT ip:port. |
| 2429 | */ |
| 2430 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2431 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2432 | txn->flags |= TX_USE_PX_CONN; |
| 2433 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2434 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2435 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2436 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2437 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2438 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2439 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2440 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2441 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2442 | /* 6: determine the transfer-length. |
| 2443 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2444 | * the presence of a message-body in a REQUEST and its transfer length |
| 2445 | * must be determined that way (in order of precedence) : |
| 2446 | * 1. The presence of a message-body in a request is signaled by the |
| 2447 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2448 | * in the request's header fields. When a request message contains |
| 2449 | * both a message-body of non-zero length and a method that does |
| 2450 | * not define any semantics for that request message-body, then an |
| 2451 | * origin server SHOULD either ignore the message-body or respond |
| 2452 | * with an appropriate error message (e.g., 413). A proxy or |
| 2453 | * gateway, when presented the same request, SHOULD either forward |
| 2454 | * the request inbound with the message- body or ignore the |
| 2455 | * message-body when determining a response. |
| 2456 | * |
| 2457 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2458 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2459 | * transfer-length is defined by the use of this transfer-coding. |
| 2460 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2461 | * transfer-coding is not present, the transfer-length is defined |
| 2462 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2463 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2464 | * 3. If a Content-Length header field is present, its decimal value in |
| 2465 | * OCTETs represents both the entity-length and the transfer-length. |
| 2466 | * If a message is received with both a Transfer-Encoding header |
| 2467 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2468 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2469 | * 4. By the server closing the connection. (Closing the connection |
| 2470 | * cannot be used to indicate the end of a request body, since that |
| 2471 | * would leave no possibility for the server to send back a response.) |
| 2472 | * |
| 2473 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2474 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2475 | * it is terminated by closing the connection. When the "chunked" |
| 2476 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2477 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2478 | */ |
| 2479 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2480 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2481 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2482 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2483 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2484 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2485 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2486 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2487 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2488 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2489 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2490 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2491 | break; |
| 2492 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2493 | } |
| 2494 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2495 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2496 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2497 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2498 | signed long long cl; |
| 2499 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2500 | if (!ctx.vlen) { |
| 2501 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2502 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2503 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2504 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2505 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2506 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2507 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2508 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2509 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2510 | if (cl < 0) { |
| 2511 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2512 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2513 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2514 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2515 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2516 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2517 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2518 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2519 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2520 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2521 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2524 | /* bodyless requests have a known length */ |
| 2525 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2526 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2527 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2528 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2529 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2530 | req->analyse_exp = TICK_ETERNITY; |
| 2531 | return 1; |
| 2532 | |
| 2533 | return_bad_req: |
| 2534 | /* We centralize bad requests processing here */ |
| 2535 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2536 | /* we detected a parsing error. We want to archive this request |
| 2537 | * in the dedicated proxy area for later troubleshooting. |
| 2538 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2539 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2543 | txn->status = 400; |
| 2544 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2545 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2546 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2547 | if (s->listener->counters) |
| 2548 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2549 | |
| 2550 | return_prx_cond: |
| 2551 | if (!(s->flags & SN_ERR_MASK)) |
| 2552 | s->flags |= SN_ERR_PRXCOND; |
| 2553 | if (!(s->flags & SN_FINST_MASK)) |
| 2554 | s->flags |= SN_FINST_R; |
| 2555 | |
| 2556 | req->analysers = 0; |
| 2557 | req->analyse_exp = TICK_ETERNITY; |
| 2558 | return 0; |
| 2559 | } |
| 2560 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2561 | /* We reached the stats page through a POST request. |
| 2562 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2563 | * 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] | 2564 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2565 | 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] | 2566 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2567 | struct proxy *px = NULL; |
| 2568 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2569 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2570 | char key[LINESIZE]; |
| 2571 | int action = ST_ADM_ACTION_NONE; |
| 2572 | int reprocess = 0; |
| 2573 | |
| 2574 | int total_servers = 0; |
| 2575 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2576 | |
| 2577 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2578 | char *st_cur_param = NULL; |
| 2579 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2580 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 2581 | first_param = req->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2582 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2583 | |
| 2584 | cur_param = next_param = end_params; |
| 2585 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2586 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2587 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2588 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2589 | return 1; |
| 2590 | } |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2591 | else if (end_params > req->p + req->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2592 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2593 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2594 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | *end_params = '\0'; |
| 2598 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2599 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2600 | |
| 2601 | /* |
| 2602 | * Parse the parameters in reverse order to only store the last value. |
| 2603 | * From the html form, the backend and the action are at the end. |
| 2604 | */ |
| 2605 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2606 | char *value; |
| 2607 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2608 | |
| 2609 | cur_param--; |
| 2610 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2611 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2612 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2613 | poffset = (cur_param != first_param ? 1 : 0); |
| 2614 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2615 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2616 | strncpy(key, cur_param + poffset, plen); |
| 2617 | key[plen - 1] = '\0'; |
| 2618 | } else { |
| 2619 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2620 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /* Parse the value */ |
| 2624 | value = key; |
| 2625 | while (*value != '\0' && *value != '=') { |
| 2626 | value++; |
| 2627 | } |
| 2628 | if (*value == '=') { |
| 2629 | /* Ok, a value is found, we can mark the end of the key */ |
| 2630 | *value++ = '\0'; |
| 2631 | } |
| 2632 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2633 | if (!url_decode(key) || !url_decode(value)) |
| 2634 | break; |
| 2635 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2636 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2637 | if (!px && (strcmp(key, "b") == 0)) { |
| 2638 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2639 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2640 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2641 | goto out; |
| 2642 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2643 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2644 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2645 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2646 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2647 | } |
| 2648 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2649 | action = ST_ADM_ACTION_ENABLE; |
| 2650 | } |
| 2651 | else { |
| 2652 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2653 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2654 | } |
| 2655 | } |
| 2656 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2657 | if (!(px && action)) { |
| 2658 | /* |
| 2659 | * Indicates that we'll need to reprocess the parameters |
| 2660 | * as soon as backend and action are known |
| 2661 | */ |
| 2662 | if (!reprocess) { |
| 2663 | st_cur_param = cur_param; |
| 2664 | st_next_param = next_param; |
| 2665 | } |
| 2666 | reprocess = 1; |
| 2667 | } |
| 2668 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2669 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2670 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2671 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2672 | /* Not already in maintenance, we can change the server state */ |
| 2673 | sv->state |= SRV_MAINTAIN; |
| 2674 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2675 | altered_servers++; |
| 2676 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2677 | } |
| 2678 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2679 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2680 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2681 | /* Already in maintenance, we can change the server state */ |
| 2682 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2683 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2684 | altered_servers++; |
| 2685 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2686 | } |
| 2687 | break; |
| 2688 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2689 | } else { |
| 2690 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2691 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2692 | } |
| 2693 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2694 | if (reprocess && px && action) { |
| 2695 | /* Now, we know the backend and the action chosen by the user. |
| 2696 | * We can safely restart from the first server parameter |
| 2697 | * to reprocess them |
| 2698 | */ |
| 2699 | cur_param = st_cur_param; |
| 2700 | next_param = st_next_param; |
| 2701 | reprocess = 0; |
| 2702 | goto reprocess_servers; |
| 2703 | } |
| 2704 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2705 | next_param = cur_param; |
| 2706 | } |
| 2707 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2708 | |
| 2709 | if (total_servers == 0) { |
| 2710 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2711 | } |
| 2712 | else if (altered_servers == 0) { |
| 2713 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2714 | } |
| 2715 | else if (altered_servers == total_servers) { |
| 2716 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2717 | } |
| 2718 | else { |
| 2719 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2720 | } |
| 2721 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2722 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2725 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2726 | * or NULL if everything's OK. |
| 2727 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2728 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2729 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2730 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2731 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2733 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2734 | int ret = 1; |
| 2735 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2736 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2737 | continue; |
| 2738 | |
| 2739 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2740 | if (rule->cond) { |
| 2741 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2742 | ret = acl_pass(ret); |
| 2743 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2744 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2745 | ret = !ret; |
| 2746 | } |
| 2747 | |
| 2748 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2749 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2750 | return NULL; /* no problem */ |
| 2751 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2752 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | return NULL; |
| 2756 | } |
| 2757 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2758 | /* This stream analyser runs all HTTP request processing which is common to |
| 2759 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2760 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2761 | * 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] | 2762 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2763 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2764 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2765 | 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] | 2766 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2767 | struct http_txn *txn = &s->txn; |
| 2768 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2769 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2770 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2771 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2772 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2773 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2774 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2775 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2776 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2777 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2778 | return 0; |
| 2779 | } |
| 2780 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2781 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2782 | req->analyse_exp = TICK_ETERNITY; |
| 2783 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2784 | 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] | 2785 | now_ms, __FUNCTION__, |
| 2786 | s, |
| 2787 | req, |
| 2788 | req->rex, req->wex, |
| 2789 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2790 | req->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2791 | req->analysers); |
| 2792 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2793 | /* first check whether we have some ACLs set to block this request */ |
| 2794 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2795 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2796 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2797 | ret = acl_pass(ret); |
| 2798 | if (cond->pol == ACL_COND_UNLESS) |
| 2799 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2800 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2801 | if (ret) { |
| 2802 | txn->status = 403; |
| 2803 | /* let's log the request time */ |
| 2804 | s->logs.tv_request = now; |
| 2805 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2806 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2807 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2808 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2809 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2810 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2811 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2812 | 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] | 2813 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2814 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2815 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2816 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2817 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2818 | 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] | 2819 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2820 | else |
| 2821 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2822 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2823 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2824 | 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] | 2825 | txn->status = 403; |
| 2826 | s->logs.tv_request = now; |
| 2827 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2828 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2829 | s->fe->fe_counters.denied_req++; |
| 2830 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2831 | s->be->be_counters.denied_req++; |
| 2832 | if (s->listener->counters) |
| 2833 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2834 | goto return_prx_cond; |
| 2835 | } |
| 2836 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2837 | /* try headers filters */ |
| 2838 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2839 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2840 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2841 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2842 | /* has the request been denied ? */ |
| 2843 | if (txn->flags & TX_CLDENY) { |
| 2844 | /* no need to go further */ |
| 2845 | txn->status = 403; |
| 2846 | /* let's log the request time */ |
| 2847 | s->logs.tv_request = now; |
| 2848 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2849 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2850 | goto return_prx_cond; |
| 2851 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2852 | |
| 2853 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2854 | * which may be the same as the connect timeout if unspecified. |
| 2855 | * If unset, then set it to zero because we really want it to |
| 2856 | * eventually expire. We build the tarpit as an analyser. |
| 2857 | */ |
| 2858 | if (txn->flags & TX_CLTARPIT) { |
| 2859 | buffer_erase(s->req); |
| 2860 | /* wipe the request out so that we can drop the connection early |
| 2861 | * if the client closes first. |
| 2862 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2863 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2864 | req->analysers = 0; /* remove switching rules etc... */ |
| 2865 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2866 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2867 | if (!req->analyse_exp) |
| 2868 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2869 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2870 | return 1; |
| 2871 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2872 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2873 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2874 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2875 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2876 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2877 | * 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] | 2878 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2879 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2880 | * The method consists in checking if options changed between the two calls |
| 2881 | * (implying that either one is non-null, or one of them is non-null and we |
| 2882 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2883 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2884 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2885 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2886 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2887 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2888 | (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] | 2889 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2890 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2891 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2892 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2893 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2894 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2895 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2896 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2897 | tmp = TX_CON_WANT_CLO; |
| 2898 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2899 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2900 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2901 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2902 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2903 | /* parse the Connection header and possibly clean it */ |
| 2904 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2905 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2906 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2907 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2908 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2909 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2910 | to_del |= 1; /* remove "close" */ |
| 2911 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2912 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2913 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2914 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2915 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2916 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2917 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2918 | (!(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] | 2919 | ((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] | 2920 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2921 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2922 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2923 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2924 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2925 | /* we can be blocked here because the request needs to be authenticated, |
| 2926 | * either to pass or to access stats. |
| 2927 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2928 | 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] | 2929 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2930 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2931 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2932 | if (!realm) |
| 2933 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2934 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2935 | 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] | 2936 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2937 | txn->status = 401; |
| 2938 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2939 | /* on 401 we still count one error, because normal browsing |
| 2940 | * won't significantly increase the counter but brute force |
| 2941 | * attempts will. |
| 2942 | */ |
| 2943 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2944 | goto return_prx_cond; |
| 2945 | } |
| 2946 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2947 | /* add request headers from the rule sets in the same order */ |
| 2948 | list_for_each_entry(wl, &px->req_add, list) { |
| 2949 | if (wl->cond) { |
| 2950 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2951 | ret = acl_pass(ret); |
| 2952 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2953 | ret = !ret; |
| 2954 | if (!ret) |
| 2955 | continue; |
| 2956 | } |
| 2957 | |
| 2958 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 2959 | goto return_bad_req; |
| 2960 | } |
| 2961 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2962 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2963 | struct stats_admin_rule *stats_admin_rule; |
| 2964 | |
| 2965 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2966 | * FIXME!!! that one is rather dangerous, we want to |
| 2967 | * make it follow standard rules (eg: clear req->analysers). |
| 2968 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2969 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2970 | /* now check whether we have some admin rules for this request */ |
| 2971 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2972 | int ret = 1; |
| 2973 | |
| 2974 | if (stats_admin_rule->cond) { |
| 2975 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 2976 | ret = acl_pass(ret); |
| 2977 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2978 | ret = !ret; |
| 2979 | } |
| 2980 | |
| 2981 | if (ret) { |
| 2982 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2983 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2988 | /* Was the status page requested with a POST ? */ |
| 2989 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2990 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2991 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2992 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2993 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2994 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2995 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2996 | struct hdr_ctx ctx; |
| 2997 | ctx.idx = 0; |
| 2998 | /* Expect is allowed in 1.1, look for it */ |
| 2999 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3000 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3001 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3002 | } |
| 3003 | } |
| 3004 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3005 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3006 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3007 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3008 | /* we need more data */ |
| 3009 | req->analysers |= an_bit; |
| 3010 | buffer_dont_connect(req); |
| 3011 | return 0; |
| 3012 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3013 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3014 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3015 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3016 | } |
| 3017 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3018 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3019 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3020 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3021 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3022 | s->rep->prod->applet.private = s; |
| 3023 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3024 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3025 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3026 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3027 | |
| 3028 | return 0; |
| 3029 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3030 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3032 | /* check whether we have some ACLs set to redirect this request */ |
| 3033 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3034 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3035 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3036 | if (rule->cond) { |
| 3037 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3038 | ret = acl_pass(ret); |
| 3039 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3040 | ret = !ret; |
| 3041 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3042 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3043 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3044 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3045 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3046 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3047 | /* build redirect message */ |
| 3048 | switch(rule->code) { |
| 3049 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3050 | msg_fmt = HTTP_303; |
| 3051 | break; |
| 3052 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3053 | msg_fmt = HTTP_301; |
| 3054 | break; |
| 3055 | case 302: |
| 3056 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3057 | msg_fmt = HTTP_302; |
| 3058 | break; |
| 3059 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3061 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3062 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3063 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3064 | switch(rule->type) { |
| 3065 | case REDIRECT_TYPE_PREFIX: { |
| 3066 | const char *path; |
| 3067 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3069 | path = http_get_path(txn); |
| 3070 | /* build message using path */ |
| 3071 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3072 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3073 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3074 | int qs = 0; |
| 3075 | while (qs < pathlen) { |
| 3076 | if (path[qs] == '?') { |
| 3077 | pathlen = qs; |
| 3078 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3079 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3081 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3082 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3083 | } else { |
| 3084 | path = "/"; |
| 3085 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3086 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3087 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3088 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3089 | goto return_bad_req; |
| 3090 | |
| 3091 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3092 | * add anything, otherwise it makes it hard for the user to |
| 3093 | * configure a self-redirection. |
| 3094 | */ |
| 3095 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3096 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3097 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3098 | } |
| 3099 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3100 | /* add path */ |
| 3101 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3102 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3103 | |
| 3104 | /* append a slash at the end of the location is needed and missing */ |
| 3105 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3106 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3107 | if (rdr.len > rdr.size - 5) |
| 3108 | goto return_bad_req; |
| 3109 | rdr.str[rdr.len] = '/'; |
| 3110 | rdr.len++; |
| 3111 | } |
| 3112 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3113 | break; |
| 3114 | } |
| 3115 | case REDIRECT_TYPE_LOCATION: |
| 3116 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3117 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3118 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3119 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3120 | /* add location */ |
| 3121 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3122 | rdr.len += rule->rdr_len; |
| 3123 | break; |
| 3124 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3125 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3126 | if (rule->cookie_len) { |
| 3127 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3128 | rdr.len += 14; |
| 3129 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3130 | rdr.len += rule->cookie_len; |
| 3131 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3132 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3133 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3134 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3135 | /* add end of headers and the keep-alive/close status. |
| 3136 | * We may choose to set keep-alive if the Location begins |
| 3137 | * with a slash, because the client will come back to the |
| 3138 | * same server. |
| 3139 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3140 | txn->status = rule->code; |
| 3141 | /* let's log the request time */ |
| 3142 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3143 | |
| 3144 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3145 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3146 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3147 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3148 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3149 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3150 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3151 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3152 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3153 | rdr.len += 30; |
| 3154 | } else { |
| 3155 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3156 | rdr.len += 24; |
| 3157 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3158 | } |
| 3159 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3160 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3161 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3162 | /* "eat" the request */ |
| 3163 | buffer_ignore(req, msg->sov - msg->som); |
| 3164 | msg->som = msg->sov; |
| 3165 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3166 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3167 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3168 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3169 | break; |
| 3170 | } else { |
| 3171 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3172 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3173 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3174 | rdr.len += 29; |
| 3175 | } else { |
| 3176 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3177 | rdr.len += 23; |
| 3178 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3179 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3180 | goto return_prx_cond; |
| 3181 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3182 | } |
| 3183 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3184 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3185 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3186 | * If this happens, then the data will not come immediately, so we must |
| 3187 | * send all what we have without waiting. Note that due to the small gain |
| 3188 | * in waiting for the body of the request, it's easier to simply put the |
| 3189 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3190 | * itself once used. |
| 3191 | */ |
| 3192 | req->flags |= BF_SEND_DONTWAIT; |
| 3193 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3194 | /* that's OK for us now, let's move on to next analysers */ |
| 3195 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3196 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3197 | return_bad_req: |
| 3198 | /* We centralize bad requests processing here */ |
| 3199 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3200 | /* we detected a parsing error. We want to archive this request |
| 3201 | * in the dedicated proxy area for later troubleshooting. |
| 3202 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3203 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3204 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3205 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3206 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3207 | txn->status = 400; |
| 3208 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3209 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3210 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3211 | if (s->listener->counters) |
| 3212 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3213 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3214 | return_prx_cond: |
| 3215 | if (!(s->flags & SN_ERR_MASK)) |
| 3216 | s->flags |= SN_ERR_PRXCOND; |
| 3217 | if (!(s->flags & SN_FINST_MASK)) |
| 3218 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3219 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3220 | req->analysers = 0; |
| 3221 | req->analyse_exp = TICK_ETERNITY; |
| 3222 | return 0; |
| 3223 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3224 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3225 | /* This function performs all the processing enabled for the current request. |
| 3226 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3227 | * needs more data, encounters an error, or wants to immediately abort the |
| 3228 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3229 | */ |
| 3230 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3231 | { |
| 3232 | struct http_txn *txn = &s->txn; |
| 3233 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3234 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3235 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3236 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3237 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3238 | return 0; |
| 3239 | } |
| 3240 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3241 | 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] | 3242 | now_ms, __FUNCTION__, |
| 3243 | s, |
| 3244 | req, |
| 3245 | req->rex, req->wex, |
| 3246 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3247 | req->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3248 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3249 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3250 | /* |
| 3251 | * Right now, we know that we have processed the entire headers |
| 3252 | * and that unwanted requests have been filtered out. We can do |
| 3253 | * whatever we want with the remaining request. Also, now we |
| 3254 | * may have separate values for ->fe, ->be. |
| 3255 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3256 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3257 | /* |
| 3258 | * If HTTP PROXY is set we simply get remote server address |
| 3259 | * parsing incoming request. |
| 3260 | */ |
| 3261 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3262 | url2sa(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] | 3263 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3264 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3265 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3266 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3267 | * Note that doing so might move headers in the request, but |
| 3268 | * the fields will stay coherent and the URI will not move. |
| 3269 | * This should only be performed in the backend. |
| 3270 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3271 | 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] | 3272 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3273 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3274 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3275 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3276 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3277 | * so let's do the same now. |
| 3278 | */ |
| 3279 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3280 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3281 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3282 | get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3283 | } |
| 3284 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3285 | /* add unique-id if "header-unique-id" is specified */ |
| 3286 | |
| 3287 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3288 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3289 | |
| 3290 | if (s->fe->header_unique_id && s->unique_id) { |
| 3291 | int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3292 | if (ret < 0 || ret > global.tune.bufsize) |
| 3293 | goto return_bad_req; |
| 3294 | if(unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, trash) < 0)) |
| 3295 | goto return_bad_req; |
| 3296 | } |
| 3297 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3298 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3299 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3300 | * asks for it. |
| 3301 | */ |
| 3302 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3303 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3304 | |
| 3305 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Sagi Bashari | 1611e2d | 2011-10-08 22:48:48 +0200 | [diff] [blame] | 3306 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3307 | /* The header is set to be added only if none is present |
| 3308 | * and we found it, so don't do anything. |
| 3309 | */ |
| 3310 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3311 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3312 | /* Add an X-Forwarded-For header unless the source IP is |
| 3313 | * in the 'except' network range. |
| 3314 | */ |
| 3315 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3316 | (((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] | 3317 | != s->fe->except_net.s_addr) && |
| 3318 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3319 | (((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] | 3320 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3321 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3322 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3323 | 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] | 3324 | |
| 3325 | /* Note: we rely on the backend to get the header name to be used for |
| 3326 | * x-forwarded-for, because the header is really meant for the backends. |
| 3327 | * However, if the backend did not specify any option, we have to rely |
| 3328 | * on the frontend's header name. |
| 3329 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3330 | if (s->be->fwdfor_hdr_len) { |
| 3331 | len = s->be->fwdfor_hdr_len; |
| 3332 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3333 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3334 | len = s->fe->fwdfor_hdr_len; |
| 3335 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3336 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3337 | 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] | 3338 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3339 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3340 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3341 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3342 | } |
| 3343 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3344 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3345 | /* FIXME: for the sake of completeness, we should also support |
| 3346 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3347 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3348 | int len; |
| 3349 | char pn[INET6_ADDRSTRLEN]; |
| 3350 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3351 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3352 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3353 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3354 | /* Note: we rely on the backend to get the header name to be used for |
| 3355 | * x-forwarded-for, because the header is really meant for the backends. |
| 3356 | * However, if the backend did not specify any option, we have to rely |
| 3357 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3358 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3359 | if (s->be->fwdfor_hdr_len) { |
| 3360 | len = s->be->fwdfor_hdr_len; |
| 3361 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3362 | } else { |
| 3363 | len = s->fe->fwdfor_hdr_len; |
| 3364 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3365 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3366 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3367 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3368 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3369 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3370 | goto return_bad_req; |
| 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3375 | * 10: add X-Original-To if either the frontend or the backend |
| 3376 | * asks for it. |
| 3377 | */ |
| 3378 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3379 | |
| 3380 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3381 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3382 | /* Add an X-Original-To header unless the destination IP is |
| 3383 | * in the 'except' network range. |
| 3384 | */ |
Willy Tarreau | 9b061e3 | 2012-04-07 18:03:52 +0200 | [diff] [blame] | 3385 | stream_sock_get_to_addr(s->req->prod); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3386 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3387 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3388 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3389 | (((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] | 3390 | != s->fe->except_to.s_addr) && |
| 3391 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3392 | (((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] | 3393 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3394 | int len; |
| 3395 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3396 | 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] | 3397 | |
| 3398 | /* Note: we rely on the backend to get the header name to be used for |
| 3399 | * x-original-to, because the header is really meant for the backends. |
| 3400 | * However, if the backend did not specify any option, we have to rely |
| 3401 | * on the frontend's header name. |
| 3402 | */ |
| 3403 | if (s->be->orgto_hdr_len) { |
| 3404 | len = s->be->orgto_hdr_len; |
| 3405 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3406 | } else { |
| 3407 | len = s->fe->orgto_hdr_len; |
| 3408 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3409 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3410 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3411 | |
| 3412 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3413 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3414 | goto return_bad_req; |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3419 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3420 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3421 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3422 | unsigned int want_flags = 0; |
| 3423 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3424 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3425 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3426 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3427 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3428 | want_flags |= TX_CON_CLO_SET; |
| 3429 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3430 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3431 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3432 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3433 | want_flags |= TX_CON_KAL_SET; |
| 3434 | } |
| 3435 | |
| 3436 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3437 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3438 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3439 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3440 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3441 | /* If we have no server assigned yet and we're balancing on url_param |
| 3442 | * with a POST request, we may be interested in checking the body for |
| 3443 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3444 | */ |
| 3445 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3446 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3447 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3448 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3449 | buffer_dont_connect(req); |
| 3450 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3451 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3452 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3453 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3454 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3455 | #ifdef TCP_QUICKACK |
| 3456 | /* We expect some data from the client. Unless we know for sure |
| 3457 | * we already have a full request, we have to re-enable quick-ack |
| 3458 | * in case we previously disabled it, otherwise we might cause |
| 3459 | * the client to delay further data. |
| 3460 | */ |
| 3461 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3462 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3463 | (msg->body_len > req->i - txn->req.eoh - 2))) |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3464 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3465 | #endif |
| 3466 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3467 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3468 | /************************************************************* |
| 3469 | * OK, that's finished for the headers. We have done what we * |
| 3470 | * could. Let's switch to the DATA state. * |
| 3471 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3472 | req->analyse_exp = TICK_ETERNITY; |
| 3473 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3474 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3475 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3476 | /* OK let's go on with the BODY now */ |
| 3477 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3478 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3479 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3480 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3481 | /* we detected a parsing error. We want to archive this request |
| 3482 | * in the dedicated proxy area for later troubleshooting. |
| 3483 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3484 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3485 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3486 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3487 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3488 | txn->status = 400; |
| 3489 | req->analysers = 0; |
| 3490 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3491 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3492 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3493 | if (s->listener->counters) |
| 3494 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3495 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3496 | if (!(s->flags & SN_ERR_MASK)) |
| 3497 | s->flags |= SN_ERR_PRXCOND; |
| 3498 | if (!(s->flags & SN_FINST_MASK)) |
| 3499 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3500 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3501 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3502 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3503 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3504 | * returns zero, at the beginning because it prevents any other processing |
| 3505 | * from occurring, and at the end because it terminates the request. |
| 3506 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3507 | 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] | 3508 | { |
| 3509 | struct http_txn *txn = &s->txn; |
| 3510 | |
| 3511 | /* This connection is being tarpitted. The CLIENT side has |
| 3512 | * already set the connect expiration date to the right |
| 3513 | * timeout. We just have to check that the client is still |
| 3514 | * there and that the timeout has not expired. |
| 3515 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3516 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3517 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3518 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3519 | return 0; |
| 3520 | |
| 3521 | /* We will set the queue timer to the time spent, just for |
| 3522 | * logging purposes. We fake a 500 server error, so that the |
| 3523 | * attacker will not suspect his connection has been tarpitted. |
| 3524 | * It will not cause trouble to the logs because we can exclude |
| 3525 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3526 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3527 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3528 | |
| 3529 | txn->status = 500; |
| 3530 | if (req->flags != BF_READ_ERROR) |
| 3531 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3532 | |
| 3533 | req->analysers = 0; |
| 3534 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3535 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3536 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3537 | if (s->listener->counters) |
| 3538 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3539 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3540 | if (!(s->flags & SN_ERR_MASK)) |
| 3541 | s->flags |= SN_ERR_PRXCOND; |
| 3542 | if (!(s->flags & SN_FINST_MASK)) |
| 3543 | s->flags |= SN_FINST_T; |
| 3544 | return 0; |
| 3545 | } |
| 3546 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3547 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3548 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3549 | * must only be called after the standard HTTP request processing has occurred, |
| 3550 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3551 | * read more data, or 1 once it has completed its analysis. |
| 3552 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3553 | 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] | 3554 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3555 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3556 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3557 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3558 | |
| 3559 | /* We have to parse the HTTP request body to find any required data. |
| 3560 | * "balance url_param check_post" should have been the only way to get |
| 3561 | * into this. We were brought here after HTTP header analysis, so all |
| 3562 | * related structures are ready. |
| 3563 | */ |
| 3564 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3565 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3566 | goto missing_data; |
| 3567 | |
| 3568 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3569 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3570 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3571 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3572 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3573 | struct hdr_ctx ctx; |
| 3574 | ctx.idx = 0; |
| 3575 | /* Expect is allowed in 1.1, look for it */ |
| 3576 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3577 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3578 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3579 | } |
| 3580 | } |
| 3581 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3582 | } |
| 3583 | |
| 3584 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3585 | /* we have msg->col and msg->sov which both point to the first |
| 3586 | * byte of message body. msg->som still points to the beginning |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3587 | * of the message. We must save the body in msg->next because it |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3588 | * survives buffer re-alignments. |
| 3589 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 3590 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3591 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3592 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3593 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3594 | else |
| 3595 | msg->msg_state = HTTP_MSG_DATA; |
| 3596 | } |
| 3597 | |
| 3598 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3599 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3600 | * 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] | 3601 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3602 | */ |
| 3603 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3604 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3605 | if (!ret) |
| 3606 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3607 | else if (ret < 0) { |
| 3608 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3609 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3610 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3611 | } |
| 3612 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3613 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3614 | * We have the first non-header byte in msg->col, which is either the |
| 3615 | * beginning of the chunk size or of the data. The first data byte is in |
| 3616 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3617 | * We're waiting for at least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3618 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3619 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3620 | if (msg->body_len < limit) |
| 3621 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3622 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3623 | 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] | 3624 | goto http_end; |
| 3625 | |
| 3626 | missing_data: |
| 3627 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3628 | if (req->flags & BF_FULL) { |
| 3629 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3630 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3631 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3632 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3633 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3634 | txn->status = 408; |
| 3635 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3636 | |
| 3637 | if (!(s->flags & SN_ERR_MASK)) |
| 3638 | s->flags |= SN_ERR_CLITO; |
| 3639 | if (!(s->flags & SN_FINST_MASK)) |
| 3640 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3641 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3642 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3643 | |
| 3644 | /* we get here if we need to wait for more data */ |
| 3645 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3646 | /* Not enough data. We'll re-use the http-request |
| 3647 | * timeout here. Ideally, we should set the timeout |
| 3648 | * relative to the accept() date. We just set the |
| 3649 | * request timeout once at the beginning of the |
| 3650 | * request. |
| 3651 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3652 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3653 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3654 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3655 | return 0; |
| 3656 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3657 | |
| 3658 | http_end: |
| 3659 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3660 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3661 | req->analysers &= ~an_bit; |
| 3662 | req->analyse_exp = TICK_ETERNITY; |
| 3663 | return 1; |
| 3664 | |
| 3665 | return_bad_req: /* let's centralize all bad requests */ |
| 3666 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3667 | txn->status = 400; |
| 3668 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3669 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3670 | if (!(s->flags & SN_ERR_MASK)) |
| 3671 | s->flags |= SN_ERR_PRXCOND; |
| 3672 | if (!(s->flags & SN_FINST_MASK)) |
| 3673 | s->flags |= SN_FINST_R; |
| 3674 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3675 | return_err_msg: |
| 3676 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3677 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3678 | if (s->listener->counters) |
| 3679 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3680 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3683 | int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) { |
| 3684 | |
| 3685 | struct hdr_ctx ctx; |
| 3686 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3687 | char *hdr_name = be->server_id_hdr_name; |
| 3688 | int hdr_name_len = be->server_id_hdr_len; |
| 3689 | |
| 3690 | char *hdr_val; |
| 3691 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3692 | ctx.idx = 0; |
| 3693 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3694 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3695 | /* remove any existing values from the header */ |
| 3696 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 3697 | } |
| 3698 | |
| 3699 | /* Add the new header requested with the server value */ |
| 3700 | hdr_val = trash; |
| 3701 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3702 | hdr_val += hdr_name_len; |
| 3703 | *hdr_val++ = ':'; |
| 3704 | *hdr_val++ = ' '; |
| 3705 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
| 3706 | http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash); |
| 3707 | |
| 3708 | return 0; |
| 3709 | } |
| 3710 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3711 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3712 | * right now but it works. |
| 3713 | */ |
| 3714 | void http_end_txn_clean_session(struct session *s) |
| 3715 | { |
| 3716 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3717 | * server's connections. We need a safer way to reinitialize buffer |
| 3718 | * flags. We also need a more accurate method for computing per-request |
| 3719 | * data. |
| 3720 | */ |
| 3721 | http_silent_debug(__LINE__, s); |
| 3722 | |
| 3723 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3724 | s->req->cons->shutr(s->req->cons); |
| 3725 | s->req->cons->shutw(s->req->cons); |
| 3726 | |
| 3727 | http_silent_debug(__LINE__, s); |
| 3728 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3729 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3730 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3731 | if (unlikely(s->srv_conn)) |
| 3732 | sess_change_server(s, NULL); |
| 3733 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3734 | |
| 3735 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3736 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3737 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3738 | |
| 3739 | if (s->txn.status) { |
| 3740 | int n; |
| 3741 | |
| 3742 | n = s->txn.status / 100; |
| 3743 | if (n < 1 || n > 5) |
| 3744 | n = 0; |
| 3745 | |
| 3746 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3747 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3748 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3749 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3750 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3751 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | /* don't count other requests' data */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3755 | s->logs.bytes_in -= s->req->i; |
| 3756 | s->logs.bytes_out -= s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3757 | |
| 3758 | /* let's do a final log if we need it */ |
| 3759 | if (s->logs.logwait && |
| 3760 | !(s->flags & SN_MONITOR) && |
| 3761 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3762 | s->do_log(s); |
| 3763 | } |
| 3764 | |
| 3765 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3766 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3767 | tv_zero(&s->logs.tv_request); |
| 3768 | s->logs.t_queue = -1; |
| 3769 | s->logs.t_connect = -1; |
| 3770 | s->logs.t_data = -1; |
| 3771 | s->logs.t_close = 0; |
| 3772 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3773 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3774 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3775 | s->logs.bytes_in = s->req->total = s->req->i; |
| 3776 | s->logs.bytes_out = s->rep->total = s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3777 | |
| 3778 | if (s->pend_pos) |
| 3779 | pendconn_free(s->pend_pos); |
| 3780 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3781 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3782 | if (s->flags & SN_CURR_SESS) { |
| 3783 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3784 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3785 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3786 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3787 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3788 | } |
| 3789 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3790 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3791 | |
| 3792 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3793 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3794 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3795 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3796 | s->req->cons->err_loc = NULL; |
| 3797 | s->req->cons->exp = TICK_ETERNITY; |
| 3798 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3799 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 3800 | 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] | 3801 | 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] | 3802 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3803 | s->txn.meth = 0; |
| 3804 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3805 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3806 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3807 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3808 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3809 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 3810 | s->req->flags |= BF_NEVER_WAIT; |
| 3811 | s->rep->flags |= BF_NEVER_WAIT; |
| 3812 | } |
| 3813 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3814 | /* if the request buffer is not empty, it means we're |
| 3815 | * about to process another request, so send pending |
| 3816 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3817 | * Just don't do this if the buffer is close to be full, |
| 3818 | * because the request will wait for it to flush a little |
| 3819 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3820 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3821 | if (s->req->i) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 3822 | if (s->rep->o && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3823 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 3824 | buffer_wrap_add(s->rep, s->rep->p + s->rep->i) <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3825 | s->rep->flags |= BF_EXPECT_MORE; |
| 3826 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3827 | |
| 3828 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3829 | buffer_auto_read(s->req); |
| 3830 | buffer_auto_close(s->req); |
| 3831 | buffer_auto_read(s->rep); |
| 3832 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3834 | s->req->analysers = s->listener->analysers; |
| 3835 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3836 | s->rep->analysers = 0; |
| 3837 | |
| 3838 | http_silent_debug(__LINE__, s); |
| 3839 | } |
| 3840 | |
| 3841 | |
| 3842 | /* This function updates the request state machine according to the response |
| 3843 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3844 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3845 | * it is only used to find when a request/response couple is complete. Both |
| 3846 | * this function and its equivalent should loop until both return zero. It |
| 3847 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3848 | */ |
| 3849 | int http_sync_req_state(struct session *s) |
| 3850 | { |
| 3851 | struct buffer *buf = s->req; |
| 3852 | struct http_txn *txn = &s->txn; |
| 3853 | unsigned int old_flags = buf->flags; |
| 3854 | unsigned int old_state = txn->req.msg_state; |
| 3855 | |
| 3856 | http_silent_debug(__LINE__, s); |
| 3857 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3858 | return 0; |
| 3859 | |
| 3860 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3861 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3862 | * We can shut the read side unless we want to abort_on_close, |
| 3863 | * or we have a POST request. The issue with POST requests is |
| 3864 | * that some browsers still send a CRLF after the request, and |
| 3865 | * this CRLF must be read so that it does not remain in the kernel |
| 3866 | * buffers, otherwise a close could cause an RST on some systems |
| 3867 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3868 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3869 | 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] | 3870 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3871 | |
| 3872 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3873 | goto wait_other_side; |
| 3874 | |
| 3875 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3876 | /* The server has not finished to respond, so we |
| 3877 | * don't want to move in order not to upset it. |
| 3878 | */ |
| 3879 | goto wait_other_side; |
| 3880 | } |
| 3881 | |
| 3882 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3883 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3884 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3886 | goto wait_other_side; |
| 3887 | } |
| 3888 | |
| 3889 | /* When we get here, it means that both the request and the |
| 3890 | * response have finished receiving. Depending on the connection |
| 3891 | * mode, we'll have to wait for the last bytes to leave in either |
| 3892 | * direction, and sometimes for a close to be effective. |
| 3893 | */ |
| 3894 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3895 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3896 | /* Server-close mode : queue a connection close to the server */ |
| 3897 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3898 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3899 | } |
| 3900 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3901 | /* Option forceclose is set, or either side wants to close, |
| 3902 | * let's enforce it now that we're not expecting any new |
| 3903 | * data to come. The caller knows the session is complete |
| 3904 | * once both states are CLOSED. |
| 3905 | */ |
| 3906 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3907 | buffer_shutr_now(buf); |
| 3908 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3909 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3910 | } |
| 3911 | else { |
| 3912 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3913 | * mode does not set the body analyser, we can't reach this place |
| 3914 | * in tunnel mode, so we're left with keep-alive only. |
| 3915 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3916 | */ |
| 3917 | buffer_auto_read(buf); |
| 3918 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3919 | } |
| 3920 | |
| 3921 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3922 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3923 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3924 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3925 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3926 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3927 | goto http_msg_closing; |
| 3928 | } |
| 3929 | else { |
| 3930 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3931 | goto http_msg_closed; |
| 3932 | } |
| 3933 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3934 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3938 | http_msg_closing: |
| 3939 | /* nothing else to forward, just waiting for the output buffer |
| 3940 | * to be empty and for the shutw_now to take effect. |
| 3941 | */ |
| 3942 | if (buf->flags & BF_OUT_EMPTY) { |
| 3943 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3944 | goto http_msg_closed; |
| 3945 | } |
| 3946 | else if (buf->flags & BF_SHUTW) { |
| 3947 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3948 | goto wait_other_side; |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3953 | http_msg_closed: |
| 3954 | goto wait_other_side; |
| 3955 | } |
| 3956 | |
| 3957 | wait_other_side: |
| 3958 | http_silent_debug(__LINE__, s); |
| 3959 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3960 | } |
| 3961 | |
| 3962 | |
| 3963 | /* This function updates the response state machine according to the request |
| 3964 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3965 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3966 | * it is only used to find when a request/response couple is complete. Both |
| 3967 | * this function and its equivalent should loop until both return zero. It |
| 3968 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3969 | */ |
| 3970 | int http_sync_res_state(struct session *s) |
| 3971 | { |
| 3972 | struct buffer *buf = s->rep; |
| 3973 | struct http_txn *txn = &s->txn; |
| 3974 | unsigned int old_flags = buf->flags; |
| 3975 | unsigned int old_state = txn->rsp.msg_state; |
| 3976 | |
| 3977 | http_silent_debug(__LINE__, s); |
| 3978 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3979 | return 0; |
| 3980 | |
| 3981 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3982 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3983 | * still monitor the server connection for a possible close |
| 3984 | * while the request is being uploaded, so we don't disable |
| 3985 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3986 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3987 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3988 | |
| 3989 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3990 | goto wait_other_side; |
| 3991 | |
| 3992 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3993 | /* The client seems to still be sending data, probably |
| 3994 | * because we got an error response during an upload. |
| 3995 | * We have the choice of either breaking the connection |
| 3996 | * or letting it pass through. Let's do the later. |
| 3997 | */ |
| 3998 | goto wait_other_side; |
| 3999 | } |
| 4000 | |
| 4001 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4002 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4003 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4004 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4005 | goto wait_other_side; |
| 4006 | } |
| 4007 | |
| 4008 | /* When we get here, it means that both the request and the |
| 4009 | * response have finished receiving. Depending on the connection |
| 4010 | * mode, we'll have to wait for the last bytes to leave in either |
| 4011 | * direction, and sometimes for a close to be effective. |
| 4012 | */ |
| 4013 | |
| 4014 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4015 | /* Server-close mode : shut read and wait for the request |
| 4016 | * side to close its output buffer. The caller will detect |
| 4017 | * when we're in DONE and the other is in CLOSED and will |
| 4018 | * catch that for the final cleanup. |
| 4019 | */ |
| 4020 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4021 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4022 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4023 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4024 | /* Option forceclose is set, or either side wants to close, |
| 4025 | * let's enforce it now that we're not expecting any new |
| 4026 | * data to come. The caller knows the session is complete |
| 4027 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4028 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4029 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4030 | buffer_shutr_now(buf); |
| 4031 | buffer_shutw_now(buf); |
| 4032 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | } |
| 4034 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4035 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4036 | * mode does not set the body analyser, we can't reach this place |
| 4037 | * in tunnel mode, so we're left with keep-alive only. |
| 4038 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4039 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4040 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4041 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4045 | /* if we've just closed an output, let's switch */ |
| 4046 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4047 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4048 | goto http_msg_closing; |
| 4049 | } |
| 4050 | else { |
| 4051 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4052 | goto http_msg_closed; |
| 4053 | } |
| 4054 | } |
| 4055 | goto wait_other_side; |
| 4056 | } |
| 4057 | |
| 4058 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4059 | http_msg_closing: |
| 4060 | /* nothing else to forward, just waiting for the output buffer |
| 4061 | * to be empty and for the shutw_now to take effect. |
| 4062 | */ |
| 4063 | if (buf->flags & BF_OUT_EMPTY) { |
| 4064 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4065 | goto http_msg_closed; |
| 4066 | } |
| 4067 | else if (buf->flags & BF_SHUTW) { |
| 4068 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4069 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4070 | if (target_srv(&s->target)) |
| 4071 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4072 | goto wait_other_side; |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4077 | http_msg_closed: |
| 4078 | /* drop any pending data */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4079 | buffer_ignore(buf, buf->i); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4080 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4081 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4082 | goto wait_other_side; |
| 4083 | } |
| 4084 | |
| 4085 | wait_other_side: |
| 4086 | http_silent_debug(__LINE__, s); |
| 4087 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4088 | } |
| 4089 | |
| 4090 | |
| 4091 | /* Resync the request and response state machines. Return 1 if either state |
| 4092 | * changes. |
| 4093 | */ |
| 4094 | int http_resync_states(struct session *s) |
| 4095 | { |
| 4096 | struct http_txn *txn = &s->txn; |
| 4097 | int old_req_state = txn->req.msg_state; |
| 4098 | int old_res_state = txn->rsp.msg_state; |
| 4099 | |
| 4100 | http_silent_debug(__LINE__, s); |
| 4101 | http_sync_req_state(s); |
| 4102 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4103 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4104 | if (!http_sync_res_state(s)) |
| 4105 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4106 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4107 | if (!http_sync_req_state(s)) |
| 4108 | break; |
| 4109 | } |
| 4110 | http_silent_debug(__LINE__, s); |
| 4111 | /* OK, both state machines agree on a compatible state. |
| 4112 | * There are a few cases we're interested in : |
| 4113 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4114 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4115 | * directions, so let's simply disable both analysers. |
| 4116 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4117 | * request. |
| 4118 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4119 | * with server-close mode means we've completed one request and we |
| 4120 | * must re-initialize the server connection. |
| 4121 | */ |
| 4122 | |
| 4123 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4124 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4125 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4126 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4127 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4128 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4129 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4130 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4131 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4132 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4133 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4134 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4135 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4136 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4137 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4138 | s->rep->analysers = 0; |
| 4139 | buffer_auto_close(s->rep); |
| 4140 | buffer_auto_read(s->rep); |
| 4141 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4142 | buffer_abort(s->req); |
| 4143 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4144 | buffer_auto_read(s->req); |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4145 | buffer_ignore(s->req, s->req->i); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4146 | } |
| 4147 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4148 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4149 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4150 | /* server-close: terminate this server connection and |
| 4151 | * reinitialize a fresh-new transaction. |
| 4152 | */ |
| 4153 | http_end_txn_clean_session(s); |
| 4154 | } |
| 4155 | |
| 4156 | http_silent_debug(__LINE__, s); |
| 4157 | return txn->req.msg_state != old_req_state || |
| 4158 | txn->rsp.msg_state != old_res_state; |
| 4159 | } |
| 4160 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4161 | /* This function is an analyser which forwards request body (including chunk |
| 4162 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4163 | * zero byte. The only situation where it must not be called is when we're in |
| 4164 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4165 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4166 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4167 | * 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] | 4168 | * 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] | 4169 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4170 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4171 | */ |
| 4172 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4173 | { |
| 4174 | struct http_txn *txn = &s->txn; |
| 4175 | struct http_msg *msg = &s->txn.req; |
| 4176 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4177 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4178 | return 0; |
| 4179 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4180 | 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] | 4181 | ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4182 | /* Output closed while we were sending data. We must abort and |
| 4183 | * wake the other side up. |
| 4184 | */ |
| 4185 | msg->msg_state = HTTP_MSG_ERROR; |
| 4186 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4187 | return 1; |
| 4188 | } |
| 4189 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4190 | /* in most states, we should abort in case of early close */ |
| 4191 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4192 | |
| 4193 | /* Note that we don't have to send 100-continue back because we don't |
| 4194 | * need the data to complete our job, and it's up to the server to |
| 4195 | * decide whether to return 100, 417 or anything else in return of |
| 4196 | * an "Expect: 100-continue" header. |
| 4197 | */ |
| 4198 | |
| 4199 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4200 | /* we have msg->col and msg->sov which both point to the first |
| 4201 | * byte of message body. msg->som still points to the beginning |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4202 | * of the message. We must save the body in msg->next because it |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4203 | * survives buffer re-alignments. |
| 4204 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 4205 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4206 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4207 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4208 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4209 | else { |
| 4210 | msg->msg_state = HTTP_MSG_DATA; |
| 4211 | } |
| 4212 | } |
| 4213 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4214 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4215 | int bytes; |
| 4216 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4217 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4218 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4219 | bytes = msg->sov - msg->som; |
| 4220 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4221 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4222 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4223 | bytes += req->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4224 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4225 | msg->chunk_len += (unsigned int)bytes; |
| 4226 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4227 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4228 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4229 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4230 | /* must still forward */ |
| 4231 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4232 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4233 | |
| 4234 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4235 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4236 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4237 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4238 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4239 | } |
| 4240 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4241 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4242 | * 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] | 4243 | * TRAILERS state. |
| 4244 | */ |
| 4245 | int ret = http_parse_chunk_size(req, msg); |
| 4246 | |
| 4247 | if (!ret) |
| 4248 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4249 | else if (ret < 0) { |
| 4250 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4251 | if (msg->err_pos >= 0) |
| 4252 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4253 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4254 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4255 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4256 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4257 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4258 | /* we want the CRLF after the data */ |
| 4259 | int ret; |
| 4260 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4261 | ret = http_skip_chunk_crlf(req, msg); |
| 4262 | |
| 4263 | if (ret == 0) |
| 4264 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4265 | else if (ret < 0) { |
| 4266 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4267 | if (msg->err_pos >= 0) |
| 4268 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4269 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4270 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4271 | /* we're in MSG_CHUNK_SIZE now */ |
| 4272 | } |
| 4273 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4274 | int ret = http_forward_trailers(req, msg); |
| 4275 | |
| 4276 | if (ret == 0) |
| 4277 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4278 | else if (ret < 0) { |
| 4279 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4280 | if (msg->err_pos >= 0) |
| 4281 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4282 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4283 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4284 | /* we're in HTTP_MSG_DONE now */ |
| 4285 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4286 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4287 | int old_state = msg->msg_state; |
| 4288 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4289 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4290 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4291 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4292 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4293 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4294 | if (http_resync_states(s)) { |
| 4295 | /* some state changes occurred, maybe the analyser |
| 4296 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4297 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4298 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4299 | if (req->flags & BF_SHUTW) { |
| 4300 | /* request errors are most likely due to |
| 4301 | * the server aborting the transfer. |
| 4302 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4303 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4304 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4305 | if (msg->err_pos >= 0) |
| 4306 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4308 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4309 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4310 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4311 | |
| 4312 | /* If "option abortonclose" is set on the backend, we |
| 4313 | * want to monitor the client's connection and forward |
| 4314 | * any shutdown notification to the server, which will |
| 4315 | * decide whether to close or to go on processing the |
| 4316 | * request. |
| 4317 | */ |
| 4318 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4319 | buffer_auto_read(req); |
| 4320 | buffer_auto_close(req); |
| 4321 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4322 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4323 | /* POST requests may require to read extra CRLF |
| 4324 | * sent by broken browsers and which could cause |
| 4325 | * an RST to be sent upon close on some systems |
| 4326 | * (eg: Linux). |
| 4327 | */ |
| 4328 | buffer_auto_read(req); |
| 4329 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4330 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4331 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4332 | } |
| 4333 | } |
| 4334 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4335 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4336 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4337 | if (req->flags & BF_SHUTR) { |
| 4338 | if (!(s->flags & SN_ERR_MASK)) |
| 4339 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4340 | if (!(s->flags & SN_FINST_MASK)) { |
| 4341 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4342 | s->flags |= SN_FINST_H; |
| 4343 | else |
| 4344 | s->flags |= SN_FINST_D; |
| 4345 | } |
| 4346 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4347 | s->fe->fe_counters.cli_aborts++; |
| 4348 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4349 | if (target_srv(&s->target)) |
| 4350 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4351 | |
| 4352 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4353 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4354 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4355 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4356 | if (req->flags & BF_SHUTW) |
| 4357 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4358 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4359 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4360 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4361 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4362 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4363 | buffer_dont_close(req); |
| 4364 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4365 | /* 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] | 4366 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4367 | * 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] | 4368 | * modes are already handled by the stream sock layer. We must not do |
| 4369 | * this in content-length mode because it could present the MSG_MORE |
| 4370 | * flag with the last block of forwarded data, which would cause an |
| 4371 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4372 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4373 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4374 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4375 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4376 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4377 | return 0; |
| 4378 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4379 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4380 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4381 | if (s->listener->counters) |
| 4382 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4383 | return_bad_req_stats_ok: |
| 4384 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4385 | if (txn->status) { |
| 4386 | /* Note: we don't send any error if some data were already sent */ |
| 4387 | stream_int_retnclose(req->prod, NULL); |
| 4388 | } else { |
| 4389 | txn->status = 400; |
| 4390 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4391 | } |
| 4392 | req->analysers = 0; |
| 4393 | 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] | 4394 | |
| 4395 | if (!(s->flags & SN_ERR_MASK)) |
| 4396 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4397 | if (!(s->flags & SN_FINST_MASK)) { |
| 4398 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4399 | s->flags |= SN_FINST_H; |
| 4400 | else |
| 4401 | s->flags |= SN_FINST_D; |
| 4402 | } |
| 4403 | return 0; |
| 4404 | |
| 4405 | aborted_xfer: |
| 4406 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4407 | if (txn->status) { |
| 4408 | /* Note: we don't send any error if some data were already sent */ |
| 4409 | stream_int_retnclose(req->prod, NULL); |
| 4410 | } else { |
| 4411 | txn->status = 502; |
| 4412 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4413 | } |
| 4414 | req->analysers = 0; |
| 4415 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4416 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4417 | s->fe->fe_counters.srv_aborts++; |
| 4418 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4419 | if (target_srv(&s->target)) |
| 4420 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4421 | |
| 4422 | if (!(s->flags & SN_ERR_MASK)) |
| 4423 | s->flags |= SN_ERR_SRVCL; |
| 4424 | if (!(s->flags & SN_FINST_MASK)) { |
| 4425 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4426 | s->flags |= SN_FINST_H; |
| 4427 | else |
| 4428 | s->flags |= SN_FINST_D; |
| 4429 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4430 | return 0; |
| 4431 | } |
| 4432 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4433 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4434 | * processing can continue on next analysers, or zero if it either needs more |
| 4435 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4436 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4437 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4438 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4439 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4440 | 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] | 4441 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4442 | struct http_txn *txn = &s->txn; |
| 4443 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4444 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4445 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4446 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4447 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4448 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4449 | 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] | 4450 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4451 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4452 | rep, |
| 4453 | rep->rex, rep->wex, |
| 4454 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4455 | rep->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4456 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4457 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4458 | /* |
| 4459 | * Now parse the partial (or complete) lines. |
| 4460 | * We will check the response syntax, and also join multi-line |
| 4461 | * headers. An index of all the lines will be elaborated while |
| 4462 | * parsing. |
| 4463 | * |
| 4464 | * For the parsing, we use a 28 states FSM. |
| 4465 | * |
| 4466 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4467 | * rep->data + msg->som = beginning of response |
| 4468 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4469 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4470 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4471 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4472 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4473 | */ |
| 4474 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4475 | /* There's a protected area at the end of the buffer for rewriting |
| 4476 | * purposes. We don't want to start to parse the request if the |
| 4477 | * protected area is affected, because we may have to move processed |
| 4478 | * data later, which is much more complicated. |
| 4479 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4480 | if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4481 | if (unlikely((rep->flags & BF_FULL) || |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4482 | buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) || |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 4483 | buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4484 | if (rep->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4485 | /* 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] | 4486 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4487 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4488 | buffer_dont_close(rep); |
| 4489 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4490 | return 0; |
| 4491 | } |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4492 | if (rep->i <= rep->size - global.tune.maxrewrite) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4493 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4494 | } |
| 4495 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4496 | if (likely(msg->next < rep->i)) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4497 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4498 | } |
| 4499 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4500 | /* 1: we might have to print this header in debug mode */ |
| 4501 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4502 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4503 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4504 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4505 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4506 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 4507 | sol = rep->p + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4508 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4509 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4510 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4511 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4512 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4513 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4514 | while (cur_idx) { |
| 4515 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4516 | debug_hdr("srvhdr", s, sol, eol); |
| 4517 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4518 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4519 | } |
| 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 | /* |
| 4523 | * Now we quickly check if we have found a full valid response. |
| 4524 | * If not so, we check the FD and buffer states before leaving. |
| 4525 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4526 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4527 | * responses are checked first. |
| 4528 | * |
| 4529 | * Depending on whether the client is still there or not, we |
| 4530 | * may send an error response back or not. Note that normally |
| 4531 | * we should only check for HTTP status there, and check I/O |
| 4532 | * errors somewhere else. |
| 4533 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4534 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4535 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4536 | /* Invalid response */ |
| 4537 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4538 | /* we detected a parsing error. We want to archive this response |
| 4539 | * in the dedicated proxy area for later troubleshooting. |
| 4540 | */ |
| 4541 | hdr_response_bad: |
| 4542 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4543 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4544 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4545 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4546 | if (target_srv(&s->target)) { |
| 4547 | target_srv(&s->target)->counters.failed_resp++; |
| 4548 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4549 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4550 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4551 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4552 | rep->analysers = 0; |
| 4553 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4554 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4555 | buffer_ignore(rep, rep->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4556 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4557 | |
| 4558 | if (!(s->flags & SN_ERR_MASK)) |
| 4559 | s->flags |= SN_ERR_PRXCOND; |
| 4560 | if (!(s->flags & SN_FINST_MASK)) |
| 4561 | s->flags |= SN_FINST_H; |
| 4562 | |
| 4563 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4564 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4565 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4566 | /* too large response does not fit in buffer. */ |
| 4567 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4568 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4569 | msg->err_pos = rep->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4570 | goto hdr_response_bad; |
| 4571 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4572 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4573 | /* read error */ |
| 4574 | else if (rep->flags & BF_READ_ERROR) { |
| 4575 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4576 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4577 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4578 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4579 | if (target_srv(&s->target)) { |
| 4580 | target_srv(&s->target)->counters.failed_resp++; |
| 4581 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4582 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4583 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4584 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4585 | rep->analysers = 0; |
| 4586 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4587 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4588 | buffer_ignore(rep, rep->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4589 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4590 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4591 | if (!(s->flags & SN_ERR_MASK)) |
| 4592 | s->flags |= SN_ERR_SRVCL; |
| 4593 | if (!(s->flags & SN_FINST_MASK)) |
| 4594 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4595 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4596 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4597 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4598 | /* read timeout : return a 504 to the client. */ |
| 4599 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4600 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4601 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4602 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4603 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4604 | if (target_srv(&s->target)) { |
| 4605 | target_srv(&s->target)->counters.failed_resp++; |
| 4606 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4607 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4608 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4609 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4610 | rep->analysers = 0; |
| 4611 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4612 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4613 | buffer_ignore(rep, rep->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4614 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4615 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4616 | if (!(s->flags & SN_ERR_MASK)) |
| 4617 | s->flags |= SN_ERR_SRVTO; |
| 4618 | if (!(s->flags & SN_FINST_MASK)) |
| 4619 | s->flags |= SN_FINST_H; |
| 4620 | return 0; |
| 4621 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4622 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4623 | /* 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] | 4624 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4625 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4626 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4627 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4628 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4629 | if (target_srv(&s->target)) { |
| 4630 | target_srv(&s->target)->counters.failed_resp++; |
| 4631 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4632 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4633 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4634 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4635 | rep->analysers = 0; |
| 4636 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4637 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4638 | buffer_ignore(rep, rep->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4639 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4640 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4641 | if (!(s->flags & SN_ERR_MASK)) |
| 4642 | s->flags |= SN_ERR_SRVCL; |
| 4643 | if (!(s->flags & SN_FINST_MASK)) |
| 4644 | s->flags |= SN_FINST_H; |
| 4645 | return 0; |
| 4646 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4647 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4648 | /* write error to client (we don't send any message then) */ |
| 4649 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4650 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4651 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4652 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4653 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4654 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4655 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4656 | |
| 4657 | if (!(s->flags & SN_ERR_MASK)) |
| 4658 | s->flags |= SN_ERR_CLICL; |
| 4659 | if (!(s->flags & SN_FINST_MASK)) |
| 4660 | s->flags |= SN_FINST_H; |
| 4661 | |
| 4662 | /* process_session() will take care of the error */ |
| 4663 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4664 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4665 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4666 | buffer_dont_close(rep); |
| 4667 | return 0; |
| 4668 | } |
| 4669 | |
| 4670 | /* More interesting part now : we know that we have a complete |
| 4671 | * response which at least looks like HTTP. We have an indicator |
| 4672 | * of each header's length, so we can parse them quickly. |
| 4673 | */ |
| 4674 | |
| 4675 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4676 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4677 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4678 | /* |
| 4679 | * 1: get the status code |
| 4680 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4681 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4682 | if (n < 1 || n > 5) |
| 4683 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4684 | /* when the client triggers a 4xx from the server, it's most often due |
| 4685 | * to a missing object or permission. These events should be tracked |
| 4686 | * because if they happen often, it may indicate a brute force or a |
| 4687 | * vulnerability scan. |
| 4688 | */ |
| 4689 | if (n == 4) |
| 4690 | session_inc_http_err_ctr(s); |
| 4691 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4692 | if (target_srv(&s->target)) |
| 4693 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4694 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4695 | /* check if the response is HTTP/1.1 or above */ |
| 4696 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4697 | ((msg->sol[5] > '1') || |
| 4698 | ((msg->sol[5] == '1') && |
| 4699 | (msg->sol[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4700 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4701 | |
| 4702 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4703 | 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] | 4704 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4705 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4706 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4707 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4708 | txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4709 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4710 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4711 | * and 505 are triggered on demand by client request, so we must not |
| 4712 | * count them as server failures. |
| 4713 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4714 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4715 | 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] | 4716 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4717 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4718 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4719 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4721 | /* |
| 4722 | * 2: check for cacheability. |
| 4723 | */ |
| 4724 | |
| 4725 | switch (txn->status) { |
| 4726 | case 200: |
| 4727 | case 203: |
| 4728 | case 206: |
| 4729 | case 300: |
| 4730 | case 301: |
| 4731 | case 410: |
| 4732 | /* RFC2616 @13.4: |
| 4733 | * "A response received with a status code of |
| 4734 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4735 | * by a cache (...) unless a cache-control |
| 4736 | * directive prohibits caching." |
| 4737 | * |
| 4738 | * RFC2616 @9.5: POST method : |
| 4739 | * "Responses to this method are not cacheable, |
| 4740 | * unless the response includes appropriate |
| 4741 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4742 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4743 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4744 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4745 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4746 | break; |
| 4747 | default: |
| 4748 | break; |
| 4749 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4750 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4751 | /* |
| 4752 | * 3: we may need to capture headers |
| 4753 | */ |
| 4754 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4755 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4756 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4757 | txn->rsp.cap, s->fe->rsp_cap); |
| 4758 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4759 | /* 4: determine the transfer-length. |
| 4760 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4761 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4762 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4763 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4764 | * All responses to the HEAD request method MUST NOT include a |
| 4765 | * message-body, even though the presence of entity-header fields |
| 4766 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4767 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4768 | * message-body. All other responses do include a message-body, |
| 4769 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4770 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4771 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4772 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4773 | * always terminated by the first empty line after the header fields, |
| 4774 | * regardless of the entity-header fields present in the message. |
| 4775 | * |
| 4776 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4777 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4778 | * transfer-length is defined by the use of this transfer-coding. |
| 4779 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4780 | * transfer-coding is not present, the transfer-length is defined by |
| 4781 | * the sender closing the connection. |
| 4782 | * |
| 4783 | * 3. If a Content-Length header field is present, its decimal value in |
| 4784 | * OCTETs represents both the entity-length and the transfer-length. |
| 4785 | * If a message is received with both a Transfer-Encoding header |
| 4786 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4787 | * |
| 4788 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4789 | * the transfer-length is not otherwise specified, then this self- |
| 4790 | * delimiting media type defines the transfer-length. This media |
| 4791 | * type MUST NOT be used unless the sender knows that the recipient |
| 4792 | * can parse it; the presence in a request of a Range header with |
| 4793 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4794 | * client can parse multipart/byteranges responses. |
| 4795 | * |
| 4796 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4797 | */ |
| 4798 | |
| 4799 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4800 | * 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] | 4801 | * 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] | 4802 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4803 | */ |
| 4804 | if (txn->meth == HTTP_METH_HEAD || |
| 4805 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4806 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4807 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4808 | goto skip_content_length; |
| 4809 | } |
| 4810 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4811 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4812 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4813 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4814 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4815 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4816 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4817 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4818 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4819 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4820 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4821 | break; |
| 4822 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4826 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4827 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4828 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4829 | signed long long cl; |
| 4830 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4831 | if (!ctx.vlen) { |
| 4832 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4833 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4834 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4835 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4836 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 4837 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4838 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4839 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4840 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4841 | if (cl < 0) { |
| 4842 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4843 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4844 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4845 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4846 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4847 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4848 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4849 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4850 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4851 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4852 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4853 | } |
| 4854 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4855 | /* FIXME: we should also implement the multipart/byterange method. |
| 4856 | * For now on, we resort to close mode in this case (unknown length). |
| 4857 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4858 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4859 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4860 | /* end of job, return OK */ |
| 4861 | rep->analysers &= ~an_bit; |
| 4862 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4863 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4864 | return 1; |
| 4865 | } |
| 4866 | |
| 4867 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4868 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4869 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4870 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4871 | */ |
| 4872 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4873 | { |
| 4874 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4875 | struct http_msg *msg = &txn->rsp; |
| 4876 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4877 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4878 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4879 | 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] | 4880 | now_ms, __FUNCTION__, |
| 4881 | t, |
| 4882 | rep, |
| 4883 | rep->rex, rep->wex, |
| 4884 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4885 | rep->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4886 | rep->analysers); |
| 4887 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4888 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4889 | return 0; |
| 4890 | |
| 4891 | rep->analysers &= ~an_bit; |
| 4892 | rep->analyse_exp = TICK_ETERNITY; |
| 4893 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4894 | /* Now we have to check if we need to modify the Connection header. |
| 4895 | * This is more difficult on the response than it is on the request, |
| 4896 | * because we can have two different HTTP versions and we don't know |
| 4897 | * how the client will interprete a response. For instance, let's say |
| 4898 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4899 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4900 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4901 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4902 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4903 | * indicating how a request MAY be understood by the client. In case |
| 4904 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4905 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4906 | * will fall back to explicit close. Note that we won't take risks with |
| 4907 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4908 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4909 | */ |
| 4910 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4911 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4912 | txn->status == 101)) { |
| 4913 | /* Either we've established an explicit tunnel, or we're |
| 4914 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4915 | * to understand the next protocols. We have to switch to tunnel |
| 4916 | * mode, so that we transfer the request and responses then let |
| 4917 | * this protocol pass unmodified. When we later implement specific |
| 4918 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4919 | * header which contains information about that protocol for |
| 4920 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4921 | */ |
| 4922 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4923 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4924 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4925 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4926 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4927 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4928 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4929 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4930 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4931 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4932 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4933 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4934 | /* now adjust header transformations depending on current state */ |
| 4935 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4936 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4937 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4938 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4939 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4940 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4941 | else { /* SCL / KAL */ |
| 4942 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4943 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4944 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4945 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4946 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4947 | /* Parse and remove some headers from the connection header */ |
| 4948 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4949 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4950 | /* Some keep-alive responses are converted to Server-close if |
| 4951 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4952 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4953 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4954 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4955 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4956 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4957 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4958 | } |
| 4959 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4960 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4961 | /* |
| 4962 | * 3: we will have to evaluate the filters. |
| 4963 | * As opposed to version 1.2, now they will be evaluated in the |
| 4964 | * filters order and not in the header order. This means that |
| 4965 | * each filter has to be validated among all headers. |
| 4966 | * |
| 4967 | * Filters are tried with ->be first, then with ->fe if it is |
| 4968 | * different from ->be. |
| 4969 | */ |
| 4970 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4971 | cur_proxy = t->be; |
| 4972 | while (1) { |
| 4973 | struct proxy *rule_set = cur_proxy; |
| 4974 | |
| 4975 | /* try headers filters */ |
| 4976 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4977 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4978 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4979 | if (target_srv(&t->target)) { |
| 4980 | target_srv(&t->target)->counters.failed_resp++; |
| 4981 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4982 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4983 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4984 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4985 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4986 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4987 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4988 | buffer_ignore(rep, rep->i); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4989 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4990 | if (!(t->flags & SN_ERR_MASK)) |
| 4991 | t->flags |= SN_ERR_PRXCOND; |
| 4992 | if (!(t->flags & SN_FINST_MASK)) |
| 4993 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4994 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4995 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4996 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4997 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4998 | /* has the response been denied ? */ |
| 4999 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5000 | if (target_srv(&t->target)) |
| 5001 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5002 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5003 | t->be->be_counters.denied_resp++; |
| 5004 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5005 | if (t->listener->counters) |
| 5006 | t->listener->counters->denied_resp++; |
| 5007 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5008 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5009 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5010 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5011 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5012 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5013 | if (txn->status < 200) |
| 5014 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5015 | if (wl->cond) { |
| 5016 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5017 | ret = acl_pass(ret); |
| 5018 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5019 | ret = !ret; |
| 5020 | if (!ret) |
| 5021 | continue; |
| 5022 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5023 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5024 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5025 | } |
| 5026 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5027 | /* check whether we're already working on the frontend */ |
| 5028 | if (cur_proxy == t->fe) |
| 5029 | break; |
| 5030 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5031 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5032 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5033 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5034 | * We may be facing a 100-continue response, in which case this |
| 5035 | * is not the right response, and we're waiting for the next one. |
| 5036 | * 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] | 5037 | * next one. |
| 5038 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5039 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5040 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5041 | msg->next -= buffer_forward(rep, rep->p + msg->next - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5042 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5043 | txn->status = 0; |
| 5044 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5045 | return 1; |
| 5046 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5047 | else if (unlikely(txn->status < 200)) |
| 5048 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5049 | |
| 5050 | /* we don't have any 1xx status code now */ |
| 5051 | |
| 5052 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5053 | * 4: check for server cookie. |
| 5054 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5055 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5056 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5057 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5058 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5059 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5060 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5061 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5062 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5063 | 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] | 5064 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5065 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5066 | /* |
| 5067 | * 6: add server cookie in the response if needed |
| 5068 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5069 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5070 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5071 | (!(t->flags & SN_DIRECT) || |
| 5072 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5073 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5074 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5075 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5076 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5077 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5078 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5079 | /* the server is known, it's not the one the client requested, or the |
| 5080 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5081 | * insert a set-cookie here, except if we want to insert only on POST |
| 5082 | * requests and this one isn't. Note that servers which don't have cookies |
| 5083 | * (eg: some backup servers) will return a full cookie removal request. |
| 5084 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5085 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5086 | len = sprintf(trash, |
| 5087 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5088 | t->be->cookie_name); |
| 5089 | } |
| 5090 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5091 | 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] | 5092 | |
| 5093 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5094 | /* emit last_date, which is mandatory */ |
| 5095 | trash[len++] = COOKIE_DELIM_DATE; |
| 5096 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5097 | if (t->be->cookie_maxlife) { |
| 5098 | /* emit first_date, which is either the original one or |
| 5099 | * the current date. |
| 5100 | */ |
| 5101 | trash[len++] = COOKIE_DELIM_DATE; |
| 5102 | s30tob64(txn->cookie_first_date ? |
| 5103 | txn->cookie_first_date >> 2 : |
| 5104 | (date.tv_sec+3) >> 2, trash + len); |
| 5105 | len += 5; |
| 5106 | } |
| 5107 | } |
| 5108 | len += sprintf(trash + len, "; path=/"); |
| 5109 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5110 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5111 | if (t->be->cookie_domain) |
| 5112 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5113 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5114 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5115 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5116 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5117 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5118 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5119 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5120 | /* the server did not change, only the date was updated */ |
| 5121 | txn->flags |= TX_SCK_UPDATED; |
| 5122 | else |
| 5123 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5124 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5125 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5126 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5127 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5128 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5129 | */ |
| 5130 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5131 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5132 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5133 | |
| 5134 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5135 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5136 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5137 | } |
| 5138 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5139 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5140 | /* |
| 5141 | * 7: check if result will be cacheable with a cookie. |
| 5142 | * We'll block the response if security checks have caught |
| 5143 | * nasty things such as a cacheable cookie. |
| 5144 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5145 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5146 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5147 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5148 | |
| 5149 | /* we're in presence of a cacheable response containing |
| 5150 | * a set-cookie header. We'll block it as requested by |
| 5151 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5152 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5153 | if (target_srv(&t->target)) |
| 5154 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5155 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5156 | t->be->be_counters.denied_resp++; |
| 5157 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5158 | if (t->listener->counters) |
| 5159 | t->listener->counters->denied_resp++; |
| 5160 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5161 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5162 | 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] | 5163 | send_log(t->be, LOG_ALERT, |
| 5164 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5165 | 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] | 5166 | goto return_srv_prx_502; |
| 5167 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5168 | |
| 5169 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5170 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5171 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5172 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5173 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5174 | unsigned int want_flags = 0; |
| 5175 | |
| 5176 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5177 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5178 | /* we want a keep-alive response here. Keep-alive header |
| 5179 | * required if either side is not 1.1. |
| 5180 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5181 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5182 | want_flags |= TX_CON_KAL_SET; |
| 5183 | } |
| 5184 | else { |
| 5185 | /* we want a close response here. Close header required if |
| 5186 | * the server is 1.1, regardless of the client. |
| 5187 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5188 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5189 | want_flags |= TX_CON_CLO_SET; |
| 5190 | } |
| 5191 | |
| 5192 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5193 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5194 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5195 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5196 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5197 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5198 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5199 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5200 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5201 | /************************************************************* |
| 5202 | * OK, that's finished for the headers. We have done what we * |
| 5203 | * could. Let's switch to the DATA state. * |
| 5204 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5205 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5206 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5207 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5208 | /* if the user wants to log as soon as possible, without counting |
| 5209 | * bytes from the server, then this is the right moment. We have |
| 5210 | * to temporarily assign bytes_out to log what we currently have. |
| 5211 | */ |
| 5212 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5213 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5214 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5215 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5216 | t->logs.bytes_out = 0; |
| 5217 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5218 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5219 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5220 | * otherwise we would not let the client side wake up. |
| 5221 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5222 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5223 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5224 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5225 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5226 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5227 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5228 | /* This function is an analyser which forwards response body (including chunk |
| 5229 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5230 | * zero byte. The only situation where it must not be called is when we're in |
| 5231 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5232 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5233 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5234 | * 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] | 5235 | * 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] | 5236 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5237 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5238 | */ |
| 5239 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5240 | { |
| 5241 | struct http_txn *txn = &s->txn; |
| 5242 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5243 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5244 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5245 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5246 | return 0; |
| 5247 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5248 | 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] | 5249 | ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5250 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5251 | /* Output closed while we were sending data. We must abort and |
| 5252 | * wake the other side up. |
| 5253 | */ |
| 5254 | msg->msg_state = HTTP_MSG_ERROR; |
| 5255 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5256 | return 1; |
| 5257 | } |
| 5258 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5259 | /* in most states, we should abort in case of early close */ |
| 5260 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5261 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5262 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5263 | /* we have msg->col and msg->sov which both point to the first |
| 5264 | * byte of message body. msg->som still points to the beginning |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5265 | * of the message. We must save the body in msg->next because it |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5266 | * survives buffer re-alignments. |
| 5267 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 5268 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5269 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5270 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5271 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5272 | else { |
| 5273 | msg->msg_state = HTTP_MSG_DATA; |
| 5274 | } |
| 5275 | } |
| 5276 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5277 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5278 | int bytes; |
| 5279 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5280 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5281 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5282 | bytes = msg->sov - msg->som; |
| 5283 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5284 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5285 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5286 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5287 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5288 | msg->chunk_len += (unsigned int)bytes; |
| 5289 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5290 | } |
| 5291 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5292 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5293 | /* must still forward */ |
| 5294 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5295 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5296 | |
| 5297 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5298 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5299 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5300 | else |
| 5301 | msg->msg_state = HTTP_MSG_DONE; |
| 5302 | } |
| 5303 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5304 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5305 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5306 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5307 | */ |
| 5308 | int ret = http_parse_chunk_size(res, msg); |
| 5309 | |
| 5310 | if (!ret) |
| 5311 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5312 | else if (ret < 0) { |
| 5313 | if (msg->err_pos >= 0) |
| 5314 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5315 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5316 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5317 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5318 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5319 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5320 | /* we want the CRLF after the data */ |
| 5321 | int ret; |
| 5322 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5323 | ret = http_skip_chunk_crlf(res, msg); |
| 5324 | |
| 5325 | if (!ret) |
| 5326 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5327 | else if (ret < 0) { |
| 5328 | if (msg->err_pos >= 0) |
| 5329 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5330 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5331 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5332 | /* we're in MSG_CHUNK_SIZE now */ |
| 5333 | } |
| 5334 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5335 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5336 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5337 | if (ret == 0) |
| 5338 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5339 | else if (ret < 0) { |
| 5340 | if (msg->err_pos >= 0) |
| 5341 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5342 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5343 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5344 | /* we're in HTTP_MSG_DONE now */ |
| 5345 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5346 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5347 | int old_state = msg->msg_state; |
| 5348 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5349 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5350 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5351 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5352 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5353 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5354 | if (http_resync_states(s)) { |
| 5355 | http_silent_debug(__LINE__, s); |
| 5356 | /* some state changes occurred, maybe the analyser |
| 5357 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5358 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5359 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5360 | if (res->flags & BF_SHUTW) { |
| 5361 | /* response errors are most likely due to |
| 5362 | * the client aborting the transfer. |
| 5363 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5364 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5365 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5366 | if (msg->err_pos >= 0) |
| 5367 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5368 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5369 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5370 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5371 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5372 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5373 | } |
| 5374 | } |
| 5375 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5376 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5377 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5378 | if (res->flags & BF_SHUTR) { |
| 5379 | if (!(s->flags & SN_ERR_MASK)) |
| 5380 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5381 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5382 | if (target_srv(&s->target)) |
| 5383 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5384 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5385 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5386 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5387 | if (res->flags & BF_SHUTW) |
| 5388 | goto aborted_xfer; |
| 5389 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5390 | /* 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] | 5391 | if (!s->req->analysers) |
| 5392 | goto return_bad_res; |
| 5393 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5394 | /* forward any pending data */ |
| 5395 | bytes = msg->sov - msg->som; |
| 5396 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5397 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5398 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5399 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5400 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5401 | msg->chunk_len += (unsigned int)bytes; |
| 5402 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5403 | } |
| 5404 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5405 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5406 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5407 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5408 | * close. |
| 5409 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5410 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5411 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5412 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5413 | buffer_dont_close(res); |
| 5414 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5415 | /* 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] | 5416 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5417 | * 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] | 5418 | * modes are already handled by the stream sock layer. We must not do |
| 5419 | * this in content-length mode because it could present the MSG_MORE |
| 5420 | * flag with the last block of forwarded data, which would cause an |
| 5421 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5422 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5423 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5424 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5425 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5426 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5427 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5428 | return 0; |
| 5429 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5430 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5431 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5432 | if (target_srv(&s->target)) |
| 5433 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5434 | |
| 5435 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5436 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5437 | /* don't send any error message as we're in the body */ |
| 5438 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5439 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5440 | 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] | 5441 | if (target_srv(&s->target)) |
| 5442 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5443 | |
| 5444 | if (!(s->flags & SN_ERR_MASK)) |
| 5445 | s->flags |= SN_ERR_PRXCOND; |
| 5446 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5447 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5448 | return 0; |
| 5449 | |
| 5450 | aborted_xfer: |
| 5451 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5452 | /* don't send any error message as we're in the body */ |
| 5453 | stream_int_retnclose(res->cons, NULL); |
| 5454 | res->analysers = 0; |
| 5455 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5456 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5457 | s->fe->fe_counters.cli_aborts++; |
| 5458 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5459 | if (target_srv(&s->target)) |
| 5460 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5461 | |
| 5462 | if (!(s->flags & SN_ERR_MASK)) |
| 5463 | s->flags |= SN_ERR_CLICL; |
| 5464 | if (!(s->flags & SN_FINST_MASK)) |
| 5465 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5466 | return 0; |
| 5467 | } |
| 5468 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5469 | /* Iterate the same filter through all request headers. |
| 5470 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5471 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5472 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5473 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5474 | 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] | 5475 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5476 | char term; |
| 5477 | char *cur_ptr, *cur_end, *cur_next; |
| 5478 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5479 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5480 | struct hdr_idx_elem *cur_hdr; |
| 5481 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5483 | last_hdr = 0; |
| 5484 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5485 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5486 | old_idx = 0; |
| 5487 | |
| 5488 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5489 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5490 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5491 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5492 | (exp->action == ACT_ALLOW || |
| 5493 | exp->action == ACT_DENY || |
| 5494 | exp->action == ACT_TARPIT)) |
| 5495 | return 0; |
| 5496 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5497 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5498 | if (!cur_idx) |
| 5499 | break; |
| 5500 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5501 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5502 | cur_ptr = cur_next; |
| 5503 | cur_end = cur_ptr + cur_hdr->len; |
| 5504 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5505 | |
| 5506 | /* Now we have one header between cur_ptr and cur_end, |
| 5507 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5508 | */ |
| 5509 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5510 | /* The annoying part is that pattern matching needs |
| 5511 | * that we modify the contents to null-terminate all |
| 5512 | * strings before testing them. |
| 5513 | */ |
| 5514 | |
| 5515 | term = *cur_end; |
| 5516 | *cur_end = '\0'; |
| 5517 | |
| 5518 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5519 | switch (exp->action) { |
| 5520 | case ACT_SETBE: |
| 5521 | /* It is not possible to jump a second time. |
| 5522 | * FIXME: should we return an HTTP/500 here so that |
| 5523 | * the admin knows there's a problem ? |
| 5524 | */ |
| 5525 | if (t->be != t->fe) |
| 5526 | break; |
| 5527 | |
| 5528 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5529 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5530 | last_hdr = 1; |
| 5531 | break; |
| 5532 | |
| 5533 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5534 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5535 | last_hdr = 1; |
| 5536 | break; |
| 5537 | |
| 5538 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5539 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5540 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5541 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5542 | t->fe->fe_counters.denied_req++; |
| 5543 | if (t->fe != t->be) |
| 5544 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5545 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5546 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5547 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5548 | break; |
| 5549 | |
| 5550 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5551 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5552 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5553 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5554 | t->fe->fe_counters.denied_req++; |
| 5555 | if (t->fe != t->be) |
| 5556 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5557 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5558 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5559 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5560 | break; |
| 5561 | |
| 5562 | case ACT_REPLACE: |
| 5563 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5564 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5565 | /* FIXME: if the user adds a newline in the replacement, the |
| 5566 | * index will not be recalculated for now, and the new line |
| 5567 | * will not be counted as a new header. |
| 5568 | */ |
| 5569 | |
| 5570 | cur_end += delta; |
| 5571 | cur_next += delta; |
| 5572 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5573 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5574 | break; |
| 5575 | |
| 5576 | case ACT_REMOVE: |
| 5577 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5578 | cur_next += delta; |
| 5579 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5580 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5581 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5582 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5583 | cur_hdr->len = 0; |
| 5584 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5585 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5586 | break; |
| 5587 | |
| 5588 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5589 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5590 | if (cur_end) |
| 5591 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5593 | /* keep the link from this header to next one in case of later |
| 5594 | * removal of next header. |
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 | old_idx = cur_idx; |
| 5597 | } |
| 5598 | return 0; |
| 5599 | } |
| 5600 | |
| 5601 | |
| 5602 | /* Apply the filter to the request line. |
| 5603 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5604 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5605 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5606 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5607 | */ |
| 5608 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5609 | { |
| 5610 | char term; |
| 5611 | char *cur_ptr, *cur_end; |
| 5612 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5613 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5614 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5615 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5616 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5617 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5618 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5619 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5620 | (exp->action == ACT_ALLOW || |
| 5621 | exp->action == ACT_DENY || |
| 5622 | exp->action == ACT_TARPIT)) |
| 5623 | return 0; |
| 5624 | else if (exp->action == ACT_REMOVE) |
| 5625 | return 0; |
| 5626 | |
| 5627 | done = 0; |
| 5628 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5629 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5630 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5631 | |
| 5632 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5633 | |
| 5634 | /* The annoying part is that pattern matching needs |
| 5635 | * that we modify the contents to null-terminate all |
| 5636 | * strings before testing them. |
| 5637 | */ |
| 5638 | |
| 5639 | term = *cur_end; |
| 5640 | *cur_end = '\0'; |
| 5641 | |
| 5642 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5643 | switch (exp->action) { |
| 5644 | case ACT_SETBE: |
| 5645 | /* It is not possible to jump a second time. |
| 5646 | * FIXME: should we return an HTTP/500 here so that |
| 5647 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5648 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5649 | if (t->be != t->fe) |
| 5650 | break; |
| 5651 | |
| 5652 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5653 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5654 | done = 1; |
| 5655 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5657 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5658 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5659 | done = 1; |
| 5660 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5662 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5663 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5664 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5665 | t->fe->fe_counters.denied_req++; |
| 5666 | if (t->fe != t->be) |
| 5667 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5668 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5669 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5671 | done = 1; |
| 5672 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5673 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5674 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5675 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5676 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5677 | t->fe->fe_counters.denied_req++; |
| 5678 | if (t->fe != t->be) |
| 5679 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5680 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5681 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5682 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5683 | done = 1; |
| 5684 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5685 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5686 | case ACT_REPLACE: |
| 5687 | *cur_end = term; /* restore the string terminator */ |
| 5688 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5689 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5690 | /* FIXME: if the user adds a newline in the replacement, the |
| 5691 | * index will not be recalculated for now, and the new line |
| 5692 | * will not be counted as a new header. |
| 5693 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5694 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5695 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5696 | cur_end += delta; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5697 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, req->p, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5698 | HTTP_MSG_RQMETH, |
| 5699 | cur_ptr, cur_end + 1, |
| 5700 | NULL, NULL); |
| 5701 | if (unlikely(!cur_end)) |
| 5702 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5703 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5704 | /* we have a full request and we know that we have either a CR |
| 5705 | * or an LF at <ptr>. |
| 5706 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5707 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5708 | 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] | 5709 | /* there is no point trying this regex on headers */ |
| 5710 | return 1; |
| 5711 | } |
| 5712 | } |
| 5713 | *cur_end = term; /* restore the string terminator */ |
| 5714 | return done; |
| 5715 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5716 | |
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 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5720 | * 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] | 5721 | * 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] | 5722 | * unparsable request. Since it can manage the switch to another backend, it |
| 5723 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5724 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5725 | 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] | 5726 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5727 | struct http_txn *txn = &s->txn; |
| 5728 | struct hdr_exp *exp; |
| 5729 | |
| 5730 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5731 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5733 | /* |
| 5734 | * The interleaving of transformations and verdicts |
| 5735 | * makes it difficult to decide to continue or stop |
| 5736 | * the evaluation. |
| 5737 | */ |
| 5738 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5739 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5740 | break; |
| 5741 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5742 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5743 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5744 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5745 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5746 | |
| 5747 | /* if this filter had a condition, evaluate it now and skip to |
| 5748 | * next filter if the condition does not match. |
| 5749 | */ |
| 5750 | if (exp->cond) { |
| 5751 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5752 | ret = acl_pass(ret); |
| 5753 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5754 | ret = !ret; |
| 5755 | |
| 5756 | if (!ret) |
| 5757 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5761 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5762 | if (unlikely(ret < 0)) |
| 5763 | return -1; |
| 5764 | |
| 5765 | if (likely(ret == 0)) { |
| 5766 | /* The filter did not match the request, it can be |
| 5767 | * iterated through all headers. |
| 5768 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5769 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5770 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5771 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5772 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5776 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5777 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5778 | * Try to retrieve the server associated to the appsession. |
| 5779 | * If the server is found, it's assigned to the session. |
| 5780 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5781 | 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] | 5782 | struct http_txn *txn = &t->txn; |
| 5783 | appsess *asession = NULL; |
| 5784 | char *sessid_temp = NULL; |
| 5785 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5786 | if (len > t->be->appsession_len) { |
| 5787 | len = t->be->appsession_len; |
| 5788 | } |
| 5789 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5790 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5791 | /* 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] | 5792 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5793 | /* 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] | 5794 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5797 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5798 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5799 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5800 | return; |
| 5801 | } |
| 5802 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5803 | memcpy(txn->sessid, buf, len); |
| 5804 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5808 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5809 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5810 | return; |
| 5811 | } |
| 5812 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5813 | memcpy(sessid_temp, buf, len); |
| 5814 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5815 | |
| 5816 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5817 | /* free previously allocated memory */ |
| 5818 | pool_free2(apools.sessid, sessid_temp); |
| 5819 | |
| 5820 | if (asession != NULL) { |
| 5821 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5822 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5823 | asession->request_count++; |
| 5824 | |
| 5825 | if (asession->serverid != NULL) { |
| 5826 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5827 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5828 | while (srv) { |
| 5829 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5830 | if ((srv->state & SRV_RUNNING) || |
| 5831 | (t->be->options & PR_O_PERSIST) || |
| 5832 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5833 | /* we found the server and it's usable */ |
| 5834 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5835 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5836 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5837 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5838 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5839 | break; |
| 5840 | } else { |
| 5841 | txn->flags &= ~TX_CK_MASK; |
| 5842 | txn->flags |= TX_CK_DOWN; |
| 5843 | } |
| 5844 | } |
| 5845 | srv = srv->next; |
| 5846 | } |
| 5847 | } |
| 5848 | } |
| 5849 | } |
| 5850 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5851 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5852 | * same way as with headers above except that the semi-colon also ends a token. |
| 5853 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5854 | * return a valid result. |
| 5855 | */ |
| 5856 | char *find_cookie_value_end(char *s, const char *e) |
| 5857 | { |
| 5858 | int quoted, qdpair; |
| 5859 | |
| 5860 | quoted = qdpair = 0; |
| 5861 | for (; s < e; s++) { |
| 5862 | if (qdpair) qdpair = 0; |
| 5863 | else if (quoted) { |
| 5864 | if (*s == '\\') qdpair = 1; |
| 5865 | else if (*s == '"') quoted = 0; |
| 5866 | } |
| 5867 | else if (*s == '"') quoted = 1; |
| 5868 | else if (*s == ',' || *s == ';') return s; |
| 5869 | } |
| 5870 | return s; |
| 5871 | } |
| 5872 | |
| 5873 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5874 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5875 | * the first delimiter is updated if required. The function tries as much as |
| 5876 | * possible to respect the following principles : |
| 5877 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5878 | * colon, in which case <next> is simply removed |
| 5879 | * - set exactly one space character after the new first delimiter, unless |
| 5880 | * there are not enough characters in the block being moved to do so. |
| 5881 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5882 | * one. |
| 5883 | * |
| 5884 | * It is the caller's responsibility to ensure that : |
| 5885 | * - <from> points to a valid delimiter or the colon ; |
| 5886 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5887 | * - there are non-space chars before <from> ; |
| 5888 | * - there is a CR/LF at or after <next>. |
| 5889 | */ |
| 5890 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5891 | { |
| 5892 | char *prev = *from; |
| 5893 | |
| 5894 | if (*prev == ':') { |
| 5895 | /* We're removing the first value, preserve the colon and add a |
| 5896 | * space if possible. |
| 5897 | */ |
| 5898 | if (!http_is_crlf[(unsigned char)*next]) |
| 5899 | next++; |
| 5900 | prev++; |
| 5901 | if (prev < next) |
| 5902 | *prev++ = ' '; |
| 5903 | |
| 5904 | while (http_is_spht[(unsigned char)*next]) |
| 5905 | next++; |
| 5906 | } else { |
| 5907 | /* Remove useless spaces before the old delimiter. */ |
| 5908 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5909 | prev--; |
| 5910 | *from = prev; |
| 5911 | |
| 5912 | /* copy the delimiter and if possible a space if we're |
| 5913 | * not at the end of the line. |
| 5914 | */ |
| 5915 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5916 | *prev++ = *next++; |
| 5917 | if (prev + 1 < next) |
| 5918 | *prev++ = ' '; |
| 5919 | while (http_is_spht[(unsigned char)*next]) |
| 5920 | next++; |
| 5921 | } |
| 5922 | } |
| 5923 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5924 | } |
| 5925 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5926 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5927 | * 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] | 5928 | * desirable to call it only when needed. This code is quite complex because |
| 5929 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5930 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5931 | */ |
| 5932 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5933 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5934 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5935 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5936 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5937 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5938 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5939 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5940 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5941 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5942 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5943 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5944 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5945 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5946 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5947 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5948 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5949 | hdr_beg = hdr_next; |
| 5950 | hdr_end = hdr_beg + cur_hdr->len; |
| 5951 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5952 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5953 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5954 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5955 | * "Cookie:" headers. |
| 5956 | */ |
| 5957 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5958 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5959 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5960 | old_idx = cur_idx; |
| 5961 | continue; |
| 5962 | } |
| 5963 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5964 | del_from = NULL; /* nothing to be deleted */ |
| 5965 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5966 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5967 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5968 | * attributes whose name begin with a '$', and associate them with |
| 5969 | * the right cookie, if we want to delete this cookie. |
| 5970 | * So there are 3 cases for each cookie read : |
| 5971 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5972 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5973 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5974 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5975 | * "special" cookie. |
| 5976 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5977 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5978 | * *MUST* delete it. |
| 5979 | * |
| 5980 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5981 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5982 | * the RFC explicitly allows spaces before it, and not within the |
| 5983 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5984 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5985 | * after the equal sign too, resulting in some (rare) buggy |
| 5986 | * implementations trying to do that. So let's do what servers do. |
| 5987 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5988 | * allowed quoted strings in values, with any possible character |
| 5989 | * after a backslash, including control chars and delimitors, which |
| 5990 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5991 | * within values even without quotes. |
| 5992 | * |
| 5993 | * We have to keep multiple pointers in order to support cookie |
| 5994 | * removal at the beginning, middle or end of header without |
| 5995 | * corrupting the header. All of these headers are valid : |
| 5996 | * |
| 5997 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5998 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5999 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6000 | * | | | | | | | | | |
| 6001 | * | | | | | | | | hdr_end <--+ |
| 6002 | * | | | | | | | +--> next |
| 6003 | * | | | | | | +----> val_end |
| 6004 | * | | | | | +-----------> val_beg |
| 6005 | * | | | | +--------------> equal |
| 6006 | * | | | +----------------> att_end |
| 6007 | * | | +---------------------> att_beg |
| 6008 | * | +--------------------------> prev |
| 6009 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6010 | */ |
| 6011 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6012 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6013 | /* Iterate through all cookies on this line */ |
| 6014 | |
| 6015 | /* find att_beg */ |
| 6016 | att_beg = prev + 1; |
| 6017 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6018 | att_beg++; |
| 6019 | |
| 6020 | /* find att_end : this is the first character after the last non |
| 6021 | * space before the equal. It may be equal to hdr_end. |
| 6022 | */ |
| 6023 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6024 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6025 | while (equal < hdr_end) { |
| 6026 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6027 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6028 | if (http_is_spht[(unsigned char)*equal++]) |
| 6029 | continue; |
| 6030 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6031 | } |
| 6032 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6033 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6034 | * is between <att_beg> and <equal>, both may be identical. |
| 6035 | */ |
| 6036 | |
| 6037 | /* look for end of cookie if there is an equal sign */ |
| 6038 | if (equal < hdr_end && *equal == '=') { |
| 6039 | /* look for the beginning of the value */ |
| 6040 | val_beg = equal + 1; |
| 6041 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6042 | val_beg++; |
| 6043 | |
| 6044 | /* find the end of the value, respecting quotes */ |
| 6045 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6046 | |
| 6047 | /* make val_end point to the first white space or delimitor after the value */ |
| 6048 | val_end = next; |
| 6049 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6050 | val_end--; |
| 6051 | } else { |
| 6052 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6053 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6054 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6055 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6056 | * they will automatically be removed if a header before them is removed, |
| 6057 | * since they're supposed to be linked together. |
| 6058 | */ |
| 6059 | if (*att_beg == '$') |
| 6060 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6061 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6062 | /* Ignore cookies with no equal sign */ |
| 6063 | if (equal == next) { |
| 6064 | /* This is not our cookie, so we must preserve it. But if we already |
| 6065 | * scheduled another cookie for removal, we cannot remove the |
| 6066 | * complete header, but we can remove the previous block itself. |
| 6067 | */ |
| 6068 | preserve_hdr = 1; |
| 6069 | if (del_from != NULL) { |
| 6070 | int delta = del_hdr_value(req, &del_from, prev); |
| 6071 | val_end += delta; |
| 6072 | next += delta; |
| 6073 | hdr_end += delta; |
| 6074 | hdr_next += delta; |
| 6075 | cur_hdr->len += delta; |
| 6076 | http_msg_move_end(&txn->req, delta); |
| 6077 | prev = del_from; |
| 6078 | del_from = NULL; |
| 6079 | } |
| 6080 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6081 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6082 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6083 | /* if there are spaces around the equal sign, we need to |
| 6084 | * strip them otherwise we'll get trouble for cookie captures, |
| 6085 | * or even for rewrites. Since this happens extremely rarely, |
| 6086 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6087 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6088 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6089 | int stripped_before = 0; |
| 6090 | int stripped_after = 0; |
| 6091 | |
| 6092 | if (att_end != equal) { |
| 6093 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6094 | equal += stripped_before; |
| 6095 | val_beg += stripped_before; |
| 6096 | } |
| 6097 | |
| 6098 | if (val_beg > equal + 1) { |
| 6099 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6100 | val_beg += stripped_after; |
| 6101 | stripped_before += stripped_after; |
| 6102 | } |
| 6103 | |
| 6104 | val_end += stripped_before; |
| 6105 | next += stripped_before; |
| 6106 | hdr_end += stripped_before; |
| 6107 | hdr_next += stripped_before; |
| 6108 | cur_hdr->len += stripped_before; |
| 6109 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6110 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6111 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6112 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6113 | /* First, let's see if we want to capture this cookie. We check |
| 6114 | * that we don't already have a client side cookie, because we |
| 6115 | * can only capture one. Also as an optimisation, we ignore |
| 6116 | * cookies shorter than the declared name. |
| 6117 | */ |
| 6118 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6119 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6120 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6121 | int log_len = val_end - att_beg; |
| 6122 | |
| 6123 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6124 | Alert("HTTP logging : out of memory.\n"); |
| 6125 | } else { |
| 6126 | if (log_len > t->fe->capture_len) |
| 6127 | log_len = t->fe->capture_len; |
| 6128 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6129 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6130 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6131 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6132 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6133 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6134 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6135 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6136 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6137 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6138 | * For cookies in prefix mode, the form is : |
| 6139 | * |
| 6140 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6141 | */ |
| 6142 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6143 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6144 | struct server *srv = t->be->srv; |
| 6145 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6146 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6147 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6148 | * have the server ID between val_beg and delim, and the original cookie between |
| 6149 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6150 | * |
| 6151 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6152 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6153 | * | || || | |+-> next |
| 6154 | * | || || | +--> val_end |
| 6155 | * | || || +---------> delim |
| 6156 | * | || |+------------> val_beg |
| 6157 | * | || +-------------> att_end = equal |
| 6158 | * | |+-----------------> att_beg |
| 6159 | * | +------------------> prev |
| 6160 | * +-------------------------> hdr_beg |
| 6161 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6162 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6163 | if (t->be->options & PR_O_COOK_PFX) { |
| 6164 | for (delim = val_beg; delim < val_end; delim++) |
| 6165 | if (*delim == COOKIE_DELIM) |
| 6166 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6167 | } else { |
| 6168 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6169 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6170 | /* Now check if the cookie contains a date field, which would |
| 6171 | * appear after a vertical bar ('|') just after the server name |
| 6172 | * and before the delimiter. |
| 6173 | */ |
| 6174 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6175 | if (vbar1) { |
| 6176 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6177 | * right is the last seen date. It is a base64 encoded |
| 6178 | * 30-bit value representing the UNIX date since the |
| 6179 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6180 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6181 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6182 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6183 | if (val_end - vbar1 >= 5) { |
| 6184 | val = b64tos30(vbar1); |
| 6185 | if (val > 0) |
| 6186 | txn->cookie_last_date = val << 2; |
| 6187 | } |
| 6188 | /* look for a second vertical bar */ |
| 6189 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6190 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6191 | val = b64tos30(vbar1 + 1); |
| 6192 | if (val > 0) |
| 6193 | txn->cookie_first_date = val << 2; |
| 6194 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6195 | } |
| 6196 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6197 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6198 | /* if the cookie has an expiration date and the proxy wants to check |
| 6199 | * it, then we do that now. We first check if the cookie is too old, |
| 6200 | * then only if it has expired. We detect strict overflow because the |
| 6201 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6202 | * in the future are ignored if their offset is beyond one day. This |
| 6203 | * allows an admin to fix timezone issues without expiring everyone |
| 6204 | * and at the same time avoids keeping unwanted side effects for too |
| 6205 | * long. |
| 6206 | */ |
| 6207 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6208 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6209 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6210 | txn->flags &= ~TX_CK_MASK; |
| 6211 | txn->flags |= TX_CK_OLD; |
| 6212 | delim = val_beg; // let's pretend we have not found the cookie |
| 6213 | txn->cookie_first_date = 0; |
| 6214 | txn->cookie_last_date = 0; |
| 6215 | } |
| 6216 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6217 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6218 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6219 | txn->flags &= ~TX_CK_MASK; |
| 6220 | txn->flags |= TX_CK_EXPIRED; |
| 6221 | delim = val_beg; // let's pretend we have not found the cookie |
| 6222 | txn->cookie_first_date = 0; |
| 6223 | txn->cookie_last_date = 0; |
| 6224 | } |
| 6225 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6226 | /* Here, we'll look for the first running server which supports the cookie. |
| 6227 | * This allows to share a same cookie between several servers, for example |
| 6228 | * to dedicate backup servers to specific servers only. |
| 6229 | * However, to prevent clients from sticking to cookie-less backup server |
| 6230 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6231 | * empty cookies and mark them as invalid. |
| 6232 | * The same behaviour is applied when persistence must be ignored. |
| 6233 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6234 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6235 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6236 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6237 | while (srv) { |
| 6238 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6239 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6240 | if ((srv->state & SRV_RUNNING) || |
| 6241 | (t->be->options & PR_O_PERSIST) || |
| 6242 | (t->flags & SN_FORCE_PRST)) { |
| 6243 | /* we found the server and we can use it */ |
| 6244 | txn->flags &= ~TX_CK_MASK; |
| 6245 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6246 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6247 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6248 | break; |
| 6249 | } else { |
| 6250 | /* we found a server, but it's down, |
| 6251 | * mark it as such and go on in case |
| 6252 | * another one is available. |
| 6253 | */ |
| 6254 | txn->flags &= ~TX_CK_MASK; |
| 6255 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6256 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6257 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6258 | srv = srv->next; |
| 6259 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6260 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6261 | 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] | 6262 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6263 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6264 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6265 | txn->flags |= TX_CK_UNUSED; |
| 6266 | else |
| 6267 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6268 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6269 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6270 | /* depending on the cookie mode, we may have to either : |
| 6271 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6272 | * the server never sees it ; |
| 6273 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6274 | * application cookie so that it does not get accidentely removed later, |
| 6275 | * if we're in cookie prefix mode |
| 6276 | */ |
| 6277 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6278 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6279 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6280 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6281 | val_end += delta; |
| 6282 | next += delta; |
| 6283 | hdr_end += delta; |
| 6284 | hdr_next += delta; |
| 6285 | cur_hdr->len += delta; |
| 6286 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6287 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6288 | del_from = NULL; |
| 6289 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6290 | } |
| 6291 | else if (del_from == NULL && |
| 6292 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6293 | del_from = prev; |
| 6294 | } |
| 6295 | } else { |
| 6296 | /* This is not our cookie, so we must preserve it. But if we already |
| 6297 | * scheduled another cookie for removal, we cannot remove the |
| 6298 | * complete header, but we can remove the previous block itself. |
| 6299 | */ |
| 6300 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6301 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6302 | if (del_from != NULL) { |
| 6303 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6304 | if (att_beg >= del_from) |
| 6305 | att_beg += delta; |
| 6306 | if (att_end >= del_from) |
| 6307 | att_end += delta; |
| 6308 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6309 | val_end += delta; |
| 6310 | next += delta; |
| 6311 | hdr_end += delta; |
| 6312 | hdr_next += delta; |
| 6313 | cur_hdr->len += delta; |
| 6314 | http_msg_move_end(&txn->req, delta); |
| 6315 | prev = del_from; |
| 6316 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6317 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6318 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6319 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6320 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6321 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6322 | int cmp_len, value_len; |
| 6323 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6324 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6325 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6326 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6327 | value_begin = att_beg + t->be->appsession_name_len; |
| 6328 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6329 | } else { |
| 6330 | cmp_len = att_end - att_beg; |
| 6331 | value_begin = val_beg; |
| 6332 | value_len = val_end - val_beg; |
| 6333 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6334 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6335 | /* let's see if the cookie is our appcookie */ |
| 6336 | if (cmp_len == t->be->appsession_name_len && |
| 6337 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6338 | manage_client_side_appsession(t, value_begin, value_len); |
| 6339 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6340 | } |
| 6341 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6342 | /* continue with next cookie on this header line */ |
| 6343 | att_beg = next; |
| 6344 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6345 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6346 | /* There are no more cookies on this line. |
| 6347 | * We may still have one (or several) marked for deletion at the |
| 6348 | * end of the line. We must do this now in two ways : |
| 6349 | * - if some cookies must be preserved, we only delete from the |
| 6350 | * mark to the end of line ; |
| 6351 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6352 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6353 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6354 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6355 | if (preserve_hdr) { |
| 6356 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6357 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6358 | cur_hdr->len += delta; |
| 6359 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6360 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6361 | |
| 6362 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6363 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6364 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6365 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6366 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6367 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6368 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6369 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6370 | } |
| 6371 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6372 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6373 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6374 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6378 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6379 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6380 | */ |
| 6381 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6382 | { |
| 6383 | char term; |
| 6384 | char *cur_ptr, *cur_end, *cur_next; |
| 6385 | int cur_idx, old_idx, last_hdr; |
| 6386 | struct http_txn *txn = &t->txn; |
| 6387 | struct hdr_idx_elem *cur_hdr; |
| 6388 | int len, delta; |
| 6389 | |
| 6390 | last_hdr = 0; |
| 6391 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6392 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6393 | old_idx = 0; |
| 6394 | |
| 6395 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6396 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6397 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6398 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6399 | (exp->action == ACT_ALLOW || |
| 6400 | exp->action == ACT_DENY)) |
| 6401 | return 0; |
| 6402 | |
| 6403 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6404 | if (!cur_idx) |
| 6405 | break; |
| 6406 | |
| 6407 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6408 | cur_ptr = cur_next; |
| 6409 | cur_end = cur_ptr + cur_hdr->len; |
| 6410 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6411 | |
| 6412 | /* Now we have one header between cur_ptr and cur_end, |
| 6413 | * and the next header starts at cur_next. |
| 6414 | */ |
| 6415 | |
| 6416 | /* The annoying part is that pattern matching needs |
| 6417 | * that we modify the contents to null-terminate all |
| 6418 | * strings before testing them. |
| 6419 | */ |
| 6420 | |
| 6421 | term = *cur_end; |
| 6422 | *cur_end = '\0'; |
| 6423 | |
| 6424 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6425 | switch (exp->action) { |
| 6426 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6427 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6428 | last_hdr = 1; |
| 6429 | break; |
| 6430 | |
| 6431 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6432 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6433 | last_hdr = 1; |
| 6434 | break; |
| 6435 | |
| 6436 | case ACT_REPLACE: |
| 6437 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6438 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6439 | /* FIXME: if the user adds a newline in the replacement, the |
| 6440 | * index will not be recalculated for now, and the new line |
| 6441 | * will not be counted as a new header. |
| 6442 | */ |
| 6443 | |
| 6444 | cur_end += delta; |
| 6445 | cur_next += delta; |
| 6446 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6447 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6448 | break; |
| 6449 | |
| 6450 | case ACT_REMOVE: |
| 6451 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6452 | cur_next += delta; |
| 6453 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6454 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6455 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6456 | txn->hdr_idx.used--; |
| 6457 | cur_hdr->len = 0; |
| 6458 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6459 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6460 | break; |
| 6461 | |
| 6462 | } |
| 6463 | } |
| 6464 | if (cur_end) |
| 6465 | *cur_end = term; /* restore the string terminator */ |
| 6466 | |
| 6467 | /* keep the link from this header to next one in case of later |
| 6468 | * removal of next header. |
| 6469 | */ |
| 6470 | old_idx = cur_idx; |
| 6471 | } |
| 6472 | return 0; |
| 6473 | } |
| 6474 | |
| 6475 | |
| 6476 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6477 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6478 | * or -1 if a replacement resulted in an invalid status line. |
| 6479 | */ |
| 6480 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6481 | { |
| 6482 | char term; |
| 6483 | char *cur_ptr, *cur_end; |
| 6484 | int done; |
| 6485 | struct http_txn *txn = &t->txn; |
| 6486 | int len, delta; |
| 6487 | |
| 6488 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6489 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6490 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6491 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6492 | (exp->action == ACT_ALLOW || |
| 6493 | exp->action == ACT_DENY)) |
| 6494 | return 0; |
| 6495 | else if (exp->action == ACT_REMOVE) |
| 6496 | return 0; |
| 6497 | |
| 6498 | done = 0; |
| 6499 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6500 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6501 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6502 | |
| 6503 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6504 | |
| 6505 | /* The annoying part is that pattern matching needs |
| 6506 | * that we modify the contents to null-terminate all |
| 6507 | * strings before testing them. |
| 6508 | */ |
| 6509 | |
| 6510 | term = *cur_end; |
| 6511 | *cur_end = '\0'; |
| 6512 | |
| 6513 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6514 | switch (exp->action) { |
| 6515 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6516 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6517 | done = 1; |
| 6518 | break; |
| 6519 | |
| 6520 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6521 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6522 | done = 1; |
| 6523 | break; |
| 6524 | |
| 6525 | case ACT_REPLACE: |
| 6526 | *cur_end = term; /* restore the string terminator */ |
| 6527 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6528 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6529 | /* FIXME: if the user adds a newline in the replacement, the |
| 6530 | * index will not be recalculated for now, and the new line |
| 6531 | * will not be counted as a new header. |
| 6532 | */ |
| 6533 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6534 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6535 | cur_end += delta; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6536 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, rtr->p, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6537 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6538 | cur_ptr, cur_end + 1, |
| 6539 | NULL, NULL); |
| 6540 | if (unlikely(!cur_end)) |
| 6541 | return -1; |
| 6542 | |
| 6543 | /* we have a full respnse and we know that we have either a CR |
| 6544 | * or an LF at <ptr>. |
| 6545 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6546 | txn->status = strl2ui(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] | 6547 | 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] | 6548 | /* there is no point trying this regex on headers */ |
| 6549 | return 1; |
| 6550 | } |
| 6551 | } |
| 6552 | *cur_end = term; /* restore the string terminator */ |
| 6553 | return done; |
| 6554 | } |
| 6555 | |
| 6556 | |
| 6557 | |
| 6558 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6559 | * 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] | 6560 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6561 | * unparsable response. |
| 6562 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6563 | 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] | 6564 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6565 | struct http_txn *txn = &s->txn; |
| 6566 | struct hdr_exp *exp; |
| 6567 | |
| 6568 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6569 | int ret; |
| 6570 | |
| 6571 | /* |
| 6572 | * The interleaving of transformations and verdicts |
| 6573 | * makes it difficult to decide to continue or stop |
| 6574 | * the evaluation. |
| 6575 | */ |
| 6576 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6577 | if (txn->flags & TX_SVDENY) |
| 6578 | break; |
| 6579 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6580 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6581 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6582 | exp->action == ACT_PASS)) { |
| 6583 | exp = exp->next; |
| 6584 | continue; |
| 6585 | } |
| 6586 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6587 | /* if this filter had a condition, evaluate it now and skip to |
| 6588 | * next filter if the condition does not match. |
| 6589 | */ |
| 6590 | if (exp->cond) { |
| 6591 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6592 | ret = acl_pass(ret); |
| 6593 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6594 | ret = !ret; |
| 6595 | if (!ret) |
| 6596 | continue; |
| 6597 | } |
| 6598 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6599 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6600 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6601 | if (unlikely(ret < 0)) |
| 6602 | return -1; |
| 6603 | |
| 6604 | if (likely(ret == 0)) { |
| 6605 | /* The filter did not match the response, it can be |
| 6606 | * iterated through all headers. |
| 6607 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6608 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6609 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6610 | } |
| 6611 | return 0; |
| 6612 | } |
| 6613 | |
| 6614 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6615 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6616 | * 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] | 6617 | * desirable to call it only when needed. This function is also used when we |
| 6618 | * 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] | 6619 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6620 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6621 | { |
| 6622 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6623 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6624 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6625 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6626 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6627 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6628 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6629 | /* Iterate through the headers. |
| 6630 | * we start with the start line. |
| 6631 | */ |
| 6632 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6633 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6634 | |
| 6635 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6636 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6637 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6638 | |
| 6639 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6640 | hdr_beg = hdr_next; |
| 6641 | hdr_end = hdr_beg + cur_hdr->len; |
| 6642 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6643 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6644 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6645 | * next header starts at hdr_next. We're only interested in |
| 6646 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6647 | */ |
| 6648 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6649 | is_cookie2 = 0; |
| 6650 | prev = hdr_beg + 10; |
| 6651 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6652 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6653 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6654 | if (!val) { |
| 6655 | old_idx = cur_idx; |
| 6656 | continue; |
| 6657 | } |
| 6658 | is_cookie2 = 1; |
| 6659 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6660 | } |
| 6661 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6662 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6663 | * <prev> points to the colon. |
| 6664 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6665 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6666 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6667 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6668 | * check-cache is enabled) and we are not interested in checking |
| 6669 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6670 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6671 | if (t->be->cookie_name == NULL && |
| 6672 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6673 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6674 | return; |
| 6675 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6676 | /* OK so now we know we have to process this response cookie. |
| 6677 | * The format of the Set-Cookie header is slightly different |
| 6678 | * from the format of the Cookie header in that it does not |
| 6679 | * support the comma as a cookie delimiter (thus the header |
| 6680 | * cannot be folded) because the Expires attribute described in |
| 6681 | * the original Netscape's spec may contain an unquoted date |
| 6682 | * with a comma inside. We have to live with this because |
| 6683 | * many browsers don't support Max-Age and some browsers don't |
| 6684 | * support quoted strings. However the Set-Cookie2 header is |
| 6685 | * clean. |
| 6686 | * |
| 6687 | * We have to keep multiple pointers in order to support cookie |
| 6688 | * removal at the beginning, middle or end of header without |
| 6689 | * corrupting the header (in case of set-cookie2). A special |
| 6690 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6691 | * fields after the first semi-colon. The <next> pointer points |
| 6692 | * either to the end of line (set-cookie) or next unquoted comma |
| 6693 | * (set-cookie2). All of these headers are valid : |
| 6694 | * |
| 6695 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6696 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6697 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6698 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6699 | * | | | | | | | | | | |
| 6700 | * | | | | | | | | +-> next hdr_end <--+ |
| 6701 | * | | | | | | | +------------> scav |
| 6702 | * | | | | | | +--------------> val_end |
| 6703 | * | | | | | +--------------------> val_beg |
| 6704 | * | | | | +----------------------> equal |
| 6705 | * | | | +------------------------> att_end |
| 6706 | * | | +----------------------------> att_beg |
| 6707 | * | +------------------------------> prev |
| 6708 | * +-----------------------------------------> hdr_beg |
| 6709 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6710 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6711 | for (; prev < hdr_end; prev = next) { |
| 6712 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6713 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6714 | /* find att_beg */ |
| 6715 | att_beg = prev + 1; |
| 6716 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6717 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6718 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6719 | /* find att_end : this is the first character after the last non |
| 6720 | * space before the equal. It may be equal to hdr_end. |
| 6721 | */ |
| 6722 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6723 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6724 | while (equal < hdr_end) { |
| 6725 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6726 | break; |
| 6727 | if (http_is_spht[(unsigned char)*equal++]) |
| 6728 | continue; |
| 6729 | att_end = equal; |
| 6730 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6731 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6732 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6733 | * is between <att_beg> and <equal>, both may be identical. |
| 6734 | */ |
| 6735 | |
| 6736 | /* look for end of cookie if there is an equal sign */ |
| 6737 | if (equal < hdr_end && *equal == '=') { |
| 6738 | /* look for the beginning of the value */ |
| 6739 | val_beg = equal + 1; |
| 6740 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6741 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6742 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6743 | /* find the end of the value, respecting quotes */ |
| 6744 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6745 | |
| 6746 | /* make val_end point to the first white space or delimitor after the value */ |
| 6747 | val_end = next; |
| 6748 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6749 | val_end--; |
| 6750 | } else { |
| 6751 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6752 | val_beg = val_end = next = equal; |
| 6753 | } |
| 6754 | |
| 6755 | if (next < hdr_end) { |
| 6756 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6757 | * a colon or semi-colon before the end. So skip all attr-value |
| 6758 | * pairs and look for the next comma. For Set-Cookie, since |
| 6759 | * commas are permitted in values, skip to the end. |
| 6760 | */ |
| 6761 | if (is_cookie2) |
| 6762 | next = find_hdr_value_end(next, hdr_end); |
| 6763 | else |
| 6764 | next = hdr_end; |
| 6765 | } |
| 6766 | |
| 6767 | /* Now everything is as on the diagram above */ |
| 6768 | |
| 6769 | /* Ignore cookies with no equal sign */ |
| 6770 | if (equal == val_end) |
| 6771 | continue; |
| 6772 | |
| 6773 | /* If there are spaces around the equal sign, we need to |
| 6774 | * strip them otherwise we'll get trouble for cookie captures, |
| 6775 | * or even for rewrites. Since this happens extremely rarely, |
| 6776 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6777 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6778 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6779 | int stripped_before = 0; |
| 6780 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6781 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6782 | if (att_end != equal) { |
| 6783 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6784 | equal += stripped_before; |
| 6785 | val_beg += stripped_before; |
| 6786 | } |
| 6787 | |
| 6788 | if (val_beg > equal + 1) { |
| 6789 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6790 | val_beg += stripped_after; |
| 6791 | stripped_before += stripped_after; |
| 6792 | } |
| 6793 | |
| 6794 | val_end += stripped_before; |
| 6795 | next += stripped_before; |
| 6796 | hdr_end += stripped_before; |
| 6797 | hdr_next += stripped_before; |
| 6798 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6799 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | /* First, let's see if we want to capture this cookie. We check |
| 6803 | * that we don't already have a server side cookie, because we |
| 6804 | * can only capture one. Also as an optimisation, we ignore |
| 6805 | * cookies shorter than the declared name. |
| 6806 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6807 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6808 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6809 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6810 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6811 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6812 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6813 | Alert("HTTP logging : out of memory.\n"); |
| 6814 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6815 | else { |
| 6816 | if (log_len > t->fe->capture_len) |
| 6817 | log_len = t->fe->capture_len; |
| 6818 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6819 | txn->srv_cookie[log_len] = 0; |
| 6820 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6821 | } |
| 6822 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6823 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6824 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6825 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6826 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6827 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6828 | /* assume passive cookie by default */ |
| 6829 | txn->flags &= ~TX_SCK_MASK; |
| 6830 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6831 | |
| 6832 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6833 | * this occurrence because we'll insert another one later. |
| 6834 | * 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] | 6835 | * a direct access. |
| 6836 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6837 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 6838 | /* The "preserve" flag was set, we don't want to touch the |
| 6839 | * server's cookie. |
| 6840 | */ |
| 6841 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6842 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6843 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6844 | /* this cookie must be deleted */ |
| 6845 | if (*prev == ':' && next == hdr_end) { |
| 6846 | /* whole header */ |
| 6847 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6848 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6849 | txn->hdr_idx.used--; |
| 6850 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6851 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6852 | hdr_next += delta; |
| 6853 | http_msg_move_end(&txn->rsp, delta); |
| 6854 | /* note: while both invalid now, <next> and <hdr_end> |
| 6855 | * are still equal, so the for() will stop as expected. |
| 6856 | */ |
| 6857 | } else { |
| 6858 | /* just remove the value */ |
| 6859 | int delta = del_hdr_value(res, &prev, next); |
| 6860 | next = prev; |
| 6861 | hdr_end += delta; |
| 6862 | hdr_next += delta; |
| 6863 | cur_hdr->len += delta; |
| 6864 | http_msg_move_end(&txn->rsp, delta); |
| 6865 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6866 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6867 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6868 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6869 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6870 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6871 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6872 | * with this server since we know it. |
| 6873 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6874 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6875 | next += delta; |
| 6876 | hdr_end += delta; |
| 6877 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6878 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6879 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6880 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6881 | txn->flags &= ~TX_SCK_MASK; |
| 6882 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6883 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6884 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6885 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6886 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6887 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6888 | 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] | 6889 | next += delta; |
| 6890 | hdr_end += delta; |
| 6891 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6892 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6893 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6894 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6895 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6896 | txn->flags &= ~TX_SCK_MASK; |
| 6897 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6898 | } |
| 6899 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6900 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6901 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6902 | int cmp_len, value_len; |
| 6903 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6904 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6905 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6906 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6907 | value_begin = att_beg + t->be->appsession_name_len; |
| 6908 | 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] | 6909 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6910 | cmp_len = att_end - att_beg; |
| 6911 | value_begin = val_beg; |
| 6912 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6913 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6914 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6915 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6916 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6917 | /* free a possibly previously allocated memory */ |
| 6918 | pool_free2(apools.sessid, txn->sessid); |
| 6919 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6920 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6921 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6922 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6923 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6924 | return; |
| 6925 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6926 | memcpy(txn->sessid, value_begin, value_len); |
| 6927 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6928 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6929 | } |
| 6930 | /* that's done for this cookie, check the next one on the same |
| 6931 | * line when next != hdr_end (only if is_cookie2). |
| 6932 | */ |
| 6933 | } |
| 6934 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6935 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6936 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6937 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6938 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6939 | appsess *asession = NULL; |
| 6940 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6941 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6942 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6943 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6944 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6945 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6946 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6947 | return; |
| 6948 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6949 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 6950 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6951 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6952 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6953 | 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] | 6954 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6955 | return; |
| 6956 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6957 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6958 | asession->sessid[t->be->appsession_len] = 0; |
| 6959 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6960 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6961 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6962 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6963 | 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] | 6964 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6965 | return; |
| 6966 | } |
| 6967 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6968 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6969 | |
| 6970 | asession->request_count = 0; |
| 6971 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6972 | } |
| 6973 | |
| 6974 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6975 | asession->request_count++; |
| 6976 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6980 | /* |
| 6981 | * Check if response is cacheable or not. Updates t->flags. |
| 6982 | */ |
| 6983 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6984 | { |
| 6985 | struct http_txn *txn = &t->txn; |
| 6986 | char *p1, *p2; |
| 6987 | |
| 6988 | char *cur_ptr, *cur_end, *cur_next; |
| 6989 | int cur_idx; |
| 6990 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6991 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6992 | return; |
| 6993 | |
| 6994 | /* Iterate through the headers. |
| 6995 | * we start with the start line. |
| 6996 | */ |
| 6997 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6998 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6999 | |
| 7000 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7001 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7002 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7003 | |
| 7004 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7005 | cur_ptr = cur_next; |
| 7006 | cur_end = cur_ptr + cur_hdr->len; |
| 7007 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7008 | |
| 7009 | /* We have one full header between cur_ptr and cur_end, and the |
| 7010 | * next header starts at cur_next. We're only interested in |
| 7011 | * "Cookie:" headers. |
| 7012 | */ |
| 7013 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7014 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7015 | if (val) { |
| 7016 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7017 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7018 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7019 | return; |
| 7020 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7021 | } |
| 7022 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7023 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7024 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7025 | continue; |
| 7026 | |
| 7027 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7028 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7029 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7030 | |
| 7031 | if (p1 >= cur_end) /* no more info */ |
| 7032 | continue; |
| 7033 | |
| 7034 | /* p1 is at the beginning of the value */ |
| 7035 | p2 = p1; |
| 7036 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7037 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7038 | p2++; |
| 7039 | |
| 7040 | /* we have a complete value between p1 and p2 */ |
| 7041 | if (p2 < cur_end && *p2 == '=') { |
| 7042 | /* we have something of the form no-cache="set-cookie" */ |
| 7043 | if ((cur_end - p1 >= 21) && |
| 7044 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7045 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7046 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7047 | continue; |
| 7048 | } |
| 7049 | |
| 7050 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7051 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7052 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7053 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7054 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7055 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7056 | return; |
| 7057 | } |
| 7058 | |
| 7059 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7060 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7061 | continue; |
| 7062 | } |
| 7063 | } |
| 7064 | } |
| 7065 | |
| 7066 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7067 | /* |
| 7068 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7069 | * If the server is found, it's assigned to the session. |
| 7070 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7071 | 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] | 7072 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7073 | char *end_params, *first_param, *cur_param, *next_param; |
| 7074 | char separator; |
| 7075 | int value_len; |
| 7076 | |
| 7077 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7078 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7079 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7080 | (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] | 7081 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7082 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7083 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7084 | first_param = NULL; |
| 7085 | switch (mode) { |
| 7086 | case PR_O2_AS_M_PP: |
| 7087 | first_param = memchr(begin, ';', len); |
| 7088 | break; |
| 7089 | case PR_O2_AS_M_QS: |
| 7090 | first_param = memchr(begin, '?', len); |
| 7091 | break; |
| 7092 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7093 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7094 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7095 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7096 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7097 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7098 | switch (mode) { |
| 7099 | case PR_O2_AS_M_PP: |
| 7100 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7101 | end_params = (char *) begin + len; |
| 7102 | } |
| 7103 | separator = ';'; |
| 7104 | break; |
| 7105 | case PR_O2_AS_M_QS: |
| 7106 | end_params = (char *) begin + len; |
| 7107 | separator = '&'; |
| 7108 | break; |
| 7109 | default: |
| 7110 | /* unknown mode, shouldn't happen */ |
| 7111 | return; |
| 7112 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7113 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7114 | cur_param = next_param = end_params; |
| 7115 | while (cur_param > first_param) { |
| 7116 | cur_param--; |
| 7117 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7118 | /* let's see if this is the appsession parameter */ |
| 7119 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7120 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7121 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7122 | /* Cool... it's the right one */ |
| 7123 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7124 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7125 | if (value_len > 0) { |
| 7126 | manage_client_side_appsession(t, cur_param, value_len); |
| 7127 | } |
| 7128 | break; |
| 7129 | } |
| 7130 | next_param = cur_param; |
| 7131 | } |
| 7132 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7133 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7134 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7135 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7136 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7137 | } |
| 7138 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7139 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7140 | * 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] | 7141 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7142 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7143 | * 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] | 7144 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7145 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7146 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7147 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7148 | 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] | 7149 | { |
| 7150 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7151 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7152 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7153 | if (!uri_auth) |
| 7154 | return 0; |
| 7155 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7156 | 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] | 7157 | return 0; |
| 7158 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7159 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7160 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7161 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7162 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7163 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7164 | return 0; |
| 7165 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7166 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7167 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7168 | /* the URI is in h */ |
| 7169 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7170 | return 0; |
| 7171 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7172 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7173 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7174 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7175 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7176 | break; |
| 7177 | } |
| 7178 | h++; |
| 7179 | } |
| 7180 | |
| 7181 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7182 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7183 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7184 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7185 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7186 | break; |
| 7187 | } |
| 7188 | h++; |
| 7189 | } |
| 7190 | } |
| 7191 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7192 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7193 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7194 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7195 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7196 | break; |
| 7197 | } |
| 7198 | h++; |
| 7199 | } |
| 7200 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7201 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7202 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7203 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7204 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7205 | h += 4; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7206 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7207 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7208 | si->applet.ctx.stats.st_code = i; |
| 7209 | break; |
| 7210 | } |
| 7211 | } |
| 7212 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7213 | break; |
| 7214 | } |
| 7215 | h++; |
| 7216 | } |
| 7217 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7218 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7219 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7220 | return 1; |
| 7221 | } |
| 7222 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7223 | /* |
| 7224 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7225 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 7226 | * assume that msg->sol = buf->p + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7227 | */ |
| 7228 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7229 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7230 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7231 | { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 7232 | if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */ |
| 7233 | int len1 = buf->size - msg->som - (buf->p - buf->data); |
| 7234 | es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size; |
| 7235 | memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf))); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7236 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7237 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7238 | } |
| 7239 | else { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 7240 | es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som); |
| 7241 | memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf))); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7242 | } |
| 7243 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7244 | if (msg->err_pos >= 0) |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 7245 | es->pos = msg->err_pos - msg->som - (buf->p - buf->data); |
| 7246 | else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som)) |
| 7247 | es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7248 | else |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame^] | 7249 | es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7250 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7251 | es->when = date; // user-visible date |
| 7252 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7253 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7254 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7255 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7256 | es->state = state; |
| 7257 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7258 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7259 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7260 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7261 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7262 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7263 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7264 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7265 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7266 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7267 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7268 | * -1. |
| 7269 | * 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] | 7270 | */ |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7271 | unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen, |
| 7272 | struct hdr_idx *idx, int occ, |
| 7273 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7274 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7275 | struct hdr_ctx local_ctx; |
| 7276 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7277 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7278 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7279 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7280 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7281 | if (!ctx) { |
| 7282 | local_ctx.idx = 0; |
| 7283 | ctx = &local_ctx; |
| 7284 | } |
| 7285 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7286 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7287 | /* search from the beginning */ |
| 7288 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7289 | occ--; |
| 7290 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7291 | *vptr = ctx->line + ctx->val; |
| 7292 | *vlen = ctx->vlen; |
| 7293 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7294 | } |
| 7295 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7296 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7297 | } |
| 7298 | |
| 7299 | /* negative occurrence, we scan all the list then walk back */ |
| 7300 | if (-occ > MAX_HDR_HISTORY) |
| 7301 | return 0; |
| 7302 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7303 | found = hist_ptr = 0; |
| 7304 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
| 7305 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7306 | len_hist[hist_ptr] = ctx->vlen; |
| 7307 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7308 | hist_ptr = 0; |
| 7309 | found++; |
| 7310 | } |
| 7311 | if (-occ > found) |
| 7312 | return 0; |
| 7313 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7314 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7315 | */ |
| 7316 | hist_ptr += occ; |
| 7317 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7318 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7319 | *vptr = ptr_hist[hist_ptr]; |
| 7320 | *vlen = len_hist[hist_ptr]; |
| 7321 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7322 | } |
| 7323 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7324 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7325 | * Print a debug line with a header |
| 7326 | */ |
| 7327 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7328 | { |
| 7329 | int len, max; |
| 7330 | 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] | 7331 | 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] | 7332 | max = end - start; |
| 7333 | UBOUND(max, sizeof(trash) - len - 1); |
| 7334 | len += strlcpy2(trash + len, start, max + 1); |
| 7335 | trash[len++] = '\n'; |
Willy Tarreau | 2133782 | 2012-04-29 14:11:38 +0200 | [diff] [blame] | 7336 | if (write(1, trash, len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7337 | } |
| 7338 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7339 | /* |
| 7340 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7341 | * the required fields are properly allocated and that we only need to (re)init |
| 7342 | * them. This should be used before processing any new request. |
| 7343 | */ |
| 7344 | void http_init_txn(struct session *s) |
| 7345 | { |
| 7346 | struct http_txn *txn = &s->txn; |
| 7347 | struct proxy *fe = s->fe; |
| 7348 | |
| 7349 | txn->flags = 0; |
| 7350 | txn->status = -1; |
| 7351 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7352 | global.req_count++; |
| 7353 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7354 | txn->cookie_first_date = 0; |
| 7355 | txn->cookie_last_date = 0; |
| 7356 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7357 | txn->req.flags = 0; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7358 | txn->req.sol = txn->req.eol = NULL; |
| 7359 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7360 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7361 | txn->rsp.flags = 0; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7362 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7363 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7364 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7365 | txn->req.chunk_len = 0LL; |
| 7366 | txn->req.body_len = 0LL; |
| 7367 | txn->rsp.chunk_len = 0LL; |
| 7368 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7369 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7370 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7371 | |
| 7372 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7373 | |
| 7374 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7375 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7376 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7377 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7378 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7379 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7380 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7381 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7382 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7383 | |
| 7384 | if (txn->hdr_idx.v) |
| 7385 | hdr_idx_init(&txn->hdr_idx); |
| 7386 | } |
| 7387 | |
| 7388 | /* to be used at the end of a transaction */ |
| 7389 | void http_end_txn(struct session *s) |
| 7390 | { |
| 7391 | struct http_txn *txn = &s->txn; |
| 7392 | |
| 7393 | /* these ones will have been dynamically allocated */ |
| 7394 | pool_free2(pool2_requri, txn->uri); |
| 7395 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7396 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7397 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7398 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7399 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7400 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7401 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7402 | txn->uri = NULL; |
| 7403 | txn->srv_cookie = NULL; |
| 7404 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7405 | |
| 7406 | if (txn->req.cap) { |
| 7407 | struct cap_hdr *h; |
| 7408 | for (h = s->fe->req_cap; h; h = h->next) |
| 7409 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7410 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7411 | } |
| 7412 | |
| 7413 | if (txn->rsp.cap) { |
| 7414 | struct cap_hdr *h; |
| 7415 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7416 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7417 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7418 | } |
| 7419 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7420 | } |
| 7421 | |
| 7422 | /* to be used at the end of a transaction to prepare a new one */ |
| 7423 | void http_reset_txn(struct session *s) |
| 7424 | { |
| 7425 | http_end_txn(s); |
| 7426 | http_init_txn(s); |
| 7427 | |
| 7428 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7429 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7430 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7431 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7432 | /* re-init store persistence */ |
| 7433 | s->store_count = 0; |
| 7434 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7435 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7436 | |
| 7437 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7438 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7439 | /* We must trim any excess data from the response buffer, because we |
| 7440 | * may have blocked an invalid response from a server that we don't |
| 7441 | * want to accidentely forward once we disable the analysers, nor do |
| 7442 | * we want those data to come along with next response. A typical |
| 7443 | * example of such data would be from a buggy server responding to |
| 7444 | * a HEAD with some data, or sending more than the advertised |
| 7445 | * content-length. |
| 7446 | */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 7447 | if (unlikely(s->rep->i)) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 7448 | s->rep->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7449 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7450 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7451 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7452 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7453 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7454 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7455 | |
| 7456 | s->req->rex = TICK_ETERNITY; |
| 7457 | s->req->wex = TICK_ETERNITY; |
| 7458 | s->req->analyse_exp = TICK_ETERNITY; |
| 7459 | s->rep->rex = TICK_ETERNITY; |
| 7460 | s->rep->wex = TICK_ETERNITY; |
| 7461 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7462 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7463 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7464 | void free_http_req_rules(struct list *r) { |
| 7465 | struct http_req_rule *tr, *pr; |
| 7466 | |
| 7467 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7468 | LIST_DEL(&pr->list); |
| 7469 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7470 | free(pr->http_auth.realm); |
| 7471 | |
| 7472 | free(pr); |
| 7473 | } |
| 7474 | } |
| 7475 | |
| 7476 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7477 | { |
| 7478 | struct http_req_rule *rule; |
| 7479 | int cur_arg; |
| 7480 | |
| 7481 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7482 | if (!rule) { |
| 7483 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7484 | return NULL; |
| 7485 | } |
| 7486 | |
| 7487 | if (!*args[0]) { |
| 7488 | goto req_error_parsing; |
| 7489 | } else if (!strcmp(args[0], "allow")) { |
| 7490 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7491 | cur_arg = 1; |
| 7492 | } else if (!strcmp(args[0], "deny")) { |
| 7493 | rule->action = HTTP_REQ_ACT_DENY; |
| 7494 | cur_arg = 1; |
| 7495 | } else if (!strcmp(args[0], "auth")) { |
| 7496 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7497 | cur_arg = 1; |
| 7498 | |
| 7499 | while(*args[cur_arg]) { |
| 7500 | if (!strcmp(args[cur_arg], "realm")) { |
| 7501 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7502 | cur_arg+=2; |
| 7503 | continue; |
| 7504 | } else |
| 7505 | break; |
| 7506 | } |
| 7507 | } else { |
| 7508 | req_error_parsing: |
| 7509 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7510 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7511 | return NULL; |
| 7512 | } |
| 7513 | |
| 7514 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7515 | struct acl_cond *cond; |
| 7516 | |
| 7517 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7518 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7519 | file, linenum, args[0]); |
| 7520 | return NULL; |
| 7521 | } |
| 7522 | rule->cond = cond; |
| 7523 | } |
| 7524 | else if (*args[cur_arg]) { |
| 7525 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7526 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7527 | file, linenum, args[0], args[cur_arg]); |
| 7528 | return NULL; |
| 7529 | } |
| 7530 | |
| 7531 | return rule; |
| 7532 | } |
| 7533 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7534 | /************************************************************************/ |
| 7535 | /* The code below is dedicated to ACL parsing and matching */ |
| 7536 | /************************************************************************/ |
| 7537 | |
| 7538 | |
| 7539 | |
| 7540 | |
| 7541 | /* 1. Check on METHOD |
| 7542 | * We use the pre-parsed method if it is known, and store its number as an |
| 7543 | * integer. If it is unknown, we use the pointer and the length. |
| 7544 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7545 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7546 | { |
| 7547 | int len, meth; |
| 7548 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7549 | len = strlen(*text); |
| 7550 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7551 | |
| 7552 | pattern->val.i = meth; |
| 7553 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7554 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7555 | if (!pattern->ptr.str) |
| 7556 | return 0; |
| 7557 | pattern->len = len; |
| 7558 | } |
| 7559 | return 1; |
| 7560 | } |
| 7561 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7562 | /* This function fetches the method of current HTTP request and stores |
| 7563 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7564 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7565 | * in <len> and <ptr> is NULL ; |
| 7566 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7567 | * <len> to its length. |
| 7568 | * This is intended to be used with acl_match_meth() only. |
| 7569 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7570 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7571 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7572 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7573 | { |
| 7574 | int meth; |
| 7575 | struct http_txn *txn = l7; |
| 7576 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7577 | if (!txn) |
| 7578 | return 0; |
| 7579 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7580 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7581 | return 0; |
| 7582 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7583 | meth = txn->meth; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7584 | temp_pattern.data.str.len = meth; |
| 7585 | temp_pattern.data.str.str = NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7586 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7587 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7588 | /* ensure the indexes are not affected */ |
| 7589 | return 0; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7590 | temp_pattern.data.str.len = txn->req.sl.rq.m_l; |
| 7591 | temp_pattern.data.str.str = txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7592 | } |
| 7593 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7594 | return 1; |
| 7595 | } |
| 7596 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7597 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7598 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7599 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7600 | int icase; |
| 7601 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7602 | |
| 7603 | if (temp_pattern.data.str.str == NULL) { |
| 7604 | /* well-known method */ |
| 7605 | if (temp_pattern.data.str.len == pattern->val.i) |
| 7606 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7607 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7608 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7609 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7610 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7611 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7612 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7613 | |
| 7614 | /* Other method, we must compare the strings */ |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7615 | if (pattern->len != temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7616 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7617 | |
| 7618 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7619 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) || |
| 7620 | (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7621 | return ACL_PAT_FAIL; |
| 7622 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7623 | } |
| 7624 | |
| 7625 | /* 2. Check on Request/Status Version |
| 7626 | * We simply compare strings here. |
| 7627 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7628 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7629 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7630 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7631 | if (!pattern->ptr.str) |
| 7632 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7633 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7634 | return 1; |
| 7635 | } |
| 7636 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7637 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7638 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7639 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7640 | { |
| 7641 | struct http_txn *txn = l7; |
| 7642 | char *ptr; |
| 7643 | int len; |
| 7644 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7645 | if (!txn) |
| 7646 | return 0; |
| 7647 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7648 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7649 | return 0; |
| 7650 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7651 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7652 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7653 | |
| 7654 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7655 | if (len <= 0) |
| 7656 | return 0; |
| 7657 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7658 | temp_pattern.data.str.str = ptr; |
| 7659 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7660 | |
| 7661 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7662 | return 1; |
| 7663 | } |
| 7664 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7665 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7666 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7667 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7668 | { |
| 7669 | struct http_txn *txn = l7; |
| 7670 | char *ptr; |
| 7671 | int len; |
| 7672 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7673 | if (!txn) |
| 7674 | return 0; |
| 7675 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7676 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7677 | return 0; |
| 7678 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7679 | len = txn->rsp.sl.st.v_l; |
| 7680 | ptr = txn->rsp.sol; |
| 7681 | |
| 7682 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7683 | if (len <= 0) |
| 7684 | return 0; |
| 7685 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7686 | temp_pattern.data.str.str = ptr; |
| 7687 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7688 | |
| 7689 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7690 | return 1; |
| 7691 | } |
| 7692 | |
| 7693 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7694 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7695 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7696 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7697 | { |
| 7698 | struct http_txn *txn = l7; |
| 7699 | char *ptr; |
| 7700 | int len; |
| 7701 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7702 | if (!txn) |
| 7703 | return 0; |
| 7704 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7705 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7706 | return 0; |
| 7707 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7708 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7709 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7710 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7711 | temp_pattern.data.integer = __strl2ui(ptr, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7712 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7713 | return 1; |
| 7714 | } |
| 7715 | |
| 7716 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7717 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7718 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7719 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7720 | { |
| 7721 | struct http_txn *txn = l7; |
| 7722 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7723 | if (!txn) |
| 7724 | return 0; |
| 7725 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7726 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7727 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7728 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7729 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7730 | /* ensure the indexes are not affected */ |
| 7731 | return 0; |
| 7732 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7733 | temp_pattern.data.str.len = txn->req.sl.rq.u_l; |
| 7734 | temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7735 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7736 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7737 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7738 | return 1; |
| 7739 | } |
| 7740 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7741 | static int |
| 7742 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7743 | struct acl_expr *expr, struct acl_test *test) |
| 7744 | { |
| 7745 | struct http_txn *txn = l7; |
| 7746 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7747 | if (!txn) |
| 7748 | return 0; |
| 7749 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7750 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7751 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7752 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7753 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7754 | /* ensure the indexes are not affected */ |
| 7755 | return 0; |
| 7756 | |
| 7757 | /* Parse HTTP request */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7758 | url2sa(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] | 7759 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 7760 | return 0; |
| 7761 | temp_pattern.type = PATTERN_TYPE_IP; |
| 7762 | temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7763 | |
| 7764 | /* |
| 7765 | * If we are parsing url in frontend space, we prepare backend stage |
| 7766 | * to not parse again the same url ! optimization lazyness... |
| 7767 | */ |
| 7768 | if (px->options & PR_O_HTTP_PROXY) |
| 7769 | l4->flags |= SN_ADDR_SET; |
| 7770 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7771 | test->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7772 | return 1; |
| 7773 | } |
| 7774 | |
| 7775 | static int |
| 7776 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7777 | struct acl_expr *expr, struct acl_test *test) |
| 7778 | { |
| 7779 | struct http_txn *txn = l7; |
| 7780 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7781 | if (!txn) |
| 7782 | return 0; |
| 7783 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7784 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7785 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7786 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7787 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7788 | /* ensure the indexes are not affected */ |
| 7789 | return 0; |
| 7790 | |
| 7791 | /* Same optimization as url_ip */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7792 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7793 | temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7794 | |
| 7795 | if (px->options & PR_O_HTTP_PROXY) |
| 7796 | l4->flags |= SN_ADDR_SET; |
| 7797 | |
| 7798 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7799 | return 1; |
| 7800 | } |
| 7801 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7802 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7803 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7804 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7805 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7806 | acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7807 | struct acl_expr *expr, struct acl_test *test) |
| 7808 | { |
| 7809 | struct http_txn *txn = l7; |
| 7810 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7811 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7812 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7813 | if (!txn) |
| 7814 | return 0; |
| 7815 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7816 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7817 | /* search for header from the beginning */ |
| 7818 | ctx->idx = 0; |
| 7819 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7820 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7821 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7822 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7823 | temp_pattern.data.str.str = (char *)ctx->line + ctx->val; |
| 7824 | temp_pattern.data.str.len = ctx->vlen; |
| 7825 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7826 | return 1; |
| 7827 | } |
| 7828 | |
| 7829 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7830 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7831 | return 0; |
| 7832 | } |
| 7833 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7834 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7835 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7836 | struct acl_expr *expr, struct acl_test *test) |
| 7837 | { |
| 7838 | struct http_txn *txn = l7; |
| 7839 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7840 | if (!txn) |
| 7841 | return 0; |
| 7842 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7843 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7844 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7845 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7846 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7847 | /* ensure the indexes are not affected */ |
| 7848 | return 0; |
| 7849 | |
| 7850 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7851 | } |
| 7852 | |
| 7853 | static int |
| 7854 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7855 | struct acl_expr *expr, struct acl_test *test) |
| 7856 | { |
| 7857 | struct http_txn *txn = l7; |
| 7858 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7859 | if (!txn) |
| 7860 | return 0; |
| 7861 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7862 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7863 | return 0; |
| 7864 | |
| 7865 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 7866 | } |
| 7867 | |
| 7868 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7869 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7870 | */ |
| 7871 | static int |
| 7872 | acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7873 | struct acl_expr *expr, struct acl_test *test) |
| 7874 | { |
| 7875 | struct http_txn *txn = l7; |
| 7876 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7877 | struct hdr_ctx ctx; |
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 | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7880 | if (!txn) |
| 7881 | return 0; |
| 7882 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7883 | ctx.idx = 0; |
| 7884 | cnt = 0; |
| 7885 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7886 | cnt++; |
| 7887 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7888 | temp_pattern.data.integer = cnt; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7889 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7890 | return 1; |
| 7891 | } |
| 7892 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7893 | static int |
| 7894 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7895 | struct acl_expr *expr, struct acl_test *test) |
| 7896 | { |
| 7897 | struct http_txn *txn = l7; |
| 7898 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7899 | if (!txn) |
| 7900 | return 0; |
| 7901 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7902 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7903 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7904 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7905 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7906 | /* ensure the indexes are not affected */ |
| 7907 | return 0; |
| 7908 | |
| 7909 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 7910 | } |
| 7911 | |
| 7912 | static int |
| 7913 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7914 | struct acl_expr *expr, struct acl_test *test) |
| 7915 | { |
| 7916 | struct http_txn *txn = l7; |
| 7917 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7918 | if (!txn) |
| 7919 | return 0; |
| 7920 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7921 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7922 | return 0; |
| 7923 | |
| 7924 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7925 | } |
| 7926 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7927 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7928 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7929 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7930 | */ |
| 7931 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7932 | acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7933 | struct acl_expr *expr, struct acl_test *test) |
| 7934 | { |
| 7935 | struct http_txn *txn = l7; |
| 7936 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7937 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7938 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7939 | if (!txn) |
| 7940 | return 0; |
| 7941 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7942 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7943 | /* search for header from the beginning */ |
| 7944 | ctx->idx = 0; |
| 7945 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7946 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7947 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7948 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7949 | temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7950 | return 1; |
| 7951 | } |
| 7952 | |
| 7953 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7954 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7955 | return 0; |
| 7956 | } |
| 7957 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7958 | static int |
| 7959 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7960 | struct acl_expr *expr, struct acl_test *test) |
| 7961 | { |
| 7962 | struct http_txn *txn = l7; |
| 7963 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7964 | if (!txn) |
| 7965 | return 0; |
| 7966 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7967 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7968 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7969 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7970 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7971 | /* ensure the indexes are not affected */ |
| 7972 | return 0; |
| 7973 | |
| 7974 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7975 | } |
| 7976 | |
| 7977 | static int |
| 7978 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7979 | struct acl_expr *expr, struct acl_test *test) |
| 7980 | { |
| 7981 | struct http_txn *txn = l7; |
| 7982 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7983 | if (!txn) |
| 7984 | return 0; |
| 7985 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7986 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7987 | return 0; |
| 7988 | |
| 7989 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 7990 | } |
| 7991 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7992 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7993 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7994 | */ |
| 7995 | static int |
| 7996 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7997 | struct acl_expr *expr, struct acl_test *test) |
| 7998 | { |
| 7999 | struct http_txn *txn = l7; |
| 8000 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8001 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 8002 | |
| 8003 | if (!txn) |
| 8004 | return 0; |
| 8005 | |
| 8006 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8007 | /* search for header from the beginning */ |
| 8008 | ctx->idx = 0; |
| 8009 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8010 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8011 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8012 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8013 | /* Same optimization as url_ip */ |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8014 | temp_pattern.type = PATTERN_TYPE_IP; |
| 8015 | if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip)) |
| 8016 | return 1; |
| 8017 | /* Dods not look like an IP address, let's fetch next one */ |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8018 | } |
| 8019 | |
| 8020 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8021 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8022 | return 0; |
| 8023 | } |
| 8024 | |
| 8025 | static int |
| 8026 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8027 | struct acl_expr *expr, struct acl_test *test) |
| 8028 | { |
| 8029 | struct http_txn *txn = l7; |
| 8030 | |
| 8031 | if (!txn) |
| 8032 | return 0; |
| 8033 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8034 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8035 | return 0; |
| 8036 | |
| 8037 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8038 | /* ensure the indexes are not affected */ |
| 8039 | return 0; |
| 8040 | |
| 8041 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8042 | } |
| 8043 | |
| 8044 | static int |
| 8045 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8046 | struct acl_expr *expr, struct acl_test *test) |
| 8047 | { |
| 8048 | struct http_txn *txn = l7; |
| 8049 | |
| 8050 | if (!txn) |
| 8051 | return 0; |
| 8052 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8053 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8054 | return 0; |
| 8055 | |
| 8056 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8057 | } |
| 8058 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8059 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8060 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8061 | */ |
| 8062 | static int |
| 8063 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8064 | struct acl_expr *expr, struct acl_test *test) |
| 8065 | { |
| 8066 | struct http_txn *txn = l7; |
| 8067 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8068 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8069 | if (!txn) |
| 8070 | return 0; |
| 8071 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8072 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8073 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8074 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8075 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8076 | /* ensure the indexes are not affected */ |
| 8077 | return 0; |
| 8078 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8079 | end = 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] | 8080 | ptr = http_get_path(txn); |
| 8081 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8082 | return 0; |
| 8083 | |
| 8084 | /* OK, we got the '/' ! */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8085 | temp_pattern.data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8086 | |
| 8087 | while (ptr < end && *ptr != '?') |
| 8088 | ptr++; |
| 8089 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8090 | temp_pattern.data.str.len = ptr - temp_pattern.data.str.str; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8091 | |
| 8092 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8093 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8094 | return 1; |
| 8095 | } |
| 8096 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8097 | static int |
| 8098 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8099 | struct acl_expr *expr, struct acl_test *test) |
| 8100 | { |
| 8101 | struct buffer *req = s->req; |
| 8102 | struct http_txn *txn = &s->txn; |
| 8103 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8104 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8105 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8106 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8107 | */ |
| 8108 | |
| 8109 | if (!s || !req) |
| 8110 | return 0; |
| 8111 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8112 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8113 | /* Already decoded as OK */ |
| 8114 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8115 | return 1; |
| 8116 | } |
| 8117 | |
| 8118 | /* Try to decode HTTP request */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 8119 | if (likely(msg->next < req->i)) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8120 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8121 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8122 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8123 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8124 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8125 | return 1; |
| 8126 | } |
| 8127 | /* wait for final state */ |
| 8128 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8129 | return 0; |
| 8130 | } |
| 8131 | |
| 8132 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8133 | * perform so that further checks can rely on HTTP tests. |
| 8134 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8135 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8136 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8137 | s->flags |= SN_REDIRECTABLE; |
| 8138 | |
| 8139 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8140 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8141 | return 1; |
| 8142 | } |
| 8143 | |
| 8144 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8145 | return 1; |
| 8146 | } |
| 8147 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8148 | /* return a valid test if the current request is the first one on the connection */ |
| 8149 | static int |
| 8150 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8151 | struct acl_expr *expr, struct acl_test *test) |
| 8152 | { |
| 8153 | if (!s) |
| 8154 | return 0; |
| 8155 | |
| 8156 | if (s->txn.flags & TX_NOT_FIRST) |
| 8157 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8158 | else |
| 8159 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8160 | |
| 8161 | return 1; |
| 8162 | } |
| 8163 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8164 | static int |
| 8165 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8166 | struct acl_expr *expr, struct acl_test *test) |
| 8167 | { |
| 8168 | |
| 8169 | if (!s) |
| 8170 | return 0; |
| 8171 | |
| 8172 | if (!get_http_auth(s)) |
| 8173 | return 0; |
| 8174 | |
| 8175 | test->ctx.a[0] = expr->arg.ul; |
| 8176 | test->ctx.a[1] = s->txn.auth.user; |
| 8177 | test->ctx.a[2] = s->txn.auth.pass; |
| 8178 | |
| 8179 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8180 | |
| 8181 | return 1; |
| 8182 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8183 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8184 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8185 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8186 | * the cookie value is returned into *value and *value_l, and the function |
| 8187 | * returns a pointer to the next pointer to search from if the value was found. |
| 8188 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8189 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8190 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8191 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8192 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8193 | * list is expected. |
| 8194 | */ |
| 8195 | static char * |
| 8196 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8197 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8198 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8199 | { |
| 8200 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8201 | char *next; |
| 8202 | |
| 8203 | /* we search at least a cookie name followed by an equal, and more |
| 8204 | * generally something like this : |
| 8205 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8206 | */ |
| 8207 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8208 | /* Iterate through all cookies on this line */ |
| 8209 | |
| 8210 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8211 | att_beg++; |
| 8212 | |
| 8213 | /* find att_end : this is the first character after the last non |
| 8214 | * space before the equal. It may be equal to hdr_end. |
| 8215 | */ |
| 8216 | equal = att_end = att_beg; |
| 8217 | |
| 8218 | while (equal < hdr_end) { |
| 8219 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8220 | break; |
| 8221 | if (http_is_spht[(unsigned char)*equal++]) |
| 8222 | continue; |
| 8223 | att_end = equal; |
| 8224 | } |
| 8225 | |
| 8226 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8227 | * is between <att_beg> and <equal>, both may be identical. |
| 8228 | */ |
| 8229 | |
| 8230 | /* look for end of cookie if there is an equal sign */ |
| 8231 | if (equal < hdr_end && *equal == '=') { |
| 8232 | /* look for the beginning of the value */ |
| 8233 | val_beg = equal + 1; |
| 8234 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8235 | val_beg++; |
| 8236 | |
| 8237 | /* find the end of the value, respecting quotes */ |
| 8238 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8239 | |
| 8240 | /* make val_end point to the first white space or delimitor after the value */ |
| 8241 | val_end = next; |
| 8242 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8243 | val_end--; |
| 8244 | } else { |
| 8245 | val_beg = val_end = next = equal; |
| 8246 | } |
| 8247 | |
| 8248 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8249 | * they will automatically be removed if a header before them is removed, |
| 8250 | * since they're supposed to be linked together. |
| 8251 | */ |
| 8252 | if (*att_beg == '$') |
| 8253 | continue; |
| 8254 | |
| 8255 | /* Ignore cookies with no equal sign */ |
| 8256 | if (equal == next) |
| 8257 | continue; |
| 8258 | |
| 8259 | /* Now we have the cookie name between att_beg and att_end, and |
| 8260 | * its value between val_beg and val_end. |
| 8261 | */ |
| 8262 | |
| 8263 | if (att_end - att_beg == cookie_name_l && |
| 8264 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8265 | /* let's return this value and indicate where to go on from */ |
| 8266 | *value = val_beg; |
| 8267 | *value_l = val_end - val_beg; |
| 8268 | return next + 1; |
| 8269 | } |
| 8270 | |
| 8271 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8272 | if (!list) |
| 8273 | break; |
| 8274 | } |
| 8275 | |
| 8276 | return NULL; |
| 8277 | } |
| 8278 | |
| 8279 | /* Iterate over all cookies present in a request. The context is stored in |
| 8280 | * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the |
| 8281 | * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is |
| 8282 | * non-null, then multiple cookies may be parsed on the same line. |
| 8283 | * The cookie name is in expr->arg and the name length in expr->arg_len. |
| 8284 | */ |
| 8285 | static int |
| 8286 | acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8287 | const char *hdr_name, int hdr_name_len, int multi, |
| 8288 | struct acl_expr *expr, struct acl_test *test) |
| 8289 | { |
| 8290 | struct http_txn *txn = l7; |
| 8291 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8292 | struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2]; |
| 8293 | |
| 8294 | if (!txn) |
| 8295 | return 0; |
| 8296 | |
| 8297 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) { |
| 8298 | /* search for the header from the beginning, we must first initialize |
| 8299 | * the search parameters. |
| 8300 | */ |
| 8301 | test->ctx.a[0] = NULL; |
| 8302 | ctx->idx = 0; |
| 8303 | } |
| 8304 | |
| 8305 | while (1) { |
| 8306 | /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8307 | if (!test->ctx.a[0]) { |
| 8308 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8309 | goto out; |
| 8310 | |
| 8311 | if (ctx->vlen < expr->arg_len + 1) |
| 8312 | continue; |
| 8313 | |
| 8314 | test->ctx.a[0] = ctx->line + ctx->val; |
| 8315 | test->ctx.a[1] = test->ctx.a[0] + ctx->vlen; |
| 8316 | } |
| 8317 | |
| 8318 | test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1], |
| 8319 | expr->arg.str, expr->arg_len, multi, |
| 8320 | &temp_pattern.data.str.str, |
| 8321 | &temp_pattern.data.str.len); |
| 8322 | if (test->ctx.a[0]) { |
| 8323 | /* one value was returned into temp_pattern.data.str.{str,len} */ |
| 8324 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8325 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8326 | return 1; |
| 8327 | } |
| 8328 | } |
| 8329 | |
| 8330 | out: |
| 8331 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8332 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8333 | return 0; |
| 8334 | } |
| 8335 | |
| 8336 | static int |
| 8337 | acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8338 | struct acl_expr *expr, struct acl_test *test) |
| 8339 | { |
| 8340 | struct http_txn *txn = l7; |
| 8341 | |
| 8342 | if (!txn) |
| 8343 | return 0; |
| 8344 | |
| 8345 | if (txn->req.msg_state < HTTP_MSG_BODY) |
| 8346 | return 0; |
| 8347 | |
| 8348 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8349 | /* ensure the indexes are not affected */ |
| 8350 | return 0; |
| 8351 | |
| 8352 | /* The Cookie header allows multiple cookies on the same line */ |
| 8353 | return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test); |
| 8354 | } |
| 8355 | |
| 8356 | static int |
| 8357 | acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8358 | struct acl_expr *expr, struct acl_test *test) |
| 8359 | { |
| 8360 | struct http_txn *txn = l7; |
| 8361 | |
| 8362 | if (!txn) |
| 8363 | return 0; |
| 8364 | |
| 8365 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8366 | return 0; |
| 8367 | |
| 8368 | /* The Set-Cookie header allows only one cookie on the same line */ |
| 8369 | return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test); |
| 8370 | } |
| 8371 | |
| 8372 | /* Iterate over all cookies present in a request to count how many occurrences |
| 8373 | * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then |
| 8374 | * multiple cookies may be parsed on the same line. |
| 8375 | */ |
| 8376 | static int |
| 8377 | acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8378 | const char *hdr_name, int hdr_name_len, int multi, |
| 8379 | struct acl_expr *expr, struct acl_test *test) |
| 8380 | { |
| 8381 | struct http_txn *txn = l7; |
| 8382 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8383 | struct hdr_ctx ctx; |
| 8384 | int cnt; |
| 8385 | char *val_beg, *val_end; |
| 8386 | |
| 8387 | if (!txn) |
| 8388 | return 0; |
| 8389 | |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8390 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8391 | ctx.idx = 0; |
| 8392 | cnt = 0; |
| 8393 | |
| 8394 | while (1) { |
| 8395 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8396 | if (!val_beg) { |
| 8397 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8398 | break; |
| 8399 | |
| 8400 | if (ctx.vlen < expr->arg_len + 1) |
| 8401 | continue; |
| 8402 | |
| 8403 | val_beg = ctx.line + ctx.val; |
| 8404 | val_end = val_beg + ctx.vlen; |
| 8405 | } |
| 8406 | |
| 8407 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
| 8408 | expr->arg.str, expr->arg_len, multi, |
| 8409 | &temp_pattern.data.str.str, |
| 8410 | &temp_pattern.data.str.len))) { |
| 8411 | cnt++; |
| 8412 | } |
| 8413 | } |
| 8414 | |
| 8415 | temp_pattern.data.integer = cnt; |
| 8416 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8417 | return 1; |
| 8418 | } |
| 8419 | |
| 8420 | static int |
| 8421 | acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8422 | struct acl_expr *expr, struct acl_test *test) |
| 8423 | { |
| 8424 | struct http_txn *txn = l7; |
| 8425 | |
| 8426 | if (!txn) |
| 8427 | return 0; |
| 8428 | |
| 8429 | if (txn->req.msg_state < HTTP_MSG_BODY) |
| 8430 | return 0; |
| 8431 | |
| 8432 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8433 | /* ensure the indexes are not affected */ |
| 8434 | return 0; |
| 8435 | |
| 8436 | /* The Cookie header allows multiple cookies on the same line */ |
| 8437 | return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test); |
| 8438 | } |
| 8439 | |
| 8440 | static int |
| 8441 | acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8442 | struct acl_expr *expr, struct acl_test *test) |
| 8443 | { |
| 8444 | struct http_txn *txn = l7; |
| 8445 | |
| 8446 | if (!txn) |
| 8447 | return 0; |
| 8448 | |
| 8449 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8450 | return 0; |
| 8451 | |
| 8452 | /* The Set-Cookie header allows only one cookie on the same line */ |
| 8453 | return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test); |
| 8454 | } |
| 8455 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8456 | /************************************************************************/ |
| 8457 | /* All supported keywords must be declared here. */ |
| 8458 | /************************************************************************/ |
| 8459 | |
| 8460 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8461 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8462 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8463 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8464 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8465 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8466 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8467 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8468 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8469 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8470 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8471 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8472 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8473 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8474 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8475 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8476 | { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8477 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8478 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8479 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8480 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8481 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8482 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8483 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8484 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8485 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8486 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8487 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8488 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8489 | { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8490 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8491 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8492 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8493 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8494 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8495 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8496 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8497 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8498 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8499 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8500 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8501 | { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8502 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8503 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8504 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8505 | { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8506 | { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8507 | { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8508 | { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8509 | { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8510 | { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8511 | { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8512 | { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
| 8513 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 8514 | |
| 8515 | { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
| 8516 | { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8517 | { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8518 | { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8519 | { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8520 | { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8521 | { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8522 | { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
| 8523 | { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 8524 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8525 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8526 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8527 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8528 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8529 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8530 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8531 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8532 | { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8533 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8534 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8535 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8536 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8537 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8538 | }}; |
| 8539 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8540 | /************************************************************************/ |
| 8541 | /* The code below is dedicated to pattern fetching and matching */ |
| 8542 | /************************************************************************/ |
| 8543 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8544 | /* Returns the last occurrence of specified header. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8545 | static int |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8546 | pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8547 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8548 | { |
| 8549 | struct http_txn *txn = l7; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8550 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8551 | return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, |
| 8552 | -1, NULL, &data->str.str, &data->str.len); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8553 | } |
| 8554 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8555 | /* |
| 8556 | * Given a path string and its length, find the position of beginning of the |
| 8557 | * query string. Returns NULL if no query string is found in the path. |
| 8558 | * |
| 8559 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8560 | * |
| 8561 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8562 | */ |
| 8563 | static inline char *find_query_string(char *path, size_t path_l) |
| 8564 | { |
| 8565 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8566 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8567 | p = memchr(path, '?', path_l); |
| 8568 | return p ? p + 1 : NULL; |
| 8569 | } |
| 8570 | |
| 8571 | static inline int is_param_delimiter(char c) |
| 8572 | { |
| 8573 | return c == '&' || c == ';'; |
| 8574 | } |
| 8575 | |
| 8576 | /* |
| 8577 | * Given a url parameter, find the starting position of the first occurence, |
| 8578 | * or NULL if the parameter is not found. |
| 8579 | * |
| 8580 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8581 | * the function will return query_string+8. |
| 8582 | */ |
| 8583 | static char* |
| 8584 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8585 | char* url_param_name, size_t url_param_name_l) |
| 8586 | { |
| 8587 | char *pos, *last; |
| 8588 | |
| 8589 | pos = query_string; |
| 8590 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8591 | |
| 8592 | while (pos <= last) { |
| 8593 | if (pos[url_param_name_l] == '=') { |
| 8594 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8595 | return pos; |
| 8596 | pos += url_param_name_l + 1; |
| 8597 | } |
| 8598 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8599 | pos++; |
| 8600 | pos++; |
| 8601 | } |
| 8602 | return NULL; |
| 8603 | } |
| 8604 | |
| 8605 | /* |
| 8606 | * Given a url parameter name, returns its value and size into *value and |
| 8607 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8608 | * zero is returned and value/value_l are not touched. |
| 8609 | */ |
| 8610 | static int |
| 8611 | find_url_param_value(char* path, size_t path_l, |
| 8612 | char* url_param_name, size_t url_param_name_l, |
| 8613 | char** value, size_t* value_l) |
| 8614 | { |
| 8615 | char *query_string, *qs_end; |
| 8616 | char *arg_start; |
| 8617 | char *value_start, *value_end; |
| 8618 | |
| 8619 | query_string = find_query_string(path, path_l); |
| 8620 | if (!query_string) |
| 8621 | return 0; |
| 8622 | |
| 8623 | qs_end = path + path_l; |
| 8624 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8625 | url_param_name, url_param_name_l); |
| 8626 | if (!arg_start) |
| 8627 | return 0; |
| 8628 | |
| 8629 | value_start = arg_start + url_param_name_l + 1; |
| 8630 | value_end = value_start; |
| 8631 | |
| 8632 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8633 | value_end++; |
| 8634 | |
| 8635 | *value = value_start; |
| 8636 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8637 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8638 | } |
| 8639 | |
| 8640 | static int |
| 8641 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8642 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8643 | { |
| 8644 | struct http_txn *txn = l7; |
| 8645 | struct http_msg *msg = &txn->req; |
| 8646 | char *url_param_value; |
| 8647 | size_t url_param_value_l; |
| 8648 | |
| 8649 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8650 | arg_p->data.str.str, arg_p->data.str.len, |
| 8651 | &url_param_value, &url_param_value_l)) |
| 8652 | return 0; |
| 8653 | |
| 8654 | data->str.str = url_param_value; |
| 8655 | data->str.len = url_param_value_l; |
| 8656 | return 1; |
| 8657 | } |
| 8658 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8659 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8660 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8661 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8662 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8663 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8664 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8665 | * value nor value_l are touched. The input hdr string should begin at the |
| 8666 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8667 | * value may represent a list of values (cookie headers). |
| 8668 | */ |
| 8669 | |
| 8670 | static int |
| 8671 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8672 | const char *hdr_name, int hdr_name_len, |
| 8673 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8674 | char **value, int *value_l) |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8675 | { |
| 8676 | struct hdr_ctx ctx; |
| 8677 | int found = 0; |
| 8678 | |
| 8679 | ctx.idx = 0; |
| 8680 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 4573af9 | 2012-04-06 18:20:06 +0200 | [diff] [blame] | 8681 | char *hdr, *end; |
| 8682 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8683 | if (ctx.vlen < cookie_name_l + 1) |
| 8684 | continue; |
| 8685 | |
Willy Tarreau | 4573af9 | 2012-04-06 18:20:06 +0200 | [diff] [blame] | 8686 | hdr = ctx.line + ctx.val; |
| 8687 | end = hdr + ctx.vlen; |
| 8688 | while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l))) |
| 8689 | found = 1; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8690 | } |
| 8691 | return found; |
| 8692 | } |
| 8693 | |
| 8694 | static int |
| 8695 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8696 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8697 | { |
| 8698 | struct http_txn *txn = l7; |
| 8699 | struct http_msg *msg = &txn->req; |
| 8700 | char *cookie_value; |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8701 | int cookie_value_l; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8702 | int found = 0; |
| 8703 | |
| 8704 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8705 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8706 | &cookie_value, &cookie_value_l); |
| 8707 | if (found) { |
| 8708 | data->str.str = cookie_value; |
| 8709 | data->str.len = cookie_value_l; |
| 8710 | } |
| 8711 | |
| 8712 | return found; |
| 8713 | } |
| 8714 | |
| 8715 | |
| 8716 | static int |
| 8717 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8718 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8719 | { |
| 8720 | struct http_txn *txn = l7; |
| 8721 | struct http_msg *msg = &txn->rsp; |
| 8722 | char *cookie_value; |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8723 | int cookie_value_l; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8724 | int found = 0; |
| 8725 | |
| 8726 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8727 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8728 | &cookie_value, &cookie_value_l); |
| 8729 | if (found) { |
| 8730 | data->str.str = cookie_value; |
| 8731 | data->str.len = cookie_value_l; |
| 8732 | } |
| 8733 | |
| 8734 | return found; |
| 8735 | } |
| 8736 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8737 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8738 | /************************************************************************/ |
| 8739 | /* All supported keywords must be declared here. */ |
| 8740 | /************************************************************************/ |
| 8741 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8742 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8743 | { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8744 | { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8745 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8746 | { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8747 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8748 | }}; |
| 8749 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8750 | |
| 8751 | __attribute__((constructor)) |
| 8752 | static void __http_protocol_init(void) |
| 8753 | { |
| 8754 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8755 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8756 | } |
| 8757 | |
| 8758 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8759 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8760 | * Local variables: |
| 8761 | * c-indent-level: 8 |
| 8762 | * c-basic-offset: 8 |
| 8763 | * End: |
| 8764 | */ |