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); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 271 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 273 | /* |
| 274 | * We have 26 list of methods (1 per first letter), each of which can have |
| 275 | * up to 3 entries (2 valid, 1 null). |
| 276 | */ |
| 277 | struct http_method_desc { |
| 278 | http_meth_t meth; |
| 279 | int len; |
| 280 | const char text[8]; |
| 281 | }; |
| 282 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 283 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 284 | ['C' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 286 | }, |
| 287 | ['D' - 'A'] = { |
| 288 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 289 | }, |
| 290 | ['G' - 'A'] = { |
| 291 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 292 | }, |
| 293 | ['H' - 'A'] = { |
| 294 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 295 | }, |
| 296 | ['P' - 'A'] = { |
| 297 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 298 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 299 | }, |
| 300 | ['T' - 'A'] = { |
| 301 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 302 | }, |
| 303 | /* rest is empty like this : |
| 304 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 305 | */ |
| 306 | }; |
| 307 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 308 | /* 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] | 309 | * 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] | 310 | * RFC2616 for those chars. |
| 311 | */ |
| 312 | |
| 313 | const char http_is_spht[256] = { |
| 314 | [' '] = 1, ['\t'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_crlf[256] = { |
| 318 | ['\r'] = 1, ['\n'] = 1, |
| 319 | }; |
| 320 | |
| 321 | const char http_is_lws[256] = { |
| 322 | [' '] = 1, ['\t'] = 1, |
| 323 | ['\r'] = 1, ['\n'] = 1, |
| 324 | }; |
| 325 | |
| 326 | const char http_is_sep[256] = { |
| 327 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 328 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 329 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 330 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 331 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 332 | }; |
| 333 | |
| 334 | const char http_is_ctl[256] = { |
| 335 | [0 ... 31] = 1, |
| 336 | [127] = 1, |
| 337 | }; |
| 338 | |
| 339 | /* |
| 340 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 341 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 342 | * them correctly. Instead, define every non-CTL char's status. |
| 343 | */ |
| 344 | const char http_is_token[256] = { |
| 345 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 346 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 347 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 348 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 349 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 350 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 351 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 352 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 353 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 354 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 355 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 356 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 357 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 358 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 359 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 360 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 361 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 362 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 363 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 364 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 365 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 366 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 367 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 368 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 369 | }; |
| 370 | |
| 371 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 372 | /* |
| 373 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 374 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 375 | */ |
| 376 | const char http_is_ver_token[256] = { |
| 377 | ['.'] = 1, ['/'] = 1, |
| 378 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 379 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 380 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 381 | }; |
| 382 | |
| 383 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 384 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 385 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 386 | */ |
| 387 | #if defined(DEBUG_FSM) |
| 388 | static void http_silent_debug(int line, struct session *s) |
| 389 | { |
| 390 | int size = 0; |
| 391 | size += snprintf(trash + size, sizeof(trash) - size, |
| 392 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 393 | line, |
| 394 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 395 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 396 | write(-1, trash, size); |
| 397 | size = 0; |
| 398 | size += snprintf(trash + size, sizeof(trash) - size, |
| 399 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 400 | line, |
| 401 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 402 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 403 | |
| 404 | write(-1, trash, size); |
| 405 | } |
| 406 | #else |
| 407 | #define http_silent_debug(l,s) do { } while (0) |
| 408 | #endif |
| 409 | |
| 410 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 411 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 412 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 413 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 414 | * of headers is automatically adjusted. The number of bytes added is returned |
| 415 | * on success, otherwise <0 is returned indicating an error. |
| 416 | */ |
| 417 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 418 | struct hdr_idx *hdr_idx, const char *text) |
| 419 | { |
| 420 | int bytes, len; |
| 421 | |
| 422 | len = strlen(text); |
| 423 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 424 | if (!bytes) |
| 425 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 426 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 427 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 432 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 433 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 434 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 435 | * of headers is automatically adjusted. The number of bytes added is returned |
| 436 | * on success, otherwise <0 is returned indicating an error. |
| 437 | */ |
| 438 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 439 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 440 | { |
| 441 | int bytes; |
| 442 | |
| 443 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 444 | if (!bytes) |
| 445 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 446 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 447 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 448 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 449 | |
| 450 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 451 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 452 | * If so, returns the position of the first non-space character relative to |
| 453 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 454 | * to return a pointer to the place after the first space. Returns 0 if the |
| 455 | * header name does not match. Checks are case-insensitive. |
| 456 | */ |
| 457 | int http_header_match2(const char *hdr, const char *end, |
| 458 | const char *name, int len) |
| 459 | { |
| 460 | const char *val; |
| 461 | |
| 462 | if (hdr + len >= end) |
| 463 | return 0; |
| 464 | if (hdr[len] != ':') |
| 465 | return 0; |
| 466 | if (strncasecmp(hdr, name, len) != 0) |
| 467 | return 0; |
| 468 | val = hdr + len + 1; |
| 469 | while (val < end && HTTP_IS_SPHT(*val)) |
| 470 | val++; |
| 471 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 472 | return len + 2; /* we may replace starting from second space */ |
| 473 | return val - hdr; |
| 474 | } |
| 475 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 476 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 477 | * par 2.2 for more information. Note that it requires a valid header to return |
| 478 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 479 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 480 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 481 | { |
| 482 | int quoted, qdpair; |
| 483 | |
| 484 | quoted = qdpair = 0; |
| 485 | for (; s < e; s++) { |
| 486 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 487 | else if (quoted) { |
| 488 | if (*s == '\\') qdpair = 1; |
| 489 | else if (*s == '"') quoted = 0; |
| 490 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 491 | else if (*s == '"') quoted = 1; |
| 492 | else if (*s == ',') return s; |
| 493 | } |
| 494 | return s; |
| 495 | } |
| 496 | |
| 497 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 498 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 499 | * structure holds everything necessary to use the header and find next |
| 500 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 501 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 503 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 504 | * is NULL when searching for a new values of a header, the current header is |
| 505 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 506 | */ |
| 507 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 508 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 509 | struct hdr_ctx *ctx) |
| 510 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 511 | char *eol, *sov; |
| 512 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 513 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 514 | cur_idx = ctx->idx; |
| 515 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 516 | /* We have previously returned a value, let's search |
| 517 | * another one on the same line. |
| 518 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 519 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 520 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 521 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 522 | eol = sol + idx->v[cur_idx].len; |
| 523 | |
| 524 | if (sov >= eol) |
| 525 | /* no more values in this header */ |
| 526 | goto next_hdr; |
| 527 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 528 | /* values remaining for this header, skip the comma but save it |
| 529 | * for later use (eg: for header deletion). |
| 530 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 531 | sov++; |
| 532 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 533 | sov++; |
| 534 | |
| 535 | goto return_hdr; |
| 536 | } |
| 537 | |
| 538 | /* first request for this header */ |
| 539 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 540 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 541 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 542 | while (cur_idx) { |
| 543 | eol = sol + idx->v[cur_idx].len; |
| 544 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 545 | if (len == 0) { |
| 546 | /* No argument was passed, we want any header. |
| 547 | * To achieve this, we simply build a fake request. */ |
| 548 | while (sol + len < eol && sol[len] != ':') |
| 549 | len++; |
| 550 | name = sol; |
| 551 | } |
| 552 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 553 | if ((len < eol - sol) && |
| 554 | (sol[len] == ':') && |
| 555 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 556 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 557 | sov = sol + len + 1; |
| 558 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 559 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 560 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 561 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 562 | ctx->prev = old_idx; |
| 563 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 564 | ctx->idx = cur_idx; |
| 565 | ctx->val = sov - sol; |
| 566 | |
| 567 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 568 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 569 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 570 | eol--; |
| 571 | ctx->tws++; |
| 572 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 573 | ctx->vlen = eol - sov; |
| 574 | return 1; |
| 575 | } |
| 576 | next_hdr: |
| 577 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 578 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 579 | cur_idx = idx->v[cur_idx].next; |
| 580 | } |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 585 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 586 | struct hdr_ctx *ctx) |
| 587 | { |
| 588 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 589 | } |
| 590 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 591 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 592 | * the http_find_header functions. The value is removed, as well as surrounding |
| 593 | * commas if any. If the removed value was alone, the whole header is removed. |
| 594 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 595 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 596 | * no more header, so any processing may stop. The ctx is always left in a form |
| 597 | * that can be handled by http_find_header2() to find next occurrence. |
| 598 | */ |
| 599 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 600 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 601 | { |
| 602 | int cur_idx = ctx->idx; |
| 603 | char *sol = ctx->line; |
| 604 | struct hdr_idx_elem *hdr; |
| 605 | int delta, skip_comma; |
| 606 | |
| 607 | if (!cur_idx) |
| 608 | return 0; |
| 609 | |
| 610 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 611 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | /* This was the only value of the header, we must now remove it entirely. */ |
| 613 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 614 | http_msg_move_end(msg, delta); |
| 615 | idx->used--; |
| 616 | hdr->len = 0; /* unused entry */ |
| 617 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 618 | if (idx->tail == ctx->idx) |
| 619 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 620 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 621 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 622 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 623 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | return ctx->idx; |
| 625 | } |
| 626 | |
| 627 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 628 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 629 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 630 | */ |
| 631 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 632 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 633 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 634 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 635 | NULL, 0); |
| 636 | hdr->len += delta; |
| 637 | http_msg_move_end(msg, delta); |
| 638 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 639 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 640 | return ctx->idx; |
| 641 | } |
| 642 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 643 | /* This function handles a server error at the stream interface level. The |
| 644 | * stream interface is assumed to be already in a closed state. An optional |
| 645 | * message is copied into the input buffer, and an HTTP status code stored. |
| 646 | * The error flags are set to the values in arguments. Any pending request |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 647 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 649 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 650 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 652 | buffer_auto_read(si->ob); |
| 653 | buffer_abort(si->ob); |
| 654 | buffer_auto_close(si->ob); |
| 655 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 656 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 657 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 658 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 659 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 660 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | } |
| 662 | if (!(t->flags & SN_ERR_MASK)) |
| 663 | t->flags |= err; |
| 664 | if (!(t->flags & SN_FINST_MASK)) |
| 665 | t->flags |= finst; |
| 666 | } |
| 667 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 668 | /* This function returns the appropriate error location for the given session |
| 669 | * and message. |
| 670 | */ |
| 671 | |
| 672 | struct chunk *error_message(struct session *s, int msgnum) |
| 673 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 674 | if (s->be->errmsg[msgnum].str) |
| 675 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 676 | else if (s->fe->errmsg[msgnum].str) |
| 677 | return &s->fe->errmsg[msgnum]; |
| 678 | else |
| 679 | return &http_err_chunks[msgnum]; |
| 680 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 681 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | /* |
| 683 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 684 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 685 | */ |
| 686 | static http_meth_t find_http_meth(const char *str, const int len) |
| 687 | { |
| 688 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 689 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 690 | |
| 691 | m = ((unsigned)*str - 'A'); |
| 692 | |
| 693 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 694 | for (h = http_methods[m]; h->len > 0; h++) { |
| 695 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 696 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 697 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 698 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 699 | }; |
| 700 | return HTTP_METH_OTHER; |
| 701 | } |
| 702 | return HTTP_METH_NONE; |
| 703 | |
| 704 | } |
| 705 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 706 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 707 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 708 | * It is returned otherwise. |
| 709 | */ |
| 710 | static char * |
| 711 | http_get_path(struct http_txn *txn) |
| 712 | { |
| 713 | char *ptr, *end; |
| 714 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 715 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 716 | end = ptr + txn->req.sl.rq.u_l; |
| 717 | |
| 718 | if (ptr >= end) |
| 719 | return NULL; |
| 720 | |
| 721 | /* RFC2616, par. 5.1.2 : |
| 722 | * Request-URI = "*" | absuri | abspath | authority |
| 723 | */ |
| 724 | |
| 725 | if (*ptr == '*') |
| 726 | return NULL; |
| 727 | |
| 728 | if (isalpha((unsigned char)*ptr)) { |
| 729 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 730 | ptr++; |
| 731 | while (ptr < end && |
| 732 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 733 | ptr++; |
| 734 | /* skip '://' */ |
| 735 | if (ptr == end || *ptr++ != ':') |
| 736 | return NULL; |
| 737 | if (ptr == end || *ptr++ != '/') |
| 738 | return NULL; |
| 739 | if (ptr == end || *ptr++ != '/') |
| 740 | return NULL; |
| 741 | } |
| 742 | /* skip [user[:passwd]@]host[:[port]] */ |
| 743 | |
| 744 | while (ptr < end && *ptr != '/') |
| 745 | ptr++; |
| 746 | |
| 747 | if (ptr == end) |
| 748 | return NULL; |
| 749 | |
| 750 | /* OK, we got the '/' ! */ |
| 751 | return ptr; |
| 752 | } |
| 753 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 755 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 756 | * left unchanged and will follow normal proxy processing. |
| 757 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 758 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 759 | { |
| 760 | struct http_txn *txn; |
| 761 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 762 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 763 | char *path; |
| 764 | int len; |
| 765 | |
| 766 | /* 1: create the response header */ |
| 767 | rdr.len = strlen(HTTP_302); |
| 768 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 769 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 770 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 771 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 772 | srv = target_srv(&s->target); |
| 773 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 774 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 775 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 776 | return; |
| 777 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 778 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 779 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 780 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 781 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 782 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 783 | |
| 784 | /* 3: add the request URI */ |
| 785 | txn = &s->txn; |
| 786 | path = http_get_path(txn); |
| 787 | if (!path) |
| 788 | return; |
| 789 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 790 | 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] | 791 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 792 | return; |
| 793 | |
| 794 | memcpy(rdr.str + rdr.len, path, len); |
| 795 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 796 | |
| 797 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 798 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 799 | rdr.len += 29; |
| 800 | } else { |
| 801 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 802 | rdr.len += 23; |
| 803 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 804 | |
| 805 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 806 | si->shutr(si); |
| 807 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | si->err_type = SI_ET_NONE; |
| 809 | si->err_loc = NULL; |
| 810 | si->state = SI_ST_CLO; |
| 811 | |
| 812 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 813 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 814 | |
| 815 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 816 | if (srv) |
| 817 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 820 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 821 | * that the server side is closed. Note that err_type is actually a |
| 822 | * bitmask, where almost only aborts may be cumulated with other |
| 823 | * values. We consider that aborted operations are more important |
| 824 | * than timeouts or errors due to the fact that nobody else in the |
| 825 | * logs might explain incomplete retries. All others should avoid |
| 826 | * being cumulated. It should normally not be possible to have multiple |
| 827 | * aborts at once, but just in case, the first one in sequence is reported. |
| 828 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 829 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 830 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 831 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | |
| 833 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 834 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 835 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 836 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 837 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 838 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 839 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 840 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 841 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 842 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 843 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 844 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 845 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 847 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 848 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 849 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 850 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 851 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 852 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 853 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 856 | extern const char sess_term_cond[8]; |
| 857 | extern const char sess_fin_state[8]; |
| 858 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 859 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 860 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 861 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 862 | /* |
| 863 | * Capture headers from message starting at <som> according to header list |
| 864 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 865 | */ |
| 866 | void capture_headers(char *som, struct hdr_idx *idx, |
| 867 | char **cap, struct cap_hdr *cap_hdr) |
| 868 | { |
| 869 | char *eol, *sol, *col, *sov; |
| 870 | int cur_idx; |
| 871 | struct cap_hdr *h; |
| 872 | int len; |
| 873 | |
| 874 | sol = som + hdr_idx_first_pos(idx); |
| 875 | cur_idx = hdr_idx_first_idx(idx); |
| 876 | |
| 877 | while (cur_idx) { |
| 878 | eol = sol + idx->v[cur_idx].len; |
| 879 | |
| 880 | col = sol; |
| 881 | while (col < eol && *col != ':') |
| 882 | col++; |
| 883 | |
| 884 | sov = col + 1; |
| 885 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 886 | sov++; |
| 887 | |
| 888 | for (h = cap_hdr; h; h = h->next) { |
| 889 | if ((h->namelen == col - sol) && |
| 890 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 891 | if (cap[h->index] == NULL) |
| 892 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 893 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 894 | |
| 895 | if (cap[h->index] == NULL) { |
| 896 | Alert("HTTP capture : out of memory.\n"); |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | len = eol - sov; |
| 901 | if (len > h->len) |
| 902 | len = h->len; |
| 903 | |
| 904 | memcpy(cap[h->index], sov, len); |
| 905 | cap[h->index][len]=0; |
| 906 | } |
| 907 | } |
| 908 | sol = eol + idx->v[cur_idx].cr + 1; |
| 909 | cur_idx = idx->v[cur_idx].next; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 914 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 915 | */ |
| 916 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 917 | |
| 918 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 919 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 920 | */ |
| 921 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 922 | ptr++; \ |
| 923 | if (likely(ptr < end)) \ |
| 924 | goto good; \ |
| 925 | else { \ |
| 926 | state = (st); \ |
| 927 | goto http_msg_ood; \ |
| 928 | } \ |
| 929 | } while (0) |
| 930 | |
| 931 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 932 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 933 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 934 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 935 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 936 | * will give undefined results. |
| 937 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 938 | * and that msg->sol points to the beginning of the response. |
| 939 | * If a complete line is found (which implies that at least one CR or LF is |
| 940 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 941 | * returned indicating an incomplete line (which does not mean that parts have |
| 942 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 943 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 944 | * upon next call. |
| 945 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 946 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 947 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 948 | * 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] | 949 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 950 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 951 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 952 | unsigned int state, const char *ptr, const char *end, |
| 953 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 954 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 955 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 956 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 957 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 958 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 959 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 960 | |
| 961 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 962 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 963 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 964 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 965 | state = HTTP_MSG_ERROR; |
| 966 | break; |
| 967 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 968 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 969 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 970 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 971 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 972 | goto http_msg_rpcode; |
| 973 | } |
| 974 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 975 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 976 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 977 | state = HTTP_MSG_ERROR; |
| 978 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 979 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 980 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 981 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 982 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 983 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 984 | |
| 985 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 986 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 987 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 988 | } |
| 989 | |
| 990 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 991 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 992 | http_msg_rsp_reason: |
| 993 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 994 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 995 | msg->sl.st.r_l = 0; |
| 996 | goto http_msg_rpline_eol; |
| 997 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 998 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 999 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1000 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1001 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1002 | goto http_msg_rpreason; |
| 1003 | } |
| 1004 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1005 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1006 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1007 | goto http_msg_rsp_reason; |
| 1008 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1009 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1010 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1011 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1012 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1013 | msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1014 | http_msg_rpline_eol: |
| 1015 | /* We have seen the end of line. Note that we do not |
| 1016 | * necessarily have the \n yet, but at least we know that we |
| 1017 | * have EITHER \r OR \n, otherwise the response would not be |
| 1018 | * complete. We can then record the response length and return |
| 1019 | * to the caller which will be able to register it. |
| 1020 | */ |
| 1021 | msg->sl.st.l = ptr - msg->sol; |
| 1022 | return ptr; |
| 1023 | |
| 1024 | #ifdef DEBUG_FULL |
| 1025 | default: |
| 1026 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1027 | exit(1); |
| 1028 | #endif |
| 1029 | } |
| 1030 | |
| 1031 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1032 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1033 | if (ret_state) |
| 1034 | *ret_state = state; |
| 1035 | if (ret_ptr) |
| 1036 | *ret_ptr = (char *)ptr; |
| 1037 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1040 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1041 | * This function parses a request line between <ptr> and <end>, starting with |
| 1042 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1043 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1044 | * will give undefined results. |
| 1045 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1046 | * and that msg->sol points to the beginning of the request. |
| 1047 | * If a complete line is found (which implies that at least one CR or LF is |
| 1048 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1049 | * returned indicating an incomplete line (which does not mean that parts have |
| 1050 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1051 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1052 | * upon next call. |
| 1053 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1054 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1055 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1056 | * 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] | 1057 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1058 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1059 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1060 | unsigned int state, const char *ptr, const char *end, |
| 1061 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1062 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1063 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1064 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1065 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1066 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1067 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1068 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1069 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1070 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1071 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1072 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1073 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1074 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1075 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1076 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1077 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1078 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1080 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1082 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | msg->sl.rq.v_l = 0; |
| 1084 | goto http_msg_rqline_eol; |
| 1085 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1086 | state = HTTP_MSG_ERROR; |
| 1087 | break; |
| 1088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1089 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1090 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1091 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1092 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | goto http_msg_rquri; |
| 1094 | } |
| 1095 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1096 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1097 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1098 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1099 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1100 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1101 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1102 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1103 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1104 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1106 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1108 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1109 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1110 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1111 | /* non-ASCII chars are forbidden unless option |
| 1112 | * accept-invalid-http-request is enabled in the frontend. |
| 1113 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1114 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1115 | if (msg->err_pos < -1) |
| 1116 | goto invalid_char; |
| 1117 | if (msg->err_pos == -1) |
| 1118 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1119 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1120 | } |
| 1121 | |
| 1122 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1123 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1124 | goto http_msg_req09_uri_e; |
| 1125 | } |
| 1126 | |
| 1127 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1128 | invalid_char: |
| 1129 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1130 | state = HTTP_MSG_ERROR; |
| 1131 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1132 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1133 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1134 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1135 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1136 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1137 | goto http_msg_rqver; |
| 1138 | } |
| 1139 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1140 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1141 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1142 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1143 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1144 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1145 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1146 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1147 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1148 | |
| 1149 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1150 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1151 | http_msg_rqline_eol: |
| 1152 | /* We have seen the end of line. Note that we do not |
| 1153 | * necessarily have the \n yet, but at least we know that we |
| 1154 | * have EITHER \r OR \n, otherwise the request would not be |
| 1155 | * complete. We can then record the request length and return |
| 1156 | * to the caller which will be able to register it. |
| 1157 | */ |
| 1158 | msg->sl.rq.l = ptr - msg->sol; |
| 1159 | return ptr; |
| 1160 | } |
| 1161 | |
| 1162 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1163 | state = HTTP_MSG_ERROR; |
| 1164 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1165 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1166 | #ifdef DEBUG_FULL |
| 1167 | default: |
| 1168 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1169 | exit(1); |
| 1170 | #endif |
| 1171 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1172 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1173 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1174 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1175 | if (ret_state) |
| 1176 | *ret_state = state; |
| 1177 | if (ret_ptr) |
| 1178 | *ret_ptr = (char *)ptr; |
| 1179 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1180 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1181 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1182 | /* |
| 1183 | * Returns the data from Authorization header. Function may be called more |
| 1184 | * than once so data is stored in txn->auth_data. When no header is found |
| 1185 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1186 | * searching again for something we are unable to find anyway. |
| 1187 | */ |
| 1188 | |
| 1189 | char get_http_auth_buff[BUFSIZE]; |
| 1190 | |
| 1191 | int |
| 1192 | get_http_auth(struct session *s) |
| 1193 | { |
| 1194 | |
| 1195 | struct http_txn *txn = &s->txn; |
| 1196 | struct chunk auth_method; |
| 1197 | struct hdr_ctx ctx; |
| 1198 | char *h, *p; |
| 1199 | int len; |
| 1200 | |
| 1201 | #ifdef DEBUG_AUTH |
| 1202 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1203 | #endif |
| 1204 | |
| 1205 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1206 | return 0; |
| 1207 | |
| 1208 | if (txn->auth.method) |
| 1209 | return 1; |
| 1210 | |
| 1211 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1212 | |
| 1213 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1214 | |
| 1215 | if (txn->flags & TX_USE_PX_CONN) { |
| 1216 | h = "Proxy-Authorization"; |
| 1217 | len = strlen(h); |
| 1218 | } else { |
| 1219 | h = "Authorization"; |
| 1220 | len = strlen(h); |
| 1221 | } |
| 1222 | |
| 1223 | 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] | 1224 | return 0; |
| 1225 | |
| 1226 | h = ctx.line + ctx.val; |
| 1227 | |
| 1228 | p = memchr(h, ' ', ctx.vlen); |
| 1229 | if (!p || p == h) |
| 1230 | return 0; |
| 1231 | |
| 1232 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1233 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1234 | |
| 1235 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1236 | |
| 1237 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1238 | get_http_auth_buff, BUFSIZE - 1); |
| 1239 | |
| 1240 | if (len < 0) |
| 1241 | return 0; |
| 1242 | |
| 1243 | |
| 1244 | get_http_auth_buff[len] = '\0'; |
| 1245 | |
| 1246 | p = strchr(get_http_auth_buff, ':'); |
| 1247 | |
| 1248 | if (!p) |
| 1249 | return 0; |
| 1250 | |
| 1251 | txn->auth.user = get_http_auth_buff; |
| 1252 | *p = '\0'; |
| 1253 | txn->auth.pass = p+1; |
| 1254 | |
| 1255 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1256 | return 1; |
| 1257 | } |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1262 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1263 | /* |
| 1264 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1265 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1266 | * when data are missing and recalled at the exact same location with no |
| 1267 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1268 | * 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] | 1269 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1270 | * the first state, so that none of the msg pointers has to be initialized |
| 1271 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1272 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1273 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1274 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1275 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1276 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1277 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1278 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1279 | ptr = buf->lr; |
| 1280 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1281 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1282 | if (unlikely(ptr >= end)) |
| 1283 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1284 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1285 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1286 | /* |
| 1287 | * First, states that are specific to the response only. |
| 1288 | * We check them first so that request and headers are |
| 1289 | * closer to each other (accessed more often). |
| 1290 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1291 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1292 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1294 | /* we have a start of message, but we have to check |
| 1295 | * first if we need to remove some CRLF. We can only |
| 1296 | * do this when send_max=0. |
| 1297 | */ |
| 1298 | char *beg = buf->w + buf->send_max; |
| 1299 | if (beg >= buf->data + buf->size) |
| 1300 | beg -= buf->size; |
| 1301 | if (unlikely(ptr != beg)) { |
| 1302 | if (buf->send_max) |
| 1303 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1304 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1305 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1306 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1307 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1308 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | hdr_idx_init(idx); |
| 1310 | state = HTTP_MSG_RPVER; |
| 1311 | goto http_msg_rpver; |
| 1312 | } |
| 1313 | |
| 1314 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1315 | goto http_msg_invalid; |
| 1316 | |
| 1317 | if (unlikely(*ptr == '\n')) |
| 1318 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1319 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1320 | /* stop here */ |
| 1321 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1323 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1324 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1325 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1326 | /* stop here */ |
| 1327 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1328 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1329 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | case HTTP_MSG_RPVER_SP: |
| 1331 | case HTTP_MSG_RPCODE: |
| 1332 | case HTTP_MSG_RPCODE_SP: |
| 1333 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1334 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1335 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | if (unlikely(!ptr)) |
| 1337 | return; |
| 1338 | |
| 1339 | /* we have a full response and we know that we have either a CR |
| 1340 | * or an LF at <ptr>. |
| 1341 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1342 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1343 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1344 | |
| 1345 | msg->sol = ptr; |
| 1346 | if (likely(*ptr == '\r')) |
| 1347 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1348 | goto http_msg_rpline_end; |
| 1349 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1350 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1351 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | /* msg->sol must point to the first of CR or LF. */ |
| 1353 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1354 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1355 | /* stop here */ |
| 1356 | |
| 1357 | /* |
| 1358 | * Second, states that are specific to the request only |
| 1359 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1360 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1361 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1363 | /* we have a start of message, but we have to check |
| 1364 | * first if we need to remove some CRLF. We can only |
| 1365 | * do this when send_max=0. |
| 1366 | */ |
| 1367 | char *beg = buf->w + buf->send_max; |
| 1368 | if (beg >= buf->data + buf->size) |
| 1369 | beg -= buf->size; |
| 1370 | if (likely(ptr != beg)) { |
| 1371 | if (buf->send_max) |
| 1372 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1373 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1374 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1376 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1377 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1378 | /* we will need this when keep-alive will be supported |
| 1379 | hdr_idx_init(idx); |
| 1380 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1381 | state = HTTP_MSG_RQMETH; |
| 1382 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1383 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1384 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1385 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1386 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | if (unlikely(*ptr == '\n')) |
| 1389 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1390 | 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] | 1391 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1392 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1394 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1396 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1397 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1398 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1399 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1400 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQMETH_SP: |
| 1402 | case HTTP_MSG_RQURI: |
| 1403 | case HTTP_MSG_RQURI_SP: |
| 1404 | case HTTP_MSG_RQVER: |
| 1405 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1406 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | if (unlikely(!ptr)) |
| 1408 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1409 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1410 | /* we have a full request and we know that we have either a CR |
| 1411 | * or an LF at <ptr>. |
| 1412 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1413 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | msg->sol = ptr; |
| 1417 | if (likely(*ptr == '\r')) |
| 1418 | 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] | 1419 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1420 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1422 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | /* check for HTTP/0.9 request : no version information available. |
| 1424 | * msg->sol must point to the first of CR or LF. |
| 1425 | */ |
| 1426 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1427 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1430 | 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] | 1431 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1433 | /* |
| 1434 | * Common states below |
| 1435 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1436 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1437 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | msg->sol = ptr; |
| 1439 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1440 | goto http_msg_hdr_name; |
| 1441 | } |
| 1442 | |
| 1443 | if (likely(*ptr == '\r')) |
| 1444 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1445 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1448 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | /* assumes msg->sol points to the first char */ |
| 1450 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1451 | 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] | 1452 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | if (likely(*ptr == ':')) { |
| 1454 | msg->col = ptr - buf->data; |
| 1455 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1456 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1457 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1458 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1459 | goto http_msg_invalid; |
| 1460 | |
| 1461 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1462 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1463 | |
| 1464 | /* and we still accept this non-token character */ |
| 1465 | 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] | 1466 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1467 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1468 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1470 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1471 | 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] | 1472 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | /* header value can be basically anything except CR/LF */ |
| 1474 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1477 | goto http_msg_hdr_val; |
| 1478 | } |
| 1479 | |
| 1480 | if (likely(*ptr == '\r')) |
| 1481 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1482 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1483 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1484 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1485 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1487 | 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] | 1488 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1489 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1490 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1492 | /* replace HT,CR,LF with spaces */ |
| 1493 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1494 | buf->data[msg->sov] = ' '; |
| 1495 | goto http_msg_hdr_l1_sp; |
| 1496 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1497 | /* we had a header consisting only in spaces ! */ |
| 1498 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1499 | goto http_msg_complete_header; |
| 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1502 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | /* assumes msg->sol points to the first char, msg->col to the |
| 1504 | * colon, and msg->sov points to the first character of the |
| 1505 | * value. |
| 1506 | */ |
| 1507 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1508 | 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] | 1509 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | msg->eol = ptr; |
| 1511 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1512 | * real header end in case it ends with lots of LWS, but is this |
| 1513 | * really needed ? |
| 1514 | */ |
| 1515 | if (likely(*ptr == '\r')) |
| 1516 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1517 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1518 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1520 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1522 | 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] | 1523 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1525 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1527 | /* LWS: replace HT,CR,LF with spaces */ |
| 1528 | for (; msg->eol < ptr; msg->eol++) |
| 1529 | *msg->eol = ' '; |
| 1530 | goto http_msg_hdr_val; |
| 1531 | } |
| 1532 | http_msg_complete_header: |
| 1533 | /* |
| 1534 | * It was a new header, so the last one is finished. |
| 1535 | * Assumes msg->sol points to the first char, msg->col to the |
| 1536 | * colon, msg->sov points to the first character of the value |
| 1537 | * and msg->eol to the first CR or LF so we know how the line |
| 1538 | * ends. We insert last header into the index. |
| 1539 | */ |
| 1540 | /* |
| 1541 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1542 | write(2, msg->sol, msg->eol-msg->sol); |
| 1543 | fprintf(stderr,"\n"); |
| 1544 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1547 | idx, idx->tail) < 0)) |
| 1548 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | msg->sol = ptr; |
| 1551 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1552 | goto http_msg_hdr_name; |
| 1553 | } |
| 1554 | |
| 1555 | if (likely(*ptr == '\r')) |
| 1556 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1557 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1560 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1562 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1563 | ptr++; |
| 1564 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1565 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1567 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1568 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | return; |
| 1570 | #ifdef DEBUG_FULL |
| 1571 | default: |
| 1572 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1573 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1574 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | } |
| 1576 | http_msg_ood: |
| 1577 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1578 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | buf->lr = ptr; |
| 1580 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1581 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | http_msg_invalid: |
| 1583 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1584 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1585 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | return; |
| 1587 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1589 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1590 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1591 | * nothing is done and 1 is returned. |
| 1592 | */ |
| 1593 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1594 | { |
| 1595 | int delta; |
| 1596 | char *cur_end; |
| 1597 | |
| 1598 | if (msg->sl.rq.v_l != 0) |
| 1599 | return 1; |
| 1600 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1601 | cur_end = msg->sol + msg->sl.rq.l; |
| 1602 | delta = 0; |
| 1603 | |
| 1604 | if (msg->sl.rq.u_l == 0) { |
| 1605 | /* if no URI was set, add "/" */ |
| 1606 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1607 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1608 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1609 | } |
| 1610 | /* add HTTP version */ |
| 1611 | 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] | 1612 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1613 | cur_end += delta; |
| 1614 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1615 | HTTP_MSG_RQMETH, |
| 1616 | msg->sol, cur_end + 1, |
| 1617 | NULL, NULL); |
| 1618 | if (unlikely(!cur_end)) |
| 1619 | return 0; |
| 1620 | |
| 1621 | /* we have a full HTTP/1.0 request now and we know that |
| 1622 | * we have either a CR or an LF at <ptr>. |
| 1623 | */ |
| 1624 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1628 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1629 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1630 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1631 | * are used for that : |
| 1632 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1633 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1634 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1635 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1636 | * harmless combinations may be removed. Do not call that after changes have |
| 1637 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1638 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1639 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1640 | 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] | 1641 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1642 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1643 | const char *hdr_val = "Connection"; |
| 1644 | int hdr_len = 10; |
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 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1647 | return; |
| 1648 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1649 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1650 | hdr_val = "Proxy-Connection"; |
| 1651 | hdr_len = 16; |
| 1652 | } |
| 1653 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1654 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1655 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1656 | 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] | 1657 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1658 | txn->flags |= TX_HDR_CONN_KAL; |
| 1659 | if ((to_del & 2) && buf) |
| 1660 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1661 | else |
| 1662 | txn->flags |= TX_CON_KAL_SET; |
| 1663 | } |
| 1664 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1665 | txn->flags |= TX_HDR_CONN_CLO; |
| 1666 | if ((to_del & 1) && buf) |
| 1667 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1668 | else |
| 1669 | txn->flags |= TX_CON_CLO_SET; |
| 1670 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1673 | txn->flags |= TX_HDR_CONN_PRS; |
| 1674 | return; |
| 1675 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1677 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1678 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1679 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1680 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1681 | */ |
| 1682 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1683 | { |
| 1684 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1685 | const char *hdr_val = "Connection"; |
| 1686 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1687 | |
| 1688 | ctx.idx = 0; |
| 1689 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1690 | |
| 1691 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1692 | hdr_val = "Proxy-Connection"; |
| 1693 | hdr_len = 16; |
| 1694 | } |
| 1695 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1696 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1697 | 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] | 1698 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1699 | if (wanted & TX_CON_KAL_SET) |
| 1700 | txn->flags |= TX_CON_KAL_SET; |
| 1701 | else |
| 1702 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1703 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1704 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1705 | if (wanted & TX_CON_CLO_SET) |
| 1706 | txn->flags |= TX_CON_CLO_SET; |
| 1707 | else |
| 1708 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1709 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1710 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1711 | |
| 1712 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1713 | return; |
| 1714 | |
| 1715 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1716 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1717 | hdr_val = "Connection: close"; |
| 1718 | hdr_len = 17; |
| 1719 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1720 | hdr_val = "Proxy-Connection: close"; |
| 1721 | hdr_len = 23; |
| 1722 | } |
| 1723 | 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] | 1724 | } |
| 1725 | |
| 1726 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1727 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1728 | hdr_val = "Connection: keep-alive"; |
| 1729 | hdr_len = 22; |
| 1730 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1731 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1732 | hdr_len = 28; |
| 1733 | } |
| 1734 | 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] | 1735 | } |
| 1736 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1739 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1740 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1741 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1742 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1743 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1744 | * 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] | 1745 | * 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] | 1746 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1747 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1748 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1749 | char *ptr = buf->lr; |
| 1750 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1751 | unsigned int chunk = 0; |
| 1752 | |
| 1753 | /* The chunk size is in the following form, though we are only |
| 1754 | * interested in the size and CRLF : |
| 1755 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1756 | */ |
| 1757 | while (1) { |
| 1758 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1759 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1760 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1761 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1762 | if (c < 0) /* not a hex digit anymore */ |
| 1763 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1764 | if (++ptr >= end) |
| 1765 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1766 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1767 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1768 | chunk = (chunk << 4) + c; |
| 1769 | } |
| 1770 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1771 | /* empty size not allowed */ |
| 1772 | if (ptr == buf->lr) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1773 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1774 | |
| 1775 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1776 | if (++ptr >= end) |
| 1777 | ptr = buf->data; |
| 1778 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1779 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1782 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1783 | * for the end of chunk size. |
| 1784 | */ |
| 1785 | while (1) { |
| 1786 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1787 | /* we now have a CR or an LF at ptr */ |
| 1788 | if (likely(*ptr == '\r')) { |
| 1789 | if (++ptr >= end) |
| 1790 | ptr = buf->data; |
| 1791 | if (ptr == buf->r) |
| 1792 | return 0; |
| 1793 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1794 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1795 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1796 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1797 | if (++ptr >= end) |
| 1798 | ptr = buf->data; |
| 1799 | /* done */ |
| 1800 | break; |
| 1801 | } |
| 1802 | else if (*ptr == ';') { |
| 1803 | /* chunk extension, ends at next CRLF */ |
| 1804 | if (++ptr >= end) |
| 1805 | ptr = buf->data; |
| 1806 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1807 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1808 | |
| 1809 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1810 | if (++ptr >= end) |
| 1811 | ptr = buf->data; |
| 1812 | if (ptr == buf->r) |
| 1813 | return 0; |
| 1814 | } |
| 1815 | /* we have a CRLF now, loop above */ |
| 1816 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1817 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1818 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1819 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1822 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 1823 | * which may or may not be present. We save that into ->lr and |
| 1824 | * ->sov. |
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 | msg->sov += ptr - buf->lr; |
| 1827 | buf->lr = ptr; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1828 | msg->chunk_len = chunk; |
| 1829 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1830 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1831 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1832 | error: |
| 1833 | msg->err_pos = ptr - buf->data; |
| 1834 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1837 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 1838 | * message <msg>. The first visited position is buf->lr. If the end of |
| 1839 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1840 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1841 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1842 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 1843 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 1844 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 1845 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1846 | * which implies that all non-trailers data have already been scheduled for |
| 1847 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1848 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1849 | * important to note that this function is designed to be able to parse wrapped |
| 1850 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1851 | */ |
| 1852 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1853 | { |
| 1854 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 1855 | while (1) { |
| 1856 | char *p1 = NULL, *p2 = NULL; |
| 1857 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1858 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1859 | |
| 1860 | /* scan current line and stop at LF or CRLF */ |
| 1861 | while (1) { |
| 1862 | if (ptr == buf->r) |
| 1863 | return 0; |
| 1864 | |
| 1865 | if (*ptr == '\n') { |
| 1866 | if (!p1) |
| 1867 | p1 = ptr; |
| 1868 | p2 = ptr; |
| 1869 | break; |
| 1870 | } |
| 1871 | |
| 1872 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1873 | if (p1) { |
| 1874 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1875 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1876 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1877 | p1 = ptr; |
| 1878 | } |
| 1879 | |
| 1880 | ptr++; |
| 1881 | if (ptr >= buf->data + buf->size) |
| 1882 | ptr = buf->data; |
| 1883 | } |
| 1884 | |
| 1885 | /* after LF; point to beginning of next line */ |
| 1886 | p2++; |
| 1887 | if (p2 >= buf->data + buf->size) |
| 1888 | p2 = buf->data; |
| 1889 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1890 | bytes = p2 - buf->lr; |
| 1891 | if (bytes < 0) |
| 1892 | bytes += buf->size; |
| 1893 | |
| 1894 | /* schedule this line for forwarding */ |
| 1895 | msg->sov += bytes; |
| 1896 | if (msg->sov >= buf->size) |
| 1897 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1898 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1899 | if (p1 == buf->lr) { |
| 1900 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1901 | * Everything was scheduled for forwarding, there's nothing |
| 1902 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1903 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1904 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1905 | msg->msg_state = HTTP_MSG_DONE; |
| 1906 | return 1; |
| 1907 | } |
| 1908 | /* OK, next line then */ |
| 1909 | buf->lr = p2; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1914 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 1915 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 1916 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1917 | * not enough data are available, the function does not change anything and |
| 1918 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1919 | * does not change anything. Note: this function is designed to parse wrapped |
| 1920 | * CRLF at the end of the buffer. |
| 1921 | */ |
| 1922 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 1923 | { |
| 1924 | char *ptr; |
| 1925 | int bytes; |
| 1926 | |
| 1927 | /* NB: we'll check data availabilty at the end. It's not a |
| 1928 | * problem because whatever we match first will be checked |
| 1929 | * against the correct length. |
| 1930 | */ |
| 1931 | bytes = 1; |
| 1932 | ptr = buf->lr; |
| 1933 | if (*ptr == '\r') { |
| 1934 | bytes++; |
| 1935 | ptr++; |
| 1936 | if (ptr >= buf->data + buf->size) |
| 1937 | ptr = buf->data; |
| 1938 | } |
| 1939 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 1940 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1941 | return 0; |
| 1942 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1943 | if (*ptr != '\n') { |
| 1944 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1945 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1946 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | |
| 1948 | ptr++; |
| 1949 | if (ptr >= buf->data + buf->size) |
| 1950 | ptr = buf->data; |
| 1951 | buf->lr = ptr; |
| 1952 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 1953 | msg->sov = ptr - buf->data; |
| 1954 | msg->som = msg->sov - bytes; |
| 1955 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1956 | return 1; |
| 1957 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1958 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1959 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 1960 | { |
| 1961 | char *end = buf->data + buf->size; |
| 1962 | int off = buf->data + buf->size - buf->w; |
| 1963 | |
| 1964 | /* two possible cases : |
| 1965 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1966 | * - 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] | 1967 | */ |
| 1968 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1969 | int block1 = buf->l; |
| 1970 | int block2 = 0; |
| 1971 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1972 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1973 | block1 = buf->data + buf->size - buf->w; |
| 1974 | block2 = buf->r - buf->data; |
| 1975 | } |
| 1976 | if (block2) |
| 1977 | memcpy(swap_buffer, buf->data, block2); |
| 1978 | memmove(buf->data, buf->w, block1); |
| 1979 | if (block2) |
| 1980 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* adjust all known pointers */ |
| 1984 | buf->w = buf->data; |
| 1985 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 1986 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 1987 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 1988 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 1989 | |
| 1990 | /* adjust relative pointers */ |
| 1991 | msg->som = 0; |
| 1992 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 1993 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 1994 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 1995 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1996 | if (msg->err_pos >= 0) { |
| 1997 | msg->err_pos += off; |
| 1998 | if (msg->err_pos >= buf->size) |
| 1999 | msg->err_pos -= buf->size; |
| 2000 | } |
| 2001 | |
| 2002 | buf->flags &= ~BF_FULL; |
| 2003 | if (buf->l >= buffer_max_len(buf)) |
| 2004 | buf->flags |= BF_FULL; |
| 2005 | } |
| 2006 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2007 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2008 | * processing can continue on next analysers, or zero if it either needs more |
| 2009 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2010 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2011 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2012 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2013 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2014 | 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] | 2015 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2016 | /* |
| 2017 | * We will parse the partial (or complete) lines. |
| 2018 | * We will check the request syntax, and also join multi-line |
| 2019 | * headers. An index of all the lines will be elaborated while |
| 2020 | * parsing. |
| 2021 | * |
| 2022 | * For the parsing, we use a 28 states FSM. |
| 2023 | * |
| 2024 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2025 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2026 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2027 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2028 | * req->lr = first non-visited byte |
| 2029 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2030 | * |
| 2031 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2032 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2033 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2034 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2035 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2036 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2037 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2038 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2039 | struct http_txn *txn = &s->txn; |
| 2040 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2041 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2042 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2043 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2044 | now_ms, __FUNCTION__, |
| 2045 | s, |
| 2046 | req, |
| 2047 | req->rex, req->wex, |
| 2048 | req->flags, |
| 2049 | req->l, |
| 2050 | req->analysers); |
| 2051 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2052 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2053 | s->srv_error = http_return_srv_error; |
| 2054 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2055 | /* There's a protected area at the end of the buffer for rewriting |
| 2056 | * purposes. We don't want to start to parse the request if the |
| 2057 | * protected area is affected, because we may have to move processed |
| 2058 | * data later, which is much more complicated. |
| 2059 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2060 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2061 | if ((txn->flags & TX_NOT_FIRST) && |
| 2062 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2063 | req->r < req->lr || |
| 2064 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2065 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2066 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2067 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2068 | /* some data has still not left the buffer, wake us once that's done */ |
| 2069 | buffer_dont_connect(req); |
| 2070 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2071 | return 0; |
| 2072 | } |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2073 | if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2074 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2077 | /* Note that we have the same problem with the response ; we |
| 2078 | * may want to send a redirect, error or anything which requires |
| 2079 | * some spare space. So we'll ensure that we have at least |
| 2080 | * maxrewrite bytes available in the response buffer before |
| 2081 | * processing that one. This will only affect pipelined |
| 2082 | * keep-alive requests. |
| 2083 | */ |
| 2084 | if ((txn->flags & TX_NOT_FIRST) && |
| 2085 | unlikely((s->rep->flags & BF_FULL) || |
| 2086 | s->rep->r < s->rep->lr || |
| 2087 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2088 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2089 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2090 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2091 | /* don't let a connection request be initiated */ |
| 2092 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2093 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2094 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2095 | return 0; |
| 2096 | } |
| 2097 | } |
| 2098 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2099 | if (likely(req->lr < req->r)) |
| 2100 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2103 | /* 1: we might have to print this header in debug mode */ |
| 2104 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2105 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2106 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2107 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2108 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2109 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2110 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2111 | eol = sol + msg->sl.rq.l; |
| 2112 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2113 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2114 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2115 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2116 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2117 | while (cur_idx) { |
| 2118 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2119 | debug_hdr("clihdr", s, sol, eol); |
| 2120 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2121 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2122 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2123 | } |
| 2124 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2125 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2126 | /* |
| 2127 | * Now we quickly check if we have found a full valid request. |
| 2128 | * If not so, we check the FD and buffer states before leaving. |
| 2129 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2130 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2131 | * requests are checked first. When waiting for a second request |
| 2132 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2133 | * we note the error in the session flags but don't set any state. |
| 2134 | * Since the error will be noted there, it will not be counted by |
| 2135 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2136 | * Last, we may increase some tracked counters' http request errors on |
| 2137 | * the cases that are deliberately the client's fault. For instance, |
| 2138 | * a timeout or connection reset is not counted as an error. However |
| 2139 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2140 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2141 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2142 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2143 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2144 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2145 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2146 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2147 | session_inc_http_req_ctr(s); |
| 2148 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2149 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2150 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2151 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2152 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2153 | /* 1: Since we are in header mode, if there's no space |
| 2154 | * left for headers, we won't be able to free more |
| 2155 | * later, so the session will never terminate. We |
| 2156 | * must terminate it now. |
| 2157 | */ |
| 2158 | if (unlikely(req->flags & BF_FULL)) { |
| 2159 | /* FIXME: check if URI is set and return Status |
| 2160 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2161 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2162 | session_inc_http_req_ctr(s); |
| 2163 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2164 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2165 | if (msg->err_pos < 0) |
| 2166 | msg->err_pos = req->l; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2167 | goto return_bad_req; |
| 2168 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2169 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2170 | /* 2: have we encountered a read error ? */ |
| 2171 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2172 | if (!(s->flags & SN_ERR_MASK)) |
| 2173 | s->flags |= SN_ERR_CLICL; |
| 2174 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2175 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2176 | goto failed_keep_alive; |
| 2177 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2178 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2179 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2180 | 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] | 2181 | session_inc_http_err_ctr(s); |
| 2182 | } |
| 2183 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2184 | msg->msg_state = HTTP_MSG_ERROR; |
| 2185 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2186 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2187 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2188 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2189 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2190 | if (s->listener->counters) |
| 2191 | s->listener->counters->failed_req++; |
| 2192 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2193 | if (!(s->flags & SN_FINST_MASK)) |
| 2194 | s->flags |= SN_FINST_R; |
| 2195 | return 0; |
| 2196 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2197 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2198 | /* 3: has the read timeout expired ? */ |
| 2199 | 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] | 2200 | if (!(s->flags & SN_ERR_MASK)) |
| 2201 | s->flags |= SN_ERR_CLITO; |
| 2202 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2203 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2204 | goto failed_keep_alive; |
| 2205 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2206 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2207 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2208 | 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] | 2209 | session_inc_http_err_ctr(s); |
| 2210 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2211 | txn->status = 408; |
| 2212 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2213 | msg->msg_state = HTTP_MSG_ERROR; |
| 2214 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2215 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2216 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2217 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2218 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2219 | if (s->listener->counters) |
| 2220 | s->listener->counters->failed_req++; |
| 2221 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2222 | if (!(s->flags & SN_FINST_MASK)) |
| 2223 | s->flags |= SN_FINST_R; |
| 2224 | return 0; |
| 2225 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2226 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2227 | /* 4: have we encountered a close ? */ |
| 2228 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2229 | if (!(s->flags & SN_ERR_MASK)) |
| 2230 | s->flags |= SN_ERR_CLICL; |
| 2231 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2232 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2233 | goto failed_keep_alive; |
| 2234 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2235 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2236 | 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] | 2237 | txn->status = 400; |
| 2238 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2239 | msg->msg_state = HTTP_MSG_ERROR; |
| 2240 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2241 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2242 | session_inc_http_err_ctr(s); |
| 2243 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2244 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2245 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2246 | if (s->listener->counters) |
| 2247 | s->listener->counters->failed_req++; |
| 2248 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2249 | if (!(s->flags & SN_FINST_MASK)) |
| 2250 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2251 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2252 | } |
| 2253 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2254 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2255 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2256 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2257 | #ifdef TCP_QUICKACK |
| 2258 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2259 | /* We need more data, we have to re-enable quick-ack in case we |
| 2260 | * previously disabled it, otherwise we might cause the client |
| 2261 | * to delay next data. |
| 2262 | */ |
| 2263 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2264 | } |
| 2265 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2266 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2267 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2268 | /* If the client starts to talk, let's fall back to |
| 2269 | * request timeout processing. |
| 2270 | */ |
| 2271 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2272 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2275 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2276 | if (!tick_isset(req->analyse_exp)) { |
| 2277 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2278 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2279 | tick_isset(s->be->timeout.httpka)) |
| 2280 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2281 | else |
| 2282 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2283 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2284 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2285 | /* we're not ready yet */ |
| 2286 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2287 | |
| 2288 | failed_keep_alive: |
| 2289 | /* Here we process low-level errors for keep-alive requests. In |
| 2290 | * short, if the request is not the first one and it experiences |
| 2291 | * a timeout, read error or shutdown, we just silently close so |
| 2292 | * that the client can try again. |
| 2293 | */ |
| 2294 | txn->status = 0; |
| 2295 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2296 | req->analysers = 0; |
| 2297 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2298 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2299 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2300 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2301 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2302 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2303 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2304 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2305 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2306 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2307 | * points to the CRLF of the request line. req->lr points to the first |
| 2308 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2309 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2310 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2311 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2312 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2313 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2314 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2315 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2316 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2317 | /* kill the pending keep-alive timeout */ |
| 2318 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2319 | req->analyse_exp = TICK_ETERNITY; |
| 2320 | } |
| 2321 | |
| 2322 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2323 | /* Maybe we found in invalid header name while we were configured not |
| 2324 | * to block on that, so we have to capture it now. |
| 2325 | */ |
| 2326 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2327 | 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] | 2328 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2329 | /* |
| 2330 | * 1: identify the method |
| 2331 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2332 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2333 | |
| 2334 | /* we can make use of server redirect on GET and HEAD */ |
| 2335 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2336 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2337 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2338 | /* |
| 2339 | * 2: check if the URI matches the monitor_uri. |
| 2340 | * We have to do this for every request which gets in, because |
| 2341 | * the monitor-uri is defined by the frontend. |
| 2342 | */ |
| 2343 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2344 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2345 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | s->fe->monitor_uri, |
| 2347 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2348 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2349 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2350 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2351 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2352 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2354 | s->fe->fe_counters.intercepted_req++; |
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 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2357 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2358 | 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] | 2359 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2360 | ret = acl_pass(ret); |
| 2361 | if (cond->pol == ACL_COND_UNLESS) |
| 2362 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2363 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2364 | if (ret) { |
| 2365 | /* we fail this request, let's return 503 service unavail */ |
| 2366 | txn->status = 503; |
| 2367 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2368 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2369 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2370 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2371 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2372 | /* nothing to fail, let's reply normaly */ |
| 2373 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2374 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2375 | goto return_prx_cond; |
| 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2380 | * Note: we cannot log anymore if the request has been |
| 2381 | * classified as invalid. |
| 2382 | */ |
| 2383 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2384 | /* we have a complete HTTP request that we must log */ |
| 2385 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2386 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2387 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2388 | if (urilen >= REQURI_LEN) |
| 2389 | urilen = REQURI_LEN - 1; |
| 2390 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2391 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2392 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2393 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2394 | s->do_log(s); |
| 2395 | } else { |
| 2396 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2397 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2398 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2399 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2400 | /* 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] | 2401 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2402 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2403 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2404 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2405 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2406 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2407 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2408 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2409 | txn->flags |= TX_REQ_VER_11; |
| 2410 | |
| 2411 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2412 | 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] | 2413 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2414 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2415 | * we have what looks like a proxied connection instead of a connection, |
| 2416 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2417 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2418 | * happen to do that despite being non-standard :-( |
| 2419 | * We consider that a request not beginning with either '/' or '*' is |
| 2420 | * a proxied connection, which covers both "scheme://location" and |
| 2421 | * CONNECT ip:port. |
| 2422 | */ |
| 2423 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2424 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2425 | txn->flags |= TX_USE_PX_CONN; |
| 2426 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2427 | /* transfer length unknown*/ |
| 2428 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2429 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2430 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2431 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2432 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2433 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2434 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2435 | /* 6: determine the transfer-length. |
| 2436 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2437 | * the presence of a message-body in a REQUEST and its transfer length |
| 2438 | * must be determined that way (in order of precedence) : |
| 2439 | * 1. The presence of a message-body in a request is signaled by the |
| 2440 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2441 | * in the request's header fields. When a request message contains |
| 2442 | * both a message-body of non-zero length and a method that does |
| 2443 | * not define any semantics for that request message-body, then an |
| 2444 | * origin server SHOULD either ignore the message-body or respond |
| 2445 | * with an appropriate error message (e.g., 413). A proxy or |
| 2446 | * gateway, when presented the same request, SHOULD either forward |
| 2447 | * the request inbound with the message- body or ignore the |
| 2448 | * message-body when determining a response. |
| 2449 | * |
| 2450 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2451 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2452 | * transfer-length is defined by the use of this transfer-coding. |
| 2453 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2454 | * transfer-coding is not present, the transfer-length is defined |
| 2455 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2456 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2457 | * 3. If a Content-Length header field is present, its decimal value in |
| 2458 | * OCTETs represents both the entity-length and the transfer-length. |
| 2459 | * If a message is received with both a Transfer-Encoding header |
| 2460 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2461 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2462 | * 4. By the server closing the connection. (Closing the connection |
| 2463 | * cannot be used to indicate the end of a request body, since that |
| 2464 | * would leave no possibility for the server to send back a response.) |
| 2465 | * |
| 2466 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2467 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2468 | * it is terminated by closing the connection. When the "chunked" |
| 2469 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2470 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2471 | */ |
| 2472 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2473 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2474 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2475 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2476 | while ((txn->flags & TX_REQ_VER_11) && |
| 2477 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2478 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2479 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2480 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2481 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2482 | use_close_only = 1; |
| 2483 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2484 | break; |
| 2485 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2486 | } |
| 2487 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2488 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2489 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2490 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2491 | signed long long cl; |
| 2492 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2493 | if (!ctx.vlen) { |
| 2494 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2495 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2496 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2497 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2498 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2499 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2500 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2501 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2502 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2503 | if (cl < 0) { |
| 2504 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2505 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2506 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2507 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2508 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) { |
| 2509 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2510 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2511 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2512 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2513 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2514 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2517 | /* bodyless requests have a known length */ |
| 2518 | if (!use_close_only) |
| 2519 | txn->flags |= TX_REQ_XFER_LEN; |
| 2520 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2521 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2522 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2523 | req->analyse_exp = TICK_ETERNITY; |
| 2524 | return 1; |
| 2525 | |
| 2526 | return_bad_req: |
| 2527 | /* We centralize bad requests processing here */ |
| 2528 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2529 | /* we detected a parsing error. We want to archive this request |
| 2530 | * in the dedicated proxy area for later troubleshooting. |
| 2531 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2532 | 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] | 2533 | } |
| 2534 | |
| 2535 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2536 | txn->status = 400; |
| 2537 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2538 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2539 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2540 | if (s->listener->counters) |
| 2541 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2542 | |
| 2543 | return_prx_cond: |
| 2544 | if (!(s->flags & SN_ERR_MASK)) |
| 2545 | s->flags |= SN_ERR_PRXCOND; |
| 2546 | if (!(s->flags & SN_FINST_MASK)) |
| 2547 | s->flags |= SN_FINST_R; |
| 2548 | |
| 2549 | req->analysers = 0; |
| 2550 | req->analyse_exp = TICK_ETERNITY; |
| 2551 | return 0; |
| 2552 | } |
| 2553 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2554 | /* We reached the stats page through a POST request. |
| 2555 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2556 | * 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] | 2557 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2558 | 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] | 2559 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2560 | struct proxy *px = NULL; |
| 2561 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2562 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2563 | char key[LINESIZE]; |
| 2564 | int action = ST_ADM_ACTION_NONE; |
| 2565 | int reprocess = 0; |
| 2566 | |
| 2567 | int total_servers = 0; |
| 2568 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2569 | |
| 2570 | char *first_param, *cur_param, *next_param, *end_params; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2571 | char *st_cur_param, *st_next_param; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2572 | |
| 2573 | first_param = req->data + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2574 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2575 | |
| 2576 | cur_param = next_param = end_params; |
| 2577 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2578 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2579 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2580 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2581 | return 1; |
| 2582 | } |
| 2583 | else if (end_params > req->data + req->l) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2584 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2585 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2586 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | *end_params = '\0'; |
| 2590 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2591 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2592 | |
| 2593 | /* |
| 2594 | * Parse the parameters in reverse order to only store the last value. |
| 2595 | * From the html form, the backend and the action are at the end. |
| 2596 | */ |
| 2597 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2598 | char *value; |
| 2599 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2600 | |
| 2601 | cur_param--; |
| 2602 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2603 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2604 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2605 | poffset = (cur_param != first_param ? 1 : 0); |
| 2606 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2607 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2608 | strncpy(key, cur_param + poffset, plen); |
| 2609 | key[plen - 1] = '\0'; |
| 2610 | } else { |
| 2611 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2612 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | /* Parse the value */ |
| 2616 | value = key; |
| 2617 | while (*value != '\0' && *value != '=') { |
| 2618 | value++; |
| 2619 | } |
| 2620 | if (*value == '=') { |
| 2621 | /* Ok, a value is found, we can mark the end of the key */ |
| 2622 | *value++ = '\0'; |
| 2623 | } |
| 2624 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2625 | if (!url_decode(key) || !url_decode(value)) |
| 2626 | break; |
| 2627 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2628 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2629 | if (!px && (strcmp(key, "b") == 0)) { |
| 2630 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2631 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2632 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2633 | goto out; |
| 2634 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2635 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2636 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2637 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2638 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2639 | } |
| 2640 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2641 | action = ST_ADM_ACTION_ENABLE; |
| 2642 | } |
| 2643 | else { |
| 2644 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2645 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2649 | if (!(px && action)) { |
| 2650 | /* |
| 2651 | * Indicates that we'll need to reprocess the parameters |
| 2652 | * as soon as backend and action are known |
| 2653 | */ |
| 2654 | if (!reprocess) { |
| 2655 | st_cur_param = cur_param; |
| 2656 | st_next_param = next_param; |
| 2657 | } |
| 2658 | reprocess = 1; |
| 2659 | } |
| 2660 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2661 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2662 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2663 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2664 | /* Not already in maintenance, we can change the server state */ |
| 2665 | sv->state |= SRV_MAINTAIN; |
| 2666 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2667 | altered_servers++; |
| 2668 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2669 | } |
| 2670 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2671 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2672 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2673 | /* Already in maintenance, we can change the server state */ |
| 2674 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2675 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2676 | altered_servers++; |
| 2677 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2678 | } |
| 2679 | break; |
| 2680 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2681 | } else { |
| 2682 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2683 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2684 | } |
| 2685 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2686 | if (reprocess && px && action) { |
| 2687 | /* Now, we know the backend and the action chosen by the user. |
| 2688 | * We can safely restart from the first server parameter |
| 2689 | * to reprocess them |
| 2690 | */ |
| 2691 | cur_param = st_cur_param; |
| 2692 | next_param = st_next_param; |
| 2693 | reprocess = 0; |
| 2694 | goto reprocess_servers; |
| 2695 | } |
| 2696 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2697 | next_param = cur_param; |
| 2698 | } |
| 2699 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2700 | |
| 2701 | if (total_servers == 0) { |
| 2702 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2703 | } |
| 2704 | else if (altered_servers == 0) { |
| 2705 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2706 | } |
| 2707 | else if (altered_servers == total_servers) { |
| 2708 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2709 | } |
| 2710 | else { |
| 2711 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2712 | } |
| 2713 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2714 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2715 | } |
| 2716 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2717 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2718 | * or NULL if everything's OK. |
| 2719 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2720 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2721 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2722 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2723 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2724 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2725 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2726 | int ret = 1; |
| 2727 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2728 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2729 | continue; |
| 2730 | |
| 2731 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2732 | if (rule->cond) { |
| 2733 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2734 | ret = acl_pass(ret); |
| 2735 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2736 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2737 | ret = !ret; |
| 2738 | } |
| 2739 | |
| 2740 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2741 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2742 | return NULL; /* no problem */ |
| 2743 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2744 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2745 | } |
| 2746 | } |
| 2747 | return NULL; |
| 2748 | } |
| 2749 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2750 | /* This stream analyser runs all HTTP request processing which is common to |
| 2751 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2752 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2753 | * 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] | 2754 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2755 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2756 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2757 | 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] | 2758 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2759 | struct http_txn *txn = &s->txn; |
| 2760 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2761 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2762 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2763 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2764 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2765 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2766 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2767 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2768 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2769 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2770 | return 0; |
| 2771 | } |
| 2772 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2773 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2774 | req->analyse_exp = TICK_ETERNITY; |
| 2775 | |
| 2776 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2777 | now_ms, __FUNCTION__, |
| 2778 | s, |
| 2779 | req, |
| 2780 | req->rex, req->wex, |
| 2781 | req->flags, |
| 2782 | req->l, |
| 2783 | req->analysers); |
| 2784 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2785 | /* first check whether we have some ACLs set to block this request */ |
| 2786 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2787 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2788 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2789 | ret = acl_pass(ret); |
| 2790 | if (cond->pol == ACL_COND_UNLESS) |
| 2791 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2792 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2793 | if (ret) { |
| 2794 | txn->status = 403; |
| 2795 | /* let's log the request time */ |
| 2796 | s->logs.tv_request = now; |
| 2797 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2798 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2799 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2801 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2802 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2803 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2804 | 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] | 2805 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2806 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2807 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2808 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2809 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2810 | 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] | 2811 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2812 | else |
| 2813 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2814 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2815 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2816 | 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] | 2817 | txn->status = 403; |
| 2818 | s->logs.tv_request = now; |
| 2819 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2820 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2821 | s->fe->fe_counters.denied_req++; |
| 2822 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2823 | s->be->be_counters.denied_req++; |
| 2824 | if (s->listener->counters) |
| 2825 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2826 | goto return_prx_cond; |
| 2827 | } |
| 2828 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2829 | /* try headers filters */ |
| 2830 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2831 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2832 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2833 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2834 | /* has the request been denied ? */ |
| 2835 | if (txn->flags & TX_CLDENY) { |
| 2836 | /* no need to go further */ |
| 2837 | txn->status = 403; |
| 2838 | /* let's log the request time */ |
| 2839 | s->logs.tv_request = now; |
| 2840 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2841 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2842 | goto return_prx_cond; |
| 2843 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2844 | |
| 2845 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2846 | * which may be the same as the connect timeout if unspecified. |
| 2847 | * If unset, then set it to zero because we really want it to |
| 2848 | * eventually expire. We build the tarpit as an analyser. |
| 2849 | */ |
| 2850 | if (txn->flags & TX_CLTARPIT) { |
| 2851 | buffer_erase(s->req); |
| 2852 | /* wipe the request out so that we can drop the connection early |
| 2853 | * if the client closes first. |
| 2854 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2855 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2856 | req->analysers = 0; /* remove switching rules etc... */ |
| 2857 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2858 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2859 | if (!req->analyse_exp) |
| 2860 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2861 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2862 | return 1; |
| 2863 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2864 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2865 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2866 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2867 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2868 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2869 | * 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] | 2870 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2871 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2872 | * The method consists in checking if options changed between the two calls |
| 2873 | * (implying that either one is non-null, or one of them is non-null and we |
| 2874 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2875 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2876 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2877 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2878 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2879 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2880 | (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] | 2881 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2882 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2883 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2884 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2885 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2886 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2887 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2888 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2889 | tmp = TX_CON_WANT_CLO; |
| 2890 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2891 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2892 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2893 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2894 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2895 | /* parse the Connection header and possibly clean it */ |
| 2896 | int to_del = 0; |
| 2897 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2898 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2899 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2900 | to_del |= 2; /* remove "keep-alive" */ |
| 2901 | if (!(txn->flags & TX_REQ_VER_11)) |
| 2902 | to_del |= 1; /* remove "close" */ |
| 2903 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2904 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2905 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2906 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2907 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2908 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2909 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2910 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2911 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2912 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 2913 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2914 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2915 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2916 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2917 | /* we can be blocked here because the request needs to be authenticated, |
| 2918 | * either to pass or to access stats. |
| 2919 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2920 | 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] | 2921 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2922 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2923 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2924 | if (!realm) |
| 2925 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2926 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2927 | 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] | 2928 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2929 | txn->status = 401; |
| 2930 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2931 | /* on 401 we still count one error, because normal browsing |
| 2932 | * won't significantly increase the counter but brute force |
| 2933 | * attempts will. |
| 2934 | */ |
| 2935 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2936 | goto return_prx_cond; |
| 2937 | } |
| 2938 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2939 | /* add request headers from the rule sets in the same order */ |
| 2940 | list_for_each_entry(wl, &px->req_add, list) { |
| 2941 | if (wl->cond) { |
| 2942 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2943 | ret = acl_pass(ret); |
| 2944 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2945 | ret = !ret; |
| 2946 | if (!ret) |
| 2947 | continue; |
| 2948 | } |
| 2949 | |
| 2950 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 2951 | goto return_bad_req; |
| 2952 | } |
| 2953 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2954 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2955 | struct stats_admin_rule *stats_admin_rule; |
| 2956 | |
| 2957 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2958 | * FIXME!!! that one is rather dangerous, we want to |
| 2959 | * make it follow standard rules (eg: clear req->analysers). |
| 2960 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2961 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2962 | /* now check whether we have some admin rules for this request */ |
| 2963 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2964 | int ret = 1; |
| 2965 | |
| 2966 | if (stats_admin_rule->cond) { |
| 2967 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 2968 | ret = acl_pass(ret); |
| 2969 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2970 | ret = !ret; |
| 2971 | } |
| 2972 | |
| 2973 | if (ret) { |
| 2974 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2975 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2976 | break; |
| 2977 | } |
| 2978 | } |
| 2979 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2980 | /* Was the status page requested with a POST ? */ |
| 2981 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2982 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2983 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2984 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2985 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2986 | */ |
| 2987 | if (txn->flags & TX_REQ_VER_11) { |
| 2988 | struct hdr_ctx ctx; |
| 2989 | ctx.idx = 0; |
| 2990 | /* Expect is allowed in 1.1, look for it */ |
| 2991 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 2992 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 2993 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 2994 | } |
| 2995 | } |
| 2996 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2997 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2998 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2999 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3000 | /* we need more data */ |
| 3001 | req->analysers |= an_bit; |
| 3002 | buffer_dont_connect(req); |
| 3003 | return 0; |
| 3004 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3005 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3006 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3007 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3008 | } |
| 3009 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3010 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3011 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3012 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3013 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3014 | s->rep->prod->applet.private = s; |
| 3015 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3016 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3017 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3018 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3019 | |
| 3020 | return 0; |
| 3021 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3022 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3023 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3024 | /* check whether we have some ACLs set to redirect this request */ |
| 3025 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3026 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3027 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3028 | if (rule->cond) { |
| 3029 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3030 | ret = acl_pass(ret); |
| 3031 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3032 | ret = !ret; |
| 3033 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3034 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3035 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3036 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3037 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3038 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3039 | /* build redirect message */ |
| 3040 | switch(rule->code) { |
| 3041 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3042 | msg_fmt = HTTP_303; |
| 3043 | break; |
| 3044 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3045 | msg_fmt = HTTP_301; |
| 3046 | break; |
| 3047 | case 302: |
| 3048 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3049 | msg_fmt = HTTP_302; |
| 3050 | break; |
| 3051 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3052 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3053 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3054 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3055 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3056 | switch(rule->type) { |
| 3057 | case REDIRECT_TYPE_PREFIX: { |
| 3058 | const char *path; |
| 3059 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3061 | path = http_get_path(txn); |
| 3062 | /* build message using path */ |
| 3063 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3064 | 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] | 3065 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3066 | int qs = 0; |
| 3067 | while (qs < pathlen) { |
| 3068 | if (path[qs] == '?') { |
| 3069 | pathlen = qs; |
| 3070 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3071 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3072 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3073 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3074 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3075 | } else { |
| 3076 | path = "/"; |
| 3077 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3078 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3079 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3080 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3081 | goto return_bad_req; |
| 3082 | |
| 3083 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3084 | * add anything, otherwise it makes it hard for the user to |
| 3085 | * configure a self-redirection. |
| 3086 | */ |
| 3087 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3088 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3089 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3092 | /* add path */ |
| 3093 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3094 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3095 | |
| 3096 | /* append a slash at the end of the location is needed and missing */ |
| 3097 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3098 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3099 | if (rdr.len > rdr.size - 5) |
| 3100 | goto return_bad_req; |
| 3101 | rdr.str[rdr.len] = '/'; |
| 3102 | rdr.len++; |
| 3103 | } |
| 3104 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3105 | break; |
| 3106 | } |
| 3107 | case REDIRECT_TYPE_LOCATION: |
| 3108 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3109 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3110 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3111 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3112 | /* add location */ |
| 3113 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3114 | rdr.len += rule->rdr_len; |
| 3115 | break; |
| 3116 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3117 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3118 | if (rule->cookie_len) { |
| 3119 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3120 | rdr.len += 14; |
| 3121 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3122 | rdr.len += rule->cookie_len; |
| 3123 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3124 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3125 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3126 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3127 | /* add end of headers and the keep-alive/close status. |
| 3128 | * We may choose to set keep-alive if the Location begins |
| 3129 | * with a slash, because the client will come back to the |
| 3130 | * same server. |
| 3131 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3132 | txn->status = rule->code; |
| 3133 | /* let's log the request time */ |
| 3134 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3135 | |
| 3136 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3137 | (txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3138 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3139 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3140 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3141 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3142 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3143 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3144 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3145 | rdr.len += 30; |
| 3146 | } else { |
| 3147 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3148 | rdr.len += 24; |
| 3149 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3150 | } |
| 3151 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3152 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3153 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3154 | /* "eat" the request */ |
| 3155 | buffer_ignore(req, msg->sov - msg->som); |
| 3156 | msg->som = msg->sov; |
| 3157 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3158 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3159 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3160 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3161 | break; |
| 3162 | } else { |
| 3163 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3164 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3165 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3166 | rdr.len += 29; |
| 3167 | } else { |
| 3168 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3169 | rdr.len += 23; |
| 3170 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3171 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3172 | goto return_prx_cond; |
| 3173 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3174 | } |
| 3175 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3176 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3177 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3178 | * If this happens, then the data will not come immediately, so we must |
| 3179 | * send all what we have without waiting. Note that due to the small gain |
| 3180 | * in waiting for the body of the request, it's easier to simply put the |
| 3181 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3182 | * itself once used. |
| 3183 | */ |
| 3184 | req->flags |= BF_SEND_DONTWAIT; |
| 3185 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3186 | /* that's OK for us now, let's move on to next analysers */ |
| 3187 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3188 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3189 | return_bad_req: |
| 3190 | /* We centralize bad requests processing here */ |
| 3191 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3192 | /* we detected a parsing error. We want to archive this request |
| 3193 | * in the dedicated proxy area for later troubleshooting. |
| 3194 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3195 | 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] | 3196 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3197 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3198 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3199 | txn->status = 400; |
| 3200 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3201 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3202 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3203 | if (s->listener->counters) |
| 3204 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3205 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3206 | return_prx_cond: |
| 3207 | if (!(s->flags & SN_ERR_MASK)) |
| 3208 | s->flags |= SN_ERR_PRXCOND; |
| 3209 | if (!(s->flags & SN_FINST_MASK)) |
| 3210 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3211 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3212 | req->analysers = 0; |
| 3213 | req->analyse_exp = TICK_ETERNITY; |
| 3214 | return 0; |
| 3215 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3216 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3217 | /* This function performs all the processing enabled for the current request. |
| 3218 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3219 | * needs more data, encounters an error, or wants to immediately abort the |
| 3220 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3221 | */ |
| 3222 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3223 | { |
| 3224 | struct http_txn *txn = &s->txn; |
| 3225 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3226 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3227 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3228 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3229 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3230 | return 0; |
| 3231 | } |
| 3232 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3233 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3234 | now_ms, __FUNCTION__, |
| 3235 | s, |
| 3236 | req, |
| 3237 | req->rex, req->wex, |
| 3238 | req->flags, |
| 3239 | req->l, |
| 3240 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3242 | /* |
| 3243 | * Right now, we know that we have processed the entire headers |
| 3244 | * and that unwanted requests have been filtered out. We can do |
| 3245 | * whatever we want with the remaining request. Also, now we |
| 3246 | * may have separate values for ->fe, ->be. |
| 3247 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3248 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3249 | /* |
| 3250 | * If HTTP PROXY is set we simply get remote server address |
| 3251 | * parsing incoming request. |
| 3252 | */ |
| 3253 | 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] | 3254 | 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] | 3255 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3256 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3257 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3258 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3259 | * Note that doing so might move headers in the request, but |
| 3260 | * the fields will stay coherent and the URI will not move. |
| 3261 | * This should only be performed in the backend. |
| 3262 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3263 | 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] | 3264 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3265 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3266 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3267 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3268 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3269 | * so let's do the same now. |
| 3270 | */ |
| 3271 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3272 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3273 | 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] | 3274 | 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] | 3275 | } |
| 3276 | |
| 3277 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3278 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3279 | * asks for it. |
| 3280 | */ |
| 3281 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3282 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3283 | |
| 3284 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Sagi Bashari | 1611e2d | 2011-10-08 22:48:48 +0200 | [diff] [blame] | 3285 | 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] | 3286 | /* The header is set to be added only if none is present |
| 3287 | * and we found it, so don't do anything. |
| 3288 | */ |
| 3289 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3290 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3291 | /* Add an X-Forwarded-For header unless the source IP is |
| 3292 | * in the 'except' network range. |
| 3293 | */ |
| 3294 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3295 | (((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] | 3296 | != s->fe->except_net.s_addr) && |
| 3297 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3298 | (((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] | 3299 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3300 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3301 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3302 | 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] | 3303 | |
| 3304 | /* Note: we rely on the backend to get the header name to be used for |
| 3305 | * x-forwarded-for, because the header is really meant for the backends. |
| 3306 | * However, if the backend did not specify any option, we have to rely |
| 3307 | * on the frontend's header name. |
| 3308 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3309 | if (s->be->fwdfor_hdr_len) { |
| 3310 | len = s->be->fwdfor_hdr_len; |
| 3311 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3312 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3313 | len = s->fe->fwdfor_hdr_len; |
| 3314 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3315 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3316 | 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] | 3317 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3318 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3319 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3320 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3321 | } |
| 3322 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3323 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3324 | /* FIXME: for the sake of completeness, we should also support |
| 3325 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3326 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3327 | int len; |
| 3328 | char pn[INET6_ADDRSTRLEN]; |
| 3329 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3330 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3331 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3332 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3333 | /* Note: we rely on the backend to get the header name to be used for |
| 3334 | * x-forwarded-for, because the header is really meant for the backends. |
| 3335 | * However, if the backend did not specify any option, we have to rely |
| 3336 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3337 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3338 | if (s->be->fwdfor_hdr_len) { |
| 3339 | len = s->be->fwdfor_hdr_len; |
| 3340 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3341 | } else { |
| 3342 | len = s->fe->fwdfor_hdr_len; |
| 3343 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3344 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3345 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3346 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3347 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3348 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3349 | goto return_bad_req; |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3354 | * 10: add X-Original-To if either the frontend or the backend |
| 3355 | * asks for it. |
| 3356 | */ |
| 3357 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3358 | |
| 3359 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3360 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3361 | /* Add an X-Original-To header unless the destination IP is |
| 3362 | * in the 'except' network range. |
| 3363 | */ |
| 3364 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3365 | get_frt_addr(s); |
| 3366 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3367 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3368 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3369 | (((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] | 3370 | != s->fe->except_to.s_addr) && |
| 3371 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3372 | (((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] | 3373 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3374 | int len; |
| 3375 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3376 | 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] | 3377 | |
| 3378 | /* Note: we rely on the backend to get the header name to be used for |
| 3379 | * x-original-to, because the header is really meant for the backends. |
| 3380 | * However, if the backend did not specify any option, we have to rely |
| 3381 | * on the frontend's header name. |
| 3382 | */ |
| 3383 | if (s->be->orgto_hdr_len) { |
| 3384 | len = s->be->orgto_hdr_len; |
| 3385 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3386 | } else { |
| 3387 | len = s->fe->orgto_hdr_len; |
| 3388 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3389 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3390 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3391 | |
| 3392 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3393 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3394 | goto return_bad_req; |
| 3395 | } |
| 3396 | } |
| 3397 | } |
| 3398 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3399 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3400 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3401 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3402 | unsigned int want_flags = 0; |
| 3403 | |
| 3404 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3405 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3406 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3407 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3408 | want_flags |= TX_CON_CLO_SET; |
| 3409 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3410 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3411 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3412 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3413 | want_flags |= TX_CON_KAL_SET; |
| 3414 | } |
| 3415 | |
| 3416 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3417 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3418 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3419 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3420 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3421 | /* If we have no server assigned yet and we're balancing on url_param |
| 3422 | * with a POST request, we may be interested in checking the body for |
| 3423 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3424 | */ |
| 3425 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3426 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3427 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 3428 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3429 | buffer_dont_connect(req); |
| 3430 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3431 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3432 | |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3433 | if (txn->flags & TX_REQ_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3434 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3435 | #ifdef TCP_QUICKACK |
| 3436 | /* We expect some data from the client. Unless we know for sure |
| 3437 | * we already have a full request, we have to re-enable quick-ack |
| 3438 | * in case we previously disabled it, otherwise we might cause |
| 3439 | * the client to delay further data. |
| 3440 | */ |
| 3441 | if ((s->listener->options & LI_O_NOQUICKACK) && |
| 3442 | ((txn->flags & TX_REQ_TE_CHNK) || |
| 3443 | (msg->body_len > req->l - txn->req.eoh - 2))) |
| 3444 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3445 | #endif |
| 3446 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3447 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3448 | /************************************************************* |
| 3449 | * OK, that's finished for the headers. We have done what we * |
| 3450 | * could. Let's switch to the DATA state. * |
| 3451 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3452 | req->analyse_exp = TICK_ETERNITY; |
| 3453 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3454 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3455 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3456 | /* OK let's go on with the BODY now */ |
| 3457 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3458 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3459 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3460 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3461 | /* we detected a parsing error. We want to archive this request |
| 3462 | * in the dedicated proxy area for later troubleshooting. |
| 3463 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3464 | 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] | 3465 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3466 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3467 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3468 | txn->status = 400; |
| 3469 | req->analysers = 0; |
| 3470 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3471 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3472 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3473 | if (s->listener->counters) |
| 3474 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3475 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3476 | if (!(s->flags & SN_ERR_MASK)) |
| 3477 | s->flags |= SN_ERR_PRXCOND; |
| 3478 | if (!(s->flags & SN_FINST_MASK)) |
| 3479 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3480 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3481 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3482 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3483 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3484 | * returns zero, at the beginning because it prevents any other processing |
| 3485 | * from occurring, and at the end because it terminates the request. |
| 3486 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3487 | 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] | 3488 | { |
| 3489 | struct http_txn *txn = &s->txn; |
| 3490 | |
| 3491 | /* This connection is being tarpitted. The CLIENT side has |
| 3492 | * already set the connect expiration date to the right |
| 3493 | * timeout. We just have to check that the client is still |
| 3494 | * there and that the timeout has not expired. |
| 3495 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3496 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3497 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3498 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3499 | return 0; |
| 3500 | |
| 3501 | /* We will set the queue timer to the time spent, just for |
| 3502 | * logging purposes. We fake a 500 server error, so that the |
| 3503 | * attacker will not suspect his connection has been tarpitted. |
| 3504 | * It will not cause trouble to the logs because we can exclude |
| 3505 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3506 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3507 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3508 | |
| 3509 | txn->status = 500; |
| 3510 | if (req->flags != BF_READ_ERROR) |
| 3511 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3512 | |
| 3513 | req->analysers = 0; |
| 3514 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3515 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3516 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3517 | if (s->listener->counters) |
| 3518 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3519 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3520 | if (!(s->flags & SN_ERR_MASK)) |
| 3521 | s->flags |= SN_ERR_PRXCOND; |
| 3522 | if (!(s->flags & SN_FINST_MASK)) |
| 3523 | s->flags |= SN_FINST_T; |
| 3524 | return 0; |
| 3525 | } |
| 3526 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3527 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3528 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3529 | * must only be called after the standard HTTP request processing has occurred, |
| 3530 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3531 | * read more data, or 1 once it has completed its analysis. |
| 3532 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3533 | 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] | 3534 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3535 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3536 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3537 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3538 | |
| 3539 | /* We have to parse the HTTP request body to find any required data. |
| 3540 | * "balance url_param check_post" should have been the only way to get |
| 3541 | * into this. We were brought here after HTTP header analysis, so all |
| 3542 | * related structures are ready. |
| 3543 | */ |
| 3544 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3545 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3546 | goto missing_data; |
| 3547 | |
| 3548 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3549 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3550 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3551 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3552 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3553 | struct hdr_ctx ctx; |
| 3554 | ctx.idx = 0; |
| 3555 | /* Expect is allowed in 1.1, look for it */ |
| 3556 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3557 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3558 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3559 | } |
| 3560 | } |
| 3561 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3562 | } |
| 3563 | |
| 3564 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3565 | /* we have msg->col and msg->sov which both point to the first |
| 3566 | * byte of message body. msg->som still points to the beginning |
| 3567 | * of the message. We must save the body in req->lr because it |
| 3568 | * survives buffer re-alignments. |
| 3569 | */ |
| 3570 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3571 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3572 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3573 | else |
| 3574 | msg->msg_state = HTTP_MSG_DATA; |
| 3575 | } |
| 3576 | |
| 3577 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3578 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3579 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3580 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3581 | */ |
| 3582 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3583 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3584 | if (!ret) |
| 3585 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3586 | else if (ret < 0) { |
| 3587 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3588 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3589 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3590 | } |
| 3591 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3592 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3593 | * We have the first non-header byte in msg->col, which is either the |
| 3594 | * beginning of the chunk size or of the data. The first data byte is in |
| 3595 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3596 | * 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] | 3597 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3598 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3599 | if (msg->body_len < limit) |
| 3600 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3601 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3602 | if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3603 | goto http_end; |
| 3604 | |
| 3605 | missing_data: |
| 3606 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3607 | if (req->flags & BF_FULL) { |
| 3608 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3609 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3610 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3611 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3612 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3613 | txn->status = 408; |
| 3614 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3615 | |
| 3616 | if (!(s->flags & SN_ERR_MASK)) |
| 3617 | s->flags |= SN_ERR_CLITO; |
| 3618 | if (!(s->flags & SN_FINST_MASK)) |
| 3619 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3620 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3621 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3622 | |
| 3623 | /* we get here if we need to wait for more data */ |
| 3624 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3625 | /* Not enough data. We'll re-use the http-request |
| 3626 | * timeout here. Ideally, we should set the timeout |
| 3627 | * relative to the accept() date. We just set the |
| 3628 | * request timeout once at the beginning of the |
| 3629 | * request. |
| 3630 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3631 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3632 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3633 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3634 | return 0; |
| 3635 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3636 | |
| 3637 | http_end: |
| 3638 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3639 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3640 | req->analysers &= ~an_bit; |
| 3641 | req->analyse_exp = TICK_ETERNITY; |
| 3642 | return 1; |
| 3643 | |
| 3644 | return_bad_req: /* let's centralize all bad requests */ |
| 3645 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3646 | txn->status = 400; |
| 3647 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3648 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3649 | if (!(s->flags & SN_ERR_MASK)) |
| 3650 | s->flags |= SN_ERR_PRXCOND; |
| 3651 | if (!(s->flags & SN_FINST_MASK)) |
| 3652 | s->flags |= SN_FINST_R; |
| 3653 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3654 | return_err_msg: |
| 3655 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3656 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3657 | if (s->listener->counters) |
| 3658 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3659 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3660 | } |
| 3661 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3662 | int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) { |
| 3663 | |
| 3664 | struct hdr_ctx ctx; |
| 3665 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3666 | char *hdr_name = be->server_id_hdr_name; |
| 3667 | int hdr_name_len = be->server_id_hdr_len; |
| 3668 | |
| 3669 | char *hdr_val; |
| 3670 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3671 | ctx.idx = 0; |
| 3672 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3673 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3674 | /* remove any existing values from the header */ |
| 3675 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 3676 | } |
| 3677 | |
| 3678 | /* Add the new header requested with the server value */ |
| 3679 | hdr_val = trash; |
| 3680 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3681 | hdr_val += hdr_name_len; |
| 3682 | *hdr_val++ = ':'; |
| 3683 | *hdr_val++ = ' '; |
| 3684 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
| 3685 | http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash); |
| 3686 | |
| 3687 | return 0; |
| 3688 | } |
| 3689 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3690 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3691 | * right now but it works. |
| 3692 | */ |
| 3693 | void http_end_txn_clean_session(struct session *s) |
| 3694 | { |
| 3695 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3696 | * server's connections. We need a safer way to reinitialize buffer |
| 3697 | * flags. We also need a more accurate method for computing per-request |
| 3698 | * data. |
| 3699 | */ |
| 3700 | http_silent_debug(__LINE__, s); |
| 3701 | |
| 3702 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3703 | s->req->cons->shutr(s->req->cons); |
| 3704 | s->req->cons->shutw(s->req->cons); |
| 3705 | |
| 3706 | http_silent_debug(__LINE__, s); |
| 3707 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3708 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3709 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3710 | if (unlikely(s->srv_conn)) |
| 3711 | sess_change_server(s, NULL); |
| 3712 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3713 | |
| 3714 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3715 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3716 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3717 | |
| 3718 | if (s->txn.status) { |
| 3719 | int n; |
| 3720 | |
| 3721 | n = s->txn.status / 100; |
| 3722 | if (n < 1 || n > 5) |
| 3723 | n = 0; |
| 3724 | |
| 3725 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3726 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3727 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3728 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3729 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3730 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | /* don't count other requests' data */ |
| 3734 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3735 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3736 | |
| 3737 | /* let's do a final log if we need it */ |
| 3738 | if (s->logs.logwait && |
| 3739 | !(s->flags & SN_MONITOR) && |
| 3740 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3741 | s->do_log(s); |
| 3742 | } |
| 3743 | |
| 3744 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3745 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3746 | tv_zero(&s->logs.tv_request); |
| 3747 | s->logs.t_queue = -1; |
| 3748 | s->logs.t_connect = -1; |
| 3749 | s->logs.t_data = -1; |
| 3750 | s->logs.t_close = 0; |
| 3751 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3752 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3753 | |
| 3754 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3755 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3756 | |
| 3757 | if (s->pend_pos) |
| 3758 | pendconn_free(s->pend_pos); |
| 3759 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3760 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3761 | if (s->flags & SN_CURR_SESS) { |
| 3762 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3763 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3764 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3765 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3766 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3767 | } |
| 3768 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3769 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3770 | |
| 3771 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3772 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3773 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3774 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3775 | s->req->cons->err_loc = NULL; |
| 3776 | s->req->cons->exp = TICK_ETERNITY; |
| 3777 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3778 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 3779 | 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] | 3780 | 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] | 3781 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3782 | s->txn.meth = 0; |
| 3783 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3784 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3785 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3786 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3787 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3788 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 3789 | s->req->flags |= BF_NEVER_WAIT; |
| 3790 | s->rep->flags |= BF_NEVER_WAIT; |
| 3791 | } |
| 3792 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3793 | /* if the request buffer is not empty, it means we're |
| 3794 | * about to process another request, so send pending |
| 3795 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3796 | * Just don't do this if the buffer is close to be full, |
| 3797 | * because the request will wait for it to flush a little |
| 3798 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3799 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3800 | if (s->req->l > s->req->send_max) { |
| 3801 | if (s->rep->send_max && |
| 3802 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3803 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3804 | s->rep->flags |= BF_EXPECT_MORE; |
| 3805 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3806 | |
| 3807 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3808 | buffer_auto_read(s->req); |
| 3809 | buffer_auto_close(s->req); |
| 3810 | buffer_auto_read(s->rep); |
| 3811 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3812 | |
| 3813 | /* make ->lr point to the first non-forwarded byte */ |
| 3814 | s->req->lr = s->req->w + s->req->send_max; |
| 3815 | if (s->req->lr >= s->req->data + s->req->size) |
| 3816 | s->req->lr -= s->req->size; |
| 3817 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3818 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3819 | s->rep->lr -= s->req->size; |
| 3820 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3821 | s->req->analysers = s->listener->analysers; |
| 3822 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3823 | s->rep->analysers = 0; |
| 3824 | |
| 3825 | http_silent_debug(__LINE__, s); |
| 3826 | } |
| 3827 | |
| 3828 | |
| 3829 | /* This function updates the request state machine according to the response |
| 3830 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3831 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3832 | * it is only used to find when a request/response couple is complete. Both |
| 3833 | * this function and its equivalent should loop until both return zero. It |
| 3834 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3835 | */ |
| 3836 | int http_sync_req_state(struct session *s) |
| 3837 | { |
| 3838 | struct buffer *buf = s->req; |
| 3839 | struct http_txn *txn = &s->txn; |
| 3840 | unsigned int old_flags = buf->flags; |
| 3841 | unsigned int old_state = txn->req.msg_state; |
| 3842 | |
| 3843 | http_silent_debug(__LINE__, s); |
| 3844 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3845 | return 0; |
| 3846 | |
| 3847 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3848 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3849 | * We can shut the read side unless we want to abort_on_close, |
| 3850 | * or we have a POST request. The issue with POST requests is |
| 3851 | * that some browsers still send a CRLF after the request, and |
| 3852 | * this CRLF must be read so that it does not remain in the kernel |
| 3853 | * buffers, otherwise a close could cause an RST on some systems |
| 3854 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3855 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3856 | 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] | 3857 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3858 | |
| 3859 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3860 | goto wait_other_side; |
| 3861 | |
| 3862 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3863 | /* The server has not finished to respond, so we |
| 3864 | * don't want to move in order not to upset it. |
| 3865 | */ |
| 3866 | goto wait_other_side; |
| 3867 | } |
| 3868 | |
| 3869 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3870 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3871 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3872 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3873 | goto wait_other_side; |
| 3874 | } |
| 3875 | |
| 3876 | /* When we get here, it means that both the request and the |
| 3877 | * response have finished receiving. Depending on the connection |
| 3878 | * mode, we'll have to wait for the last bytes to leave in either |
| 3879 | * direction, and sometimes for a close to be effective. |
| 3880 | */ |
| 3881 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3882 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3883 | /* Server-close mode : queue a connection close to the server */ |
| 3884 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3886 | } |
| 3887 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3888 | /* Option forceclose is set, or either side wants to close, |
| 3889 | * let's enforce it now that we're not expecting any new |
| 3890 | * data to come. The caller knows the session is complete |
| 3891 | * once both states are CLOSED. |
| 3892 | */ |
| 3893 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3894 | buffer_shutr_now(buf); |
| 3895 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3896 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3897 | } |
| 3898 | else { |
| 3899 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3900 | * mode does not set the body analyser, we can't reach this place |
| 3901 | * in tunnel mode, so we're left with keep-alive only. |
| 3902 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3903 | */ |
| 3904 | buffer_auto_read(buf); |
| 3905 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3909 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3910 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3911 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3912 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3913 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3914 | goto http_msg_closing; |
| 3915 | } |
| 3916 | else { |
| 3917 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3918 | goto http_msg_closed; |
| 3919 | } |
| 3920 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3921 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3925 | http_msg_closing: |
| 3926 | /* nothing else to forward, just waiting for the output buffer |
| 3927 | * to be empty and for the shutw_now to take effect. |
| 3928 | */ |
| 3929 | if (buf->flags & BF_OUT_EMPTY) { |
| 3930 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3931 | goto http_msg_closed; |
| 3932 | } |
| 3933 | else if (buf->flags & BF_SHUTW) { |
| 3934 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3935 | goto wait_other_side; |
| 3936 | } |
| 3937 | } |
| 3938 | |
| 3939 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3940 | http_msg_closed: |
| 3941 | goto wait_other_side; |
| 3942 | } |
| 3943 | |
| 3944 | wait_other_side: |
| 3945 | http_silent_debug(__LINE__, s); |
| 3946 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3947 | } |
| 3948 | |
| 3949 | |
| 3950 | /* This function updates the response state machine according to the request |
| 3951 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3952 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3953 | * it is only used to find when a request/response couple is complete. Both |
| 3954 | * this function and its equivalent should loop until both return zero. It |
| 3955 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3956 | */ |
| 3957 | int http_sync_res_state(struct session *s) |
| 3958 | { |
| 3959 | struct buffer *buf = s->rep; |
| 3960 | struct http_txn *txn = &s->txn; |
| 3961 | unsigned int old_flags = buf->flags; |
| 3962 | unsigned int old_state = txn->rsp.msg_state; |
| 3963 | |
| 3964 | http_silent_debug(__LINE__, s); |
| 3965 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3966 | return 0; |
| 3967 | |
| 3968 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3969 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3970 | * still monitor the server connection for a possible close |
| 3971 | * while the request is being uploaded, so we don't disable |
| 3972 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3973 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3974 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3975 | |
| 3976 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3977 | goto wait_other_side; |
| 3978 | |
| 3979 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3980 | /* The client seems to still be sending data, probably |
| 3981 | * because we got an error response during an upload. |
| 3982 | * We have the choice of either breaking the connection |
| 3983 | * or letting it pass through. Let's do the later. |
| 3984 | */ |
| 3985 | goto wait_other_side; |
| 3986 | } |
| 3987 | |
| 3988 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3989 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3990 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3991 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3992 | goto wait_other_side; |
| 3993 | } |
| 3994 | |
| 3995 | /* When we get here, it means that both the request and the |
| 3996 | * response have finished receiving. Depending on the connection |
| 3997 | * mode, we'll have to wait for the last bytes to leave in either |
| 3998 | * direction, and sometimes for a close to be effective. |
| 3999 | */ |
| 4000 | |
| 4001 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4002 | /* Server-close mode : shut read and wait for the request |
| 4003 | * side to close its output buffer. The caller will detect |
| 4004 | * when we're in DONE and the other is in CLOSED and will |
| 4005 | * catch that for the final cleanup. |
| 4006 | */ |
| 4007 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4008 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4009 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4010 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4011 | /* Option forceclose is set, or either side wants to close, |
| 4012 | * let's enforce it now that we're not expecting any new |
| 4013 | * data to come. The caller knows the session is complete |
| 4014 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4015 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4016 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4017 | buffer_shutr_now(buf); |
| 4018 | buffer_shutw_now(buf); |
| 4019 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4020 | } |
| 4021 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4022 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4023 | * mode does not set the body analyser, we can't reach this place |
| 4024 | * in tunnel mode, so we're left with keep-alive only. |
| 4025 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4026 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4027 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4028 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4032 | /* if we've just closed an output, let's switch */ |
| 4033 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4034 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4035 | goto http_msg_closing; |
| 4036 | } |
| 4037 | else { |
| 4038 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4039 | goto http_msg_closed; |
| 4040 | } |
| 4041 | } |
| 4042 | goto wait_other_side; |
| 4043 | } |
| 4044 | |
| 4045 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4046 | http_msg_closing: |
| 4047 | /* nothing else to forward, just waiting for the output buffer |
| 4048 | * to be empty and for the shutw_now to take effect. |
| 4049 | */ |
| 4050 | if (buf->flags & BF_OUT_EMPTY) { |
| 4051 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4052 | goto http_msg_closed; |
| 4053 | } |
| 4054 | else if (buf->flags & BF_SHUTW) { |
| 4055 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4056 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4057 | if (target_srv(&s->target)) |
| 4058 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4059 | goto wait_other_side; |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4064 | http_msg_closed: |
| 4065 | /* drop any pending data */ |
| 4066 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4067 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4068 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4069 | goto wait_other_side; |
| 4070 | } |
| 4071 | |
| 4072 | wait_other_side: |
| 4073 | http_silent_debug(__LINE__, s); |
| 4074 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4075 | } |
| 4076 | |
| 4077 | |
| 4078 | /* Resync the request and response state machines. Return 1 if either state |
| 4079 | * changes. |
| 4080 | */ |
| 4081 | int http_resync_states(struct session *s) |
| 4082 | { |
| 4083 | struct http_txn *txn = &s->txn; |
| 4084 | int old_req_state = txn->req.msg_state; |
| 4085 | int old_res_state = txn->rsp.msg_state; |
| 4086 | |
| 4087 | http_silent_debug(__LINE__, s); |
| 4088 | http_sync_req_state(s); |
| 4089 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4090 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4091 | if (!http_sync_res_state(s)) |
| 4092 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4093 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4094 | if (!http_sync_req_state(s)) |
| 4095 | break; |
| 4096 | } |
| 4097 | http_silent_debug(__LINE__, s); |
| 4098 | /* OK, both state machines agree on a compatible state. |
| 4099 | * There are a few cases we're interested in : |
| 4100 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4101 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4102 | * directions, so let's simply disable both analysers. |
| 4103 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4104 | * request. |
| 4105 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4106 | * with server-close mode means we've completed one request and we |
| 4107 | * must re-initialize the server connection. |
| 4108 | */ |
| 4109 | |
| 4110 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4111 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4112 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4113 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4114 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4115 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4116 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4117 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4118 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4119 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4120 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4121 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4122 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4123 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4124 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4125 | s->rep->analysers = 0; |
| 4126 | buffer_auto_close(s->rep); |
| 4127 | buffer_auto_read(s->rep); |
| 4128 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4129 | buffer_abort(s->req); |
| 4130 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4131 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4132 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4133 | } |
| 4134 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4135 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4136 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4137 | /* server-close: terminate this server connection and |
| 4138 | * reinitialize a fresh-new transaction. |
| 4139 | */ |
| 4140 | http_end_txn_clean_session(s); |
| 4141 | } |
| 4142 | |
| 4143 | http_silent_debug(__LINE__, s); |
| 4144 | return txn->req.msg_state != old_req_state || |
| 4145 | txn->rsp.msg_state != old_res_state; |
| 4146 | } |
| 4147 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4148 | /* This function is an analyser which forwards request body (including chunk |
| 4149 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4150 | * zero byte. The only situation where it must not be called is when we're in |
| 4151 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4152 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4153 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4154 | * 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] | 4155 | * 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] | 4156 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4157 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4158 | */ |
| 4159 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4160 | { |
| 4161 | struct http_txn *txn = &s->txn; |
| 4162 | struct http_msg *msg = &s->txn.req; |
| 4163 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4164 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4165 | return 0; |
| 4166 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4167 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4168 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4169 | /* Output closed while we were sending data. We must abort and |
| 4170 | * wake the other side up. |
| 4171 | */ |
| 4172 | msg->msg_state = HTTP_MSG_ERROR; |
| 4173 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4174 | return 1; |
| 4175 | } |
| 4176 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4177 | /* in most states, we should abort in case of early close */ |
| 4178 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4179 | |
| 4180 | /* Note that we don't have to send 100-continue back because we don't |
| 4181 | * need the data to complete our job, and it's up to the server to |
| 4182 | * decide whether to return 100, 417 or anything else in return of |
| 4183 | * an "Expect: 100-continue" header. |
| 4184 | */ |
| 4185 | |
| 4186 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4187 | /* we have msg->col and msg->sov which both point to the first |
| 4188 | * byte of message body. msg->som still points to the beginning |
| 4189 | * of the message. We must save the body in req->lr because it |
| 4190 | * survives buffer re-alignments. |
| 4191 | */ |
| 4192 | req->lr = req->data + msg->sov; |
| 4193 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4194 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4195 | else { |
| 4196 | msg->msg_state = HTTP_MSG_DATA; |
| 4197 | } |
| 4198 | } |
| 4199 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4200 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4201 | int bytes; |
| 4202 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4203 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4204 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4205 | bytes = msg->sov - msg->som; |
| 4206 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4207 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4208 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4209 | bytes += req->size; |
| 4210 | msg->chunk_len += (unsigned int)bytes; |
| 4211 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4212 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4213 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4214 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4215 | /* must still forward */ |
| 4216 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4217 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4218 | |
| 4219 | /* nothing left to forward */ |
| 4220 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4221 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4222 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4223 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4224 | } |
| 4225 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4226 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4227 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4228 | * TRAILERS state. |
| 4229 | */ |
| 4230 | int ret = http_parse_chunk_size(req, msg); |
| 4231 | |
| 4232 | if (!ret) |
| 4233 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4234 | else if (ret < 0) { |
| 4235 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4236 | if (msg->err_pos >= 0) |
| 4237 | 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] | 4238 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4239 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4240 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4241 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4242 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4243 | /* we want the CRLF after the data */ |
| 4244 | int ret; |
| 4245 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4246 | req->lr = req->w + req->send_max; |
| 4247 | if (req->lr >= req->data + req->size) |
| 4248 | req->lr -= req->size; |
| 4249 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4250 | ret = http_skip_chunk_crlf(req, msg); |
| 4251 | |
| 4252 | if (ret == 0) |
| 4253 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4254 | else if (ret < 0) { |
| 4255 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4256 | if (msg->err_pos >= 0) |
| 4257 | 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] | 4258 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4259 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4260 | /* we're in MSG_CHUNK_SIZE now */ |
| 4261 | } |
| 4262 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4263 | int ret = http_forward_trailers(req, msg); |
| 4264 | |
| 4265 | if (ret == 0) |
| 4266 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4267 | else if (ret < 0) { |
| 4268 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4269 | if (msg->err_pos >= 0) |
| 4270 | 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] | 4271 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4272 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4273 | /* we're in HTTP_MSG_DONE now */ |
| 4274 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4275 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4276 | int old_state = msg->msg_state; |
| 4277 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4278 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4279 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4280 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4281 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4282 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4283 | if (http_resync_states(s)) { |
| 4284 | /* some state changes occurred, maybe the analyser |
| 4285 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4286 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4287 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4288 | if (req->flags & BF_SHUTW) { |
| 4289 | /* request errors are most likely due to |
| 4290 | * the server aborting the transfer. |
| 4291 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4292 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4293 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4294 | if (msg->err_pos >= 0) |
| 4295 | 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] | 4296 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4297 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4298 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4299 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4300 | |
| 4301 | /* If "option abortonclose" is set on the backend, we |
| 4302 | * want to monitor the client's connection and forward |
| 4303 | * any shutdown notification to the server, which will |
| 4304 | * decide whether to close or to go on processing the |
| 4305 | * request. |
| 4306 | */ |
| 4307 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4308 | buffer_auto_read(req); |
| 4309 | buffer_auto_close(req); |
| 4310 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4311 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4312 | /* POST requests may require to read extra CRLF |
| 4313 | * sent by broken browsers and which could cause |
| 4314 | * an RST to be sent upon close on some systems |
| 4315 | * (eg: Linux). |
| 4316 | */ |
| 4317 | buffer_auto_read(req); |
| 4318 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4319 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4320 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4324 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4325 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4326 | if (req->flags & BF_SHUTR) { |
| 4327 | if (!(s->flags & SN_ERR_MASK)) |
| 4328 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4329 | if (!(s->flags & SN_FINST_MASK)) { |
| 4330 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4331 | s->flags |= SN_FINST_H; |
| 4332 | else |
| 4333 | s->flags |= SN_FINST_D; |
| 4334 | } |
| 4335 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4336 | s->fe->fe_counters.cli_aborts++; |
| 4337 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4338 | if (target_srv(&s->target)) |
| 4339 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4340 | |
| 4341 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4342 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4343 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4344 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4345 | if (req->flags & BF_SHUTW) |
| 4346 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4347 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4348 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4349 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4350 | */ |
| 4351 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4352 | buffer_dont_close(req); |
| 4353 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4354 | /* 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] | 4355 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4356 | * 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] | 4357 | * modes are already handled by the stream sock layer. We must not do |
| 4358 | * this in content-length mode because it could present the MSG_MORE |
| 4359 | * flag with the last block of forwarded data, which would cause an |
| 4360 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4361 | */ |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4362 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4363 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4364 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4365 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4366 | return 0; |
| 4367 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4368 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4369 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4370 | if (s->listener->counters) |
| 4371 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4372 | return_bad_req_stats_ok: |
| 4373 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4374 | if (txn->status) { |
| 4375 | /* Note: we don't send any error if some data were already sent */ |
| 4376 | stream_int_retnclose(req->prod, NULL); |
| 4377 | } else { |
| 4378 | txn->status = 400; |
| 4379 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4380 | } |
| 4381 | req->analysers = 0; |
| 4382 | 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] | 4383 | |
| 4384 | if (!(s->flags & SN_ERR_MASK)) |
| 4385 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4386 | if (!(s->flags & SN_FINST_MASK)) { |
| 4387 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4388 | s->flags |= SN_FINST_H; |
| 4389 | else |
| 4390 | s->flags |= SN_FINST_D; |
| 4391 | } |
| 4392 | return 0; |
| 4393 | |
| 4394 | aborted_xfer: |
| 4395 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4396 | if (txn->status) { |
| 4397 | /* Note: we don't send any error if some data were already sent */ |
| 4398 | stream_int_retnclose(req->prod, NULL); |
| 4399 | } else { |
| 4400 | txn->status = 502; |
| 4401 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4402 | } |
| 4403 | req->analysers = 0; |
| 4404 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4405 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4406 | s->fe->fe_counters.srv_aborts++; |
| 4407 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4408 | if (target_srv(&s->target)) |
| 4409 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4410 | |
| 4411 | if (!(s->flags & SN_ERR_MASK)) |
| 4412 | s->flags |= SN_ERR_SRVCL; |
| 4413 | if (!(s->flags & SN_FINST_MASK)) { |
| 4414 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4415 | s->flags |= SN_FINST_H; |
| 4416 | else |
| 4417 | s->flags |= SN_FINST_D; |
| 4418 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4419 | return 0; |
| 4420 | } |
| 4421 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4422 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4423 | * processing can continue on next analysers, or zero if it either needs more |
| 4424 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4425 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4426 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4427 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4428 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4429 | 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] | 4430 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4431 | struct http_txn *txn = &s->txn; |
| 4432 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4433 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4434 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4435 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4436 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4437 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4438 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4439 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4440 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4441 | rep, |
| 4442 | rep->rex, rep->wex, |
| 4443 | rep->flags, |
| 4444 | rep->l, |
| 4445 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4446 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4447 | /* |
| 4448 | * Now parse the partial (or complete) lines. |
| 4449 | * We will check the response syntax, and also join multi-line |
| 4450 | * headers. An index of all the lines will be elaborated while |
| 4451 | * parsing. |
| 4452 | * |
| 4453 | * For the parsing, we use a 28 states FSM. |
| 4454 | * |
| 4455 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4456 | * rep->data + msg->som = beginning of response |
| 4457 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4458 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4459 | * rep->lr = first non-visited byte |
| 4460 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4461 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4462 | */ |
| 4463 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4464 | /* There's a protected area at the end of the buffer for rewriting |
| 4465 | * purposes. We don't want to start to parse the request if the |
| 4466 | * protected area is affected, because we may have to move processed |
| 4467 | * data later, which is much more complicated. |
| 4468 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4469 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4470 | if (unlikely((rep->flags & BF_FULL) || |
| 4471 | rep->r < rep->lr || |
| 4472 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4473 | if (rep->send_max) { |
| 4474 | /* 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] | 4475 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4476 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4477 | buffer_dont_close(rep); |
| 4478 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4479 | return 0; |
| 4480 | } |
| 4481 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4482 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4483 | } |
| 4484 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4485 | if (likely(rep->lr < rep->r)) |
| 4486 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4489 | /* 1: we might have to print this header in debug mode */ |
| 4490 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4491 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4492 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4493 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4494 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4495 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4496 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4497 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4498 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4499 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4500 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4501 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4502 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4503 | while (cur_idx) { |
| 4504 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4505 | debug_hdr("srvhdr", s, sol, eol); |
| 4506 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4507 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4508 | } |
| 4509 | } |
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 | /* |
| 4512 | * Now we quickly check if we have found a full valid response. |
| 4513 | * If not so, we check the FD and buffer states before leaving. |
| 4514 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4515 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4516 | * responses are checked first. |
| 4517 | * |
| 4518 | * Depending on whether the client is still there or not, we |
| 4519 | * may send an error response back or not. Note that normally |
| 4520 | * we should only check for HTTP status there, and check I/O |
| 4521 | * errors somewhere else. |
| 4522 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4523 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4524 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4525 | /* Invalid response */ |
| 4526 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4527 | /* we detected a parsing error. We want to archive this response |
| 4528 | * in the dedicated proxy area for later troubleshooting. |
| 4529 | */ |
| 4530 | hdr_response_bad: |
| 4531 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4532 | 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] | 4533 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4534 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4535 | if (target_srv(&s->target)) { |
| 4536 | target_srv(&s->target)->counters.failed_resp++; |
| 4537 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4538 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4539 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4540 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4541 | rep->analysers = 0; |
| 4542 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4543 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4544 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4545 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4546 | |
| 4547 | if (!(s->flags & SN_ERR_MASK)) |
| 4548 | s->flags |= SN_ERR_PRXCOND; |
| 4549 | if (!(s->flags & SN_FINST_MASK)) |
| 4550 | s->flags |= SN_FINST_H; |
| 4551 | |
| 4552 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4553 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4554 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4555 | /* too large response does not fit in buffer. */ |
| 4556 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4557 | if (msg->err_pos < 0) |
| 4558 | msg->err_pos = rep->l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4559 | goto hdr_response_bad; |
| 4560 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4561 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4562 | /* read error */ |
| 4563 | else if (rep->flags & BF_READ_ERROR) { |
| 4564 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4565 | 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] | 4566 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4567 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4568 | if (target_srv(&s->target)) { |
| 4569 | target_srv(&s->target)->counters.failed_resp++; |
| 4570 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4571 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4572 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4573 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4574 | rep->analysers = 0; |
| 4575 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4576 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4577 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4578 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4579 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4580 | if (!(s->flags & SN_ERR_MASK)) |
| 4581 | s->flags |= SN_ERR_SRVCL; |
| 4582 | if (!(s->flags & SN_FINST_MASK)) |
| 4583 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4584 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4585 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4586 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4587 | /* read timeout : return a 504 to the client. */ |
| 4588 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4589 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4590 | 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] | 4591 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4592 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4593 | if (target_srv(&s->target)) { |
| 4594 | target_srv(&s->target)->counters.failed_resp++; |
| 4595 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4596 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4597 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4598 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4599 | rep->analysers = 0; |
| 4600 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4601 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4602 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4603 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4604 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4605 | if (!(s->flags & SN_ERR_MASK)) |
| 4606 | s->flags |= SN_ERR_SRVTO; |
| 4607 | if (!(s->flags & SN_FINST_MASK)) |
| 4608 | s->flags |= SN_FINST_H; |
| 4609 | return 0; |
| 4610 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4611 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4612 | /* 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] | 4613 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4614 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4615 | 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] | 4616 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4617 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4618 | if (target_srv(&s->target)) { |
| 4619 | target_srv(&s->target)->counters.failed_resp++; |
| 4620 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4621 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4622 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4623 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4624 | rep->analysers = 0; |
| 4625 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4626 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4627 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4628 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4629 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4630 | if (!(s->flags & SN_ERR_MASK)) |
| 4631 | s->flags |= SN_ERR_SRVCL; |
| 4632 | if (!(s->flags & SN_FINST_MASK)) |
| 4633 | s->flags |= SN_FINST_H; |
| 4634 | return 0; |
| 4635 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4636 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4637 | /* write error to client (we don't send any message then) */ |
| 4638 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4639 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4640 | 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] | 4641 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4642 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4643 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4644 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4645 | |
| 4646 | if (!(s->flags & SN_ERR_MASK)) |
| 4647 | s->flags |= SN_ERR_CLICL; |
| 4648 | if (!(s->flags & SN_FINST_MASK)) |
| 4649 | s->flags |= SN_FINST_H; |
| 4650 | |
| 4651 | /* process_session() will take care of the error */ |
| 4652 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4653 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4654 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4655 | buffer_dont_close(rep); |
| 4656 | return 0; |
| 4657 | } |
| 4658 | |
| 4659 | /* More interesting part now : we know that we have a complete |
| 4660 | * response which at least looks like HTTP. We have an indicator |
| 4661 | * of each header's length, so we can parse them quickly. |
| 4662 | */ |
| 4663 | |
| 4664 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4665 | 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] | 4666 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4667 | /* |
| 4668 | * 1: get the status code |
| 4669 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4670 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4671 | if (n < 1 || n > 5) |
| 4672 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4673 | /* when the client triggers a 4xx from the server, it's most often due |
| 4674 | * to a missing object or permission. These events should be tracked |
| 4675 | * because if they happen often, it may indicate a brute force or a |
| 4676 | * vulnerability scan. |
| 4677 | */ |
| 4678 | if (n == 4) |
| 4679 | session_inc_http_err_ctr(s); |
| 4680 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4681 | if (target_srv(&s->target)) |
| 4682 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4683 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4684 | /* check if the response is HTTP/1.1 or above */ |
| 4685 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4686 | ((msg->sol[5] > '1') || |
| 4687 | ((msg->sol[5] == '1') && |
| 4688 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4689 | txn->flags |= TX_RES_VER_11; |
| 4690 | |
| 4691 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4692 | 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] | 4693 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4694 | /* transfer length unknown*/ |
| 4695 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4696 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4697 | 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] | 4698 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4699 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4700 | * and 505 are triggered on demand by client request, so we must not |
| 4701 | * count them as server failures. |
| 4702 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4703 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4704 | 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] | 4705 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4706 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4707 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4708 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4709 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4710 | /* |
| 4711 | * 2: check for cacheability. |
| 4712 | */ |
| 4713 | |
| 4714 | switch (txn->status) { |
| 4715 | case 200: |
| 4716 | case 203: |
| 4717 | case 206: |
| 4718 | case 300: |
| 4719 | case 301: |
| 4720 | case 410: |
| 4721 | /* RFC2616 @13.4: |
| 4722 | * "A response received with a status code of |
| 4723 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4724 | * by a cache (...) unless a cache-control |
| 4725 | * directive prohibits caching." |
| 4726 | * |
| 4727 | * RFC2616 @9.5: POST method : |
| 4728 | * "Responses to this method are not cacheable, |
| 4729 | * unless the response includes appropriate |
| 4730 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4731 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4732 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4733 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4734 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4735 | break; |
| 4736 | default: |
| 4737 | break; |
| 4738 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4739 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4740 | /* |
| 4741 | * 3: we may need to capture headers |
| 4742 | */ |
| 4743 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4744 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4745 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4746 | txn->rsp.cap, s->fe->rsp_cap); |
| 4747 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4748 | /* 4: determine the transfer-length. |
| 4749 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4750 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4751 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4752 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4753 | * All responses to the HEAD request method MUST NOT include a |
| 4754 | * message-body, even though the presence of entity-header fields |
| 4755 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4756 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4757 | * message-body. All other responses do include a message-body, |
| 4758 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4759 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4760 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4761 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4762 | * always terminated by the first empty line after the header fields, |
| 4763 | * regardless of the entity-header fields present in the message. |
| 4764 | * |
| 4765 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4766 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4767 | * transfer-length is defined by the use of this transfer-coding. |
| 4768 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4769 | * transfer-coding is not present, the transfer-length is defined by |
| 4770 | * the sender closing the connection. |
| 4771 | * |
| 4772 | * 3. If a Content-Length header field is present, its decimal value in |
| 4773 | * OCTETs represents both the entity-length and the transfer-length. |
| 4774 | * If a message is received with both a Transfer-Encoding header |
| 4775 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4776 | * |
| 4777 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4778 | * the transfer-length is not otherwise specified, then this self- |
| 4779 | * delimiting media type defines the transfer-length. This media |
| 4780 | * type MUST NOT be used unless the sender knows that the recipient |
| 4781 | * can parse it; the presence in a request of a Range header with |
| 4782 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4783 | * client can parse multipart/byteranges responses. |
| 4784 | * |
| 4785 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4786 | */ |
| 4787 | |
| 4788 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4789 | * 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] | 4790 | * 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] | 4791 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4792 | */ |
| 4793 | if (txn->meth == HTTP_METH_HEAD || |
| 4794 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4795 | txn->status == 204 || txn->status == 304) { |
| 4796 | txn->flags |= TX_RES_XFER_LEN; |
| 4797 | goto skip_content_length; |
| 4798 | } |
| 4799 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4800 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4801 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4802 | while ((txn->flags & TX_RES_VER_11) && |
| 4803 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4804 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4805 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4806 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4807 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4808 | use_close_only = 1; |
| 4809 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4810 | break; |
| 4811 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4815 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4816 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4817 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4818 | signed long long cl; |
| 4819 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4820 | if (!ctx.vlen) { |
| 4821 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4822 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4823 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4824 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4825 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 4826 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4827 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4828 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4829 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4830 | if (cl < 0) { |
| 4831 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4832 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4833 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4834 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4835 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) { |
| 4836 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4837 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4838 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4839 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4840 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4841 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4842 | } |
| 4843 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4844 | /* FIXME: we should also implement the multipart/byterange method. |
| 4845 | * For now on, we resort to close mode in this case (unknown length). |
| 4846 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4847 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4848 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4849 | /* end of job, return OK */ |
| 4850 | rep->analysers &= ~an_bit; |
| 4851 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4852 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4853 | return 1; |
| 4854 | } |
| 4855 | |
| 4856 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4857 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4858 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4859 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4860 | */ |
| 4861 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4862 | { |
| 4863 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4864 | struct http_msg *msg = &txn->rsp; |
| 4865 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4866 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4867 | |
| 4868 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4869 | now_ms, __FUNCTION__, |
| 4870 | t, |
| 4871 | rep, |
| 4872 | rep->rex, rep->wex, |
| 4873 | rep->flags, |
| 4874 | rep->l, |
| 4875 | rep->analysers); |
| 4876 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4877 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4878 | return 0; |
| 4879 | |
| 4880 | rep->analysers &= ~an_bit; |
| 4881 | rep->analyse_exp = TICK_ETERNITY; |
| 4882 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4883 | /* Now we have to check if we need to modify the Connection header. |
| 4884 | * This is more difficult on the response than it is on the request, |
| 4885 | * because we can have two different HTTP versions and we don't know |
| 4886 | * how the client will interprete a response. For instance, let's say |
| 4887 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4888 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4889 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4890 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4891 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4892 | * indicating how a request MAY be understood by the client. In case |
| 4893 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4894 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4895 | * will fall back to explicit close. Note that we won't take risks with |
| 4896 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4897 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4898 | */ |
| 4899 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4900 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4901 | txn->status == 101)) { |
| 4902 | /* Either we've established an explicit tunnel, or we're |
| 4903 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4904 | * to understand the next protocols. We have to switch to tunnel |
| 4905 | * mode, so that we transfer the request and responses then let |
| 4906 | * this protocol pass unmodified. When we later implement specific |
| 4907 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4908 | * header which contains information about that protocol for |
| 4909 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4910 | */ |
| 4911 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4912 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4913 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4914 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4915 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4916 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4917 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4918 | /* on unknown transfer length, we must close */ |
| 4919 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4920 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4921 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4922 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4923 | /* now adjust header transformations depending on current state */ |
| 4924 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4925 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4926 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4927 | if (!(txn->flags & TX_RES_VER_11)) |
| 4928 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4929 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4930 | else { /* SCL / KAL */ |
| 4931 | to_del |= 1; /* remove "close" on any response */ |
| 4932 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4933 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4934 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4935 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4936 | /* Parse and remove some headers from the connection header */ |
| 4937 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4938 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4939 | /* Some keep-alive responses are converted to Server-close if |
| 4940 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4941 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4942 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4943 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4944 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4945 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4946 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4947 | } |
| 4948 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4949 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4950 | /* |
| 4951 | * 3: we will have to evaluate the filters. |
| 4952 | * As opposed to version 1.2, now they will be evaluated in the |
| 4953 | * filters order and not in the header order. This means that |
| 4954 | * each filter has to be validated among all headers. |
| 4955 | * |
| 4956 | * Filters are tried with ->be first, then with ->fe if it is |
| 4957 | * different from ->be. |
| 4958 | */ |
| 4959 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4960 | cur_proxy = t->be; |
| 4961 | while (1) { |
| 4962 | struct proxy *rule_set = cur_proxy; |
| 4963 | |
| 4964 | /* try headers filters */ |
| 4965 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4966 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4967 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4968 | if (target_srv(&t->target)) { |
| 4969 | target_srv(&t->target)->counters.failed_resp++; |
| 4970 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4971 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4972 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4973 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4974 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4975 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4976 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4977 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4978 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4979 | if (!(t->flags & SN_ERR_MASK)) |
| 4980 | t->flags |= SN_ERR_PRXCOND; |
| 4981 | if (!(t->flags & SN_FINST_MASK)) |
| 4982 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4983 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4984 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4985 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4986 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4987 | /* has the response been denied ? */ |
| 4988 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4989 | if (target_srv(&t->target)) |
| 4990 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4991 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4992 | t->be->be_counters.denied_resp++; |
| 4993 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4994 | if (t->listener->counters) |
| 4995 | t->listener->counters->denied_resp++; |
| 4996 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4997 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4998 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4999 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5000 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5001 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5002 | if (txn->status < 200) |
| 5003 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5004 | if (wl->cond) { |
| 5005 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5006 | ret = acl_pass(ret); |
| 5007 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5008 | ret = !ret; |
| 5009 | if (!ret) |
| 5010 | continue; |
| 5011 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5012 | 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] | 5013 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5014 | } |
| 5015 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5016 | /* check whether we're already working on the frontend */ |
| 5017 | if (cur_proxy == t->fe) |
| 5018 | break; |
| 5019 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5020 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5021 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5022 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5023 | * We may be facing a 100-continue response, in which case this |
| 5024 | * is not the right response, and we're waiting for the next one. |
| 5025 | * 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] | 5026 | * next one. |
| 5027 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5028 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5029 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5030 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5031 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5032 | txn->status = 0; |
| 5033 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5034 | return 1; |
| 5035 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5036 | else if (unlikely(txn->status < 200)) |
| 5037 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5038 | |
| 5039 | /* we don't have any 1xx status code now */ |
| 5040 | |
| 5041 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5042 | * 4: check for server cookie. |
| 5043 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5044 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5045 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5046 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5047 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5048 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5049 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5050 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5051 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5052 | 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] | 5053 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5054 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5055 | /* |
| 5056 | * 6: add server cookie in the response if needed |
| 5057 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5058 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5059 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5060 | (!(t->flags & SN_DIRECT) || |
| 5061 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5062 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5063 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5064 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5065 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5066 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5067 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5068 | /* the server is known, it's not the one the client requested, or the |
| 5069 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5070 | * insert a set-cookie here, except if we want to insert only on POST |
| 5071 | * requests and this one isn't. Note that servers which don't have cookies |
| 5072 | * (eg: some backup servers) will return a full cookie removal request. |
| 5073 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5074 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5075 | len = sprintf(trash, |
| 5076 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5077 | t->be->cookie_name); |
| 5078 | } |
| 5079 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5080 | 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] | 5081 | |
| 5082 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5083 | /* emit last_date, which is mandatory */ |
| 5084 | trash[len++] = COOKIE_DELIM_DATE; |
| 5085 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5086 | if (t->be->cookie_maxlife) { |
| 5087 | /* emit first_date, which is either the original one or |
| 5088 | * the current date. |
| 5089 | */ |
| 5090 | trash[len++] = COOKIE_DELIM_DATE; |
| 5091 | s30tob64(txn->cookie_first_date ? |
| 5092 | txn->cookie_first_date >> 2 : |
| 5093 | (date.tv_sec+3) >> 2, trash + len); |
| 5094 | len += 5; |
| 5095 | } |
| 5096 | } |
| 5097 | len += sprintf(trash + len, "; path=/"); |
| 5098 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5099 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5100 | if (t->be->cookie_domain) |
| 5101 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5102 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5103 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5104 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5105 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5106 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5107 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5108 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5109 | /* the server did not change, only the date was updated */ |
| 5110 | txn->flags |= TX_SCK_UPDATED; |
| 5111 | else |
| 5112 | txn->flags |= TX_SCK_INSERTED; |
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 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5115 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5116 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5117 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5118 | */ |
| 5119 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5120 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5121 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5122 | |
| 5123 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5124 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5125 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5126 | } |
| 5127 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5128 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5129 | /* |
| 5130 | * 7: check if result will be cacheable with a cookie. |
| 5131 | * We'll block the response if security checks have caught |
| 5132 | * nasty things such as a cacheable cookie. |
| 5133 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5134 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5135 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5136 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5137 | |
| 5138 | /* we're in presence of a cacheable response containing |
| 5139 | * a set-cookie header. We'll block it as requested by |
| 5140 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5141 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5142 | if (target_srv(&t->target)) |
| 5143 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5144 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5145 | t->be->be_counters.denied_resp++; |
| 5146 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5147 | if (t->listener->counters) |
| 5148 | t->listener->counters->denied_resp++; |
| 5149 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5150 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5151 | 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] | 5152 | send_log(t->be, LOG_ALERT, |
| 5153 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5154 | 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] | 5155 | goto return_srv_prx_502; |
| 5156 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5157 | |
| 5158 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5159 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5160 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5161 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5162 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5163 | unsigned int want_flags = 0; |
| 5164 | |
| 5165 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5166 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5167 | /* we want a keep-alive response here. Keep-alive header |
| 5168 | * required if either side is not 1.1. |
| 5169 | */ |
| 5170 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 5171 | want_flags |= TX_CON_KAL_SET; |
| 5172 | } |
| 5173 | else { |
| 5174 | /* we want a close response here. Close header required if |
| 5175 | * the server is 1.1, regardless of the client. |
| 5176 | */ |
| 5177 | if (txn->flags & TX_RES_VER_11) |
| 5178 | want_flags |= TX_CON_CLO_SET; |
| 5179 | } |
| 5180 | |
| 5181 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5182 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5183 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5184 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5185 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5186 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 5187 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5188 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5189 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5190 | /************************************************************* |
| 5191 | * OK, that's finished for the headers. We have done what we * |
| 5192 | * could. Let's switch to the DATA state. * |
| 5193 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5194 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5195 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5196 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5197 | /* if the user wants to log as soon as possible, without counting |
| 5198 | * bytes from the server, then this is the right moment. We have |
| 5199 | * to temporarily assign bytes_out to log what we currently have. |
| 5200 | */ |
| 5201 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5202 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5203 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5204 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5205 | t->logs.bytes_out = 0; |
| 5206 | } |
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 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5209 | * otherwise we would not let the client side wake up. |
| 5210 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5211 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5212 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5213 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5214 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5215 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5216 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5217 | /* This function is an analyser which forwards response body (including chunk |
| 5218 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5219 | * zero byte. The only situation where it must not be called is when we're in |
| 5220 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5221 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5222 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5223 | * 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] | 5224 | * 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] | 5225 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5226 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5227 | */ |
| 5228 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5229 | { |
| 5230 | struct http_txn *txn = &s->txn; |
| 5231 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5232 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5233 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5234 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5235 | return 0; |
| 5236 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5237 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5238 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5239 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5240 | /* Output closed while we were sending data. We must abort and |
| 5241 | * wake the other side up. |
| 5242 | */ |
| 5243 | msg->msg_state = HTTP_MSG_ERROR; |
| 5244 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5245 | return 1; |
| 5246 | } |
| 5247 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5248 | /* in most states, we should abort in case of early close */ |
| 5249 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5250 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5251 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5252 | /* we have msg->col and msg->sov which both point to the first |
| 5253 | * byte of message body. msg->som still points to the beginning |
| 5254 | * of the message. We must save the body in req->lr because it |
| 5255 | * survives buffer re-alignments. |
| 5256 | */ |
| 5257 | res->lr = res->data + msg->sov; |
| 5258 | if (txn->flags & TX_RES_TE_CHNK) |
| 5259 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5260 | else { |
| 5261 | msg->msg_state = HTTP_MSG_DATA; |
| 5262 | } |
| 5263 | } |
| 5264 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5265 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5266 | int bytes; |
| 5267 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5268 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5269 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5270 | bytes = msg->sov - msg->som; |
| 5271 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5272 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5273 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5274 | bytes += res->size; |
| 5275 | msg->chunk_len += (unsigned int)bytes; |
| 5276 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5277 | } |
| 5278 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5279 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5280 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5281 | /* must still forward */ |
| 5282 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5283 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5284 | |
| 5285 | /* nothing left to forward */ |
| 5286 | if (txn->flags & TX_RES_TE_CHNK) |
| 5287 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5288 | else |
| 5289 | msg->msg_state = HTTP_MSG_DONE; |
| 5290 | } |
| 5291 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5292 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5293 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5294 | */ |
| 5295 | int ret = http_parse_chunk_size(res, msg); |
| 5296 | |
| 5297 | if (!ret) |
| 5298 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5299 | else if (ret < 0) { |
| 5300 | if (msg->err_pos >= 0) |
| 5301 | 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] | 5302 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5303 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5304 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5305 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5306 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5307 | /* we want the CRLF after the data */ |
| 5308 | int ret; |
| 5309 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5310 | res->lr = res->w + res->send_max; |
| 5311 | if (res->lr >= res->data + res->size) |
| 5312 | res->lr -= res->size; |
| 5313 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5314 | ret = http_skip_chunk_crlf(res, msg); |
| 5315 | |
| 5316 | if (!ret) |
| 5317 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5318 | else if (ret < 0) { |
| 5319 | if (msg->err_pos >= 0) |
| 5320 | 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] | 5321 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5322 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5323 | /* we're in MSG_CHUNK_SIZE now */ |
| 5324 | } |
| 5325 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5326 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5327 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5328 | if (ret == 0) |
| 5329 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5330 | else if (ret < 0) { |
| 5331 | if (msg->err_pos >= 0) |
| 5332 | 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] | 5333 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5334 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5335 | /* we're in HTTP_MSG_DONE now */ |
| 5336 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5337 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5338 | int old_state = msg->msg_state; |
| 5339 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5340 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5341 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5342 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5343 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5344 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5345 | if (http_resync_states(s)) { |
| 5346 | http_silent_debug(__LINE__, s); |
| 5347 | /* some state changes occurred, maybe the analyser |
| 5348 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5349 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5350 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5351 | if (res->flags & BF_SHUTW) { |
| 5352 | /* response errors are most likely due to |
| 5353 | * the client aborting the transfer. |
| 5354 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5355 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5356 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5357 | if (msg->err_pos >= 0) |
| 5358 | 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] | 5359 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5360 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5361 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5362 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5363 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5364 | } |
| 5365 | } |
| 5366 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5367 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5368 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5369 | if (res->flags & BF_SHUTR) { |
| 5370 | if (!(s->flags & SN_ERR_MASK)) |
| 5371 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5372 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5373 | if (target_srv(&s->target)) |
| 5374 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5375 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5376 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5377 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5378 | if (res->flags & BF_SHUTW) |
| 5379 | goto aborted_xfer; |
| 5380 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5381 | /* 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] | 5382 | if (!s->req->analysers) |
| 5383 | goto return_bad_res; |
| 5384 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5385 | /* forward any pending data */ |
| 5386 | bytes = msg->sov - msg->som; |
| 5387 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5388 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5389 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5390 | bytes += res->size; |
| 5391 | msg->chunk_len += (unsigned int)bytes; |
| 5392 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5393 | } |
| 5394 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5395 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5396 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5397 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5398 | * close. |
| 5399 | */ |
| 5400 | if ((txn->flags & TX_RES_TE_CHNK) || |
| 5401 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5402 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5403 | buffer_dont_close(res); |
| 5404 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5405 | /* 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] | 5406 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5407 | * 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] | 5408 | * modes are already handled by the stream sock layer. We must not do |
| 5409 | * this in content-length mode because it could present the MSG_MORE |
| 5410 | * flag with the last block of forwarded data, which would cause an |
| 5411 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5412 | */ |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5413 | if (txn->flags & TX_RES_TE_CHNK) |
| 5414 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5415 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5416 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5417 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5418 | return 0; |
| 5419 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5420 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5421 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5422 | if (target_srv(&s->target)) |
| 5423 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5424 | |
| 5425 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5426 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5427 | /* don't send any error message as we're in the body */ |
| 5428 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5429 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5430 | 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] | 5431 | if (target_srv(&s->target)) |
| 5432 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5433 | |
| 5434 | if (!(s->flags & SN_ERR_MASK)) |
| 5435 | s->flags |= SN_ERR_PRXCOND; |
| 5436 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5437 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5438 | return 0; |
| 5439 | |
| 5440 | aborted_xfer: |
| 5441 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5442 | /* don't send any error message as we're in the body */ |
| 5443 | stream_int_retnclose(res->cons, NULL); |
| 5444 | res->analysers = 0; |
| 5445 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5446 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5447 | s->fe->fe_counters.cli_aborts++; |
| 5448 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5449 | if (target_srv(&s->target)) |
| 5450 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5451 | |
| 5452 | if (!(s->flags & SN_ERR_MASK)) |
| 5453 | s->flags |= SN_ERR_CLICL; |
| 5454 | if (!(s->flags & SN_FINST_MASK)) |
| 5455 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5456 | return 0; |
| 5457 | } |
| 5458 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5459 | /* Iterate the same filter through all request headers. |
| 5460 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5461 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5462 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5463 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5464 | 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] | 5465 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5466 | char term; |
| 5467 | char *cur_ptr, *cur_end, *cur_next; |
| 5468 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5469 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5470 | struct hdr_idx_elem *cur_hdr; |
| 5471 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5472 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5473 | last_hdr = 0; |
| 5474 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5475 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5476 | old_idx = 0; |
| 5477 | |
| 5478 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5479 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5480 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5481 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5482 | (exp->action == ACT_ALLOW || |
| 5483 | exp->action == ACT_DENY || |
| 5484 | exp->action == ACT_TARPIT)) |
| 5485 | return 0; |
| 5486 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5487 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5488 | if (!cur_idx) |
| 5489 | break; |
| 5490 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5491 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5492 | cur_ptr = cur_next; |
| 5493 | cur_end = cur_ptr + cur_hdr->len; |
| 5494 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5495 | |
| 5496 | /* Now we have one header between cur_ptr and cur_end, |
| 5497 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5498 | */ |
| 5499 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5500 | /* The annoying part is that pattern matching needs |
| 5501 | * that we modify the contents to null-terminate all |
| 5502 | * strings before testing them. |
| 5503 | */ |
| 5504 | |
| 5505 | term = *cur_end; |
| 5506 | *cur_end = '\0'; |
| 5507 | |
| 5508 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5509 | switch (exp->action) { |
| 5510 | case ACT_SETBE: |
| 5511 | /* It is not possible to jump a second time. |
| 5512 | * FIXME: should we return an HTTP/500 here so that |
| 5513 | * the admin knows there's a problem ? |
| 5514 | */ |
| 5515 | if (t->be != t->fe) |
| 5516 | break; |
| 5517 | |
| 5518 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5519 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5520 | last_hdr = 1; |
| 5521 | break; |
| 5522 | |
| 5523 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5524 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5525 | last_hdr = 1; |
| 5526 | break; |
| 5527 | |
| 5528 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5529 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5530 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5531 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5532 | t->fe->fe_counters.denied_req++; |
| 5533 | if (t->fe != t->be) |
| 5534 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5535 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5536 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5537 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5538 | break; |
| 5539 | |
| 5540 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5541 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5542 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5543 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5544 | t->fe->fe_counters.denied_req++; |
| 5545 | if (t->fe != t->be) |
| 5546 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5547 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5548 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5550 | break; |
| 5551 | |
| 5552 | case ACT_REPLACE: |
| 5553 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5554 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5555 | /* FIXME: if the user adds a newline in the replacement, the |
| 5556 | * index will not be recalculated for now, and the new line |
| 5557 | * will not be counted as a new header. |
| 5558 | */ |
| 5559 | |
| 5560 | cur_end += delta; |
| 5561 | cur_next += delta; |
| 5562 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5563 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5564 | break; |
| 5565 | |
| 5566 | case ACT_REMOVE: |
| 5567 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5568 | cur_next += delta; |
| 5569 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5570 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5571 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5572 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5573 | cur_hdr->len = 0; |
| 5574 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5575 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5576 | break; |
| 5577 | |
| 5578 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5579 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5580 | if (cur_end) |
| 5581 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5582 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5583 | /* keep the link from this header to next one in case of later |
| 5584 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5585 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5586 | old_idx = cur_idx; |
| 5587 | } |
| 5588 | return 0; |
| 5589 | } |
| 5590 | |
| 5591 | |
| 5592 | /* Apply the filter to the request line. |
| 5593 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5594 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5595 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5596 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5597 | */ |
| 5598 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5599 | { |
| 5600 | char term; |
| 5601 | char *cur_ptr, *cur_end; |
| 5602 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5603 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5604 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5605 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5606 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5607 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5608 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5609 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5610 | (exp->action == ACT_ALLOW || |
| 5611 | exp->action == ACT_DENY || |
| 5612 | exp->action == ACT_TARPIT)) |
| 5613 | return 0; |
| 5614 | else if (exp->action == ACT_REMOVE) |
| 5615 | return 0; |
| 5616 | |
| 5617 | done = 0; |
| 5618 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5619 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5620 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5621 | |
| 5622 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5623 | |
| 5624 | /* The annoying part is that pattern matching needs |
| 5625 | * that we modify the contents to null-terminate all |
| 5626 | * strings before testing them. |
| 5627 | */ |
| 5628 | |
| 5629 | term = *cur_end; |
| 5630 | *cur_end = '\0'; |
| 5631 | |
| 5632 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5633 | switch (exp->action) { |
| 5634 | case ACT_SETBE: |
| 5635 | /* It is not possible to jump a second time. |
| 5636 | * FIXME: should we return an HTTP/500 here so that |
| 5637 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5638 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5639 | if (t->be != t->fe) |
| 5640 | break; |
| 5641 | |
| 5642 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5643 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5644 | done = 1; |
| 5645 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5646 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5647 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5648 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5649 | done = 1; |
| 5650 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5651 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5652 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5653 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5654 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5655 | t->fe->fe_counters.denied_req++; |
| 5656 | if (t->fe != t->be) |
| 5657 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5658 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5659 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5660 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5661 | done = 1; |
| 5662 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5663 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5664 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5665 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5666 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5667 | t->fe->fe_counters.denied_req++; |
| 5668 | if (t->fe != t->be) |
| 5669 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5670 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5671 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5672 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5673 | done = 1; |
| 5674 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5676 | case ACT_REPLACE: |
| 5677 | *cur_end = term; /* restore the string terminator */ |
| 5678 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5679 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5680 | /* FIXME: if the user adds a newline in the replacement, the |
| 5681 | * index will not be recalculated for now, and the new line |
| 5682 | * will not be counted as a new header. |
| 5683 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5684 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5685 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5686 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5687 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5688 | HTTP_MSG_RQMETH, |
| 5689 | cur_ptr, cur_end + 1, |
| 5690 | NULL, NULL); |
| 5691 | if (unlikely(!cur_end)) |
| 5692 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5693 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5694 | /* we have a full request and we know that we have either a CR |
| 5695 | * or an LF at <ptr>. |
| 5696 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5697 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5698 | 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] | 5699 | /* there is no point trying this regex on headers */ |
| 5700 | return 1; |
| 5701 | } |
| 5702 | } |
| 5703 | *cur_end = term; /* restore the string terminator */ |
| 5704 | return done; |
| 5705 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5706 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5707 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5708 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5709 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5710 | * 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] | 5711 | * 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] | 5712 | * unparsable request. Since it can manage the switch to another backend, it |
| 5713 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5714 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5715 | 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] | 5716 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5717 | struct http_txn *txn = &s->txn; |
| 5718 | struct hdr_exp *exp; |
| 5719 | |
| 5720 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5721 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5722 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5723 | /* |
| 5724 | * The interleaving of transformations and verdicts |
| 5725 | * makes it difficult to decide to continue or stop |
| 5726 | * the evaluation. |
| 5727 | */ |
| 5728 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5729 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5730 | break; |
| 5731 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5732 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5733 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5734 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5735 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5736 | |
| 5737 | /* if this filter had a condition, evaluate it now and skip to |
| 5738 | * next filter if the condition does not match. |
| 5739 | */ |
| 5740 | if (exp->cond) { |
| 5741 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5742 | ret = acl_pass(ret); |
| 5743 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5744 | ret = !ret; |
| 5745 | |
| 5746 | if (!ret) |
| 5747 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5751 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5752 | if (unlikely(ret < 0)) |
| 5753 | return -1; |
| 5754 | |
| 5755 | if (likely(ret == 0)) { |
| 5756 | /* The filter did not match the request, it can be |
| 5757 | * iterated through all headers. |
| 5758 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5759 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5760 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5761 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5762 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5766 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5767 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5768 | * Try to retrieve the server associated to the appsession. |
| 5769 | * If the server is found, it's assigned to the session. |
| 5770 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5771 | 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] | 5772 | struct http_txn *txn = &t->txn; |
| 5773 | appsess *asession = NULL; |
| 5774 | char *sessid_temp = NULL; |
| 5775 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5776 | if (len > t->be->appsession_len) { |
| 5777 | len = t->be->appsession_len; |
| 5778 | } |
| 5779 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5780 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5781 | /* 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] | 5782 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5783 | /* 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] | 5784 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5787 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5788 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5789 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5790 | return; |
| 5791 | } |
| 5792 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5793 | memcpy(txn->sessid, buf, len); |
| 5794 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 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 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5803 | memcpy(sessid_temp, buf, len); |
| 5804 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5805 | |
| 5806 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5807 | /* free previously allocated memory */ |
| 5808 | pool_free2(apools.sessid, sessid_temp); |
| 5809 | |
| 5810 | if (asession != NULL) { |
| 5811 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5812 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5813 | asession->request_count++; |
| 5814 | |
| 5815 | if (asession->serverid != NULL) { |
| 5816 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5817 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5818 | while (srv) { |
| 5819 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5820 | if ((srv->state & SRV_RUNNING) || |
| 5821 | (t->be->options & PR_O_PERSIST) || |
| 5822 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5823 | /* we found the server and it's usable */ |
| 5824 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5825 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5826 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5827 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5828 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5829 | break; |
| 5830 | } else { |
| 5831 | txn->flags &= ~TX_CK_MASK; |
| 5832 | txn->flags |= TX_CK_DOWN; |
| 5833 | } |
| 5834 | } |
| 5835 | srv = srv->next; |
| 5836 | } |
| 5837 | } |
| 5838 | } |
| 5839 | } |
| 5840 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5841 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5842 | * same way as with headers above except that the semi-colon also ends a token. |
| 5843 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5844 | * return a valid result. |
| 5845 | */ |
| 5846 | char *find_cookie_value_end(char *s, const char *e) |
| 5847 | { |
| 5848 | int quoted, qdpair; |
| 5849 | |
| 5850 | quoted = qdpair = 0; |
| 5851 | for (; s < e; s++) { |
| 5852 | if (qdpair) qdpair = 0; |
| 5853 | else if (quoted) { |
| 5854 | if (*s == '\\') qdpair = 1; |
| 5855 | else if (*s == '"') quoted = 0; |
| 5856 | } |
| 5857 | else if (*s == '"') quoted = 1; |
| 5858 | else if (*s == ',' || *s == ';') return s; |
| 5859 | } |
| 5860 | return s; |
| 5861 | } |
| 5862 | |
| 5863 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5864 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5865 | * the first delimiter is updated if required. The function tries as much as |
| 5866 | * possible to respect the following principles : |
| 5867 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5868 | * colon, in which case <next> is simply removed |
| 5869 | * - set exactly one space character after the new first delimiter, unless |
| 5870 | * there are not enough characters in the block being moved to do so. |
| 5871 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5872 | * one. |
| 5873 | * |
| 5874 | * It is the caller's responsibility to ensure that : |
| 5875 | * - <from> points to a valid delimiter or the colon ; |
| 5876 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5877 | * - there are non-space chars before <from> ; |
| 5878 | * - there is a CR/LF at or after <next>. |
| 5879 | */ |
| 5880 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5881 | { |
| 5882 | char *prev = *from; |
| 5883 | |
| 5884 | if (*prev == ':') { |
| 5885 | /* We're removing the first value, preserve the colon and add a |
| 5886 | * space if possible. |
| 5887 | */ |
| 5888 | if (!http_is_crlf[(unsigned char)*next]) |
| 5889 | next++; |
| 5890 | prev++; |
| 5891 | if (prev < next) |
| 5892 | *prev++ = ' '; |
| 5893 | |
| 5894 | while (http_is_spht[(unsigned char)*next]) |
| 5895 | next++; |
| 5896 | } else { |
| 5897 | /* Remove useless spaces before the old delimiter. */ |
| 5898 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5899 | prev--; |
| 5900 | *from = prev; |
| 5901 | |
| 5902 | /* copy the delimiter and if possible a space if we're |
| 5903 | * not at the end of the line. |
| 5904 | */ |
| 5905 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5906 | *prev++ = *next++; |
| 5907 | if (prev + 1 < next) |
| 5908 | *prev++ = ' '; |
| 5909 | while (http_is_spht[(unsigned char)*next]) |
| 5910 | next++; |
| 5911 | } |
| 5912 | } |
| 5913 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5914 | } |
| 5915 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5916 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5917 | * 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] | 5918 | * desirable to call it only when needed. This code is quite complex because |
| 5919 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5920 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5921 | */ |
| 5922 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5923 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5924 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5925 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5926 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5927 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5928 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5929 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5930 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5931 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5932 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5933 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5934 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5935 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5936 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5937 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5938 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5939 | hdr_beg = hdr_next; |
| 5940 | hdr_end = hdr_beg + cur_hdr->len; |
| 5941 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5942 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5943 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5944 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5945 | * "Cookie:" headers. |
| 5946 | */ |
| 5947 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5948 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5949 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5950 | old_idx = cur_idx; |
| 5951 | continue; |
| 5952 | } |
| 5953 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5954 | del_from = NULL; /* nothing to be deleted */ |
| 5955 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5956 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5957 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5958 | * attributes whose name begin with a '$', and associate them with |
| 5959 | * the right cookie, if we want to delete this cookie. |
| 5960 | * So there are 3 cases for each cookie read : |
| 5961 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5962 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5963 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5964 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5965 | * "special" cookie. |
| 5966 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5967 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5968 | * *MUST* delete it. |
| 5969 | * |
| 5970 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5971 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5972 | * the RFC explicitly allows spaces before it, and not within the |
| 5973 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5974 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5975 | * after the equal sign too, resulting in some (rare) buggy |
| 5976 | * implementations trying to do that. So let's do what servers do. |
| 5977 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5978 | * allowed quoted strings in values, with any possible character |
| 5979 | * after a backslash, including control chars and delimitors, which |
| 5980 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5981 | * within values even without quotes. |
| 5982 | * |
| 5983 | * We have to keep multiple pointers in order to support cookie |
| 5984 | * removal at the beginning, middle or end of header without |
| 5985 | * corrupting the header. All of these headers are valid : |
| 5986 | * |
| 5987 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5988 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5989 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5990 | * | | | | | | | | | |
| 5991 | * | | | | | | | | hdr_end <--+ |
| 5992 | * | | | | | | | +--> next |
| 5993 | * | | | | | | +----> val_end |
| 5994 | * | | | | | +-----------> val_beg |
| 5995 | * | | | | +--------------> equal |
| 5996 | * | | | +----------------> att_end |
| 5997 | * | | +---------------------> att_beg |
| 5998 | * | +--------------------------> prev |
| 5999 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6000 | */ |
| 6001 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6002 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6003 | /* Iterate through all cookies on this line */ |
| 6004 | |
| 6005 | /* find att_beg */ |
| 6006 | att_beg = prev + 1; |
| 6007 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6008 | att_beg++; |
| 6009 | |
| 6010 | /* find att_end : this is the first character after the last non |
| 6011 | * space before the equal. It may be equal to hdr_end. |
| 6012 | */ |
| 6013 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6014 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6015 | while (equal < hdr_end) { |
| 6016 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6017 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6018 | if (http_is_spht[(unsigned char)*equal++]) |
| 6019 | continue; |
| 6020 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6021 | } |
| 6022 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6023 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6024 | * is between <att_beg> and <equal>, both may be identical. |
| 6025 | */ |
| 6026 | |
| 6027 | /* look for end of cookie if there is an equal sign */ |
| 6028 | if (equal < hdr_end && *equal == '=') { |
| 6029 | /* look for the beginning of the value */ |
| 6030 | val_beg = equal + 1; |
| 6031 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6032 | val_beg++; |
| 6033 | |
| 6034 | /* find the end of the value, respecting quotes */ |
| 6035 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6036 | |
| 6037 | /* make val_end point to the first white space or delimitor after the value */ |
| 6038 | val_end = next; |
| 6039 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6040 | val_end--; |
| 6041 | } else { |
| 6042 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6043 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6044 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6045 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6046 | * they will automatically be removed if a header before them is removed, |
| 6047 | * since they're supposed to be linked together. |
| 6048 | */ |
| 6049 | if (*att_beg == '$') |
| 6050 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6051 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6052 | /* Ignore cookies with no equal sign */ |
| 6053 | if (equal == next) { |
| 6054 | /* This is not our cookie, so we must preserve it. But if we already |
| 6055 | * scheduled another cookie for removal, we cannot remove the |
| 6056 | * complete header, but we can remove the previous block itself. |
| 6057 | */ |
| 6058 | preserve_hdr = 1; |
| 6059 | if (del_from != NULL) { |
| 6060 | int delta = del_hdr_value(req, &del_from, prev); |
| 6061 | val_end += delta; |
| 6062 | next += delta; |
| 6063 | hdr_end += delta; |
| 6064 | hdr_next += delta; |
| 6065 | cur_hdr->len += delta; |
| 6066 | http_msg_move_end(&txn->req, delta); |
| 6067 | prev = del_from; |
| 6068 | del_from = NULL; |
| 6069 | } |
| 6070 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6071 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6072 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6073 | /* if there are spaces around the equal sign, we need to |
| 6074 | * strip them otherwise we'll get trouble for cookie captures, |
| 6075 | * or even for rewrites. Since this happens extremely rarely, |
| 6076 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6077 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6078 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6079 | int stripped_before = 0; |
| 6080 | int stripped_after = 0; |
| 6081 | |
| 6082 | if (att_end != equal) { |
| 6083 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6084 | equal += stripped_before; |
| 6085 | val_beg += stripped_before; |
| 6086 | } |
| 6087 | |
| 6088 | if (val_beg > equal + 1) { |
| 6089 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6090 | val_beg += stripped_after; |
| 6091 | stripped_before += stripped_after; |
| 6092 | } |
| 6093 | |
| 6094 | val_end += stripped_before; |
| 6095 | next += stripped_before; |
| 6096 | hdr_end += stripped_before; |
| 6097 | hdr_next += stripped_before; |
| 6098 | cur_hdr->len += stripped_before; |
| 6099 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6100 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6101 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6102 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6103 | /* First, let's see if we want to capture this cookie. We check |
| 6104 | * that we don't already have a client side cookie, because we |
| 6105 | * can only capture one. Also as an optimisation, we ignore |
| 6106 | * cookies shorter than the declared name. |
| 6107 | */ |
| 6108 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6109 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6110 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6111 | int log_len = val_end - att_beg; |
| 6112 | |
| 6113 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6114 | Alert("HTTP logging : out of memory.\n"); |
| 6115 | } else { |
| 6116 | if (log_len > t->fe->capture_len) |
| 6117 | log_len = t->fe->capture_len; |
| 6118 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6119 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6120 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6121 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6122 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6123 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6124 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6125 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6126 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6127 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6128 | * For cookies in prefix mode, the form is : |
| 6129 | * |
| 6130 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6131 | */ |
| 6132 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6133 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6134 | struct server *srv = t->be->srv; |
| 6135 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6136 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6137 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6138 | * have the server ID between val_beg and delim, and the original cookie between |
| 6139 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6140 | * |
| 6141 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6142 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6143 | * | || || | |+-> next |
| 6144 | * | || || | +--> val_end |
| 6145 | * | || || +---------> delim |
| 6146 | * | || |+------------> val_beg |
| 6147 | * | || +-------------> att_end = equal |
| 6148 | * | |+-----------------> att_beg |
| 6149 | * | +------------------> prev |
| 6150 | * +-------------------------> hdr_beg |
| 6151 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6152 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6153 | if (t->be->options & PR_O_COOK_PFX) { |
| 6154 | for (delim = val_beg; delim < val_end; delim++) |
| 6155 | if (*delim == COOKIE_DELIM) |
| 6156 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6157 | } else { |
| 6158 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6159 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6160 | /* Now check if the cookie contains a date field, which would |
| 6161 | * appear after a vertical bar ('|') just after the server name |
| 6162 | * and before the delimiter. |
| 6163 | */ |
| 6164 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6165 | if (vbar1) { |
| 6166 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6167 | * right is the last seen date. It is a base64 encoded |
| 6168 | * 30-bit value representing the UNIX date since the |
| 6169 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6170 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6171 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6172 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6173 | if (val_end - vbar1 >= 5) { |
| 6174 | val = b64tos30(vbar1); |
| 6175 | if (val > 0) |
| 6176 | txn->cookie_last_date = val << 2; |
| 6177 | } |
| 6178 | /* look for a second vertical bar */ |
| 6179 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6180 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6181 | val = b64tos30(vbar1 + 1); |
| 6182 | if (val > 0) |
| 6183 | txn->cookie_first_date = val << 2; |
| 6184 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6185 | } |
| 6186 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6187 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6188 | /* if the cookie has an expiration date and the proxy wants to check |
| 6189 | * it, then we do that now. We first check if the cookie is too old, |
| 6190 | * then only if it has expired. We detect strict overflow because the |
| 6191 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6192 | * in the future are ignored if their offset is beyond one day. This |
| 6193 | * allows an admin to fix timezone issues without expiring everyone |
| 6194 | * and at the same time avoids keeping unwanted side effects for too |
| 6195 | * long. |
| 6196 | */ |
| 6197 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6198 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6199 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6200 | txn->flags &= ~TX_CK_MASK; |
| 6201 | txn->flags |= TX_CK_OLD; |
| 6202 | delim = val_beg; // let's pretend we have not found the cookie |
| 6203 | txn->cookie_first_date = 0; |
| 6204 | txn->cookie_last_date = 0; |
| 6205 | } |
| 6206 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6207 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6208 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6209 | txn->flags &= ~TX_CK_MASK; |
| 6210 | txn->flags |= TX_CK_EXPIRED; |
| 6211 | delim = val_beg; // let's pretend we have not found the cookie |
| 6212 | txn->cookie_first_date = 0; |
| 6213 | txn->cookie_last_date = 0; |
| 6214 | } |
| 6215 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6216 | /* Here, we'll look for the first running server which supports the cookie. |
| 6217 | * This allows to share a same cookie between several servers, for example |
| 6218 | * to dedicate backup servers to specific servers only. |
| 6219 | * However, to prevent clients from sticking to cookie-less backup server |
| 6220 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6221 | * empty cookies and mark them as invalid. |
| 6222 | * The same behaviour is applied when persistence must be ignored. |
| 6223 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame^] | 6224 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6225 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6227 | while (srv) { |
| 6228 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6229 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6230 | if ((srv->state & SRV_RUNNING) || |
| 6231 | (t->be->options & PR_O_PERSIST) || |
| 6232 | (t->flags & SN_FORCE_PRST)) { |
| 6233 | /* we found the server and we can use it */ |
| 6234 | txn->flags &= ~TX_CK_MASK; |
| 6235 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6236 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6237 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6238 | break; |
| 6239 | } else { |
| 6240 | /* we found a server, but it's down, |
| 6241 | * mark it as such and go on in case |
| 6242 | * another one is available. |
| 6243 | */ |
| 6244 | txn->flags &= ~TX_CK_MASK; |
| 6245 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6246 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6247 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6248 | srv = srv->next; |
| 6249 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6250 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6251 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6252 | /* no server matched this cookie */ |
| 6253 | txn->flags &= ~TX_CK_MASK; |
| 6254 | txn->flags |= TX_CK_INVALID; |
| 6255 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6256 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6257 | /* depending on the cookie mode, we may have to either : |
| 6258 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6259 | * the server never sees it ; |
| 6260 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6261 | * application cookie so that it does not get accidentely removed later, |
| 6262 | * if we're in cookie prefix mode |
| 6263 | */ |
| 6264 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6265 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6266 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6267 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6268 | val_end += delta; |
| 6269 | next += delta; |
| 6270 | hdr_end += delta; |
| 6271 | hdr_next += delta; |
| 6272 | cur_hdr->len += delta; |
| 6273 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6274 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6275 | del_from = NULL; |
| 6276 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6277 | } |
| 6278 | else if (del_from == NULL && |
| 6279 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6280 | del_from = prev; |
| 6281 | } |
| 6282 | } else { |
| 6283 | /* This is not our cookie, so we must preserve it. But if we already |
| 6284 | * scheduled another cookie for removal, we cannot remove the |
| 6285 | * complete header, but we can remove the previous block itself. |
| 6286 | */ |
| 6287 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6288 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6289 | if (del_from != NULL) { |
| 6290 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6291 | if (att_beg >= del_from) |
| 6292 | att_beg += delta; |
| 6293 | if (att_end >= del_from) |
| 6294 | att_end += delta; |
| 6295 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6296 | val_end += delta; |
| 6297 | next += delta; |
| 6298 | hdr_end += delta; |
| 6299 | hdr_next += delta; |
| 6300 | cur_hdr->len += delta; |
| 6301 | http_msg_move_end(&txn->req, delta); |
| 6302 | prev = del_from; |
| 6303 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6304 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6305 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6306 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6307 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6308 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6309 | int cmp_len, value_len; |
| 6310 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6311 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6312 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6313 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6314 | value_begin = att_beg + t->be->appsession_name_len; |
| 6315 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6316 | } else { |
| 6317 | cmp_len = att_end - att_beg; |
| 6318 | value_begin = val_beg; |
| 6319 | value_len = val_end - val_beg; |
| 6320 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6321 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6322 | /* let's see if the cookie is our appcookie */ |
| 6323 | if (cmp_len == t->be->appsession_name_len && |
| 6324 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6325 | manage_client_side_appsession(t, value_begin, value_len); |
| 6326 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6327 | } |
| 6328 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6329 | /* continue with next cookie on this header line */ |
| 6330 | att_beg = next; |
| 6331 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6332 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6333 | /* There are no more cookies on this line. |
| 6334 | * We may still have one (or several) marked for deletion at the |
| 6335 | * end of the line. We must do this now in two ways : |
| 6336 | * - if some cookies must be preserved, we only delete from the |
| 6337 | * mark to the end of line ; |
| 6338 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6339 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6340 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6341 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6342 | if (preserve_hdr) { |
| 6343 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6344 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6345 | cur_hdr->len += delta; |
| 6346 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6347 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6348 | |
| 6349 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6350 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6351 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6352 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6353 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6354 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6355 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6356 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6357 | } |
| 6358 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6359 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6360 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6361 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6362 | } |
| 6363 | |
| 6364 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6365 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6366 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6367 | */ |
| 6368 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6369 | { |
| 6370 | char term; |
| 6371 | char *cur_ptr, *cur_end, *cur_next; |
| 6372 | int cur_idx, old_idx, last_hdr; |
| 6373 | struct http_txn *txn = &t->txn; |
| 6374 | struct hdr_idx_elem *cur_hdr; |
| 6375 | int len, delta; |
| 6376 | |
| 6377 | last_hdr = 0; |
| 6378 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6379 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6380 | old_idx = 0; |
| 6381 | |
| 6382 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6383 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6384 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6385 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6386 | (exp->action == ACT_ALLOW || |
| 6387 | exp->action == ACT_DENY)) |
| 6388 | return 0; |
| 6389 | |
| 6390 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6391 | if (!cur_idx) |
| 6392 | break; |
| 6393 | |
| 6394 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6395 | cur_ptr = cur_next; |
| 6396 | cur_end = cur_ptr + cur_hdr->len; |
| 6397 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6398 | |
| 6399 | /* Now we have one header between cur_ptr and cur_end, |
| 6400 | * and the next header starts at cur_next. |
| 6401 | */ |
| 6402 | |
| 6403 | /* The annoying part is that pattern matching needs |
| 6404 | * that we modify the contents to null-terminate all |
| 6405 | * strings before testing them. |
| 6406 | */ |
| 6407 | |
| 6408 | term = *cur_end; |
| 6409 | *cur_end = '\0'; |
| 6410 | |
| 6411 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6412 | switch (exp->action) { |
| 6413 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6414 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6415 | last_hdr = 1; |
| 6416 | break; |
| 6417 | |
| 6418 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6419 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6420 | last_hdr = 1; |
| 6421 | break; |
| 6422 | |
| 6423 | case ACT_REPLACE: |
| 6424 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6425 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6426 | /* FIXME: if the user adds a newline in the replacement, the |
| 6427 | * index will not be recalculated for now, and the new line |
| 6428 | * will not be counted as a new header. |
| 6429 | */ |
| 6430 | |
| 6431 | cur_end += delta; |
| 6432 | cur_next += delta; |
| 6433 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6434 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6435 | break; |
| 6436 | |
| 6437 | case ACT_REMOVE: |
| 6438 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6439 | cur_next += delta; |
| 6440 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6441 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6442 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6443 | txn->hdr_idx.used--; |
| 6444 | cur_hdr->len = 0; |
| 6445 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6446 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6447 | break; |
| 6448 | |
| 6449 | } |
| 6450 | } |
| 6451 | if (cur_end) |
| 6452 | *cur_end = term; /* restore the string terminator */ |
| 6453 | |
| 6454 | /* keep the link from this header to next one in case of later |
| 6455 | * removal of next header. |
| 6456 | */ |
| 6457 | old_idx = cur_idx; |
| 6458 | } |
| 6459 | return 0; |
| 6460 | } |
| 6461 | |
| 6462 | |
| 6463 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6464 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6465 | * or -1 if a replacement resulted in an invalid status line. |
| 6466 | */ |
| 6467 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6468 | { |
| 6469 | char term; |
| 6470 | char *cur_ptr, *cur_end; |
| 6471 | int done; |
| 6472 | struct http_txn *txn = &t->txn; |
| 6473 | int len, delta; |
| 6474 | |
| 6475 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6476 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6477 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6478 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6479 | (exp->action == ACT_ALLOW || |
| 6480 | exp->action == ACT_DENY)) |
| 6481 | return 0; |
| 6482 | else if (exp->action == ACT_REMOVE) |
| 6483 | return 0; |
| 6484 | |
| 6485 | done = 0; |
| 6486 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6487 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6488 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6489 | |
| 6490 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6491 | |
| 6492 | /* The annoying part is that pattern matching needs |
| 6493 | * that we modify the contents to null-terminate all |
| 6494 | * strings before testing them. |
| 6495 | */ |
| 6496 | |
| 6497 | term = *cur_end; |
| 6498 | *cur_end = '\0'; |
| 6499 | |
| 6500 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6501 | switch (exp->action) { |
| 6502 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6503 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6504 | done = 1; |
| 6505 | break; |
| 6506 | |
| 6507 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6508 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6509 | done = 1; |
| 6510 | break; |
| 6511 | |
| 6512 | case ACT_REPLACE: |
| 6513 | *cur_end = term; /* restore the string terminator */ |
| 6514 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6515 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6516 | /* FIXME: if the user adds a newline in the replacement, the |
| 6517 | * index will not be recalculated for now, and the new line |
| 6518 | * will not be counted as a new header. |
| 6519 | */ |
| 6520 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6521 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6522 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6523 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6524 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6525 | cur_ptr, cur_end + 1, |
| 6526 | NULL, NULL); |
| 6527 | if (unlikely(!cur_end)) |
| 6528 | return -1; |
| 6529 | |
| 6530 | /* we have a full respnse and we know that we have either a CR |
| 6531 | * or an LF at <ptr>. |
| 6532 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6533 | 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] | 6534 | 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] | 6535 | /* there is no point trying this regex on headers */ |
| 6536 | return 1; |
| 6537 | } |
| 6538 | } |
| 6539 | *cur_end = term; /* restore the string terminator */ |
| 6540 | return done; |
| 6541 | } |
| 6542 | |
| 6543 | |
| 6544 | |
| 6545 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6546 | * 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] | 6547 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6548 | * unparsable response. |
| 6549 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6550 | 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] | 6551 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6552 | struct http_txn *txn = &s->txn; |
| 6553 | struct hdr_exp *exp; |
| 6554 | |
| 6555 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6556 | int ret; |
| 6557 | |
| 6558 | /* |
| 6559 | * The interleaving of transformations and verdicts |
| 6560 | * makes it difficult to decide to continue or stop |
| 6561 | * the evaluation. |
| 6562 | */ |
| 6563 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6564 | if (txn->flags & TX_SVDENY) |
| 6565 | break; |
| 6566 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6567 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6568 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6569 | exp->action == ACT_PASS)) { |
| 6570 | exp = exp->next; |
| 6571 | continue; |
| 6572 | } |
| 6573 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6574 | /* if this filter had a condition, evaluate it now and skip to |
| 6575 | * next filter if the condition does not match. |
| 6576 | */ |
| 6577 | if (exp->cond) { |
| 6578 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6579 | ret = acl_pass(ret); |
| 6580 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6581 | ret = !ret; |
| 6582 | if (!ret) |
| 6583 | continue; |
| 6584 | } |
| 6585 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6586 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6587 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6588 | if (unlikely(ret < 0)) |
| 6589 | return -1; |
| 6590 | |
| 6591 | if (likely(ret == 0)) { |
| 6592 | /* The filter did not match the response, it can be |
| 6593 | * iterated through all headers. |
| 6594 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6595 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6596 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6597 | } |
| 6598 | return 0; |
| 6599 | } |
| 6600 | |
| 6601 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6602 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6603 | * 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] | 6604 | * desirable to call it only when needed. This function is also used when we |
| 6605 | * 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] | 6606 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6607 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6608 | { |
| 6609 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6610 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6611 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6612 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6613 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6614 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6615 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6616 | /* Iterate through the headers. |
| 6617 | * we start with the start line. |
| 6618 | */ |
| 6619 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6620 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6621 | |
| 6622 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6623 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6624 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6625 | |
| 6626 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6627 | hdr_beg = hdr_next; |
| 6628 | hdr_end = hdr_beg + cur_hdr->len; |
| 6629 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6630 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6631 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6632 | * next header starts at hdr_next. We're only interested in |
| 6633 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6634 | */ |
| 6635 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6636 | is_cookie2 = 0; |
| 6637 | prev = hdr_beg + 10; |
| 6638 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6639 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6640 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6641 | if (!val) { |
| 6642 | old_idx = cur_idx; |
| 6643 | continue; |
| 6644 | } |
| 6645 | is_cookie2 = 1; |
| 6646 | prev = hdr_beg + 11; |
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 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6650 | * <prev> points to the colon. |
| 6651 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6652 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6653 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6654 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6655 | * check-cache is enabled) and we are not interested in checking |
| 6656 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6657 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6658 | if (t->be->cookie_name == NULL && |
| 6659 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6660 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6661 | return; |
| 6662 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6663 | /* OK so now we know we have to process this response cookie. |
| 6664 | * The format of the Set-Cookie header is slightly different |
| 6665 | * from the format of the Cookie header in that it does not |
| 6666 | * support the comma as a cookie delimiter (thus the header |
| 6667 | * cannot be folded) because the Expires attribute described in |
| 6668 | * the original Netscape's spec may contain an unquoted date |
| 6669 | * with a comma inside. We have to live with this because |
| 6670 | * many browsers don't support Max-Age and some browsers don't |
| 6671 | * support quoted strings. However the Set-Cookie2 header is |
| 6672 | * clean. |
| 6673 | * |
| 6674 | * We have to keep multiple pointers in order to support cookie |
| 6675 | * removal at the beginning, middle or end of header without |
| 6676 | * corrupting the header (in case of set-cookie2). A special |
| 6677 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6678 | * fields after the first semi-colon. The <next> pointer points |
| 6679 | * either to the end of line (set-cookie) or next unquoted comma |
| 6680 | * (set-cookie2). All of these headers are valid : |
| 6681 | * |
| 6682 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6683 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6684 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6685 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6686 | * | | | | | | | | | | |
| 6687 | * | | | | | | | | +-> next hdr_end <--+ |
| 6688 | * | | | | | | | +------------> scav |
| 6689 | * | | | | | | +--------------> val_end |
| 6690 | * | | | | | +--------------------> val_beg |
| 6691 | * | | | | +----------------------> equal |
| 6692 | * | | | +------------------------> att_end |
| 6693 | * | | +----------------------------> att_beg |
| 6694 | * | +------------------------------> prev |
| 6695 | * +-----------------------------------------> hdr_beg |
| 6696 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6697 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6698 | for (; prev < hdr_end; prev = next) { |
| 6699 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6700 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6701 | /* find att_beg */ |
| 6702 | att_beg = prev + 1; |
| 6703 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6704 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6705 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6706 | /* find att_end : this is the first character after the last non |
| 6707 | * space before the equal. It may be equal to hdr_end. |
| 6708 | */ |
| 6709 | equal = att_end = att_beg; |
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 | while (equal < hdr_end) { |
| 6712 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6713 | break; |
| 6714 | if (http_is_spht[(unsigned char)*equal++]) |
| 6715 | continue; |
| 6716 | att_end = equal; |
| 6717 | } |
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 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6720 | * is between <att_beg> and <equal>, both may be identical. |
| 6721 | */ |
| 6722 | |
| 6723 | /* look for end of cookie if there is an equal sign */ |
| 6724 | if (equal < hdr_end && *equal == '=') { |
| 6725 | /* look for the beginning of the value */ |
| 6726 | val_beg = equal + 1; |
| 6727 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6728 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6729 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6730 | /* find the end of the value, respecting quotes */ |
| 6731 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6732 | |
| 6733 | /* make val_end point to the first white space or delimitor after the value */ |
| 6734 | val_end = next; |
| 6735 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6736 | val_end--; |
| 6737 | } else { |
| 6738 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6739 | val_beg = val_end = next = equal; |
| 6740 | } |
| 6741 | |
| 6742 | if (next < hdr_end) { |
| 6743 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6744 | * a colon or semi-colon before the end. So skip all attr-value |
| 6745 | * pairs and look for the next comma. For Set-Cookie, since |
| 6746 | * commas are permitted in values, skip to the end. |
| 6747 | */ |
| 6748 | if (is_cookie2) |
| 6749 | next = find_hdr_value_end(next, hdr_end); |
| 6750 | else |
| 6751 | next = hdr_end; |
| 6752 | } |
| 6753 | |
| 6754 | /* Now everything is as on the diagram above */ |
| 6755 | |
| 6756 | /* Ignore cookies with no equal sign */ |
| 6757 | if (equal == val_end) |
| 6758 | continue; |
| 6759 | |
| 6760 | /* If there are spaces around the equal sign, we need to |
| 6761 | * strip them otherwise we'll get trouble for cookie captures, |
| 6762 | * or even for rewrites. Since this happens extremely rarely, |
| 6763 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6764 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6765 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6766 | int stripped_before = 0; |
| 6767 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6768 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6769 | if (att_end != equal) { |
| 6770 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6771 | equal += stripped_before; |
| 6772 | val_beg += stripped_before; |
| 6773 | } |
| 6774 | |
| 6775 | if (val_beg > equal + 1) { |
| 6776 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6777 | val_beg += stripped_after; |
| 6778 | stripped_before += stripped_after; |
| 6779 | } |
| 6780 | |
| 6781 | val_end += stripped_before; |
| 6782 | next += stripped_before; |
| 6783 | hdr_end += stripped_before; |
| 6784 | hdr_next += stripped_before; |
| 6785 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6786 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | /* First, let's see if we want to capture this cookie. We check |
| 6790 | * that we don't already have a server side cookie, because we |
| 6791 | * can only capture one. Also as an optimisation, we ignore |
| 6792 | * cookies shorter than the declared name. |
| 6793 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6794 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6795 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6796 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6797 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6798 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6799 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6800 | Alert("HTTP logging : out of memory.\n"); |
| 6801 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6802 | else { |
| 6803 | if (log_len > t->fe->capture_len) |
| 6804 | log_len = t->fe->capture_len; |
| 6805 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6806 | txn->srv_cookie[log_len] = 0; |
| 6807 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6808 | } |
| 6809 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6810 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6811 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6812 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6813 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6814 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6815 | /* assume passive cookie by default */ |
| 6816 | txn->flags &= ~TX_SCK_MASK; |
| 6817 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6818 | |
| 6819 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6820 | * this occurrence because we'll insert another one later. |
| 6821 | * 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] | 6822 | * a direct access. |
| 6823 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6824 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 6825 | /* The "preserve" flag was set, we don't want to touch the |
| 6826 | * server's cookie. |
| 6827 | */ |
| 6828 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6829 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6830 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6831 | /* this cookie must be deleted */ |
| 6832 | if (*prev == ':' && next == hdr_end) { |
| 6833 | /* whole header */ |
| 6834 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6835 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6836 | txn->hdr_idx.used--; |
| 6837 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6838 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6839 | hdr_next += delta; |
| 6840 | http_msg_move_end(&txn->rsp, delta); |
| 6841 | /* note: while both invalid now, <next> and <hdr_end> |
| 6842 | * are still equal, so the for() will stop as expected. |
| 6843 | */ |
| 6844 | } else { |
| 6845 | /* just remove the value */ |
| 6846 | int delta = del_hdr_value(res, &prev, next); |
| 6847 | next = prev; |
| 6848 | hdr_end += delta; |
| 6849 | hdr_next += delta; |
| 6850 | cur_hdr->len += delta; |
| 6851 | http_msg_move_end(&txn->rsp, delta); |
| 6852 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6853 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6854 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6855 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6856 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6857 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6858 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6859 | * with this server since we know it. |
| 6860 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6861 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6862 | next += delta; |
| 6863 | hdr_end += delta; |
| 6864 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6865 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6866 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6867 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6868 | txn->flags &= ~TX_SCK_MASK; |
| 6869 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6870 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6871 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6872 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6873 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6874 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6875 | 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] | 6876 | next += delta; |
| 6877 | hdr_end += delta; |
| 6878 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6879 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6880 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6881 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6882 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6883 | txn->flags &= ~TX_SCK_MASK; |
| 6884 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6885 | } |
| 6886 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6887 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6888 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6889 | int cmp_len, value_len; |
| 6890 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6891 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6892 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6893 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6894 | value_begin = att_beg + t->be->appsession_name_len; |
| 6895 | 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] | 6896 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6897 | cmp_len = att_end - att_beg; |
| 6898 | value_begin = val_beg; |
| 6899 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6900 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6901 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6902 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6903 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6904 | /* free a possibly previously allocated memory */ |
| 6905 | pool_free2(apools.sessid, txn->sessid); |
| 6906 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6907 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6908 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6909 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6910 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6911 | return; |
| 6912 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6913 | memcpy(txn->sessid, value_begin, value_len); |
| 6914 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6915 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6916 | } |
| 6917 | /* that's done for this cookie, check the next one on the same |
| 6918 | * line when next != hdr_end (only if is_cookie2). |
| 6919 | */ |
| 6920 | } |
| 6921 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6922 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6923 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6924 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6925 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6926 | appsess *asession = NULL; |
| 6927 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6928 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6929 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6930 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6931 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6932 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6933 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6934 | return; |
| 6935 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6936 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 6937 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6938 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6939 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6940 | 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] | 6941 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6942 | return; |
| 6943 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6944 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6945 | asession->sessid[t->be->appsession_len] = 0; |
| 6946 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6947 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6948 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6949 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6950 | 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] | 6951 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6952 | return; |
| 6953 | } |
| 6954 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6955 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6956 | |
| 6957 | asession->request_count = 0; |
| 6958 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6959 | } |
| 6960 | |
| 6961 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6962 | asession->request_count++; |
| 6963 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6964 | } |
| 6965 | |
| 6966 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6967 | /* |
| 6968 | * Check if response is cacheable or not. Updates t->flags. |
| 6969 | */ |
| 6970 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6971 | { |
| 6972 | struct http_txn *txn = &t->txn; |
| 6973 | char *p1, *p2; |
| 6974 | |
| 6975 | char *cur_ptr, *cur_end, *cur_next; |
| 6976 | int cur_idx; |
| 6977 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6978 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6979 | return; |
| 6980 | |
| 6981 | /* Iterate through the headers. |
| 6982 | * we start with the start line. |
| 6983 | */ |
| 6984 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6985 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6986 | |
| 6987 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6988 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6989 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6990 | |
| 6991 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6992 | cur_ptr = cur_next; |
| 6993 | cur_end = cur_ptr + cur_hdr->len; |
| 6994 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6995 | |
| 6996 | /* We have one full header between cur_ptr and cur_end, and the |
| 6997 | * next header starts at cur_next. We're only interested in |
| 6998 | * "Cookie:" headers. |
| 6999 | */ |
| 7000 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7001 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7002 | if (val) { |
| 7003 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7004 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7005 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7006 | return; |
| 7007 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7008 | } |
| 7009 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7010 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7011 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7012 | continue; |
| 7013 | |
| 7014 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7015 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7016 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7017 | |
| 7018 | if (p1 >= cur_end) /* no more info */ |
| 7019 | continue; |
| 7020 | |
| 7021 | /* p1 is at the beginning of the value */ |
| 7022 | p2 = p1; |
| 7023 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7024 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7025 | p2++; |
| 7026 | |
| 7027 | /* we have a complete value between p1 and p2 */ |
| 7028 | if (p2 < cur_end && *p2 == '=') { |
| 7029 | /* we have something of the form no-cache="set-cookie" */ |
| 7030 | if ((cur_end - p1 >= 21) && |
| 7031 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7032 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7033 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7034 | continue; |
| 7035 | } |
| 7036 | |
| 7037 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7038 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7039 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7040 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7041 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7042 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7043 | return; |
| 7044 | } |
| 7045 | |
| 7046 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7047 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7048 | continue; |
| 7049 | } |
| 7050 | } |
| 7051 | } |
| 7052 | |
| 7053 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7054 | /* |
| 7055 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7056 | * If the server is found, it's assigned to the session. |
| 7057 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7058 | 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] | 7059 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7060 | char *end_params, *first_param, *cur_param, *next_param; |
| 7061 | char separator; |
| 7062 | int value_len; |
| 7063 | |
| 7064 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7065 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7066 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7067 | (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] | 7068 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7069 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7070 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7071 | first_param = NULL; |
| 7072 | switch (mode) { |
| 7073 | case PR_O2_AS_M_PP: |
| 7074 | first_param = memchr(begin, ';', len); |
| 7075 | break; |
| 7076 | case PR_O2_AS_M_QS: |
| 7077 | first_param = memchr(begin, '?', len); |
| 7078 | break; |
| 7079 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7080 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7081 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7082 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7083 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7084 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7085 | switch (mode) { |
| 7086 | case PR_O2_AS_M_PP: |
| 7087 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7088 | end_params = (char *) begin + len; |
| 7089 | } |
| 7090 | separator = ';'; |
| 7091 | break; |
| 7092 | case PR_O2_AS_M_QS: |
| 7093 | end_params = (char *) begin + len; |
| 7094 | separator = '&'; |
| 7095 | break; |
| 7096 | default: |
| 7097 | /* unknown mode, shouldn't happen */ |
| 7098 | return; |
| 7099 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7100 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7101 | cur_param = next_param = end_params; |
| 7102 | while (cur_param > first_param) { |
| 7103 | cur_param--; |
| 7104 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7105 | /* let's see if this is the appsession parameter */ |
| 7106 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7107 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7108 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7109 | /* Cool... it's the right one */ |
| 7110 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7111 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7112 | if (value_len > 0) { |
| 7113 | manage_client_side_appsession(t, cur_param, value_len); |
| 7114 | } |
| 7115 | break; |
| 7116 | } |
| 7117 | next_param = cur_param; |
| 7118 | } |
| 7119 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7120 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7121 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7122 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7123 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7124 | } |
| 7125 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7126 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7127 | * 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] | 7128 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7129 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7130 | * 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] | 7131 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7132 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7133 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7134 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7135 | 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] | 7136 | { |
| 7137 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7138 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7139 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7140 | if (!uri_auth) |
| 7141 | return 0; |
| 7142 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7143 | 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] | 7144 | return 0; |
| 7145 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7146 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7147 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7148 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7149 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7150 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7151 | return 0; |
| 7152 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7153 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7154 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7155 | /* the URI is in h */ |
| 7156 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7157 | return 0; |
| 7158 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7159 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7160 | 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] | 7161 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7162 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7163 | break; |
| 7164 | } |
| 7165 | h++; |
| 7166 | } |
| 7167 | |
| 7168 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7169 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7170 | 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] | 7171 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7172 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7173 | break; |
| 7174 | } |
| 7175 | h++; |
| 7176 | } |
| 7177 | } |
| 7178 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7179 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7180 | 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] | 7181 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7182 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7183 | break; |
| 7184 | } |
| 7185 | h++; |
| 7186 | } |
| 7187 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7188 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7189 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7190 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7191 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7192 | h += 4; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7193 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7194 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7195 | si->applet.ctx.stats.st_code = i; |
| 7196 | break; |
| 7197 | } |
| 7198 | } |
| 7199 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7200 | break; |
| 7201 | } |
| 7202 | h++; |
| 7203 | } |
| 7204 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7205 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7206 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7207 | return 1; |
| 7208 | } |
| 7209 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7210 | /* |
| 7211 | * 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] | 7212 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7213 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7214 | */ |
| 7215 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7216 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7217 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7218 | { |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7219 | if (buf->r <= (buf->data + msg->som)) { /* message wraps */ |
| 7220 | int len1 = buf->size - msg->som; |
| 7221 | es->len = buf->r - (buf->data + msg->som) + buf->size; |
| 7222 | memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf))); |
| 7223 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7224 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7225 | } |
| 7226 | else { |
| 7227 | es->len = buf->r - (buf->data + msg->som); |
| 7228 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
| 7229 | } |
| 7230 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7231 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7232 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7233 | else if (buf->lr >= (buf->data + msg->som)) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7234 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7235 | else |
| 7236 | es->pos = buf->lr - (buf->data + msg->som) + buf->size; |
| 7237 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7238 | es->when = date; // user-visible date |
| 7239 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7240 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7241 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7242 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7243 | es->state = state; |
| 7244 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7245 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7246 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7247 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7248 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7249 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7250 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7251 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7252 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7253 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7254 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7255 | * -1. |
| 7256 | * 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] | 7257 | */ |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7258 | unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen, |
| 7259 | struct hdr_idx *idx, int occ, |
| 7260 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7261 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7262 | struct hdr_ctx local_ctx; |
| 7263 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7264 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7265 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7266 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7267 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7268 | if (!ctx) { |
| 7269 | local_ctx.idx = 0; |
| 7270 | ctx = &local_ctx; |
| 7271 | } |
| 7272 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7273 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7274 | /* search from the beginning */ |
| 7275 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7276 | occ--; |
| 7277 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7278 | *vptr = ctx->line + ctx->val; |
| 7279 | *vlen = ctx->vlen; |
| 7280 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7281 | } |
| 7282 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7283 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7284 | } |
| 7285 | |
| 7286 | /* negative occurrence, we scan all the list then walk back */ |
| 7287 | if (-occ > MAX_HDR_HISTORY) |
| 7288 | return 0; |
| 7289 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7290 | found = hist_ptr = 0; |
| 7291 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
| 7292 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7293 | len_hist[hist_ptr] = ctx->vlen; |
| 7294 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7295 | hist_ptr = 0; |
| 7296 | found++; |
| 7297 | } |
| 7298 | if (-occ > found) |
| 7299 | return 0; |
| 7300 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7301 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7302 | */ |
| 7303 | hist_ptr += occ; |
| 7304 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7305 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7306 | *vptr = ptr_hist[hist_ptr]; |
| 7307 | *vlen = len_hist[hist_ptr]; |
| 7308 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7309 | } |
| 7310 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7311 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7312 | * Print a debug line with a header |
| 7313 | */ |
| 7314 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7315 | { |
| 7316 | int len, max; |
| 7317 | 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] | 7318 | 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] | 7319 | max = end - start; |
| 7320 | UBOUND(max, sizeof(trash) - len - 1); |
| 7321 | len += strlcpy2(trash + len, start, max + 1); |
| 7322 | trash[len++] = '\n'; |
| 7323 | write(1, trash, len); |
| 7324 | } |
| 7325 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7326 | /* |
| 7327 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7328 | * the required fields are properly allocated and that we only need to (re)init |
| 7329 | * them. This should be used before processing any new request. |
| 7330 | */ |
| 7331 | void http_init_txn(struct session *s) |
| 7332 | { |
| 7333 | struct http_txn *txn = &s->txn; |
| 7334 | struct proxy *fe = s->fe; |
| 7335 | |
| 7336 | txn->flags = 0; |
| 7337 | txn->status = -1; |
| 7338 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7339 | txn->cookie_first_date = 0; |
| 7340 | txn->cookie_last_date = 0; |
| 7341 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7342 | txn->req.sol = txn->req.eol = NULL; |
| 7343 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 7344 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7345 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7346 | txn->req.chunk_len = 0LL; |
| 7347 | txn->req.body_len = 0LL; |
| 7348 | txn->rsp.chunk_len = 0LL; |
| 7349 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7350 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7351 | 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] | 7352 | |
| 7353 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7354 | |
| 7355 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7356 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7357 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7358 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7359 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7360 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7361 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7362 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7363 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7364 | |
| 7365 | if (txn->hdr_idx.v) |
| 7366 | hdr_idx_init(&txn->hdr_idx); |
| 7367 | } |
| 7368 | |
| 7369 | /* to be used at the end of a transaction */ |
| 7370 | void http_end_txn(struct session *s) |
| 7371 | { |
| 7372 | struct http_txn *txn = &s->txn; |
| 7373 | |
| 7374 | /* these ones will have been dynamically allocated */ |
| 7375 | pool_free2(pool2_requri, txn->uri); |
| 7376 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7377 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7378 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7379 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7380 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7381 | txn->uri = NULL; |
| 7382 | txn->srv_cookie = NULL; |
| 7383 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7384 | |
| 7385 | if (txn->req.cap) { |
| 7386 | struct cap_hdr *h; |
| 7387 | for (h = s->fe->req_cap; h; h = h->next) |
| 7388 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7389 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7390 | } |
| 7391 | |
| 7392 | if (txn->rsp.cap) { |
| 7393 | struct cap_hdr *h; |
| 7394 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7395 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7396 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7397 | } |
| 7398 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | /* to be used at the end of a transaction to prepare a new one */ |
| 7402 | void http_reset_txn(struct session *s) |
| 7403 | { |
| 7404 | http_end_txn(s); |
| 7405 | http_init_txn(s); |
| 7406 | |
| 7407 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7408 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7409 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7410 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7411 | /* re-init store persistence */ |
| 7412 | s->store_count = 0; |
| 7413 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7414 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7415 | |
| 7416 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7417 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7418 | /* We must trim any excess data from the response buffer, because we |
| 7419 | * may have blocked an invalid response from a server that we don't |
| 7420 | * want to accidentely forward once we disable the analysers, nor do |
| 7421 | * we want those data to come along with next response. A typical |
| 7422 | * example of such data would be from a buggy server responding to |
| 7423 | * a HEAD with some data, or sending more than the advertised |
| 7424 | * content-length. |
| 7425 | */ |
| 7426 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7427 | s->rep->l = s->rep->send_max; |
| 7428 | s->rep->r = s->rep->w + s->rep->l; |
| 7429 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7430 | s->rep->r -= s->rep->size; |
| 7431 | } |
| 7432 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7433 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7434 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7435 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7436 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7437 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7438 | |
| 7439 | s->req->rex = TICK_ETERNITY; |
| 7440 | s->req->wex = TICK_ETERNITY; |
| 7441 | s->req->analyse_exp = TICK_ETERNITY; |
| 7442 | s->rep->rex = TICK_ETERNITY; |
| 7443 | s->rep->wex = TICK_ETERNITY; |
| 7444 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7446 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7447 | void free_http_req_rules(struct list *r) { |
| 7448 | struct http_req_rule *tr, *pr; |
| 7449 | |
| 7450 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7451 | LIST_DEL(&pr->list); |
| 7452 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7453 | free(pr->http_auth.realm); |
| 7454 | |
| 7455 | free(pr); |
| 7456 | } |
| 7457 | } |
| 7458 | |
| 7459 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7460 | { |
| 7461 | struct http_req_rule *rule; |
| 7462 | int cur_arg; |
| 7463 | |
| 7464 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7465 | if (!rule) { |
| 7466 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7467 | return NULL; |
| 7468 | } |
| 7469 | |
| 7470 | if (!*args[0]) { |
| 7471 | goto req_error_parsing; |
| 7472 | } else if (!strcmp(args[0], "allow")) { |
| 7473 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7474 | cur_arg = 1; |
| 7475 | } else if (!strcmp(args[0], "deny")) { |
| 7476 | rule->action = HTTP_REQ_ACT_DENY; |
| 7477 | cur_arg = 1; |
| 7478 | } else if (!strcmp(args[0], "auth")) { |
| 7479 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7480 | cur_arg = 1; |
| 7481 | |
| 7482 | while(*args[cur_arg]) { |
| 7483 | if (!strcmp(args[cur_arg], "realm")) { |
| 7484 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7485 | cur_arg+=2; |
| 7486 | continue; |
| 7487 | } else |
| 7488 | break; |
| 7489 | } |
| 7490 | } else { |
| 7491 | req_error_parsing: |
| 7492 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7493 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7494 | return NULL; |
| 7495 | } |
| 7496 | |
| 7497 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7498 | struct acl_cond *cond; |
| 7499 | |
| 7500 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7501 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7502 | file, linenum, args[0]); |
| 7503 | return NULL; |
| 7504 | } |
| 7505 | rule->cond = cond; |
| 7506 | } |
| 7507 | else if (*args[cur_arg]) { |
| 7508 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7509 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7510 | file, linenum, args[0], args[cur_arg]); |
| 7511 | return NULL; |
| 7512 | } |
| 7513 | |
| 7514 | return rule; |
| 7515 | } |
| 7516 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7517 | /************************************************************************/ |
| 7518 | /* The code below is dedicated to ACL parsing and matching */ |
| 7519 | /************************************************************************/ |
| 7520 | |
| 7521 | |
| 7522 | |
| 7523 | |
| 7524 | /* 1. Check on METHOD |
| 7525 | * We use the pre-parsed method if it is known, and store its number as an |
| 7526 | * integer. If it is unknown, we use the pointer and the length. |
| 7527 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7528 | 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] | 7529 | { |
| 7530 | int len, meth; |
| 7531 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7532 | len = strlen(*text); |
| 7533 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7534 | |
| 7535 | pattern->val.i = meth; |
| 7536 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7537 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7538 | if (!pattern->ptr.str) |
| 7539 | return 0; |
| 7540 | pattern->len = len; |
| 7541 | } |
| 7542 | return 1; |
| 7543 | } |
| 7544 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7545 | /* This function fetches the method of current HTTP request and stores |
| 7546 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7547 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7548 | * in <len> and <ptr> is NULL ; |
| 7549 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7550 | * <len> to its length. |
| 7551 | * This is intended to be used with acl_match_meth() only. |
| 7552 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7553 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7554 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7555 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7556 | { |
| 7557 | int meth; |
| 7558 | struct http_txn *txn = l7; |
| 7559 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7560 | if (!txn) |
| 7561 | return 0; |
| 7562 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7563 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7564 | return 0; |
| 7565 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7566 | meth = txn->meth; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7567 | temp_pattern.data.str.len = meth; |
| 7568 | temp_pattern.data.str.str = NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7569 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7570 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7571 | /* ensure the indexes are not affected */ |
| 7572 | return 0; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7573 | temp_pattern.data.str.len = txn->req.sl.rq.m_l; |
| 7574 | temp_pattern.data.str.str = txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7575 | } |
| 7576 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7577 | return 1; |
| 7578 | } |
| 7579 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7580 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7581 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7582 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7583 | int icase; |
| 7584 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7585 | |
| 7586 | if (temp_pattern.data.str.str == NULL) { |
| 7587 | /* well-known method */ |
| 7588 | if (temp_pattern.data.str.len == pattern->val.i) |
| 7589 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7590 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7591 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7592 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7593 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7594 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7595 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7596 | |
| 7597 | /* Other method, we must compare the strings */ |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7598 | if (pattern->len != temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7599 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7600 | |
| 7601 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7602 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) || |
| 7603 | (!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] | 7604 | return ACL_PAT_FAIL; |
| 7605 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7606 | } |
| 7607 | |
| 7608 | /* 2. Check on Request/Status Version |
| 7609 | * We simply compare strings here. |
| 7610 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7611 | 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] | 7612 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7613 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7614 | if (!pattern->ptr.str) |
| 7615 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7616 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7617 | return 1; |
| 7618 | } |
| 7619 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7620 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7621 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7622 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7623 | { |
| 7624 | struct http_txn *txn = l7; |
| 7625 | char *ptr; |
| 7626 | int len; |
| 7627 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7628 | if (!txn) |
| 7629 | return 0; |
| 7630 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7631 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7632 | return 0; |
| 7633 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7634 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7635 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7636 | |
| 7637 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7638 | if (len <= 0) |
| 7639 | return 0; |
| 7640 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7641 | temp_pattern.data.str.str = ptr; |
| 7642 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7643 | |
| 7644 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7645 | return 1; |
| 7646 | } |
| 7647 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7648 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7649 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7650 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7651 | { |
| 7652 | struct http_txn *txn = l7; |
| 7653 | char *ptr; |
| 7654 | int len; |
| 7655 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7656 | if (!txn) |
| 7657 | return 0; |
| 7658 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7659 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7660 | return 0; |
| 7661 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7662 | len = txn->rsp.sl.st.v_l; |
| 7663 | ptr = txn->rsp.sol; |
| 7664 | |
| 7665 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7666 | if (len <= 0) |
| 7667 | return 0; |
| 7668 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7669 | temp_pattern.data.str.str = ptr; |
| 7670 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7671 | |
| 7672 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7673 | return 1; |
| 7674 | } |
| 7675 | |
| 7676 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7677 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7678 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7679 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7680 | { |
| 7681 | struct http_txn *txn = l7; |
| 7682 | char *ptr; |
| 7683 | int len; |
| 7684 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7685 | if (!txn) |
| 7686 | return 0; |
| 7687 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7688 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7689 | return 0; |
| 7690 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7691 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7692 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7693 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7694 | temp_pattern.data.integer = __strl2ui(ptr, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7695 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7696 | return 1; |
| 7697 | } |
| 7698 | |
| 7699 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7700 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7701 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7702 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7703 | { |
| 7704 | struct http_txn *txn = l7; |
| 7705 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7706 | if (!txn) |
| 7707 | return 0; |
| 7708 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7709 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7710 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7711 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7712 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7713 | /* ensure the indexes are not affected */ |
| 7714 | return 0; |
| 7715 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7716 | temp_pattern.data.str.len = txn->req.sl.rq.u_l; |
| 7717 | 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] | 7718 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7719 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7720 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7721 | return 1; |
| 7722 | } |
| 7723 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7724 | static int |
| 7725 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7726 | struct acl_expr *expr, struct acl_test *test) |
| 7727 | { |
| 7728 | struct http_txn *txn = l7; |
| 7729 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7730 | if (!txn) |
| 7731 | return 0; |
| 7732 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7733 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7734 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7735 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7736 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7737 | /* ensure the indexes are not affected */ |
| 7738 | return 0; |
| 7739 | |
| 7740 | /* Parse HTTP request */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7741 | 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] | 7742 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 7743 | return 0; |
| 7744 | temp_pattern.type = PATTERN_TYPE_IP; |
| 7745 | 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] | 7746 | |
| 7747 | /* |
| 7748 | * If we are parsing url in frontend space, we prepare backend stage |
| 7749 | * to not parse again the same url ! optimization lazyness... |
| 7750 | */ |
| 7751 | if (px->options & PR_O_HTTP_PROXY) |
| 7752 | l4->flags |= SN_ADDR_SET; |
| 7753 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7754 | test->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7755 | return 1; |
| 7756 | } |
| 7757 | |
| 7758 | static int |
| 7759 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7760 | struct acl_expr *expr, struct acl_test *test) |
| 7761 | { |
| 7762 | struct http_txn *txn = l7; |
| 7763 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7764 | if (!txn) |
| 7765 | return 0; |
| 7766 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7767 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7768 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7769 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7770 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7771 | /* ensure the indexes are not affected */ |
| 7772 | return 0; |
| 7773 | |
| 7774 | /* Same optimization as url_ip */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7775 | 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] | 7776 | 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] | 7777 | |
| 7778 | if (px->options & PR_O_HTTP_PROXY) |
| 7779 | l4->flags |= SN_ADDR_SET; |
| 7780 | |
| 7781 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7782 | return 1; |
| 7783 | } |
| 7784 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7785 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7786 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7787 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7788 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7789 | 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] | 7790 | struct acl_expr *expr, struct acl_test *test) |
| 7791 | { |
| 7792 | struct http_txn *txn = l7; |
| 7793 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7794 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7795 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7796 | if (!txn) |
| 7797 | return 0; |
| 7798 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7799 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7800 | /* search for header from the beginning */ |
| 7801 | ctx->idx = 0; |
| 7802 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7803 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7804 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7805 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7806 | temp_pattern.data.str.str = (char *)ctx->line + ctx->val; |
| 7807 | temp_pattern.data.str.len = ctx->vlen; |
| 7808 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7809 | return 1; |
| 7810 | } |
| 7811 | |
| 7812 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7813 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7817 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7818 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7819 | struct acl_expr *expr, struct acl_test *test) |
| 7820 | { |
| 7821 | struct http_txn *txn = l7; |
| 7822 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7823 | if (!txn) |
| 7824 | return 0; |
| 7825 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7826 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7827 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7828 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7829 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7830 | /* ensure the indexes are not affected */ |
| 7831 | return 0; |
| 7832 | |
| 7833 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7834 | } |
| 7835 | |
| 7836 | static int |
| 7837 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7838 | struct acl_expr *expr, struct acl_test *test) |
| 7839 | { |
| 7840 | struct http_txn *txn = l7; |
| 7841 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7842 | if (!txn) |
| 7843 | return 0; |
| 7844 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7845 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7846 | return 0; |
| 7847 | |
| 7848 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 7849 | } |
| 7850 | |
| 7851 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7852 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7853 | */ |
| 7854 | static int |
| 7855 | 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] | 7856 | struct acl_expr *expr, struct acl_test *test) |
| 7857 | { |
| 7858 | struct http_txn *txn = l7; |
| 7859 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7860 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7861 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7862 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7863 | if (!txn) |
| 7864 | return 0; |
| 7865 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7866 | ctx.idx = 0; |
| 7867 | cnt = 0; |
| 7868 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7869 | cnt++; |
| 7870 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7871 | temp_pattern.data.integer = cnt; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7872 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7873 | return 1; |
| 7874 | } |
| 7875 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7876 | static int |
| 7877 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7878 | struct acl_expr *expr, struct acl_test *test) |
| 7879 | { |
| 7880 | struct http_txn *txn = l7; |
| 7881 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7882 | if (!txn) |
| 7883 | return 0; |
| 7884 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7885 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7886 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7887 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7888 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7889 | /* ensure the indexes are not affected */ |
| 7890 | return 0; |
| 7891 | |
| 7892 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 7893 | } |
| 7894 | |
| 7895 | static int |
| 7896 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7897 | struct acl_expr *expr, struct acl_test *test) |
| 7898 | { |
| 7899 | struct http_txn *txn = l7; |
| 7900 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7901 | if (!txn) |
| 7902 | return 0; |
| 7903 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7904 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7905 | return 0; |
| 7906 | |
| 7907 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7908 | } |
| 7909 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7910 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7911 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7912 | * 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] | 7913 | */ |
| 7914 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7915 | 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] | 7916 | struct acl_expr *expr, struct acl_test *test) |
| 7917 | { |
| 7918 | struct http_txn *txn = l7; |
| 7919 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7920 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7921 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7922 | if (!txn) |
| 7923 | return 0; |
| 7924 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7925 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7926 | /* search for header from the beginning */ |
| 7927 | ctx->idx = 0; |
| 7928 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7929 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7930 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7931 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7932 | temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7933 | return 1; |
| 7934 | } |
| 7935 | |
| 7936 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7937 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7938 | return 0; |
| 7939 | } |
| 7940 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7941 | static int |
| 7942 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7943 | struct acl_expr *expr, struct acl_test *test) |
| 7944 | { |
| 7945 | struct http_txn *txn = l7; |
| 7946 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7947 | if (!txn) |
| 7948 | return 0; |
| 7949 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7950 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7951 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7952 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7953 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7954 | /* ensure the indexes are not affected */ |
| 7955 | return 0; |
| 7956 | |
| 7957 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7958 | } |
| 7959 | |
| 7960 | static int |
| 7961 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7962 | struct acl_expr *expr, struct acl_test *test) |
| 7963 | { |
| 7964 | struct http_txn *txn = l7; |
| 7965 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7966 | if (!txn) |
| 7967 | return 0; |
| 7968 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7969 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7970 | return 0; |
| 7971 | |
| 7972 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 7973 | } |
| 7974 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7975 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7976 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7977 | */ |
| 7978 | static int |
| 7979 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7980 | struct acl_expr *expr, struct acl_test *test) |
| 7981 | { |
| 7982 | struct http_txn *txn = l7; |
| 7983 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7984 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 7985 | |
| 7986 | if (!txn) |
| 7987 | return 0; |
| 7988 | |
| 7989 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7990 | /* search for header from the beginning */ |
| 7991 | ctx->idx = 0; |
| 7992 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7993 | 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] | 7994 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7995 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7996 | /* Same optimization as url_ip */ |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7997 | temp_pattern.type = PATTERN_TYPE_IP; |
| 7998 | if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip)) |
| 7999 | return 1; |
| 8000 | /* Dods not look like an IP address, let's fetch next one */ |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8001 | } |
| 8002 | |
| 8003 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8004 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8005 | return 0; |
| 8006 | } |
| 8007 | |
| 8008 | static int |
| 8009 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8010 | struct acl_expr *expr, struct acl_test *test) |
| 8011 | { |
| 8012 | struct http_txn *txn = l7; |
| 8013 | |
| 8014 | if (!txn) |
| 8015 | return 0; |
| 8016 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8017 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8018 | return 0; |
| 8019 | |
| 8020 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8021 | /* ensure the indexes are not affected */ |
| 8022 | return 0; |
| 8023 | |
| 8024 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8025 | } |
| 8026 | |
| 8027 | static int |
| 8028 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8029 | struct acl_expr *expr, struct acl_test *test) |
| 8030 | { |
| 8031 | struct http_txn *txn = l7; |
| 8032 | |
| 8033 | if (!txn) |
| 8034 | return 0; |
| 8035 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8036 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8037 | return 0; |
| 8038 | |
| 8039 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8040 | } |
| 8041 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8042 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8043 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8044 | */ |
| 8045 | static int |
| 8046 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8047 | struct acl_expr *expr, struct acl_test *test) |
| 8048 | { |
| 8049 | struct http_txn *txn = l7; |
| 8050 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8051 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8052 | if (!txn) |
| 8053 | return 0; |
| 8054 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8055 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8056 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8057 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8058 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8059 | /* ensure the indexes are not affected */ |
| 8060 | return 0; |
| 8061 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8062 | 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] | 8063 | ptr = http_get_path(txn); |
| 8064 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8065 | return 0; |
| 8066 | |
| 8067 | /* OK, we got the '/' ! */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8068 | temp_pattern.data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8069 | |
| 8070 | while (ptr < end && *ptr != '?') |
| 8071 | ptr++; |
| 8072 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8073 | temp_pattern.data.str.len = ptr - temp_pattern.data.str.str; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8074 | |
| 8075 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8076 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8077 | return 1; |
| 8078 | } |
| 8079 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8080 | static int |
| 8081 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8082 | struct acl_expr *expr, struct acl_test *test) |
| 8083 | { |
| 8084 | struct buffer *req = s->req; |
| 8085 | struct http_txn *txn = &s->txn; |
| 8086 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8087 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8088 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8089 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8090 | */ |
| 8091 | |
| 8092 | if (!s || !req) |
| 8093 | return 0; |
| 8094 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8095 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8096 | /* Already decoded as OK */ |
| 8097 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8098 | return 1; |
| 8099 | } |
| 8100 | |
| 8101 | /* Try to decode HTTP request */ |
| 8102 | if (likely(req->lr < req->r)) |
| 8103 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8104 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8105 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8106 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8107 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8108 | return 1; |
| 8109 | } |
| 8110 | /* wait for final state */ |
| 8111 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8112 | return 0; |
| 8113 | } |
| 8114 | |
| 8115 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8116 | * perform so that further checks can rely on HTTP tests. |
| 8117 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8118 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8119 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8120 | s->flags |= SN_REDIRECTABLE; |
| 8121 | |
| 8122 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8123 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8124 | return 1; |
| 8125 | } |
| 8126 | |
| 8127 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8128 | return 1; |
| 8129 | } |
| 8130 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8131 | /* return a valid test if the current request is the first one on the connection */ |
| 8132 | static int |
| 8133 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8134 | struct acl_expr *expr, struct acl_test *test) |
| 8135 | { |
| 8136 | if (!s) |
| 8137 | return 0; |
| 8138 | |
| 8139 | if (s->txn.flags & TX_NOT_FIRST) |
| 8140 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8141 | else |
| 8142 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8143 | |
| 8144 | return 1; |
| 8145 | } |
| 8146 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8147 | static int |
| 8148 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8149 | struct acl_expr *expr, struct acl_test *test) |
| 8150 | { |
| 8151 | |
| 8152 | if (!s) |
| 8153 | return 0; |
| 8154 | |
| 8155 | if (!get_http_auth(s)) |
| 8156 | return 0; |
| 8157 | |
| 8158 | test->ctx.a[0] = expr->arg.ul; |
| 8159 | test->ctx.a[1] = s->txn.auth.user; |
| 8160 | test->ctx.a[2] = s->txn.auth.pass; |
| 8161 | |
| 8162 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8163 | |
| 8164 | return 1; |
| 8165 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8166 | |
| 8167 | /************************************************************************/ |
| 8168 | /* All supported keywords must be declared here. */ |
| 8169 | /************************************************************************/ |
| 8170 | |
| 8171 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8172 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8173 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8174 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8175 | { "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] | 8176 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8177 | { "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] | 8178 | { "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] | 8179 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8180 | { "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] | 8181 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8182 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8183 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8184 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8185 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8186 | { "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] | 8187 | { "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] | 8188 | { "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] | 8189 | { "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] | 8190 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8191 | /* 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] | 8192 | { "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] | 8193 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8194 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8195 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8196 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8197 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8198 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8199 | { "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] | 8200 | { "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] | 8201 | { "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] | 8202 | { "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] | 8203 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8204 | { "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] | 8205 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8206 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8207 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8208 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8209 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8210 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8211 | { "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] | 8212 | { "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] | 8213 | { "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] | 8214 | { "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] | 8215 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8216 | { "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] | 8217 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8218 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8219 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8220 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8221 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8222 | { "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] | 8223 | { "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] | 8224 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 8225 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8226 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 8227 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 8228 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 8229 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 8230 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 8231 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 8232 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 8233 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8234 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 8235 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 8236 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 8237 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 8238 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 8239 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 8240 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 8241 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8242 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8243 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8244 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8245 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8246 | { "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] | 8247 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8248 | }}; |
| 8249 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8250 | /************************************************************************/ |
| 8251 | /* The code below is dedicated to pattern fetching and matching */ |
| 8252 | /************************************************************************/ |
| 8253 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8254 | /* Returns the last occurrence of specified header. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8255 | static int |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8256 | pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8257 | 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] | 8258 | { |
| 8259 | struct http_txn *txn = l7; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8260 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8261 | return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, |
| 8262 | -1, NULL, &data->str.str, &data->str.len); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8263 | } |
| 8264 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8265 | /* |
| 8266 | * Given a path string and its length, find the position of beginning of the |
| 8267 | * query string. Returns NULL if no query string is found in the path. |
| 8268 | * |
| 8269 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8270 | * |
| 8271 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8272 | */ |
| 8273 | static inline char *find_query_string(char *path, size_t path_l) |
| 8274 | { |
| 8275 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8276 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8277 | p = memchr(path, '?', path_l); |
| 8278 | return p ? p + 1 : NULL; |
| 8279 | } |
| 8280 | |
| 8281 | static inline int is_param_delimiter(char c) |
| 8282 | { |
| 8283 | return c == '&' || c == ';'; |
| 8284 | } |
| 8285 | |
| 8286 | /* |
| 8287 | * Given a url parameter, find the starting position of the first occurence, |
| 8288 | * or NULL if the parameter is not found. |
| 8289 | * |
| 8290 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8291 | * the function will return query_string+8. |
| 8292 | */ |
| 8293 | static char* |
| 8294 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8295 | char* url_param_name, size_t url_param_name_l) |
| 8296 | { |
| 8297 | char *pos, *last; |
| 8298 | |
| 8299 | pos = query_string; |
| 8300 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8301 | |
| 8302 | while (pos <= last) { |
| 8303 | if (pos[url_param_name_l] == '=') { |
| 8304 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8305 | return pos; |
| 8306 | pos += url_param_name_l + 1; |
| 8307 | } |
| 8308 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8309 | pos++; |
| 8310 | pos++; |
| 8311 | } |
| 8312 | return NULL; |
| 8313 | } |
| 8314 | |
| 8315 | /* |
| 8316 | * Given a url parameter name, returns its value and size into *value and |
| 8317 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8318 | * zero is returned and value/value_l are not touched. |
| 8319 | */ |
| 8320 | static int |
| 8321 | find_url_param_value(char* path, size_t path_l, |
| 8322 | char* url_param_name, size_t url_param_name_l, |
| 8323 | char** value, size_t* value_l) |
| 8324 | { |
| 8325 | char *query_string, *qs_end; |
| 8326 | char *arg_start; |
| 8327 | char *value_start, *value_end; |
| 8328 | |
| 8329 | query_string = find_query_string(path, path_l); |
| 8330 | if (!query_string) |
| 8331 | return 0; |
| 8332 | |
| 8333 | qs_end = path + path_l; |
| 8334 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8335 | url_param_name, url_param_name_l); |
| 8336 | if (!arg_start) |
| 8337 | return 0; |
| 8338 | |
| 8339 | value_start = arg_start + url_param_name_l + 1; |
| 8340 | value_end = value_start; |
| 8341 | |
| 8342 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8343 | value_end++; |
| 8344 | |
| 8345 | *value = value_start; |
| 8346 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8347 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8348 | } |
| 8349 | |
| 8350 | static int |
| 8351 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8352 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8353 | { |
| 8354 | struct http_txn *txn = l7; |
| 8355 | struct http_msg *msg = &txn->req; |
| 8356 | char *url_param_value; |
| 8357 | size_t url_param_value_l; |
| 8358 | |
| 8359 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8360 | arg_p->data.str.str, arg_p->data.str.len, |
| 8361 | &url_param_value, &url_param_value_l)) |
| 8362 | return 0; |
| 8363 | |
| 8364 | data->str.str = url_param_value; |
| 8365 | data->str.len = url_param_value_l; |
| 8366 | return 1; |
| 8367 | } |
| 8368 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8369 | /* Try to find the last occurrence of a cookie name in a cookie header value. |
| 8370 | * The pointer and size of the last occurrence of the cookie value is returned |
| 8371 | * into *value and *value_l, and the function returns non-zero if the value was |
| 8372 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8373 | * value nor value_l are touched. The input hdr string should begin at the |
| 8374 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8375 | * value may represent a list of values (cookie headers). |
| 8376 | */ |
| 8377 | static int |
| 8378 | extract_cookie_value(char *hdr, size_t hdr_l, |
| 8379 | char *cookie_name, size_t cookie_name_l, int list, |
| 8380 | char **value, size_t *value_l) |
| 8381 | { |
| 8382 | int found = 0; |
| 8383 | char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end; |
| 8384 | char *next; |
| 8385 | |
| 8386 | /* Note that multiple cookies may be delimited with semi-colons, so we |
| 8387 | * also have to loop on this. |
| 8388 | */ |
| 8389 | |
| 8390 | /* we search at least a cookie name followed by an equal, and more |
| 8391 | * generally something like this : |
| 8392 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8393 | */ |
| 8394 | if (hdr_l < cookie_name_l + 1) |
| 8395 | return 0; |
| 8396 | |
| 8397 | hdr_end = hdr + hdr_l; |
| 8398 | |
| 8399 | for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) { |
| 8400 | /* Iterate through all cookies on this line */ |
| 8401 | |
| 8402 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8403 | att_beg++; |
| 8404 | |
| 8405 | /* find att_end : this is the first character after the last non |
| 8406 | * space before the equal. It may be equal to hdr_end. |
| 8407 | */ |
| 8408 | equal = att_end = att_beg; |
| 8409 | |
| 8410 | while (equal < hdr_end) { |
| 8411 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8412 | break; |
| 8413 | if (http_is_spht[(unsigned char)*equal++]) |
| 8414 | continue; |
| 8415 | att_end = equal; |
| 8416 | } |
| 8417 | |
| 8418 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8419 | * is between <att_beg> and <equal>, both may be identical. |
| 8420 | */ |
| 8421 | |
| 8422 | /* look for end of cookie if there is an equal sign */ |
| 8423 | if (equal < hdr_end && *equal == '=') { |
| 8424 | /* look for the beginning of the value */ |
| 8425 | val_beg = equal + 1; |
| 8426 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8427 | val_beg++; |
| 8428 | |
| 8429 | /* find the end of the value, respecting quotes */ |
| 8430 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8431 | |
| 8432 | /* make val_end point to the first white space or delimitor after the value */ |
| 8433 | val_end = next; |
| 8434 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8435 | val_end--; |
| 8436 | } else { |
| 8437 | val_beg = val_end = next = equal; |
| 8438 | } |
| 8439 | |
| 8440 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8441 | * they will automatically be removed if a header before them is removed, |
| 8442 | * since they're supposed to be linked together. |
| 8443 | */ |
| 8444 | if (*att_beg == '$') |
| 8445 | continue; |
| 8446 | |
| 8447 | /* Ignore cookies with no equal sign */ |
| 8448 | if (equal == next) |
| 8449 | continue; |
| 8450 | |
| 8451 | /* Now we have the cookie name between att_beg and att_end, and |
| 8452 | * its value between val_beg and val_end. |
| 8453 | */ |
| 8454 | |
| 8455 | if (att_end - att_beg == cookie_name_l && |
| 8456 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8457 | found = 1; |
| 8458 | *value = val_beg; |
| 8459 | *value_l = val_end - val_beg; |
| 8460 | /* right now we want to catch the last occurrence |
| 8461 | * of the cookie, so let's go on searching. |
| 8462 | */ |
| 8463 | } |
| 8464 | |
| 8465 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8466 | if (!list) |
| 8467 | break; |
| 8468 | } |
| 8469 | |
| 8470 | return found; |
| 8471 | } |
| 8472 | |
| 8473 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8474 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8475 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8476 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8477 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8478 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8479 | * value nor value_l are touched. The input hdr string should begin at the |
| 8480 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8481 | * value may represent a list of values (cookie headers). |
| 8482 | */ |
| 8483 | |
| 8484 | static int |
| 8485 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8486 | const char *hdr_name, int hdr_name_len, |
| 8487 | char *cookie_name, size_t cookie_name_l, int list, |
| 8488 | char **value, size_t *value_l) |
| 8489 | { |
| 8490 | struct hdr_ctx ctx; |
| 8491 | int found = 0; |
| 8492 | |
| 8493 | ctx.idx = 0; |
| 8494 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 8495 | if (ctx.vlen < cookie_name_l + 1) |
| 8496 | continue; |
| 8497 | |
| 8498 | found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen, |
| 8499 | cookie_name, cookie_name_l, 1, |
| 8500 | value, value_l); |
| 8501 | } |
| 8502 | return found; |
| 8503 | } |
| 8504 | |
| 8505 | static int |
| 8506 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8507 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8508 | { |
| 8509 | struct http_txn *txn = l7; |
| 8510 | struct http_msg *msg = &txn->req; |
| 8511 | char *cookie_value; |
| 8512 | size_t cookie_value_l; |
| 8513 | int found = 0; |
| 8514 | |
| 8515 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8516 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8517 | &cookie_value, &cookie_value_l); |
| 8518 | if (found) { |
| 8519 | data->str.str = cookie_value; |
| 8520 | data->str.len = cookie_value_l; |
| 8521 | } |
| 8522 | |
| 8523 | return found; |
| 8524 | } |
| 8525 | |
| 8526 | |
| 8527 | static int |
| 8528 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8529 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8530 | { |
| 8531 | struct http_txn *txn = l7; |
| 8532 | struct http_msg *msg = &txn->rsp; |
| 8533 | char *cookie_value; |
| 8534 | size_t cookie_value_l; |
| 8535 | int found = 0; |
| 8536 | |
| 8537 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8538 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8539 | &cookie_value, &cookie_value_l); |
| 8540 | if (found) { |
| 8541 | data->str.str = cookie_value; |
| 8542 | data->str.len = cookie_value_l; |
| 8543 | } |
| 8544 | |
| 8545 | return found; |
| 8546 | } |
| 8547 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8548 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8549 | /************************************************************************/ |
| 8550 | /* All supported keywords must be declared here. */ |
| 8551 | /************************************************************************/ |
| 8552 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8553 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8554 | { "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] | 8555 | { "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] | 8556 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8557 | { "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] | 8558 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8559 | }}; |
| 8560 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8561 | |
| 8562 | __attribute__((constructor)) |
| 8563 | static void __http_protocol_init(void) |
| 8564 | { |
| 8565 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8566 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8567 | } |
| 8568 | |
| 8569 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8570 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8571 | * Local variables: |
| 8572 | * c-indent-level: 8 |
| 8573 | * c-basic-offset: 8 |
| 8574 | * End: |
| 8575 | */ |